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