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