]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
group groups for offline mode
[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                                 try {
44                                         if (arguments[i] != 'hash' && arguments[i] != 'list') this.error.sdump('D_DATA_STASH','stashing ' + arguments[i] + ' : ' + this[arguments[i]] + (typeof this[arguments[i]] == 'object' ? ' = ' + js2JSON(this[arguments[i]]) : '') + '\n');
45                                 } catch(F) { alert(F); }
46                                 data_cache.wrappedJSObject.OpenILS.prototype.data[arguments[i]] = this[arguments[i]];
47                         }
48                 } catch(E) {
49                         this.error.sdump('D_ERROR','Error in OpenILS.data.stash(): ' + js2JSON(E) );
50                 }
51         },
52
53         '_debug_stash' : function() {
54                 try {
55                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
56                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
57                         var data_cache=new OpenILS( );
58                         for (var i in data_cache.wrappedJSObject.OpenILS.prototype.data) {
59                                 dump('_debug_stash ' + i + '\n');
60                         }
61                 } catch(E) {
62                         this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
63                 }
64         },
65
66         '_fm_objects' : {
67
68                 'pgt' : [ api.FM_PGT_RETRIEVE.app, api.FM_PGT_RETRIEVE.method, [], true ],
69                 'cit' : [ api.FM_CIT_RETRIEVE.app, api.FM_CIT_RETRIEVE.method, [], true ],
70                 'citm' : [ api.FM_CITM_RETRIEVE.app, api.FM_CITM_RETRIEVE.method, [], true ],
71                 /*
72                 'cst' : [ api.FM_CST_RETRIEVE.app, api.FM_CST_RETRIEVE.method, [], true ],
73                 */
74                 'acpl' : [ api.FM_ACPL_RETRIEVE.app, api.FM_ACPL_RETRIEVE.method, [], true ],
75                 'ccs' : [ api.FM_CCS_RETRIEVE.app, api.FM_CCS_RETRIEVE.method, [], true ],
76                 'aou' : [ api.FM_AOU_RETRIEVE.app, api.FM_AOU_RETRIEVE.method, [], true ],
77                 'aout' : [ api.FM_AOUT_RETRIEVE.app, api.FM_AOUT_RETRIEVE.method, [], true ],
78                 'crahp' : [ api.FM_CRAHP_RETRIEVE.app, api.FM_CRAHP_RETRIEVE.method, [], true ],
79         },
80
81         'stash_retrieve' : function() {
82                 try {
83                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
84                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
85                         var data_cache=new OpenILS( );
86                         var dc = data_cache.wrappedJSObject.OpenILS.prototype.data;
87                         for (var i in dc) {
88                                 this.error.sdump('D_DATA_RETRIEVE','Retrieving ' + i + ' : ' + dc[i] + '\n');
89                                 this[i] = dc[i];
90                         }
91                         if (typeof this.on_complete == 'function') {
92
93                                 this.on_complete();
94                         }
95                 } catch(E) {
96                         this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
97                 }
98         },
99
100         'print_list_defaults' : function() {
101                 var obj = this;
102                 //if (typeof obj.print_list_templates == 'undefined') {
103                 {
104                         obj.print_list_types = [ 
105                                 'offline_checkout', 
106                                 'offline_checkin', 
107                                 'offline_renew', 
108                                 'offline_inhouse_use', 
109                                 'items', 
110                                 'bills', 
111                                 'payment', 
112                                 'holds', 
113                                 /* 'patrons' */
114                         ];
115                         obj.print_list_templates = { 
116                                 'item_status' : {
117                                         'type' : 'items',
118                                         'header' : 'The following items have been examined:<hr/><ol>',
119                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%\r\n',
120                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
121                                 }, 
122                                 'items_out' : {
123                                         'type' : 'items',
124                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>\r\nYou have the following items:<hr/><ol>',
125                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
126                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%<br/>\r\n<br/>\r\n',
127                                 }, 
128                                 'checkout' : {
129                                         'type' : 'items',
130                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>\r\nYou checked out the following items:<hr/><ol>',
131                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
132                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%<br/>\r\n<br/>\r\n',
133                                 }, 
134                                 'offline_checkout' : {
135                                         'type' : 'offline_checkout',
136                                         'header' : 'Patron %patron_barcode%<br/>\r\nYou checked out the following items:<hr/><ol>',
137                                         'line_item' : '<li>Barcode: %barcode%<br/>\r\nDue: %due_date%\r\n',
138                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
139                                 },
140                                 'checkin' : {
141                                         'type' : 'items',
142                                         'header' : 'You checked in the following items:<hr/><ol>',
143                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%  Call Number: %call_number%\r\n',
144                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
145                                 }, 
146                                 'bill_payment' : {
147                                         'type' : 'payment',
148                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>A receipt of your  transaction:<hr/> <table width="100%"> <tr> <td>Original Balance:</td> <td align="right">$%original_balance%</td> </tr> <tr> <td>Payment Method:</td> <td align="right">%payment_type%</td> </tr> <tr> <td>Payment Received:</td> <td align="right">$%payment_received%</td> </tr> <tr> <td>Payment Applied:</td> <td align="right">$%payment_applied%</td> </tr> <tr> <td>Change Given:</td> <td align="right">$%change_given%</td> </tr> <tr> <td>Credit Given:</td> <td align="right">$%credit_given%</td> </tr> <tr> <td>New Balance:</td> <td align="right">$%new_balance%</td> </tr> </table> <p> Note: %note% </p> <p> Specific bills: <blockquote>',
149                                         'line_item' : 'Bill #%bill_id%  Received: $%payment%<br />',
150                                         'footer' : '</blockquote> </p> <hr />%PINES_CODE% %TODAY_TRIM%<br/> <br/> ',
151                                 },
152                                 'bills_historical' : {
153                                         'type' : 'bills',
154                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>You had the following bills:<hr/><ol>',
155                                         'line_item' : '<dt><b>Bill #%id%</b></dt> <dd> <table> <tr valign="top"><td>Date:</td><td>%xact_start%</td></tr> <tr valign="top"><td>Type:</td><td>%xact_type%</td></tr> <tr valign="top"><td>Last Billing:</td><td>%last_billing_type%<br/>%last_billing_note%</td></tr> <tr valign="top"><td>Total Billed:</td><td>$%total_owed%</td></tr> <tr valign="top"><td>Last Payment:</td><td>%last_payment_type%<br/>%last_payment_note%</td></tr> <tr valign="top"><td>Total Paid:</td><td>$%total_paid%</td></tr> <tr valign="top"><td><b>Balance:</b></td><td><b>$%balance_owed%</b></td></tr> </table><br/>',
156                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
157                                 }, 
158                                 'bills_current' : {
159                                         'type' : 'bills',
160                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>You have the following bills:<hr/><ol>',
161                                         'line_item' : '<dt><b>Bill #%id%</b></dt> <dd> <table> <tr valign="top"><td>Date:</td><td>%xact_start%</td></tr> <tr valign="top"><td>Type:</td><td>%xact_type%</td></tr> <tr valign="top"><td>Last Billing:</td><td>%last_billing_type%<br/>%last_billing_note%</td></tr> <tr valign="top"><td>Total Billed:</td><td>$%total_owed%</td></tr> <tr valign="top"><td>Last Payment:</td><td>%last_payment_type%<br/>%last_payment_note%</td></tr> <tr valign="top"><td>Total Paid:</td><td>$%total_paid%</td></tr> <tr valign="top"><td><b>Balance:</b></td><td><b>$%balance_owed%</b></td></tr> </table><br/>',
162                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
163                                 }, 
164                                 'offline_checkin' : {
165                                         'type' : 'offline_checkin',
166                                         'header' : 'You checked in the following items:<hr/><ol>',
167                                         'line_item' : '<li>Barcode: %barcode%\r\n',
168                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
169                                 },
170                                 'offline_renew' : {
171                                         'type' : 'offline_renew',
172                                         'header' : 'You renewed the following items:<hr/><ol>',
173                                         'line_item' : '<li>Barcode: %barcode%\r\n',
174                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
175                                 },
176                                 'offline_inhouse_use' : {
177                                         'type' : 'offline_inhouse_use',
178                                         'header' : 'You marked the following in-house items used:<hr/><ol>',
179                                         'line_item' : '<li>Barcode: %barcode%\r\nUses: %count%',
180                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
181                                 },
182                                 'in_house_use' : {
183                                         'type' : 'items',
184                                         'header' : 'You marked the following in-house items used:<hr/><ol>',
185                                         'line_item' : '<li>Barcode: %barcode%\r\nUses: %uses%\r\n<br />%alert_message%',
186                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
187                                 },
188                                 'holds' : {
189                                         'type' : 'holds',
190                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>\r\nYou have the following titles on hold:<hr/><ol>',
191                                         'line_item' : '<li>%title%\r\n',
192                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%<br/>\r\n<br/>\r\n',
193                                 } 
194                         }; 
195
196                         obj.stash( 'print_list_templates', 'print_list_types' );
197                 }
198         },
199
200         'network_retrieve' : function() {
201                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
202                 var obj = this;
203
204
205                 JSAN.use('util.file'); var file = new util.file('print_list_templates');
206                 obj.print_list_defaults();
207                 if (file._file.exists()) {
208                         try {
209                                 var x = file.get_object();
210                                 if (x) {
211                                         for (var i in x) {
212                                                 obj.print_list_templates[i] = x[i];
213                                         }
214                                         obj.stash('print_list_templates');
215                                 }
216                         } catch(E) {
217                                 alert(E);
218                         }
219                 }
220                 file.close();
221
222                 JSAN.use('util.file');
223                 JSAN.use('util.functional');
224                 JSAN.use('util.fm_utils');
225
226                 function gen_fm_retrieval_func(classname,data) {
227                         var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
228                         return function () {
229                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
230
231                                 function convert() {
232                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
233                                         try {
234                                                 if (obj.list[classname].constructor.name == 'Array') {
235                                                         obj.hash[classname] = 
236                                                                 util.functional.convert_object_list_to_hash(
237                                                                         obj.list[classname]
238                                                                 );
239                                                 }
240                                         } catch(E) {
241
242                                                 obj.error.sdump('D_ERROR',E + '\n');
243                                         }
244
245                                 }
246
247                                 try {
248                                         var level = obj.error.sdump_levels.D_SES_RESULT;
249                                         if (classname == 'aou' || classname == 'my_aou')
250                                                 obj.error.sdump_levels.D_SES_RESULT = false;
251                                         var robj = obj.network.request( app, method, params);
252                                         if (!robj || robj.ilsevent) {
253                                                 obj.error.standard_unexpected_error_alert('The staff client failed to retrieve expected data from this call, "' + method + '"',robj);
254                                                 throw(robj);
255                                         }
256                                         obj.list[classname] = robj;
257                                         obj.error.sdump_levels.D_SES_RESULT = level;
258                                         convert();
259                                         // if cacheable, store an offline copy
260                                         /* FIXME -- we're going to revisit caching and do it differently
261                                         if (cacheable) {
262                                                 var file = new util.file( classname );
263                                                 file.set_object( obj.list[classname] );
264                                         }
265                                         */
266
267                                 } catch(E) {
268                                         // if cacheable, try offline
269                                         if (cacheable) {
270                                                 /* FIXME -- we're going to revisit caching and do it differently
271                                                 try {
272                                                         var file = new util.file( classname );
273                                                         obj.list[classname] = file.get_object(); file.close();
274                                                         convert();
275                                                 } catch(E) {
276                                                         throw(E);
277                                                 }
278                                                 */
279                                                 throw(E); // for now
280                                         } else {
281                                                 throw(E); // for now
282                                         }
283                                 }
284                         }
285                 }
286
287                 this.chain = [];
288
289                 this.chain.push(
290                         function() {
291                                 var f = gen_fm_retrieval_func(
292                                         'cnal',
293                                         [
294                                                 api.FM_CNAL_RETRIEVE.app,
295                                                 api.FM_CNAL_RETRIEVE.method,
296                                                 [ obj.session.key ],
297                                                 false
298                                         ]
299                                 );
300                                 try {
301                                         f();
302                                 } catch(E) {
303                                         var error = 'Error: ' + js2JSON(E);
304                                         obj.error.sdump('D_ERROR',error);
305                                         throw(E);
306                                 }
307                         }
308                 );
309
310                 this.chain.push(
311                         function() {
312                                 var f = gen_fm_retrieval_func(
313                                         'au',
314                                         [
315                                                 api.FM_AU_RETRIEVE_VIA_SESSION.app,
316                                                 api.FM_AU_RETRIEVE_VIA_SESSION.method,
317                                                 [ obj.session.key ],
318                                                 false
319                                         ]
320                                 );
321                                 try {
322                                         f();
323                                 } catch(E) {
324                                         var error = 'Error: ' + js2JSON(E);
325                                         obj.error.sdump('D_ERROR',error);
326                                         throw(E);
327                                 }
328                                 obj.list.au = [ obj.list.au ];
329                         }
330                 );
331
332                 this.chain.push(
333                         function() {
334                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
335                                 var f = gen_fm_retrieval_func(
336                                         'my_asv',
337                                         [
338                                                 api.FM_ASV_RETRIEVE_REQUIRED.app,
339                                                 api.FM_ASV_RETRIEVE_REQUIRED.method,
340                                                 [ obj.session.key ],
341                                                 true
342                                         ]
343                                 );
344                                 try {
345                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
346                                         f();
347                                 } catch(E) {
348                                         var error = 'Error: ' + js2JSON(E);
349                                         obj.error.sdump('D_ERROR',error);
350                                         throw(E);
351                                 }
352                         }
353                 );
354
355                 this.chain.push(
356                         function() {
357                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
358                                 var f = gen_fm_retrieval_func(
359                                         'asv',
360                                         [
361                                                 api.FM_ASV_RETRIEVE.app,
362                                                 api.FM_ASV_RETRIEVE.method,
363                                                 [ obj.session.key ],
364                                                 true
365                                         ]
366                                 );
367                                 try {
368                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
369                                         f();
370                                 } catch(E) {
371                                         var error = 'Error: ' + js2JSON(E);
372                                         obj.error.sdump('D_ERROR',error);
373                                         throw(E);
374                                 }
375                         }
376                 );
377
378                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
379
380                 for (var i in this._fm_objects) {
381                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
382                 }
383
384                 // The previous org_tree call returned a tree, not a list or hash.
385                 this.chain.push(
386                         function () {
387                                 obj.tree.aou = obj.list.aou;
388                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
389                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
390                         }
391                 );
392
393                 // The previous pgt call returned a tree, not a list or hash.
394                 this.chain.push(
395                         function () {
396                                 obj.tree.pgt = obj.list.pgt;
397                                 obj.list.pgt = util.fm_utils.flatten_ou_branch( obj.tree.pgt );
398                                 obj.hash.pgt = util.functional.convert_object_list_to_hash( obj.list.pgt );
399                         }
400                 );
401
402                 // Do this after we get the user object
403                 this.chain.push(
404
405                         function() {
406
407                                 gen_fm_retrieval_func('my_aou', 
408                                         [ 
409                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
410                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
411                                                 [ obj.session.key, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
412                                                 true
413                                         ]
414                                 )();
415                         }
416                 );
417
418                 this.chain.push(
419
420                         function () {
421
422                                 gen_fm_retrieval_func( 'my_actsc', 
423                                         [ 
424                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
425                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
426                                                 [ obj.session.key, obj.list.au[0].ws_ou() ],
427                                                 true
428                                         ]
429                                 )();
430                         }
431                 );
432
433                 this.chain.push(
434
435                         function () {
436
437                                 gen_fm_retrieval_func( 'my_asc', 
438                                         [ 
439                                                 api.FM_ASC_RETRIEVE_VIA_AOU.app,
440                                                 api.FM_ASC_RETRIEVE_VIA_AOU.method,
441                                                 [ obj.session.key, obj.list.au[0].ws_ou() ],
442                                                 true
443                                         ]
444                                 )();
445                         }
446                 );
447
448
449                 this.chain.push(
450                         function() {
451                                 var f = gen_fm_retrieval_func(
452                                         'cnct',
453                                         [
454                                                 api.FM_CNCT_RETRIEVE.app,
455                                                 api.FM_CNCT_RETRIEVE.method,
456                                                 [ obj.list.au[0].ws_ou() ], 
457                                                 false
458                                         ]
459                                 );
460                                 try {
461                                         f();
462                                 } catch(E) {
463                                         var error = 'Error: ' + js2JSON(E);
464                                         obj.error.sdump('D_ERROR',error);
465                                         throw(E);
466                                 }
467                         }
468                 );
469
470
471                 if (typeof this.on_complete == 'function') {
472
473                         this.chain.push( this.on_complete );
474                 }
475                 JSAN.use('util.exec'); this.exec = new util.exec();
476                 this.exec.on_error = function(E) { 
477                 
478                         if (typeof obj.on_error == 'function') {
479                                 obj.on_error();
480                         } else {
481                                 alert('oops: ' + E ); 
482                         }
483
484                         return false; /* break chain */
485                 }
486
487                 this.exec.chain( this.chain );
488
489         }
490 }
491
492 dump('exiting OpenILS/data.js\n');