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