]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
store print templates locally for now
[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 have been 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 %patron_barcode%\r\nYou checked out the following items:<hr/><ol>',
128                                         'line_item' : '<li>Barcode: %barcode%\r\nDue: %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: %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: %barcode%\r\n',
147                                         'footer' : '</ol><hr />%TODAY%',
148                                 },
149                                 'offline_inhouse_use' : {
150                                         'type' : 'offline_inhouse_use',
151                                         'header' : 'You marked the following in-house items used:<hr/><ol>',
152                                         'line_item' : '<li>Barcode: %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
172                 JSAN.use('util.file'); var file = new util.file('print_list_templates');
173                 if (file._file.exists()) {
174                         try {
175                                 var x = file.get_object();
176                                 if (x) {
177                                         obj.print_list_templates = x;
178                                         obj.stash('print_list_templates');
179                                 } else {
180                                         obj.print_list_defaults();
181                                 }
182                         } catch(E) {
183                                 alert(E);
184                                 obj.print_list_defaults();
185                         }
186                 } else {
187                         obj.print_list_defaults();
188                 }
189                 file.close();
190
191                 JSAN.use('util.file');
192                 JSAN.use('util.functional');
193                 JSAN.use('util.fm_utils');
194
195                 function gen_fm_retrieval_func(classname,data) {
196                         var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
197                         return function () {
198                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
199
200                                 function convert() {
201                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
202                                         try {
203                                                 if (obj.list[classname].constructor.name == 'Array') {
204                                                         obj.hash[classname] = 
205                                                                 util.functional.convert_object_list_to_hash(
206                                                                         obj.list[classname]
207                                                                 );
208                                                 }
209                                         } catch(E) {
210
211                                                 obj.error.sdump('D_ERROR',E + '\n');
212                                         }
213
214                                 }
215
216                                 try {
217                                         var level = obj.error.sdump_levels.D_SES_RESULT;
218                                         if (classname == 'aou' || classname == 'my_aou')
219                                                 obj.error.sdump_levels.D_SES_RESULT = false;
220                                         var robj = obj.network.request( app, method, params);
221                                         if (!robj || robj.ilsevent) {
222                                                 obj.error.standard_unexpected_error_alert('The staff client failed to retrieve expected data from this call, "' + method + '"',robj);
223                                                 throw(robj);
224                                         }
225                                         obj.list[classname] = robj;
226                                         obj.error.sdump_levels.D_SES_RESULT = level;
227                                         convert();
228                                         // if cacheable, store an offline copy
229                                         /* FIXME -- we're going to revisit caching and do it differently
230                                         if (cacheable) {
231                                                 var file = new util.file( classname );
232                                                 file.set_object( obj.list[classname] );
233                                         }
234                                         */
235
236                                 } catch(E) {
237                                         // if cacheable, try offline
238                                         if (cacheable) {
239                                                 /* FIXME -- we're going to revisit caching and do it differently
240                                                 try {
241                                                         var file = new util.file( classname );
242                                                         obj.list[classname] = file.get_object(); file.close();
243                                                         convert();
244                                                 } catch(E) {
245                                                         throw(E);
246                                                 }
247                                                 */
248                                                 throw(E); // for now
249                                         } else {
250                                                 throw(E); // for now
251                                         }
252                                 }
253                         }
254                 }
255
256                 this.chain = [];
257
258                 this.chain.push(
259                         function() {
260                                 var f = gen_fm_retrieval_func(
261                                         'au',
262                                         [
263                                                 api.FM_AU_RETRIEVE_VIA_SESSION.app,
264                                                 api.FM_AU_RETRIEVE_VIA_SESSION.method,
265                                                 [ obj.session.key ],
266                                                 false
267                                         ]
268                                 );
269                                 try {
270                                         f();
271                                 } catch(E) {
272                                         var error = 'Error: ' + js2JSON(E);
273                                         obj.error.sdump('D_ERROR',error);
274                                         throw(E);
275                                 }
276                                 obj.list.au = [ obj.list.au ];
277                         }
278                 );
279
280                 this.chain.push(
281                         function() {
282                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
283                                 var f = gen_fm_retrieval_func(
284                                         'my_asv',
285                                         [
286                                                 api.FM_ASV_RETRIEVE_REQUIRED.app,
287                                                 api.FM_ASV_RETRIEVE_REQUIRED.method,
288                                                 [ obj.session.key ],
289                                                 true
290                                         ]
291                                 );
292                                 try {
293                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
294                                         f();
295                                 } catch(E) {
296                                         var error = 'Error: ' + js2JSON(E);
297                                         obj.error.sdump('D_ERROR',error);
298                                         throw(E);
299                                 }
300                         }
301                 );
302
303                 this.chain.push(
304                         function() {
305                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
306                                 var f = gen_fm_retrieval_func(
307                                         'asv',
308                                         [
309                                                 api.FM_ASV_RETRIEVE.app,
310                                                 api.FM_ASV_RETRIEVE.method,
311                                                 [ obj.session.key ],
312                                                 true
313                                         ]
314                                 );
315                                 try {
316                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
317                                         f();
318                                 } catch(E) {
319                                         var error = 'Error: ' + js2JSON(E);
320                                         obj.error.sdump('D_ERROR',error);
321                                         throw(E);
322                                 }
323                         }
324                 );
325
326                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
327
328                 for (var i in this._fm_objects) {
329                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
330                 }
331
332                 // The previous org_tree call returned a tree, not a list or hash.
333                 this.chain.push(
334                         function () {
335                                 obj.tree.aou = obj.list.aou;
336                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
337                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
338                         }
339                 );
340
341                 // Do this after we get the user object
342                 this.chain.push(
343
344                         function() {
345
346                                 gen_fm_retrieval_func('my_aou', 
347                                         [ 
348                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
349                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
350                                                 [ obj.session.key, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
351                                                 true
352                                         ]
353                                 )();
354                         }
355                 );
356
357                 this.chain.push(
358
359                         function () {
360
361                                 gen_fm_retrieval_func( 'my_actsc', 
362                                         [ 
363                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
364                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
365                                                 [ obj.session.key, obj.list.au[0].ws_ou() ],
366                                                 true
367                                         ]
368                                 )();
369                         }
370                 );
371
372                 this.chain.push(
373
374                         function () {
375
376                                 gen_fm_retrieval_func( 'my_asc', 
377                                         [ 
378                                                 api.FM_ASC_RETRIEVE_VIA_AOU.app,
379                                                 api.FM_ASC_RETRIEVE_VIA_AOU.method,
380                                                 [ obj.session.key, obj.list.au[0].ws_ou() ],
381                                                 true
382                                         ]
383                                 )();
384                         }
385                 );
386
387
388                 this.chain.push(
389                         function() {
390                                 var f = gen_fm_retrieval_func(
391                                         'cnct',
392                                         [
393                                                 api.FM_CNCT_RETRIEVE.app,
394                                                 api.FM_CNCT_RETRIEVE.method,
395                                                 [ obj.list.au[0].ws_ou() ], 
396                                                 false
397                                         ]
398                                 );
399                                 try {
400                                         f();
401                                 } catch(E) {
402                                         var error = 'Error: ' + js2JSON(E);
403                                         obj.error.sdump('D_ERROR',error);
404                                         throw(E);
405                                 }
406                         }
407                 );
408
409
410                 if (typeof this.on_complete == 'function') {
411
412                         this.chain.push( this.on_complete );
413                 }
414                 JSAN.use('util.exec'); this.exec = new util.exec();
415                 this.exec.on_error = function(E) { 
416                 
417                         if (typeof obj.on_error == 'function') {
418                                 obj.on_error();
419                         } else {
420                                 alert('oops: ' + E ); 
421                         }
422
423                         return false; /* break chain */
424                 }
425
426                 this.exec.chain( this.chain );
427
428         }
429 }
430
431 dump('exiting OpenILS/data.js\n');