]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
offline print templates
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / OpenILS / data.js
1 dump('entering OpenILS/data.js\n');
2
3 if (typeof OpenILS == 'undefined') OpenILS = {};
4 OpenILS.data = function () {
5
6         JSAN.use('util.error'); this.error = new util.error();
7         JSAN.use('util.network'); this.network = new util.network();
8
9         return this;
10 }
11
12 OpenILS.data.prototype = {
13
14         'list' : {},
15         'hash' : {},
16         'tree' : {},
17
18         'temp' : '',
19
20         'init' : function (params) {
21
22                 try {
23                         if (params && params.via == 'stash') {  
24                                 this.stash_retrieve();
25                         } else {
26                                 this.network_retrieve();
27                         }
28                 
29                 } catch(E) {
30                         this.error.sdump('D_ERROR','Error in OpenILS.data.init('
31                                 +js2JSON(params)+'): ' + js2JSON(E) );
32                 }
33
34
35         },
36
37         'stash' : function () {
38                 try {
39                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
40                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
41                         var data_cache=new OpenILS( );
42                         for (var i = 0; i < arguments.length; i++) {
43                                 this.error.sdump('D_DATA','stashing ' + arguments[i] + ' : ' + this[arguments[i]] + '\n');
44                                 data_cache.wrappedJSObject.OpenILS.prototype.data[arguments[i]] = this[arguments[i]];
45                         }
46                 } catch(E) {
47                         this.error.sdump('D_ERROR','Error in OpenILS.data.stash(): ' + js2JSON(E) );
48                 }
49         },
50
51         '_debug_stash' : function() {
52                 try {
53                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
54                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
55                         var data_cache=new OpenILS( );
56                         for (var i in data_cache.wrappedJSObject.OpenILS.prototype.data) {
57                                 dump('_debug_stash ' + i + '\n');
58                         }
59                 } catch(E) {
60                         this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
61                 }
62         },
63
64         '_fm_objects' : {
65
66                 'pgt' : [ api.FM_PGT_RETRIEVE.app, api.FM_PGT_RETRIEVE.method, [], true ],
67                 'cit' : [ api.FM_CIT_RETRIEVE.app, api.FM_CIT_RETRIEVE.method, [], true ],
68                 'cst' : [ api.FM_CST_RETRIEVE.app, api.FM_CST_RETRIEVE.method, [], true ],
69                 'acpl' : [ api.FM_ACPL_RETRIEVE.app, api.FM_ACPL_RETRIEVE.method, [], true ],
70                 'ccs' : [ api.FM_CCS_RETRIEVE.app, api.FM_CCS_RETRIEVE.method, [], true ],
71                 'aou' : [ api.FM_AOU_RETRIEVE.app, api.FM_AOU_RETRIEVE.method, [], true ],
72                 'aout' : [ api.FM_AOUT_RETRIEVE.app, api.FM_AOUT_RETRIEVE.method, [], true ]    
73         },
74
75         'stash_retrieve' : function() {
76                 try {
77                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
78                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
79                         var data_cache=new OpenILS( );
80                         var dc = data_cache.wrappedJSObject.OpenILS.prototype.data;
81                         for (var i in dc) {
82                                 this.error.sdump('D_DATA','Retrieving ' + i + ' : ' + dc[i] + '\n');
83                                 this[i] = dc[i];
84                         }
85                         if (typeof this.on_complete == 'function') {
86
87                                 this.on_complete();
88                         }
89                 } catch(E) {
90                         this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
91                 }
92         },
93
94         'print_list_defaults' : function() {
95                 var obj = this;
96                 if (typeof obj.print_list_templates == 'undefined') {
97                         obj.print_list_types = [ 
98                                 'offline_checkout', 
99                                 'offline_checkin', 
100                                 'offline_renew', 
101                                 'offline_inhouse_use', 
102                                 'items', 
103                                 'holds', 
104                                 'patrons' 
105                         ];
106                         obj.print_list_templates = { 
107                                 'item_status' : {
108                                         'type' : 'items',
109                                         'header' : 'The following items are examined:<hr/><ol>',
110                                         'line_item' : '<li>%title%\r\nBarcode: %barcode%\r\n',
111                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\n',
112                                 }, 
113                                 'items_out' : {
114                                         'type' : 'items',
115                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!\r\nYou have the following items:<hr/><ol>',
116                                         'line_item' : '<li>%title%\r\nBarcode: %barcode% Due: %due_date%\r\n',
117                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%',
118                                 }, 
119                                 'checkout' : {
120                                         'type' : 'items',
121                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!\r\nYou checked out the following items:<hr/><ol>',
122                                         'line_item' : '<li>%title%\r\nBarcode: %barcode% Due: %due_date%\r\n',
123                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%',
124                                 }, 
125                                 'offline_checkout' : {
126                                         'type' : 'offline_checkout',
127                                         'header' : 'Patron %OFFLINE_PATRON_BARCODE%\r\nYou checked out the following items:<hr/><ol>',
128                                         'line_item' : '<li>Barcode: %OFFLINE_BARCODE%\r\nDue: %OFFLINE_DUE_DATE%\r\n',
129                                         'footer' : '</ol><hr />%TODAY%',
130                                 },
131                                 'checkin' : {
132                                         'type' : 'items',
133                                         'header' : 'You checked in the following items:<hr/><ol>',
134                                         'line_item' : '<li>%title%\r\nBarcode: %barcode%  Call Number: %call_number%\r\n',
135                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\n',
136                                 }, 
137                                 'offline_checkin' : {
138                                         'type' : 'offline_checkin',
139                                         'header' : 'You checked in the following items:<hr/><ol>',
140                                         'line_item' : '<li>Barcode: %OFFLINE_BARCODE%\r\n',
141                                         'footer' : '</ol><hr />%TODAY%',
142                                 },
143                                 'offline_renew' : {
144                                         'type' : 'offline_renew',
145                                         'header' : 'You renewed the following items:<hr/><ol>',
146                                         'line_item' : '<li>Barcode: %OFFLINE_BARCODE%\r\n',
147                                         'footer' : '</ol><hr />%TODAY%',
148                                 },
149                                 'offline_items_used' : {
150                                         'type' : 'offline_inhouse_use',
151                                         'header' : 'You marked the following in-house items used:<hr/><ol>',
152                                         'line_item' : '<li>Barcode: %OFFLINE_BARCODE%\r\nUses: %COUNT%',
153                                         'footer' : '</ol><hr />%TODAY%',
154                                 },
155                                 'holds' : {
156                                         'type' : 'holds',
157                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!\r\nYou have the following titles on hold:<hr/><ol>',
158                                         'line_item' : '<li>%title%\r\n',
159                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%',
160                                 } 
161                         }; 
162
163                         obj.stash( 'print_list_templates', 'print_list_types' );
164                 }
165         },
166
167         'network_retrieve' : function() {
168                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
169                 var obj = this;
170
171                 try { obj.print_list_defaults(); } catch(E) { alert(E); }
172
173                 JSAN.use('util.file');
174                 JSAN.use('util.functional');
175                 JSAN.use('util.fm_utils');
176
177                 function gen_fm_retrieval_func(classname,data) {
178                         var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
179                         return function () {
180                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
181
182                                 function convert() {
183                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
184                                         try {
185                                                 if (obj.list[classname].constructor.name == 'Array') {
186                                                         obj.hash[classname] = 
187                                                                 util.functional.convert_object_list_to_hash(
188                                                                         obj.list[classname]
189                                                                 );
190                                                 }
191                                         } catch(E) {
192
193                                                 obj.error.sdump('D_ERROR',E + '\n');
194                                         }
195
196                                 }
197
198                                 try {
199                                         var level = obj.error.sdump_levels.D_SES_RESULT;
200                                         if (classname == 'aou' || classname == 'my_aou')
201                                                 obj.error.sdump_levels.D_SES_RESULT = false;
202                                         obj.list[classname] = obj.network.request( app, method, params);
203                                         obj.error.sdump_levels.D_SES_RESULT = level;
204                                         convert();
205                                         // if cacheable, store an offline copy
206                                         /* FIXME -- we're going to revisit caching and do it differently
207                                         if (cacheable) {
208                                                 var file = new util.file( classname );
209                                                 file.set_object( obj.list[classname] );
210                                         }
211                                         */
212
213                                 } catch(E) {
214                                         // if cacheable, try offline
215                                         if (cacheable) {
216                                                 /* FIXME -- we're going to revisit caching and do it differently
217                                                 try {
218                                                         var file = new util.file( classname );
219                                                         obj.list[classname] = file.get_object(); file.close();
220                                                         convert();
221                                                 } catch(E) {
222                                                         throw(E);
223                                                 }
224                                                 */
225                                                 throw(E); // for now
226                                         } else {
227                                                 throw(E); // for now
228                                         }
229                                 }
230                         }
231                 }
232
233                 this.chain = [];
234
235                 this.chain.push(
236                         function() {
237                                 var f = gen_fm_retrieval_func(
238                                         'au',
239                                         [
240                                                 api.FM_AU_RETRIEVE_VIA_SESSION.app,
241                                                 api.FM_AU_RETRIEVE_VIA_SESSION.method,
242                                                 [ obj.session ],
243                                                 false
244                                         ]
245                                 );
246                                 try {
247                                         f();
248                                 } catch(E) {
249                                         var error = 'Error: ' + js2JSON(E);
250                                         obj.error.sdump('D_ERROR',error);
251                                         alert(error);
252                                         throw(E);
253                                 }
254                                 obj.list.au = [ obj.list.au ];
255                         }
256                 );
257
258                 this.chain.push(
259                         function() {
260                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
261                                 var f = gen_fm_retrieval_func(
262                                         'my_asv',
263                                         [
264                                                 api.FM_ASV_RETRIEVE_REQUIRED.app,
265                                                 api.FM_ASV_RETRIEVE_REQUIRED.method,
266                                                 [ obj.session ],
267                                                 true
268                                         ]
269                                 );
270                                 try {
271                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
272                                         f();
273                                 } catch(E) {
274                                         var error = 'Error: ' + js2JSON(E);
275                                         obj.error.sdump('D_ERROR',error);
276                                         alert(error);
277                                         throw(E);
278                                 }
279                         }
280                 );
281
282                 this.chain.push(
283                         function() {
284                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
285                                 var f = gen_fm_retrieval_func(
286                                         'asv',
287                                         [
288                                                 api.FM_ASV_RETRIEVE.app,
289                                                 api.FM_ASV_RETRIEVE.method,
290                                                 [ obj.session ],
291                                                 true
292                                         ]
293                                 );
294                                 try {
295                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
296                                         f();
297                                 } catch(E) {
298                                         var error = 'Error: ' + js2JSON(E);
299                                         obj.error.sdump('D_ERROR',error);
300                                         alert(error);
301                                         throw(E);
302                                 }
303                         }
304                 );
305
306                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
307
308                 for (var i in this._fm_objects) {
309                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
310                 }
311
312                 // The previous org_tree call returned a tree, not a list or hash.
313                 this.chain.push(
314                         function () {
315                                 obj.tree.aou = obj.list.aou;
316                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
317                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
318                         }
319                 );
320
321                 // Do this after we get the user object
322                 this.chain.push(
323
324                         function() {
325
326                                 gen_fm_retrieval_func('my_aou', 
327                                         [ 
328                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
329                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
330                                                 [ obj.session, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
331                                                 true
332                                         ]
333                                 )();
334                         }
335                 );
336
337                 this.chain.push(
338
339                         function () {
340
341                                 gen_fm_retrieval_func( 'my_actsc', 
342                                         [ 
343                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
344                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
345                                                 [ obj.session, obj.list.au[0].ws_ou() ],
346                                                 true
347                                         ]
348                                 )();
349                         }
350                 );
351
352                 this.chain.push(
353
354                         function () {
355
356                                 gen_fm_retrieval_func( 'my_asc', 
357                                         [ 
358                                                 api.FM_ASC_RETRIEVE_VIA_AOU.app,
359                                                 api.FM_ASC_RETRIEVE_VIA_AOU.method,
360                                                 [ obj.session, obj.list.au[0].ws_ou() ],
361                                                 true
362                                         ]
363                                 )();
364                         }
365                 );
366
367
368                 this.chain.push(
369                         function() {
370                                 var f = gen_fm_retrieval_func(
371                                         'cnct',
372                                         [
373                                                 api.FM_CNCT_RETRIEVE.app,
374                                                 api.FM_CNCT_RETRIEVE.method,
375                                                 [ obj.list.au[0].ws_ou() ], 
376                                                 false
377                                         ]
378                                 );
379                                 try {
380                                         f();
381                                 } catch(E) {
382                                         var error = 'Error: ' + js2JSON(E);
383                                         obj.error.sdump('D_ERROR',error);
384                                         alert(error);
385                                         throw(E);
386                                 }
387                         }
388                 );
389
390
391                 if (typeof this.on_complete == 'function') {
392
393                         this.chain.push( this.on_complete );
394                 }
395                 JSAN.use('util.exec'); this.exec = new util.exec();
396                 this.exec.on_error = function(E) { 
397                 
398                         if (typeof obj.on_error == 'function') {
399                                 obj.on_error();
400                         } else {
401                                 alert('oops: ' + E ); 
402                         }
403
404                         return false; /* break chain */
405                 }
406
407                 this.exec.chain( this.chain );
408
409         }
410 }
411
412 dump('exiting OpenILS/data.js\n');