1 dump('entering OpenILS/data.js\n');
3 if (typeof OpenILS == 'undefined') OpenILS = {};
4 OpenILS.data = function () {
6 JSAN.use('util.error'); this.error = new util.error();
7 JSAN.use('util.network'); this.network = new util.network();
12 OpenILS.data.prototype = {
17 'cached_request' : {},
21 'data_progress' : function(msg) {
23 var x = document.getElementById('data_progress');
25 x.appendChild( document.createTextNode( msg ) );
28 this.error.sdump('D_ERROR',msg + '\n' + E);
32 'init' : function (params) {
35 if (params && params.via == 'stash') {
36 this.stash_retrieve();
38 this.network_retrieve();
42 this.error.sdump('D_ERROR','Error in OpenILS.data.init('
43 +js2JSON(params)+'): ' + js2JSON(E) );
49 // This should be invoked only once per application, in a persistant window
50 'init_observer_functions' : function() {
52 var obj = this; // OpenILS.data
53 obj.observers = {}; //
54 obj.observers.id = 1; // Unique id for each observer function added
55 obj.observers.id2path = {}; // Lookup for full_path via observer id
56 obj.observers.cache = {}; // Observer funcs go in here
58 // For a given path, this executes all the registered observer funcs
59 obj.observers.dispatch = function(full_path, old_value, new_value) {
60 obj.error.sdump('D_OBSERVERS', 'entering observers.dispatch\nfull_path = ' + full_path + '\nold_value = ' + js2JSON(old_value) + '\nnew_value = ' + js2JSON(new_value) + '\n');
62 var path = full_path.split(/\./).pop();
63 for (var i in obj.observers.cache[full_path]) {
65 var o = obj.observers.cache[full_path][i];
66 if (typeof o.func == 'function') o.func(path, old_value, new_value);
68 obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.dispatch(): ' + js2JSON(E) );
72 obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.dispatch(): ' + js2JSON(E) );
76 // This registers an observer function for a given path
77 obj.observers.add = function(full_path, func) {
79 obj.error.sdump('D_OBSERVERS', 'entering observers.add\nfull_path = ' + full_path + '\nfunc = ' + func + '\n');
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 stash = data_cache.wrappedJSObject.OpenILS.prototype.data;
85 var id = obj.observers.id++;
86 if (typeof obj.observers.cache[ full_path ] == 'undefined') obj.observers.cache[ full_path ] = {};
87 obj.observers.cache[ full_path ][ id ] = { 'func' : func, 'time_added' : new Date() };
88 obj.observers.id2path[ id ] = [ full_path ];
90 var path_list = full_path.split(/\./);
91 var observed_prop = path_list.pop();
93 // Convert soft path to object reference. Error if any but the last node is undefined
94 for (var i in path_list) stash = stash[ path_list[i] ];
97 function(p,old_value,new_value) {
98 obj.observers.dispatch(full_path,old_value,new_value);
105 obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.add(): ' + js2JSON(E) );
109 // This unregisters an observer function for a given observer id
110 obj.observers.remove = function(id) {
112 obj.error.sdump('D_OBSERVERS', 'entering observers.remove\nid = ' + id + '\n');
113 var path = obj.observers.id2path[ id ];
114 delete obj.observers.cache[ path ][ id ];
115 delete obj.observers.id2path[ id ];
117 obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.remove(): ' + js2JSON(E) );
121 // This purges observer functions for a given path
122 obj.observers.purge = function(full_path) {
123 obj.error.sdump('D_OBSERVERS', 'entering observers.purge\nfull_path = ' + full_path + '\n');
125 var remove_these = [];
126 for (var id in obj.observers.cache[ full_path ]) remove_these.push( id );
127 for (var id in remove_these) delete obj.observers.id2path[ id ];
128 delete obj.observers.cache[ full_path ];
130 obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.purge(): ' + js2JSON(E) );
134 obj.stash('observers'); // make this accessible globally
137 this.error.sdump('D_ERROR','Error in OpenILS.data.init_observer_functions(): ' + js2JSON(E) );
141 'stash' : function () {
143 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
144 const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
145 var data_cache=new OpenILS( );
146 for (var i = 0; i < arguments.length; i++) {
148 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');
149 } catch(F) { alert(F); }
150 data_cache.wrappedJSObject.OpenILS.prototype.data[arguments[i]] = this[arguments[i]];
153 this.error.sdump('D_ERROR','Error in OpenILS.data.stash(): ' + js2JSON(E) );
157 'lookup' : function(key,value) {
159 var obj = this; var found;
160 if (obj.hash[key] && obj.hash[key][value]) return obj.hash[key][value];
163 found = obj.network.simple_request('FM_ACPL_RETRIEVE_VIA_ID',[ value ]);
165 default: return undefined; break;
167 if (typeof found.ilsevent != 'undefined') throw(found);
168 if (!obj.hash[key]) obj.hash[key] = {};
169 obj.hash[key][value] = found; obj.list[key].push( found ); obj.stash('hash','list');
172 this.error.sdump('D_ERROR','Error in OpenILS.data.lookup('+key+','+value+'): ' + js2JSON(E) );
177 '_debug_stash' : function() {
179 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
180 const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
181 var data_cache=new OpenILS( );
182 for (var i in data_cache.wrappedJSObject.OpenILS.prototype.data) {
183 dump('_debug_stash ' + i + '\n');
186 this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
192 'pgt' : [ api.FM_PGT_RETRIEVE.app, api.FM_PGT_RETRIEVE.method, [], true ],
193 'cit' : [ api.FM_CIT_RETRIEVE.app, api.FM_CIT_RETRIEVE.method, [], true ],
194 'citm' : [ api.FM_CITM_RETRIEVE.app, api.FM_CITM_RETRIEVE.method, [], true ],
196 'cst' : [ api.FM_CST_RETRIEVE.app, api.FM_CST_RETRIEVE.method, [], true ],
199 'acpl' : [ api.FM_ACPL_RETRIEVE.app, api.FM_ACPL_RETRIEVE.method, [], true ],
201 'ccs' : [ api.FM_CCS_RETRIEVE.app, api.FM_CCS_RETRIEVE.method, [], true ],
202 'aou' : [ api.FM_AOU_RETRIEVE.app, api.FM_AOU_RETRIEVE.method, [], true ],
203 'aout' : [ api.FM_AOUT_RETRIEVE.app, api.FM_AOUT_RETRIEVE.method, [], true ],
204 'crahp' : [ api.FM_CRAHP_RETRIEVE.app, api.FM_CRAHP_RETRIEVE.method, [], true ],
207 'stash_retrieve' : function() {
209 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
210 const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
211 var data_cache=new OpenILS( );
212 var dc = data_cache.wrappedJSObject.OpenILS.prototype.data;
214 this.error.sdump('D_DATA_RETRIEVE','Retrieving ' + i + ' : ' + dc[i] + '\n');
217 if (typeof this.on_complete == 'function') {
222 this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
226 'print_list_defaults' : function() {
228 //if (typeof obj.print_list_templates == 'undefined') {
230 obj.print_list_types = [
234 'offline_inhouse_use',
241 obj.print_list_templates = {
244 'header' : 'The following items have been examined:<hr/><ol>',
245 'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%\r\n',
246 'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
250 'header' : 'Transits:<hr/><ol>',
251 'line_item' : '<li>From: %transit_source% To: %transit_dest_lib%<br/>\r\nWhen: %transit_source_send_time%<br />\r\nBarcode: %transit_item_barcode% Title: %transit_item_title%<br/>\r\n',
252 'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
256 'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have the following items:<hr/><ol>',
257 'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
258 'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n',
262 'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou checked out the following items:<hr/><ol>',
263 'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
264 'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n',
266 'offline_checkout' : {
267 'type' : 'offline_checkout',
268 'header' : 'Patron %patron_barcode%<br/>\r\nYou checked out the following items:<hr/><ol>',
269 'line_item' : '<li>Barcode: %barcode%<br/>\r\nDue: %due_date%\r\n',
270 'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
274 'header' : 'You checked in the following items:<hr/><ol>',
275 'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Call Number: %call_number%\r\n',
276 'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
280 'header' : 'Welcome 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>Billings Voided:</td> <td align="right">%voided_balance%</td> </tr> <tr> <td>Change Given:</td> <td align="right">$%change_given%</td> </tr> <tr> <td>New Balance:</td> <td align="right">$%new_balance%</td> </tr> </table> <p> Note: %note% </p> <p> Specific bills: <blockquote>',
281 'line_item' : 'Bill #%bill_id% %last_billing_type% Received: $%payment%<br />%barcode% %title%<br /><br />',
282 'footer' : '</blockquote> </p> <hr />%SHORTNAME% %TODAY_TRIM%<br/> <br/> ',
284 'bills_historical' : {
286 'header' : 'Welcome to %LIBRARY%!<br/>You had the following bills:<hr/><ol>',
287 '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/>',
288 'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
292 'header' : 'Welcome to %LIBRARY%!<br/>You have the following bills:<hr/><ol>',
293 '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/>',
294 'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
296 'offline_checkin' : {
297 'type' : 'offline_checkin',
298 'header' : 'You checked in the following items:<hr/><ol>',
299 'line_item' : '<li>Barcode: %barcode%\r\n',
300 'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
303 'type' : 'offline_renew',
304 'header' : 'You renewed the following items:<hr/><ol>',
305 'line_item' : '<li>Barcode: %barcode%\r\n',
306 'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
308 'offline_inhouse_use' : {
309 'type' : 'offline_inhouse_use',
310 'header' : 'You marked the following in-house items used:<hr/><ol>',
311 'line_item' : '<li>Barcode: %barcode%\r\nUses: %count%',
312 'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
316 'header' : 'You marked the following in-house items used:<hr/><ol>',
317 'line_item' : '<li>Barcode: %barcode%\r\nUses: %uses%\r\n<br />%alert_message%',
318 'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
322 'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have the following titles on hold:<hr/><ol>',
323 'line_item' : '<li>%title%\r\n',
324 'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n',
328 obj.stash( 'print_list_templates', 'print_list_types' );
332 'network_retrieve' : function() {
333 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
337 JSAN.use('util.file'); var file = new util.file('print_list_templates');
338 obj.print_list_defaults();
339 obj.data_progress('Default print templates set. ');
340 if (file._file.exists()) {
342 var x = file.get_object();
345 obj.print_list_templates[i] = x[i];
347 obj.stash('print_list_templates');
348 obj.data_progress('Saved print templates retrieved from file. ');
356 JSAN.use('util.file'); var file = new util.file('global_font_adjust');
357 if (file._file.exists()) {
359 var x = file.get_object();
361 obj.global_font_adjust = x;
362 obj.stash('global_font_adjust');
363 obj.data_progress('Saved font settings retrieved from file. ');
371 JSAN.use('util.file'); var file = new util.file('no_sound');
372 if (file._file.exists()) {
374 var x = file.get_content();
377 obj.stash('no_sound');
378 obj.data_progress('Saved sound settings retrieved from file. ');
386 JSAN.use('util.file'); var file = new util.file('print_strategy');
387 if (file._file.exists()) {
389 var x = file.get_content();
391 obj.print_strategy = x;
392 obj.stash('print_strategy');
393 obj.data_progress('Print strategy retrieved from file. ');
401 JSAN.use('util.functional');
402 JSAN.use('util.fm_utils');
404 function gen_fm_retrieval_func(classname,data) {
405 var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
407 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
410 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
412 if (obj.list[classname].constructor.name == 'Array') {
413 obj.hash[classname] =
414 util.functional.convert_object_list_to_hash(
420 obj.error.sdump('D_ERROR',E + '\n');
426 var level = obj.error.sdump_levels.D_SES_RESULT;
427 if (classname == 'aou' || classname == 'my_aou')
428 obj.error.sdump_levels.D_SES_RESULT = false;
429 var robj = obj.network.request( app, method, params);
430 if (!robj || robj.ilsevent) {
431 obj.error.standard_unexpected_error_alert('The staff client failed to retrieve expected data from this call, "' + method + '"',robj);
434 obj.list[classname] = robj;
435 obj.error.sdump_levels.D_SES_RESULT = level;
437 obj.data_progress('Retrieved list for ' + classname + ' objects. ');
438 // if cacheable, store an offline copy
439 /* FIXME -- we're going to revisit caching and do it differently
441 var file = new util.file( classname );
442 file.set_object( obj.list[classname] );
447 // if cacheable, try offline
449 /* FIXME -- we're going to revisit caching and do it differently
451 var file = new util.file( classname );
452 obj.list[classname] = file.get_object(); file.close();
471 var robj = obj.network.simple_request('CIRC_MODIFIER_LIST',[]);
472 if (typeof robj.ilsevent != 'undefined') throw(robj);
473 obj.list.circ_modifier = robj;
474 obj.data_progress('Retrieved circ modifier list. ');
476 var error = 'Error: ' + js2JSON(E);
477 obj.error.sdump('D_ERROR',error);
486 var robj = obj.network.simple_request('BILLING_TYPE_LIST',[]);
487 if (typeof robj.ilsevent != 'undefined') throw(robj);
488 obj.list.billing_type = robj;
489 obj.data_progress('Retrieved billing type list. ');
491 var error = 'Error: ' + js2JSON(E);
492 obj.error.sdump('D_ERROR',error);
501 var f = gen_fm_retrieval_func(
504 api.FM_CNAL_RETRIEVE.app,
505 api.FM_CNAL_RETRIEVE.method,
513 var error = 'Error: ' + js2JSON(E);
514 obj.error.sdump('D_ERROR',error);
522 var f = gen_fm_retrieval_func(
525 api.FM_AU_RETRIEVE_VIA_SESSION.app,
526 api.FM_AU_RETRIEVE_VIA_SESSION.method,
534 var error = 'Error: ' + js2JSON(E);
535 obj.error.sdump('D_ERROR',error);
538 obj.list.au = [ obj.list.au ];
544 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
545 var f = gen_fm_retrieval_func(
548 api.FM_ASV_RETRIEVE_REQUIRED.app,
549 api.FM_ASV_RETRIEVE_REQUIRED.method,
555 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
558 var error = 'Error: ' + js2JSON(E);
559 obj.error.sdump('D_ERROR',error);
567 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
568 var f = gen_fm_retrieval_func(
571 api.FM_ASV_RETRIEVE.app,
572 api.FM_ASV_RETRIEVE.method,
578 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
581 var error = 'Error: ' + js2JSON(E);
582 obj.error.sdump('D_ERROR',error);
588 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
590 for (var i in this._fm_objects) {
591 this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
594 // The previous org_tree call returned a tree, not a list or hash.
597 obj.tree.aou = obj.list.aou;
598 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
599 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
603 // The previous pgt call returned a tree, not a list or hash.
606 obj.tree.pgt = obj.list.pgt;
607 obj.list.pgt = util.fm_utils.flatten_ou_branch( obj.tree.pgt );
608 obj.hash.pgt = util.functional.convert_object_list_to_hash( obj.list.pgt );
612 // Do this after we get the user object
617 gen_fm_retrieval_func('my_aou',
619 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
620 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
621 [ obj.session.key, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
632 gen_fm_retrieval_func( 'my_actsc',
634 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
635 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
636 [ obj.session.key, obj.list.au[0].ws_ou() ],
647 gen_fm_retrieval_func( 'my_asc',
649 api.FM_ASC_RETRIEVE_VIA_AOU.app,
650 api.FM_ASC_RETRIEVE_VIA_AOU.method,
651 [ obj.session.key, obj.list.au[0].ws_ou() ],
661 var f = gen_fm_retrieval_func(
664 api.FM_CNCT_RETRIEVE.app,
665 api.FM_CNCT_RETRIEVE.method,
666 [ obj.list.au[0].ws_ou() ],
673 var error = 'Error: ' + js2JSON(E);
674 obj.error.sdump('D_ERROR',error);
682 var f = gen_fm_retrieval_func(
685 api.FM_ACPL_RETRIEVE.app,
686 api.FM_ACPL_RETRIEVE.method,
687 [ obj.list.au[0].ws_ou() ],
694 var error = 'Error: ' + js2JSON(E);
695 obj.error.sdump('D_ERROR',error);
703 if (typeof this.on_complete == 'function') {
705 this.chain.push( this.on_complete );
707 JSAN.use('util.exec'); this.exec = new util.exec();
708 this.exec.on_error = function(E) {
710 if (typeof obj.on_error == 'function') {
713 alert('oops: ' + E );
716 return false; /* break chain */
719 this.exec.chain( this.chain );
724 dump('exiting OpenILS/data.js\n');