]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
some test data for receipt editor, and toward bill receipts
[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                 {
98                         obj.print_list_types = [ 
99                                 'offline_checkout', 
100                                 'offline_checkin', 
101                                 'offline_renew', 
102                                 'offline_inhouse_use', 
103                                 'items', 
104                                 'bills', 
105                                 'holds', 
106                                 'patrons' 
107                         ];
108                         obj.print_list_templates = { 
109                                 'item_status' : {
110                                         'type' : 'items',
111                                         'header' : 'The following items have been examined:<hr/><ol>',
112                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%\r\n',
113                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
114                                 }, 
115                                 'items_out' : {
116                                         'type' : 'items',
117                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>\r\nYou have the following items:<hr/><ol>',
118                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
119                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%<br/>\r\n<br/>\r\n',
120                                 }, 
121                                 'checkout' : {
122                                         'type' : 'items',
123                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>\r\nYou checked out the following items:<hr/><ol>',
124                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
125                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%<br/>\r\n<br/>\r\n',
126                                 }, 
127                                 'offline_checkout' : {
128                                         'type' : 'offline_checkout',
129                                         'header' : 'Patron %patron_barcode%<br/>\r\nYou checked out the following items:<hr/><ol>',
130                                         'line_item' : '<li>Barcode: %barcode%<br/>\r\nDue: %due_date%\r\n',
131                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
132                                 },
133                                 'checkin' : {
134                                         'type' : 'items',
135                                         'header' : 'You checked in the following items:<hr/><ol>',
136                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%  Call Number: %call_number%\r\n',
137                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
138                                 }, 
139                                 'bills' : {
140                                         'type' : 'bills',
141                                         'header' : 'You have or had the following bills:<hr/><ol>',
142                                         'line_item' : '<li>test\r\n',
143                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
144                                 }, 
145                                 'offline_checkin' : {
146                                         'type' : 'offline_checkin',
147                                         'header' : 'You checked in the following items:<hr/><ol>',
148                                         'line_item' : '<li>Barcode: %barcode%\r\n',
149                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
150                                 },
151                                 'offline_renew' : {
152                                         'type' : 'offline_renew',
153                                         'header' : 'You renewed the following items:<hr/><ol>',
154                                         'line_item' : '<li>Barcode: %barcode%\r\n',
155                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
156                                 },
157                                 'offline_inhouse_use' : {
158                                         'type' : 'offline_inhouse_use',
159                                         'header' : 'You marked the following in-house items used:<hr/><ol>',
160                                         'line_item' : '<li>Barcode: %barcode%\r\nUses: %count%',
161                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
162                                 },
163                                 'holds' : {
164                                         'type' : 'holds',
165                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>\r\nYou have the following titles on hold:<hr/><ol>',
166                                         'line_item' : '<li>%title%\r\n',
167                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%<br/>\r\n<br/>\r\n',
168                                 } 
169                         }; 
170
171                         obj.stash( 'print_list_templates', 'print_list_types' );
172                 }
173         },
174
175         'network_retrieve' : function() {
176                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
177                 var obj = this;
178
179
180                 JSAN.use('util.file'); var file = new util.file('print_list_templates');
181                 obj.print_list_defaults();
182                 if (file._file.exists()) {
183                         try {
184                                 var x = file.get_object();
185                                 if (x) {
186                                         for (var i in x) {
187                                                 obj.print_list_templates[i] = x[i];
188                                         }
189                                         obj.stash('print_list_templates');
190                                 }
191                         } catch(E) {
192                                 alert(E);
193                         }
194                 }
195                 file.close();
196
197                 JSAN.use('util.file');
198                 JSAN.use('util.functional');
199                 JSAN.use('util.fm_utils');
200
201                 function gen_fm_retrieval_func(classname,data) {
202                         var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
203                         return function () {
204                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
205
206                                 function convert() {
207                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
208                                         try {
209                                                 if (obj.list[classname].constructor.name == 'Array') {
210                                                         obj.hash[classname] = 
211                                                                 util.functional.convert_object_list_to_hash(
212                                                                         obj.list[classname]
213                                                                 );
214                                                 }
215                                         } catch(E) {
216
217                                                 obj.error.sdump('D_ERROR',E + '\n');
218                                         }
219
220                                 }
221
222                                 try {
223                                         var level = obj.error.sdump_levels.D_SES_RESULT;
224                                         if (classname == 'aou' || classname == 'my_aou')
225                                                 obj.error.sdump_levels.D_SES_RESULT = false;
226                                         var robj = obj.network.request( app, method, params);
227                                         if (!robj || robj.ilsevent) {
228                                                 obj.error.standard_unexpected_error_alert('The staff client failed to retrieve expected data from this call, "' + method + '"',robj);
229                                                 throw(robj);
230                                         }
231                                         obj.list[classname] = robj;
232                                         obj.error.sdump_levels.D_SES_RESULT = level;
233                                         convert();
234                                         // if cacheable, store an offline copy
235                                         /* FIXME -- we're going to revisit caching and do it differently
236                                         if (cacheable) {
237                                                 var file = new util.file( classname );
238                                                 file.set_object( obj.list[classname] );
239                                         }
240                                         */
241
242                                 } catch(E) {
243                                         // if cacheable, try offline
244                                         if (cacheable) {
245                                                 /* FIXME -- we're going to revisit caching and do it differently
246                                                 try {
247                                                         var file = new util.file( classname );
248                                                         obj.list[classname] = file.get_object(); file.close();
249                                                         convert();
250                                                 } catch(E) {
251                                                         throw(E);
252                                                 }
253                                                 */
254                                                 throw(E); // for now
255                                         } else {
256                                                 throw(E); // for now
257                                         }
258                                 }
259                         }
260                 }
261
262                 this.chain = [];
263
264                 this.chain.push(
265                         function() {
266                                 var f = gen_fm_retrieval_func(
267                                         'au',
268                                         [
269                                                 api.FM_AU_RETRIEVE_VIA_SESSION.app,
270                                                 api.FM_AU_RETRIEVE_VIA_SESSION.method,
271                                                 [ obj.session.key ],
272                                                 false
273                                         ]
274                                 );
275                                 try {
276                                         f();
277                                 } catch(E) {
278                                         var error = 'Error: ' + js2JSON(E);
279                                         obj.error.sdump('D_ERROR',error);
280                                         throw(E);
281                                 }
282                                 obj.list.au = [ obj.list.au ];
283                         }
284                 );
285
286                 this.chain.push(
287                         function() {
288                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
289                                 var f = gen_fm_retrieval_func(
290                                         'my_asv',
291                                         [
292                                                 api.FM_ASV_RETRIEVE_REQUIRED.app,
293                                                 api.FM_ASV_RETRIEVE_REQUIRED.method,
294                                                 [ obj.session.key ],
295                                                 true
296                                         ]
297                                 );
298                                 try {
299                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
300                                         f();
301                                 } catch(E) {
302                                         var error = 'Error: ' + js2JSON(E);
303                                         obj.error.sdump('D_ERROR',error);
304                                         throw(E);
305                                 }
306                         }
307                 );
308
309                 this.chain.push(
310                         function() {
311                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
312                                 var f = gen_fm_retrieval_func(
313                                         'asv',
314                                         [
315                                                 api.FM_ASV_RETRIEVE.app,
316                                                 api.FM_ASV_RETRIEVE.method,
317                                                 [ obj.session.key ],
318                                                 true
319                                         ]
320                                 );
321                                 try {
322                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
323                                         f();
324                                 } catch(E) {
325                                         var error = 'Error: ' + js2JSON(E);
326                                         obj.error.sdump('D_ERROR',error);
327                                         throw(E);
328                                 }
329                         }
330                 );
331
332                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
333
334                 for (var i in this._fm_objects) {
335                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
336                 }
337
338                 // The previous org_tree call returned a tree, not a list or hash.
339                 this.chain.push(
340                         function () {
341                                 obj.tree.aou = obj.list.aou;
342                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
343                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
344                         }
345                 );
346
347                 // Do this after we get the user object
348                 this.chain.push(
349
350                         function() {
351
352                                 gen_fm_retrieval_func('my_aou', 
353                                         [ 
354                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
355                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
356                                                 [ obj.session.key, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
357                                                 true
358                                         ]
359                                 )();
360                         }
361                 );
362
363                 this.chain.push(
364
365                         function () {
366
367                                 gen_fm_retrieval_func( 'my_actsc', 
368                                         [ 
369                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
370                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
371                                                 [ obj.session.key, obj.list.au[0].ws_ou() ],
372                                                 true
373                                         ]
374                                 )();
375                         }
376                 );
377
378                 this.chain.push(
379
380                         function () {
381
382                                 gen_fm_retrieval_func( 'my_asc', 
383                                         [ 
384                                                 api.FM_ASC_RETRIEVE_VIA_AOU.app,
385                                                 api.FM_ASC_RETRIEVE_VIA_AOU.method,
386                                                 [ obj.session.key, obj.list.au[0].ws_ou() ],
387                                                 true
388                                         ]
389                                 )();
390                         }
391                 );
392
393
394                 this.chain.push(
395                         function() {
396                                 var f = gen_fm_retrieval_func(
397                                         'cnct',
398                                         [
399                                                 api.FM_CNCT_RETRIEVE.app,
400                                                 api.FM_CNCT_RETRIEVE.method,
401                                                 [ obj.list.au[0].ws_ou() ], 
402                                                 false
403                                         ]
404                                 );
405                                 try {
406                                         f();
407                                 } catch(E) {
408                                         var error = 'Error: ' + js2JSON(E);
409                                         obj.error.sdump('D_ERROR',error);
410                                         throw(E);
411                                 }
412                         }
413                 );
414
415
416                 if (typeof this.on_complete == 'function') {
417
418                         this.chain.push( this.on_complete );
419                 }
420                 JSAN.use('util.exec'); this.exec = new util.exec();
421                 this.exec.on_error = function(E) { 
422                 
423                         if (typeof obj.on_error == 'function') {
424                                 obj.on_error();
425                         } else {
426                                 alert('oops: ' + E ); 
427                         }
428
429                         return false; /* break chain */
430                 }
431
432                 this.exec.chain( this.chain );
433
434         }
435 }
436
437 dump('exiting OpenILS/data.js\n');