]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
Floating Groups
[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                     'cbc',
650                     [
651                         api.FM_CBC_PCRUD_SEARCH.app,
652                         api.FM_CBC_PCRUD_SEARCH.method,
653                         [ obj.session.key, {"active":"t"}, {"order_by":{"cbc":"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                     'cfg',
671                     [
672                         api.FM_CFG_RETRIEVE_VIA_PCRUD.app,
673                         api.FM_CFG_RETRIEVE_VIA_PCRUD.method,
674                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"cfg":"id"}} ],
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                     'csp',
692                     [
693                         api.FM_CSP_PCRUD_SEARCH.app,
694                         api.FM_CSP_PCRUD_SEARCH.method,
695                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"csp":"id"}} ],
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         this.chain.push(
710             function() {
711                 var f = gen_fm_retrieval_func(
712                     'acnc',
713                     [
714                         api.FM_ACNC_RETRIEVE_VIA_PCRUD.app,
715                         api.FM_ACNC_RETRIEVE_VIA_PCRUD.method,
716                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"acnc":"name"}} ],
717                         false
718                     ]
719                 );
720                 try {
721                     f();
722                 } catch(E) {
723                     var error = 'Error: ' + js2JSON(E);
724                     obj.error.sdump('D_ERROR',error);
725                     throw(E);
726                 }
727             }
728         );
729
730         this.chain.push(
731             function() {
732                 var f = gen_fm_retrieval_func(
733                     'ahrcc',
734                     [
735                         api.FM_AHRCC_PCRUD_SEARCH.app,
736                         api.FM_AHRCC_PCRUD_SEARCH.method,
737                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"ahrcc":"label"}} ],
738                         false
739                     ]
740                 );
741                 try {
742                     f();
743                 } catch(E) {
744                     var error = 'Error: ' + js2JSON(E);
745                     obj.error.sdump('D_ERROR',error);
746                     throw(E);
747                 }
748             }
749         );
750
751
752         this.chain.push(
753             function() {
754                 var f = gen_fm_retrieval_func(
755                     'au',
756                     [
757                         api.FM_AU_RETRIEVE_VIA_SESSION.app,
758                         api.FM_AU_RETRIEVE_VIA_SESSION.method,
759                         [ obj.session.key ],
760                         false
761                     ]
762                 );
763                 try {
764                     f();
765                 } catch(E) {
766                     var error = 'Error: ' + js2JSON(E);
767                     obj.error.sdump('D_ERROR',error);
768                     throw(E);
769                 }
770                 obj.list.au = [ obj.list.au ];
771             }
772         );
773
774         this.chain.push(
775             function() {
776                 var f = gen_fm_retrieval_func(
777                     'my_asv',
778                     [
779                         api.FM_ASV_RETRIEVE_REQUIRED.app,
780                         api.FM_ASV_RETRIEVE_REQUIRED.method,
781                         [ obj.session.key ],
782                         true
783                     ]
784                 );
785                 try {
786                     f();
787                 } catch(E) {
788                     var error = 'Error: ' + js2JSON(E);
789                     obj.error.sdump('D_ERROR',error);
790                     throw(E);
791                 }
792             }
793         );
794
795         this.chain.push(
796             function() {
797                 var f = gen_fm_retrieval_func(
798                     'asv',
799                     [
800                         api.FM_ASV_RETRIEVE.app,
801                         api.FM_ASV_RETRIEVE.method,
802                         [ obj.session.key ],
803                         true
804                     ]
805                 );
806                 try {
807                     f();
808                 } catch(E) {
809                     var error = 'Error: ' + js2JSON(E);
810                     obj.error.sdump('D_ERROR',error);
811                     throw(E);
812                 }
813             }
814         );
815
816         obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
817
818         for (var i in this._fm_objects) {
819             this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
820         }
821
822         // The previous org_tree call returned a tree, not a list or hash.
823         this.chain.push(
824             function () {
825                 obj.tree.aou = obj.list.aou;
826                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
827                 for (var i = 0; i < obj.list.aou.length; i++) {
828                     var c = obj.list.aou[i].children();
829                     if (!c) c = [];
830                     c = c.sort(
831                         function( a, b ) {
832                             if (a.shortname() < b.shortname()) return -1;
833                             if (a.shortname() > b.shortname()) return 1;
834                             return 0;
835                         }
836                     );
837                     obj.list.aou[i].children( c );
838                 }
839                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
840                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
841             }
842         );
843
844         // The previous pgt call returned a tree, not a list or hash.
845         this.chain.push(
846             function () {
847                 obj.tree.pgt = obj.list.pgt;
848                 obj.list.pgt = util.fm_utils.flatten_ou_branch( obj.tree.pgt );
849                 obj.hash.pgt = util.functional.convert_object_list_to_hash( obj.list.pgt );
850             }
851         );
852
853         // Do these after we get the user object
854
855         this.chain.push(
856             function() {
857                 try {
858                     var robj = obj.network.simple_request('FM_AOUS_RETRIEVE',[ obj.session.key, obj.list.au[0].ws_ou() ]);
859                     if (typeof robj.ilsevent != 'undefined') throw(robj);
860                     obj.hash.aous = robj;
861                     obj.data_progress('Retrieved org unit settings. ');
862                 } catch(E) {
863                     var error = 'Error: ' + js2JSON(E);
864                     obj.error.sdump('D_ERROR',error);
865                     throw(E);
866                 }
867             }
868         );
869
870         this.chain.push(
871
872             function() {
873
874                 gen_fm_retrieval_func('my_aou', 
875                     [ 
876                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
877                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
878                         [ obj.session.key, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
879                         true
880                     ]
881                 )();
882             }
883         );
884
885         this.chain.push(
886
887             function () {
888
889                 gen_fm_retrieval_func( 'my_actsc', 
890                     [ 
891                         api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
892                         api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
893                         [ obj.session.key, obj.list.au[0].ws_ou() ],
894                         true
895                     ]
896                 )();
897             }
898         );
899
900         this.chain.push(
901
902             function () {
903
904                 gen_fm_retrieval_func( 'my_asc', 
905                     [ 
906                         api.FM_ASC_RETRIEVE_VIA_AOU.app,
907                         api.FM_ASC_RETRIEVE_VIA_AOU.method,
908                         [ obj.session.key, obj.list.au[0].ws_ou() ],
909                         true
910                     ]
911                 )();
912             }
913         );
914
915
916         this.chain.push(
917             function() {
918                 var f = gen_fm_retrieval_func(
919                     'cnct',
920                     [
921                         api.FM_CNCT_RETRIEVE.app,
922                         api.FM_CNCT_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                     'my_cnct',
941                     [
942                         api.FM_CNCT_RETRIEVE.app,
943                         api.FM_CNCT_RETRIEVE.method,
944                         [ obj.list.au[0].ws_ou() ], 
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                     'acpl',
962                     [
963                         api.FM_ACPL_RETRIEVE.app,
964                         api.FM_ACPL_RETRIEVE.method,
965                         [ obj.list.au[0].ws_ou() ],
966                         false
967                     ]
968                 );
969                 try {
970                     f();
971                 } catch(E) {
972                     var error = 'Error: ' + js2JSON(E);
973                     obj.error.sdump('D_ERROR',error);
974                     throw(E);
975                 }
976             }
977         );
978
979         this.chain.push(
980             function() {
981                 var f = gen_fm_retrieval_func(
982                     'csc',
983                     [
984                         api.FM_CSC_RETRIEVE_VIA_PCRUD.app,
985                         api.FM_CSC_RETRIEVE_VIA_PCRUD.method,
986                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"csc":"name"}} ],
987                         false
988                     ]
989                 );
990                 try {
991                     f();
992                 } catch(E) {
993                     var error = 'Error: ' + js2JSON(E);
994                     obj.error.sdump('D_ERROR',error);
995                     throw(E);
996                 }
997             }
998         );
999
1000         this.chain.push(
1001             function() {
1002                 var f = gen_fm_retrieval_func(
1003                     'atb',
1004                     [
1005                         api.FM_ATB_RETRIEVE_VIA_PCRUD.app,
1006                         api.FM_ATB_RETRIEVE_VIA_PCRUD.method,
1007                         [
1008                             obj.session.key,
1009                             {
1010                                 "-or": [
1011                                     { "ws" : obj.list.au[0].wsid() },
1012                                     { "usr" : obj.list.au[0].id() },
1013                                     { "org" : util.functional.map_list( obj.list.my_aou, function(o) { return o.id(); } ) }
1014                                 ]
1015                             },
1016                             {
1017                                 "order_by":{"atb":"label"}
1018                             }
1019                         ],
1020                         false
1021                     ]
1022                 );
1023                 try {
1024                     f();
1025                 } catch(E) {
1026                     var error = 'Error: ' + js2JSON(E);
1027                     obj.error.sdump('D_ERROR',error);
1028                     throw(E);
1029                 }
1030             }
1031         );
1032
1033
1034         this.chain.push(
1035             function() {
1036                 var f = gen_fm_retrieval_func(
1037                     'acnp',
1038                     [
1039                         api.FM_ACNP_RETRIEVE_VIA_PCRUD.app,
1040                         api.FM_ACNP_RETRIEVE_VIA_PCRUD.method,
1041                         [ obj.session.key, {"owning_lib":{"=":obj.list.au[0].ws_ou()}}, {"order_by":{"acnp":"label_sortkey"}} ],
1042                         false
1043                     ]
1044                 );
1045                 try {
1046                     f();
1047                     obj.list['acnp_for_lib_'+obj.list.au[0].ws_ou()] = obj.list.acnp;
1048                 } catch(E) {
1049                     var error = 'Error: ' + js2JSON(E);
1050                     obj.error.sdump('D_ERROR',error);
1051                     throw(E);
1052                 }
1053             }
1054         );
1055
1056         this.chain.push(
1057             function() {
1058                 var f = gen_fm_retrieval_func(
1059                     'acns',
1060                     [
1061                         api.FM_ACNS_RETRIEVE_VIA_PCRUD.app,
1062                         api.FM_ACNS_RETRIEVE_VIA_PCRUD.method,
1063                         [ obj.session.key, {"owning_lib":{"=":obj.list.au[0].ws_ou()}}, {"order_by":{"acns":"label_sortkey"}} ],
1064                         false
1065                     ]
1066                 );
1067                 try {
1068                     f();
1069                     obj.list['acns_for_lib_'+obj.list.au[0].ws_ou()] = obj.list.acns;
1070                 } catch(E) {
1071                     var error = 'Error: ' + js2JSON(E);
1072                     obj.error.sdump('D_ERROR',error);
1073                     throw(E);
1074                 }
1075             }
1076         );
1077
1078         this.chain.push(
1079             function() {
1080                 var f = gen_fm_retrieval_func(
1081                     'cbt',
1082                     [
1083                         api.FM_CBT_RETRIEVE.app,
1084                         api.FM_CBT_RETRIEVE.method,
1085                         [ obj.session.key, obj.list.au[0].ws_ou() ],
1086                         false
1087                     ]
1088                 );
1089                 try {
1090                     f();
1091                 } catch(E) {
1092                     var error = 'Error: ' + js2JSON(E);
1093                     obj.error.sdump('D_ERROR',error);
1094                     throw(E);
1095                 }
1096             }
1097         );
1098
1099         if (typeof this.on_complete == 'function') {
1100
1101             this.chain.push( this.on_complete );
1102         }
1103         JSAN.use('util.exec'); this.exec = new util.exec();
1104         this.exec.on_error = function(E) { 
1105         
1106             if (typeof obj.on_error == 'function') {
1107                 return obj.on_error(E); /* false breaks chain */
1108             } else {
1109                 alert('oops: ' + E ); 
1110                 return false; /* break chain */
1111             }
1112
1113         }
1114
1115         this.exec.chain( this.chain );
1116
1117     }
1118 }
1119
1120 dump('exiting OpenILS/data.js\n');