silent macro updates
[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     try {
7         /* We're going to turn this guy into a singleton, at least for a given window, and look for it in xulG */
8         if (! window.xulG) { window.xulG = {}; }
9         if (window.xulG._data) { return window.xulG._data; }
10
11         JSAN.use('util.error'); this.error = new util.error();
12         JSAN.use('util.network'); this.network = new util.network();
13     } catch(E) {
14         alert(location.href + '\nError in OpenILS.data constructor: ' + E);
15         throw(E);
16     }
17
18     window.xulG._data = this;
19     return this;
20 }
21
22 OpenILS.data.prototype = {
23
24     'list' : {},
25     'hash' : {},
26     'tree' : {},
27
28     'temp' : '',
29
30     'data_progress' : function(msg) {
31         try {
32             var x = document.getElementById('data_progress');
33             if (x) {
34                 x.appendChild( document.createTextNode( msg ) );
35             }
36         } catch(E) {
37             this.error.sdump('D_ERROR',msg + '\n' + E);
38         }
39     },
40
41     'init' : function (params) {
42
43         try {
44             if (params && params.via == 'stash') {    
45                 this.stash_retrieve();
46             } else {
47                 this.network_retrieve();
48             }
49         
50         } catch(E) {
51             this.error.sdump('D_ERROR','Error in OpenILS.data.init('
52                 +js2JSON(params)+'): ' + js2JSON(E) );
53         }
54
55
56     },
57
58     // This should be invoked only once per application, in a persistant window
59     'init_observer_functions' : function() {
60         try {
61             var obj = this;                // OpenILS.data
62             obj.observers = {};            //
63             obj.observers.id = 1;        // Unique id for each observer function added
64             obj.observers.id2path = {}; // Lookup for full_path via observer id
65             obj.observers.cache = {};    // Observer funcs go in here
66
67             // For a given path, this executes all the registered observer funcs
68             obj.observers.dispatch = function(full_path, old_value, new_value) {
69                 obj.error.sdump('D_OBSERVERS', 'entering observers.dispatch\nfull_path = ' + full_path + '\nold_value = ' + js2JSON(old_value) + '\nnew_value = ' + js2JSON(new_value) + '\n');
70                 try {
71                     var path = full_path.split(/\./).pop();
72                     for (var i in obj.observers.cache[full_path]) {
73                         try {
74                             var o = obj.observers.cache[full_path][i];
75                             if (typeof o.func == 'function') o.func(path, old_value, new_value);
76                         } catch(E) {
77                             obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.dispatch(): ' + js2JSON(E) );
78                         }
79                     }
80                 } catch(E) {
81                     obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.dispatch(): ' + js2JSON(E) );
82                 }
83             }
84
85             // This registers an observer function for a given path
86             obj.observers.add = function(full_path, func) {
87                 try {
88                     obj.error.sdump('D_OBSERVERS', 'entering observers.add\nfull_path = ' + full_path + '\nfunc = ' + func + '\n');
89                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
90                     const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
91                     var data_cache=new OpenILS( );
92                     var stash = data_cache.wrappedJSObject.OpenILS.prototype.data;
93
94                     var id = obj.observers.id++;
95                     if (typeof obj.observers.cache[ full_path ] == 'undefined') obj.observers.cache[ full_path ] = {};
96                     obj.observers.cache[ full_path ][ id ] = { 'func' : func, 'time_added' : new Date() };
97                     obj.observers.id2path[ id ] = [ full_path ];
98
99                     var path_list = full_path.split(/\./);
100                     var observed_prop = path_list.pop();
101
102                     // Convert soft path to object reference.  Error if any but the last node is undefined
103                     for (var i in path_list) stash = stash[ path_list[i] ];
104
105                     /*
106
107                     // experiment with storing only json in cache to avoid the [ ] -> { '0' : .., '1' : .. } bug
108
109                     if (stash[observed_prop] && getKeys( obj.observers.cache[ full_path ] ).length == 0) {
110                         stash['_' + observed_prop] = js2JSON(stash[observed_prop]);
111                     }
112
113                     stash.__defineSetter__(observed_prop, function(x) { this['_'+observed_prop] = js2JSON(x); });
114                     stash.__defineGetter__(observed_prop, function() { return JSON2js(this['_'+observed_prop]); });
115                     */
116
117                     stash.watch(
118                         observed_prop,
119                         function(p,old_value,new_value) {
120                             obj.observers.dispatch(full_path,old_value,new_value);
121                             return new_value;
122                         }
123                     );
124
125                     return id;
126                 } catch(E) {
127                     obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.add(): ' + js2JSON(E) );
128                 }
129             }
130
131             // This unregisters an observer function for a given observer id
132             obj.observers.remove = function(id) {
133                 try {
134                     obj.error.sdump('D_OBSERVERS', 'entering observers.remove\nid = ' + id + '\n');
135                     var path = obj.observers.id2path[ id ];
136                     delete obj.observers.cache[ path ][ id ];
137                     delete obj.observers.id2path[ id ];
138                 } catch(E) {
139                     obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.remove(): ' + js2JSON(E) );
140                 }
141             }
142
143             // This purges observer functions for a given path
144             obj.observers.purge = function(full_path) {
145                 obj.error.sdump('D_OBSERVERS', 'entering observers.purge\nfull_path = ' + full_path + '\n');
146                 try {
147                     var remove_these = [];
148                     for (var id in obj.observers.cache[ full_path ]) remove_these.push( id );
149                     for (var id in remove_these) delete obj.observers.id2path[ id ];
150                     delete obj.observers.cache[ full_path ];
151                 } catch(E) {
152                     obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.purge(): ' + js2JSON(E) );
153                 }
154             }
155
156             obj.stash('observers'); // make this accessible globally
157
158         } catch(E) {
159             this.error.sdump('D_ERROR','Error in OpenILS.data.init_observer_functions(): ' + js2JSON(E) );
160         }
161     },
162
163     'stash' : function () {
164         try {
165             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
166             const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
167             var data_cache=new OpenILS( );
168             for (var i = 0; i < arguments.length; i++) {
169                 try {
170                     if (arguments[i] != 'hash' && arguments[i] != 'list') this.error.sdump('D_DATA_STASH','stashing ' + arguments[i] + ' : ' + this[arguments[i]] + (typeof this[arguments[i]] == 'object' ? ' = ' + (this[arguments[i]]) : '') + '\n');
171                 } catch(F) { alert(F); }
172                 data_cache.wrappedJSObject.OpenILS.prototype.data[arguments[i]] = this[arguments[i]];
173             }
174         } catch(E) {
175             this.error.sdump('D_ERROR','Error in OpenILS.data.stash(): ' + js2JSON(E) );
176         }
177     },
178
179     'lookup' : function(key,value) {
180         try {
181             var obj = this; var found;
182             if (obj.hash[key] && obj.hash[key][value]) return obj.hash[key][value];
183             switch(key) {
184                 case 'acnp':
185                     found = obj.network.simple_request('FM_ACNP_RETRIEVE_VIA_PCRUD',[ ses(), { 'id' : { '=' : value } }]);
186                     if (typeof found.ilsevent != 'undefined') throw(js2JSON(found));
187                     found = found[0];
188                 break;
189                 case 'acns':
190                     found = obj.network.simple_request('FM_ACNS_RETRIEVE_VIA_PCRUD',[ ses(), { 'id' : { '=' : value } }]);
191                     if (typeof found.ilsevent != 'undefined') throw(js2JSON(found));
192                     found = found[0];
193                 break;
194                 case 'acpl': 
195                     found = obj.network.simple_request('FM_ACPL_RETRIEVE_VIA_ID.authoritative',[ value ]);
196                 break;
197                 case 'actsc':
198                     found = obj.network.simple_request('FM_ACTSC_RETRIEVE_VIA_PCRUD',[ ses(), { 'id' : { '=' : value } }]);
199                     if (typeof found.ilsevent != 'undefined') throw(js2JSON(found));
200                     found = found[0];
201                 break;
202                 default: return undefined; break;
203             }
204             if (typeof found.ilsevent != 'undefined') throw(found);
205             if (!obj.hash[key]) obj.hash[key] = {};
206             obj.hash[key][value] = found;
207             if (!obj.list[key]) obj.list[key] = [];
208             obj.list[key].push( found );
209             obj.stash('hash','list');
210             return found;
211         } catch(E) {
212             alert('Error in OpenILS.data.lookup('+key+','+value+'): ' + E );
213             return undefined;
214         }
215     },
216
217     '_debug_stash' : function() {
218         try {
219             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
220             const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
221             var data_cache=new OpenILS( );
222             for (var i in data_cache.wrappedJSObject.OpenILS.prototype.data) {
223                 dump('_debug_stash ' + i + '\n');
224             }
225         } catch(E) {
226             this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
227         }
228     },
229
230     '_fm_objects' : {
231
232         'pgt' : [ api.FM_PGT_RETRIEVE.app, api.FM_PGT_RETRIEVE.method, [], true ],
233         'cit' : [ api.FM_CIT_RETRIEVE.app, api.FM_CIT_RETRIEVE.method, [], true ],
234         'citm' : [ api.FM_CITM_RETRIEVE.app, api.FM_CITM_RETRIEVE.method, [{'query':{'ctype' : 'item_type'}}], true ],
235         /*
236         'cst' : [ api.FM_CST_RETRIEVE.app, api.FM_CST_RETRIEVE.method, [], true ],
237         */
238         /*
239         'acpl' : [ api.FM_ACPL_RETRIEVE.app, api.FM_ACPL_RETRIEVE.method, [], true ],
240         */
241         'ccs' : [ api.FM_CCS_RETRIEVE.app, api.FM_CCS_RETRIEVE.method, [], true ],
242         'aou' : [ api.FM_AOU_RETRIEVE.app, api.FM_AOU_RETRIEVE.method, [], true ],
243         'aout' : [ api.FM_AOUT_RETRIEVE.app, api.FM_AOUT_RETRIEVE.method, [], true ],
244         'crahp' : [ api.FM_CRAHP_RETRIEVE.app, api.FM_CRAHP_RETRIEVE.method, [], true ]
245     },
246
247     'stash_retrieve' : function() {
248         try {
249             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
250             const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
251             var data_cache=new OpenILS( );
252             var dc = data_cache.wrappedJSObject.OpenILS.prototype.data;
253             for (var i in dc) {
254                 this.error.sdump('D_DATA_RETRIEVE','Retrieving ' + i + ' : ' + dc[i] + '\n');
255                 this[i] = dc[i];
256             }
257         } catch(E) {
258             this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
259         }
260     },
261
262     'load_saved_print_templates' : function() {
263         var obj = this;
264         try {
265             JSAN.use('util.file'); var file = new util.file('print_list_templates');
266             if (file._file.exists()) {
267                 try {
268                     var x = file.get_object();
269                     if (x) {
270                         for (var i in x) {
271                             obj.print_list_templates[i] = x[i];
272                         }
273                         // handle macro changes
274                         var templates = [ 'bills_current', 'bills_historical' ];
275                         for (var i = 0; i < templates.length; i++) {
276                             if (obj.print_list_templates[templates[i]]) {
277                                 // mbts_id
278                                 obj.print_list_templates[templates[i]].line_item =
279                                     obj.print_list_templates[templates[i]].line_item.replace(
280                                         /%id%/g, '%mbts_id%');
281                                 // mbts_xact_start
282                                 obj.print_list_templates[templates[i]].line_item =
283                                     obj.print_list_templates[templates[i]].line_item.replace(
284                                         /%xact_start%/g, '%mbts_xact_start%');
285                                 // mbts_xact_finish
286                                 obj.print_list_templates[templates[i]].line_item =
287                                     obj.print_list_templates[templates[i]].line_item.replace(
288                                         /%xact_finish%/g, '%mbts_xact_finish%');
289                             }
290                         }
291                         //
292                         obj.stash('print_list_templates');
293                         obj.data_progress('Saved print templates retrieved from file. ');
294                     }
295                 } catch(E) {
296                     alert(E);
297                 }
298             }
299             file.close();
300         } catch(E) {
301             alert("Error in OpenILS.data, load_saved_print_templates(): " + E);
302         }
303     },
304
305     'fetch_print_strategy' : function() {
306         var obj = this;
307         try {
308             obj.print_strategy = {};
309             var print_contexts = [ 'default', 'receipt', 'label', 'mail', 'offline' ];
310             for (var i in print_contexts) {
311                 JSAN.use('util.file'); var file = new util.file('print_strategy.' + print_contexts[i]);
312                 if (file._file.exists()) {
313                     try {
314                         var x = file.get_content();
315                         if (x) {
316                             obj.print_strategy[ print_contexts[i] ] = x;
317                             obj.data_progress('Print strategy ' + print_contexts[i] + ' retrieved from file. ');
318                         }
319                     } catch(E) {
320                         alert(E);
321                     }
322                 }
323                 file.close();
324             }
325             obj.stash('print_strategy');
326         } catch(E) {
327             alert('Error in OpenILS.data, fetch_print_strategy(): ' + E);
328         }
329     },
330
331     'print_list_defaults' : function() {
332         var obj = this;
333         //if (typeof obj.print_list_templates == 'undefined') {
334         {
335             obj.print_list_types = [ 
336                 'offline_checkout', 
337                 'offline_checkin', 
338                 'offline_renew', 
339                 'offline_inhouse_use', 
340                 'items', 
341                 'bills', 
342                 'payment', 
343                 'holds', 
344                 /* 'patrons' */
345             ];
346             obj.print_list_templates = { 
347                 'item_status' : {
348                     'type' : 'items',
349                     'header' : 'The following items have been examined:<hr/><ol>',
350                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%\r\n',
351                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
352                 }, 
353                 'transit_list' : {
354                     'type' : 'transits',
355                     'header' : 'Transits:<hr/><ol>',
356                     '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',
357                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
358                 }, 
359                 'items_out' : {
360                     'type' : 'items',
361                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have the following items:<hr/><ol>',
362                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
363                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
364                 }, 
365                 'renew' : {
366                     'type' : 'items',
367                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have renewed the following items:<hr/><ol>',
368                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
369                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
370                 }, 
371                 'checkout' : {
372                     'type' : 'items',
373                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou checked out the following items:<hr/><ol>',
374                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
375                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
376                 }, 
377                 'offline_checkout' : {
378                     'type' : 'offline_checkout',
379                     'header' : 'Patron %patron_barcode%<br/>\r\nYou checked out the following items:<hr/><ol>',
380                     'line_item' : '<li>Barcode: %barcode%<br/>\r\nDue: %due_date%\r\n',
381                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
382                 },
383                 'checkin' : {
384                     'type' : 'items',
385                     'header' : 'You checked in the following items:<hr/><ol>',
386                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%  Call Number: %call_number%\r\n',
387                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
388                 }, 
389                 'bill_payment' : {
390                     'type' : 'payment',
391                     '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>',
392                     'line_item' : 'Bill #%bill_id%  %last_billing_type% Received: $%payment%<br />%barcode% %title%<br /><br />',
393                     'footer' : '</blockquote> </p> <hr />%SHORTNAME% %TODAY_TRIM%<br/> <br/> '
394                 },
395                 'bills_historical' : {
396                     'type' : 'bills',
397                     'header' : 'Welcome to %LIBRARY%!<br/>You had the following bills:<hr/><ol>',
398                     'line_item' : '<dt><b>Bill #%mbts_id%</b> %title% </dt> <dd> <table> <tr valign="top"><td>Date:</td><td>%mbts_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/>',
399                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
400                 }, 
401                 'bills_current' : {
402                     'type' : 'bills',
403                     'header' : 'Welcome to %LIBRARY%!<br/>You have the following bills:<hr/><ol>',
404                     'line_item' : '<dt><b>Bill #%mbts_id%</b></dt> <dd> <table> <tr valign="top"><td>Date:</td><td>%mbts_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/>',
405                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
406                 },
407                 'offline_checkin' : {
408                     'type' : 'offline_checkin',
409                     'header' : 'You checked in the following items:<hr/><ol>',
410                     'line_item' : '<li>Barcode: %barcode%\r\n',
411                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
412                 },
413                 'offline_renew' : {
414                     'type' : 'offline_renew',
415                     'header' : 'You renewed the following items:<hr/><ol>',
416                     'line_item' : '<li>Barcode: %barcode%\r\n',
417                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
418                 },
419                 'offline_inhouse_use' : {
420                     'type' : 'offline_inhouse_use',
421                     'header' : 'You marked the following in-house items used:<hr/><ol>',
422                     'line_item' : '<li>Barcode: %barcode%\r\nUses: %count%',
423                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
424                 },
425                 'in_house_use' : {
426                     'type' : 'items',
427                     'header' : 'You marked the following in-house items used:<hr/><ol>',
428                     'line_item' : '<li>Barcode: %barcode%\r\nUses: %uses%\r\n<br />%alert_message%',
429                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
430                 },
431                 'holds' : {
432                     'type' : 'holds',
433                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have the following titles on hold:<hr/><ol>',
434                     'line_item' : '<li>%title%\r\n',
435                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
436                 },
437                 'hold_slip' : {
438                     'type' : 'holds',
439                     'header' : 'This item needs to be routed to <b>%route_to%</b>:<br/>\r\nBarcode: %item_barcode%<br/>\r\nTitle: %item_title%<br/>\r\n<br/>\r\n%hold_for_msg%<br/>\r\nBarcode: %PATRON_BARCODE%<br/>\r\nNotify by phone: %notify_by_phone%<br/>\r\nNotify by email: %notify_by_email%<br/>\r\n',
440                     'line_item' : '%formatted_note%<br/>\r\n',
441                     'footer' : '<br/>\r\nRequest date: %request_date%<br/>\r\nSlip Date: %TODAY_TRIM%<br/>\r\nPrinted by %STAFF_FIRSTNAME% at %SHORTNAME%<br/>\r\n<br/>\r\n'
442                 },
443                 'transit_slip' : {
444                     'type' : 'transits',
445                     'header' : 'This item needs to be routed to <b>%route_to%</b>:<br/>\r\n%route_to_org_fullname%<br/>\r\n%street1%<br/>\r\n%street2%<br/>\r\n%city_state_zip%<br/>\r\n<br/>\r\nBarcode: %item_barcode%<br/>\r\nTitle: %item_title%<br/>\r\nAuthor: %item_author%<br>\r\n<br/>\r\n',
446                     'line_item' : '',
447                     'footer' : 'Slip Date: %TODAY_TRIM%<br/>\r\nPrinted by %STAFF_FIRSTNAME% at %SHORTNAME%<br/>\r\n<br/>\r\n'
448                 },
449                 'hold_transit_slip' : {
450                     'type' : 'transits',
451                     'header' : 'This item needs to be routed to <b>%route_to%</b>:<br/>\r\n%route_to_org_fullname%<br/>\r\n%street1%<br/>\r\n%street2%<br/>\r\n%city_state_zip%<br/>\r\n<br/>\r\nBarcode: %item_barcode%<br/>\r\nTitle: %item_title%<br/>\r\nAuthor: %item_author%<br>\r\n<br/>\r\n%hold_for_msg%<br/>\r\nBarcode: %PATRON_BARCODE%<br/>\r\nNotify by phone: %notify_by_phone%<br/>\r\nNotify by email: %notify_by_email%<br/>\r\n',
452                     'line_item' : '%formatted_note%<br/>\r\n',
453                     'footer' : '<br/>\r\nRequest date: %request_date%<br/>\r\nSlip Date: %TODAY_TRIM%<br/>\r\nPrinted by %STAFF_FIRSTNAME% at %SHORTNAME%<br/>\r\n<br/>\r\n'
454                 },
455                 'holdings_maintenance' : {
456                     'type' : 'items',
457                     'header' : 'Title: %title%<br/>\r\nAuthor: %author%<br/>\r\nISBN: %isbn% Edition: %edition% PubDate: %pubdate%<br/>\r\nTCN: %tcn_value% Record ID: %mvr_doc_id%<br/>\r\nCreator: %creator% Create Date: %create_date%<br/>\r\nEditor: %editor% Edit Date: %edit_date%<hr/>\r\n',
458                     'line_item' : '%prefix% %tree_location% %suffix% %parts% %acp_status%<br/>\r\n',
459                     'footer' : '<hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
460                 }
461             }; 
462
463             obj.stash( 'print_list_templates', 'print_list_types' );
464         }
465     },
466
467     'network_retrieve' : function() {
468         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
469         var obj = this;
470
471         JSAN.use('util.file'); var file = new util.file('global_font_adjust');
472         if (file._file.exists()) {
473             try {
474                 var x = file.get_object();
475                 if (x) {
476                     obj.global_font_adjust = x;
477                     obj.stash('global_font_adjust');
478                     obj.data_progress('Saved font settings retrieved from file. ');
479                 }
480             } catch(E) {
481                 alert(E);
482             }
483         }
484         file.close();
485
486         JSAN.use('util.file'); var file = new util.file('no_sound');
487         if (file._file.exists()) {
488             try {
489                 var x = file.get_content();
490                 if (x) {
491                     obj.no_sound = x;
492                     obj.stash('no_sound');
493                     obj.data_progress('Saved sound settings retrieved from file. ');
494                 }
495             } catch(E) {
496                 alert(E);
497             }
498         }
499         file.close();
500
501         obj.print_list_defaults();
502         obj.data_progress('Default print templates set. ');
503         obj.load_saved_print_templates();
504         obj.fetch_print_strategy();
505         JSAN.use('util.print'); (new util.print()).GetPrintSettings();
506         obj.data_progress('Printer settings retrieved. ');
507
508         JSAN.use('util.functional');
509         JSAN.use('util.fm_utils');
510
511         function gen_fm_retrieval_func(classname,data) {
512             var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
513             return function () {
514                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
515
516                 function convert() {
517                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
518                     try {
519                         if (obj.list[classname].constructor.name == 'Array') {
520                             obj.hash[classname] = 
521                                 util.functional.convert_object_list_to_hash(
522                                     obj.list[classname],
523                                     classname == 'citm' ? 'code' : null
524                                 );
525                         }
526                     } catch(E) {
527
528                         obj.error.sdump('D_ERROR',E + '\n');
529                     }
530
531                 }
532
533                 try {
534                     var level = obj.error.sdump_levels.D_SES_RESULT;
535                     if (classname == 'aou' || classname == 'my_aou')
536                         obj.error.sdump_levels.D_SES_RESULT = false;
537                     var robj = obj.network.request( app, method, params);
538                     if (robj != null && typeof robj.ilsevent != 'undefined') {
539                         obj.error.standard_unexpected_error_alert('The staff client failed to retrieve expected data from this call, "' + method + '"',robj);
540                         throw(robj);
541                     }
542                     obj.list[classname] = robj == null ? [] : robj;
543                     obj.error.sdump_levels.D_SES_RESULT = level;
544                     convert();
545                     obj.data_progress('Retrieved list for ' + classname + ' objects. ');
546
547                 } catch(E) {
548                     // if cacheable, try offline
549                     if (cacheable) {
550                         /* FIXME -- we're going to revisit caching and do it differently
551                         try {
552                             var file = new util.file( classname );
553                             obj.list[classname] = file.get_object(); file.close();
554                             convert();
555                         } catch(E) {
556                             throw(E);
557                         }
558                         */
559                         throw(E); // for now
560                     } else {
561                         throw(E); // for now
562                     }
563                 }
564             }
565         }
566
567         // If we don't clear these, then things like obj.list['acnp_for_lib_1'] may stick around
568         obj.hash = {}; obj.list = {};
569
570         this.chain = [];
571
572         this.chain.push(
573             function() {
574                 try {
575                     var robj = obj.network.simple_request('CIRC_MODIFIER_LIST',[{'full':true}]);
576                     if (typeof robj.ilsevent != 'undefined') throw(robj);
577                     obj.list.ccm = robj == null ? [] : robj;
578                     obj.hash.ccm = util.functional.convert_object_list_to_hash( obj.list.ccm );
579                     obj.list.circ_modifier = util.functional.map_list( obj.list.ccm, function(o) { return o.code(); } );
580                     obj.data_progress('Retrieved circ modifier list. ');
581                 } catch(E) {
582                     var error = 'Error: ' + js2JSON(E);
583                     obj.error.sdump('D_ERROR',error);
584                     throw(E);
585                 }
586             }
587         );
588
589         this.chain.push(
590             function() {
591                 var f = gen_fm_retrieval_func(
592                     'cnal',
593                     [
594                         api.FM_CNAL_RETRIEVE.app,
595                         api.FM_CNAL_RETRIEVE.method,
596                         [ obj.session.key ],
597                         false
598                     ]
599                 );
600                 try {
601                     f();
602                 } catch(E) {
603                     var error = 'Error: ' + js2JSON(E);
604                     obj.error.sdump('D_ERROR',error);
605                     throw(E);
606                 }
607             }
608         );
609
610         this.chain.push(
611             function() {
612                 var f = gen_fm_retrieval_func(
613                     'bpt',
614                     [
615                         api.FM_BPT_PCRUD_SEARCH.app,
616                         api.FM_BPT_PCRUD_SEARCH.method,
617                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"bpt":"id"}} ],
618                         false
619                     ]
620                 );
621                 try {
622                     f();
623                 } catch(E) {
624                     var error = 'Error: ' + js2JSON(E);
625                     obj.error.sdump('D_ERROR',error);
626                     throw(E);
627                 }
628             }
629         );
630
631         this.chain.push(
632             function() {
633                 var f = gen_fm_retrieval_func(
634                     'csp',
635                     [
636                         api.FM_CSP_PCRUD_SEARCH.app,
637                         api.FM_CSP_PCRUD_SEARCH.method,
638                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"csp":"id"}} ],
639                         false
640                     ]
641                 );
642                 try {
643                     f();
644                 } catch(E) {
645                     var error = 'Error: ' + js2JSON(E);
646                     obj.error.sdump('D_ERROR',error);
647                     throw(E);
648                 }
649             }
650         );
651
652         this.chain.push(
653             function() {
654                 var f = gen_fm_retrieval_func(
655                     'acnc',
656                     [
657                         api.FM_ACNC_RETRIEVE_VIA_PCRUD.app,
658                         api.FM_ACNC_RETRIEVE_VIA_PCRUD.method,
659                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"acnc":"name"}} ],
660                         false
661                     ]
662                 );
663                 try {
664                     f();
665                 } catch(E) {
666                     var error = 'Error: ' + js2JSON(E);
667                     obj.error.sdump('D_ERROR',error);
668                     throw(E);
669                 }
670             }
671         );
672
673         this.chain.push(
674             function() {
675                 var f = gen_fm_retrieval_func(
676                     'ahrcc',
677                     [
678                         api.FM_AHRCC_PCRUD_SEARCH.app,
679                         api.FM_AHRCC_PCRUD_SEARCH.method,
680                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"ahrcc":"label"}} ],
681                         false
682                     ]
683                 );
684                 try {
685                     f();
686                 } catch(E) {
687                     var error = 'Error: ' + js2JSON(E);
688                     obj.error.sdump('D_ERROR',error);
689                     throw(E);
690                 }
691             }
692         );
693
694
695         this.chain.push(
696             function() {
697                 var f = gen_fm_retrieval_func(
698                     'au',
699                     [
700                         api.FM_AU_RETRIEVE_VIA_SESSION.app,
701                         api.FM_AU_RETRIEVE_VIA_SESSION.method,
702                         [ obj.session.key ],
703                         false
704                     ]
705                 );
706                 try {
707                     f();
708                 } catch(E) {
709                     var error = 'Error: ' + js2JSON(E);
710                     obj.error.sdump('D_ERROR',error);
711                     throw(E);
712                 }
713                 obj.list.au = [ obj.list.au ];
714             }
715         );
716
717         this.chain.push(
718             function() {
719                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
720                 var f = gen_fm_retrieval_func(
721                     'my_asv',
722                     [
723                         api.FM_ASV_RETRIEVE_REQUIRED.app,
724                         api.FM_ASV_RETRIEVE_REQUIRED.method,
725                         [ obj.session.key ],
726                         true
727                     ]
728                 );
729                 try {
730                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
731                     f();
732                 } catch(E) {
733                     var error = 'Error: ' + js2JSON(E);
734                     obj.error.sdump('D_ERROR',error);
735                     throw(E);
736                 }
737             }
738         );
739
740         this.chain.push(
741             function() {
742                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
743                 var f = gen_fm_retrieval_func(
744                     'asv',
745                     [
746                         api.FM_ASV_RETRIEVE.app,
747                         api.FM_ASV_RETRIEVE.method,
748                         [ obj.session.key ],
749                         true
750                     ]
751                 );
752                 try {
753                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
754                     f();
755                 } catch(E) {
756                     var error = 'Error: ' + js2JSON(E);
757                     obj.error.sdump('D_ERROR',error);
758                     throw(E);
759                 }
760             }
761         );
762
763         obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
764
765         for (var i in this._fm_objects) {
766             this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
767         }
768
769         // The previous org_tree call returned a tree, not a list or hash.
770         this.chain.push(
771             function () {
772                 obj.tree.aou = obj.list.aou;
773                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
774                 for (var i = 0; i < obj.list.aou.length; i++) {
775                     var c = obj.list.aou[i].children();
776                     if (!c) c = [];
777                     c = c.sort(
778                         function( a, b ) {
779                             if (a.shortname() < b.shortname()) return -1;
780                             if (a.shortname() > b.shortname()) return 1;
781                             return 0;
782                         }
783                     );
784                     obj.list.aou[i].children( c );
785                 }
786                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
787                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
788             }
789         );
790
791         // The previous pgt call returned a tree, not a list or hash.
792         this.chain.push(
793             function () {
794                 obj.tree.pgt = obj.list.pgt;
795                 obj.list.pgt = util.fm_utils.flatten_ou_branch( obj.tree.pgt );
796                 obj.hash.pgt = util.functional.convert_object_list_to_hash( obj.list.pgt );
797             }
798         );
799
800         // Do these after we get the user object
801
802         this.chain.push(
803             function() {
804                 try {
805                     var robj = obj.network.simple_request('FM_AOUS_RETRIEVE',[ obj.session.key, obj.list.au[0].ws_ou() ]);
806                     if (typeof robj.ilsevent != 'undefined') throw(robj);
807                     obj.hash.aous = robj;
808                     obj.data_progress('Retrieved org unit settings. ');
809                 } catch(E) {
810                     var error = 'Error: ' + js2JSON(E);
811                     obj.error.sdump('D_ERROR',error);
812                     throw(E);
813                 }
814             }
815         );
816
817         this.chain.push(
818
819             function() {
820
821                 gen_fm_retrieval_func('my_aou', 
822                     [ 
823                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
824                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
825                         [ obj.session.key, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
826                         true
827                     ]
828                 )();
829             }
830         );
831
832         this.chain.push(
833
834             function () {
835
836                 gen_fm_retrieval_func( 'my_actsc', 
837                     [ 
838                         api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
839                         api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
840                         [ obj.session.key, obj.list.au[0].ws_ou() ],
841                         true
842                     ]
843                 )();
844             }
845         );
846
847         this.chain.push(
848
849             function () {
850
851                 gen_fm_retrieval_func( 'my_asc', 
852                     [ 
853                         api.FM_ASC_RETRIEVE_VIA_AOU.app,
854                         api.FM_ASC_RETRIEVE_VIA_AOU.method,
855                         [ obj.session.key, obj.list.au[0].ws_ou() ],
856                         true
857                     ]
858                 )();
859             }
860         );
861
862
863         this.chain.push(
864             function() {
865                 var f = gen_fm_retrieval_func(
866                     'cnct',
867                     [
868                         api.FM_CNCT_RETRIEVE.app,
869                         api.FM_CNCT_RETRIEVE.method,
870                         [ obj.list.au[0].ws_ou() ], 
871                         false
872                     ]
873                 );
874                 try {
875                     f();
876                 } catch(E) {
877                     var error = 'Error: ' + js2JSON(E);
878                     obj.error.sdump('D_ERROR',error);
879                     throw(E);
880                 }
881             }
882         );
883
884         this.chain.push(
885             function() {
886                 var f = gen_fm_retrieval_func(
887                     'my_cnct',
888                     [
889                         api.FM_CNCT_RETRIEVE.app,
890                         api.FM_CNCT_RETRIEVE.method,
891                         [ obj.list.au[0].ws_ou() ], 
892                         false
893                     ]
894                 );
895                 try {
896                     f();
897                 } catch(E) {
898                     var error = 'Error: ' + js2JSON(E);
899                     obj.error.sdump('D_ERROR',error);
900                     throw(E);
901                 }
902             }
903         );
904
905         this.chain.push(
906             function() {
907                 var f = gen_fm_retrieval_func(
908                     'acpl',
909                     [
910                         api.FM_ACPL_RETRIEVE.app,
911                         api.FM_ACPL_RETRIEVE.method,
912                         [ obj.list.au[0].ws_ou() ],
913                         false
914                     ]
915                 );
916                 try {
917                     f();
918                 } catch(E) {
919                     var error = 'Error: ' + js2JSON(E);
920                     obj.error.sdump('D_ERROR',error);
921                     throw(E);
922                 }
923             }
924         );
925
926         this.chain.push(
927             function() {
928                 var f = gen_fm_retrieval_func(
929                     'acnp',
930                     [
931                         api.FM_ACNP_RETRIEVE_VIA_PCRUD.app,
932                         api.FM_ACNP_RETRIEVE_VIA_PCRUD.method,
933                         [ obj.session.key, {"owning_lib":{"=":obj.list.au[0].ws_ou()}}, {"order_by":{"acnp":"label_sortkey"}} ],
934                         false
935                     ]
936                 );
937                 try {
938                     f();
939                     obj.list['acnp_for_lib_'+obj.list.au[0].ws_ou()] = obj.list.acnp;
940                 } catch(E) {
941                     var error = 'Error: ' + js2JSON(E);
942                     obj.error.sdump('D_ERROR',error);
943                     throw(E);
944                 }
945             }
946         );
947
948         this.chain.push(
949             function() {
950                 var f = gen_fm_retrieval_func(
951                     'acns',
952                     [
953                         api.FM_ACNS_RETRIEVE_VIA_PCRUD.app,
954                         api.FM_ACNS_RETRIEVE_VIA_PCRUD.method,
955                         [ obj.session.key, {"owning_lib":{"=":obj.list.au[0].ws_ou()}}, {"order_by":{"acns":"label_sortkey"}} ],
956                         false
957                     ]
958                 );
959                 try {
960                     f();
961                     obj.list['acns_for_lib_'+obj.list.au[0].ws_ou()] = obj.list.acns;
962                 } catch(E) {
963                     var error = 'Error: ' + js2JSON(E);
964                     obj.error.sdump('D_ERROR',error);
965                     throw(E);
966                 }
967             }
968         );
969
970         this.chain.push(
971             function() {
972                 var f = gen_fm_retrieval_func(
973                     'cbt',
974                     [
975                         api.FM_CBT_RETRIEVE.app,
976                         api.FM_CBT_RETRIEVE.method,
977                         [ obj.session.key, obj.list.au[0].ws_ou() ],
978                         false
979                     ]
980                 );
981                 try {
982                     f();
983                 } catch(E) {
984                     var error = 'Error: ' + js2JSON(E);
985                     obj.error.sdump('D_ERROR',error);
986                     throw(E);
987                 }
988             }
989         );
990
991         if (typeof this.on_complete == 'function') {
992
993             this.chain.push( this.on_complete );
994         }
995         JSAN.use('util.exec'); this.exec = new util.exec();
996         this.exec.on_error = function(E) { 
997         
998             if (typeof obj.on_error == 'function') {
999                 return obj.on_error(E); /* false breaks chain */
1000             } else {
1001                 alert('oops: ' + E ); 
1002                 return false; /* break chain */
1003             }
1004
1005         }
1006
1007         this.exec.chain( this.chain );
1008
1009     }
1010 }
1011
1012 dump('exiting OpenILS/data.js\n');