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