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