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