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