]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
toward a singleton for OpenILS.data
[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                 default: return undefined; break;
188             }
189             if (typeof found.ilsevent != 'undefined') throw(found);
190             if (!obj.hash[key]) obj.hash[key] = {};
191             obj.hash[key][value] = found; obj.list[key].push( found ); obj.stash('hash','list');
192             return found;
193         } catch(E) {
194             this.error.sdump('D_ERROR','Error in OpenILS.data.lookup('+key+','+value+'): ' + js2JSON(E) );
195             return undefined;
196         }
197     },
198
199     '_debug_stash' : function() {
200         try {
201             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
202             const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
203             var data_cache=new OpenILS( );
204             for (var i in data_cache.wrappedJSObject.OpenILS.prototype.data) {
205                 dump('_debug_stash ' + i + '\n');
206             }
207         } catch(E) {
208             this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
209         }
210     },
211
212     '_fm_objects' : {
213
214         'pgt' : [ api.FM_PGT_RETRIEVE.app, api.FM_PGT_RETRIEVE.method, [], true ],
215         'cit' : [ api.FM_CIT_RETRIEVE.app, api.FM_CIT_RETRIEVE.method, [], true ],
216         'citm' : [ api.FM_CITM_RETRIEVE.app, api.FM_CITM_RETRIEVE.method, [], true ],
217         /*
218         'cst' : [ api.FM_CST_RETRIEVE.app, api.FM_CST_RETRIEVE.method, [], true ],
219         */
220         /*
221         'acpl' : [ api.FM_ACPL_RETRIEVE.app, api.FM_ACPL_RETRIEVE.method, [], true ],
222         */
223         'ccs' : [ api.FM_CCS_RETRIEVE.app, api.FM_CCS_RETRIEVE.method, [], true ],
224         'aou' : [ api.FM_AOU_RETRIEVE.app, api.FM_AOU_RETRIEVE.method, [], true ],
225         'aout' : [ api.FM_AOUT_RETRIEVE.app, api.FM_AOUT_RETRIEVE.method, [], true ],
226         'crahp' : [ api.FM_CRAHP_RETRIEVE.app, api.FM_CRAHP_RETRIEVE.method, [], true ]
227     },
228
229     'stash_retrieve' : function() {
230         try {
231             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
232             const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
233             var data_cache=new OpenILS( );
234             var dc = data_cache.wrappedJSObject.OpenILS.prototype.data;
235             for (var i in dc) {
236                 this.error.sdump('D_DATA_RETRIEVE','Retrieving ' + i + ' : ' + dc[i] + '\n');
237                 this[i] = dc[i];
238             }
239         } catch(E) {
240             this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
241         }
242     },
243
244     'print_list_defaults' : function() {
245         var obj = this;
246         //if (typeof obj.print_list_templates == 'undefined') {
247         {
248             obj.print_list_types = [ 
249                 'offline_checkout', 
250                 'offline_checkin', 
251                 'offline_renew', 
252                 'offline_inhouse_use', 
253                 'items', 
254                 'bills', 
255                 'payment', 
256                 'holds', 
257                 /* 'patrons' */
258             ];
259             obj.print_list_templates = { 
260                 'item_status' : {
261                     'type' : 'items',
262                     'header' : 'The following items have been examined:<hr/><ol>',
263                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%\r\n',
264                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
265                 }, 
266                 'transit_list' : {
267                     'type' : 'transits',
268                     'header' : 'Transits:<hr/><ol>',
269                     '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',
270                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
271                 }, 
272                 'items_out' : {
273                     'type' : 'items',
274                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have the following items:<hr/><ol>',
275                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
276                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
277                 }, 
278                 'renew' : {
279                     'type' : 'items',
280                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have renewed the following items:<hr/><ol>',
281                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
282                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
283                 }, 
284                 'checkout' : {
285                     'type' : 'items',
286                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou checked out the following items:<hr/><ol>',
287                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
288                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
289                 }, 
290                 'offline_checkout' : {
291                     'type' : 'offline_checkout',
292                     'header' : 'Patron %patron_barcode%<br/>\r\nYou checked out the following items:<hr/><ol>',
293                     'line_item' : '<li>Barcode: %barcode%<br/>\r\nDue: %due_date%\r\n',
294                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
295                 },
296                 'checkin' : {
297                     'type' : 'items',
298                     'header' : 'You checked in the following items:<hr/><ol>',
299                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%  Call Number: %call_number%\r\n',
300                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
301                 }, 
302                 'bill_payment' : {
303                     'type' : 'payment',
304                     '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>',
305                     'line_item' : 'Bill #%bill_id%  %last_billing_type% Received: $%payment%<br />%barcode% %title%<br /><br />',
306                     'footer' : '</blockquote> </p> <hr />%SHORTNAME% %TODAY_TRIM%<br/> <br/> '
307                 },
308                 'bills_historical' : {
309                     'type' : 'bills',
310                     'header' : 'Welcome to %LIBRARY%!<br/>You had the following bills:<hr/><ol>',
311                     '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/>',
312                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
313                 }, 
314                 'bills_current' : {
315                     'type' : 'bills',
316                     'header' : 'Welcome to %LIBRARY%!<br/>You have the following bills:<hr/><ol>',
317                     '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/>',
318                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
319                 },
320                 'bills_main_view' : {
321                     'type' : 'bills',
322                     'header' : 'Welcome to %LIBRARY%!<br/>You have the following bills:<hr/><ol>',
323                     '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/>',
324                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
325                 },
326                 'offline_checkin' : {
327                     'type' : 'offline_checkin',
328                     'header' : 'You checked in the following items:<hr/><ol>',
329                     'line_item' : '<li>Barcode: %barcode%\r\n',
330                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
331                 },
332                 'offline_renew' : {
333                     'type' : 'offline_renew',
334                     'header' : 'You renewed the following items:<hr/><ol>',
335                     'line_item' : '<li>Barcode: %barcode%\r\n',
336                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
337                 },
338                 'offline_inhouse_use' : {
339                     'type' : 'offline_inhouse_use',
340                     'header' : 'You marked the following in-house items used:<hr/><ol>',
341                     'line_item' : '<li>Barcode: %barcode%\r\nUses: %count%',
342                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
343                 },
344                 'in_house_use' : {
345                     'type' : 'items',
346                     'header' : 'You marked the following in-house items used:<hr/><ol>',
347                     'line_item' : '<li>Barcode: %barcode%\r\nUses: %uses%\r\n<br />%alert_message%',
348                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
349                 },
350                 'holds' : {
351                     'type' : 'holds',
352                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have the following titles on hold:<hr/><ol>',
353                     'line_item' : '<li>%title%\r\n',
354                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
355                 },
356                 'hold_slip' : {
357                     'type' : 'holds',
358                     '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',
359                     'line_item' : '%formatted_note%<br/>\r\n',
360                     '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'
361                 },
362                 'transit_slip' : {
363                     'type' : 'transits',
364                     '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',
365                     'line_item' : 'Barcode: %item_barcode%<br/>\r\nTitle: %item_title%<br/>\r\nAuthor: %item_author%<br>\r\n<br/>\r\n',
366                     'footer' : 'Slip Date: %TODAY_TRIM%<br/>\r\nPrinted by %STAFF_FIRSTNAME% at %SHORTNAME%<br/>\r\n<br/>\r\n'
367                 },
368                 'hold_transit_slip' : {
369                     'type' : 'transits',
370                     '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',
371                     'line_item' : '%formatted_note%<br/>\r\n',
372                     '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'
373                 }
374             }; 
375
376             obj.stash( 'print_list_templates', 'print_list_types' );
377         }
378     },
379
380     'network_retrieve' : function() {
381         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
382         var obj = this;
383
384
385         JSAN.use('util.file'); var file = new util.file('print_list_templates');
386         obj.print_list_defaults();
387         obj.data_progress('Default print templates set. ');
388         if (file._file.exists()) {
389             try {
390                 var x = file.get_object();
391                 if (x) {
392                     for (var i in x) {
393                         obj.print_list_templates[i] = x[i];
394                     }
395                     obj.stash('print_list_templates');
396                     obj.data_progress('Saved print templates retrieved from file. ');
397                 }
398             } catch(E) {
399                 alert(E);
400             }
401         }
402         file.close();
403
404         JSAN.use('util.file'); var file = new util.file('global_font_adjust');
405         if (file._file.exists()) {
406             try {
407                 var x = file.get_object();
408                 if (x) {
409                     obj.global_font_adjust = x;
410                     obj.stash('global_font_adjust');
411                     obj.data_progress('Saved font settings retrieved from file. ');
412                 }
413             } catch(E) {
414                 alert(E);
415             }
416         }
417         file.close();
418
419         JSAN.use('util.file'); var file = new util.file('no_sound');
420         if (file._file.exists()) {
421             try {
422                 var x = file.get_content();
423                 if (x) {
424                     obj.no_sound = x;
425                     obj.stash('no_sound');
426                     obj.data_progress('Saved sound settings retrieved from file. ');
427                 }
428             } catch(E) {
429                 alert(E);
430             }
431         }
432         file.close();
433
434         JSAN.use('util.file'); var file = new util.file('print_strategy');
435         if (file._file.exists()) {
436             try {
437                 var x = file.get_content();
438                 if (x) {
439                     obj.print_strategy = x;
440                     obj.stash('print_strategy');
441                     obj.data_progress('Print strategy retrieved from file. ');
442                 }
443             } catch(E) {
444                 alert(E);
445             }
446         }
447         file.close();
448
449         JSAN.use('util.print'); (new util.print()).GetPrintSettings();
450         obj.data_progress('Printer settings retrieved. ');
451
452         JSAN.use('util.functional');
453         JSAN.use('util.fm_utils');
454
455         function gen_fm_retrieval_func(classname,data) {
456             var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
457             return function () {
458                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
459
460                 function convert() {
461                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
462                     try {
463                         if (obj.list[classname].constructor.name == 'Array') {
464                             obj.hash[classname] = 
465                                 util.functional.convert_object_list_to_hash(
466                                     obj.list[classname]
467                                 );
468                         }
469                     } catch(E) {
470
471                         obj.error.sdump('D_ERROR',E + '\n');
472                     }
473
474                 }
475
476                 try {
477                     var level = obj.error.sdump_levels.D_SES_RESULT;
478                     if (classname == 'aou' || classname == 'my_aou')
479                         obj.error.sdump_levels.D_SES_RESULT = false;
480                     var robj = obj.network.request( app, method, params);
481                     if (robj != null && typeof robj.ilsevent != 'undefined') {
482                         obj.error.standard_unexpected_error_alert('The staff client failed to retrieve expected data from this call, "' + method + '"',robj);
483                         throw(robj);
484                     }
485                     obj.list[classname] = robj == null ? [] : robj;
486                     obj.error.sdump_levels.D_SES_RESULT = level;
487                     convert();
488                     obj.data_progress('Retrieved list for ' + classname + ' objects. ');
489
490                 } catch(E) {
491                     // if cacheable, try offline
492                     if (cacheable) {
493                         /* FIXME -- we're going to revisit caching and do it differently
494                         try {
495                             var file = new util.file( classname );
496                             obj.list[classname] = file.get_object(); file.close();
497                             convert();
498                         } catch(E) {
499                             throw(E);
500                         }
501                         */
502                         throw(E); // for now
503                     } else {
504                         throw(E); // for now
505                     }
506                 }
507             }
508         }
509
510         this.chain = [];
511
512         this.chain.push(
513             function() {
514                 try {
515                     var robj = obj.network.simple_request('CIRC_MODIFIER_LIST',[]);
516                     if (typeof robj.ilsevent != 'undefined') throw(robj);
517                     obj.list.circ_modifier = robj;
518                     obj.data_progress('Retrieved circ modifier list. ');
519                 } catch(E) {
520                     var error = 'Error: ' + js2JSON(E);
521                     obj.error.sdump('D_ERROR',error);
522                     throw(E);
523                 }
524             }
525         );
526
527         this.chain.push(
528             function() {
529                 var f = gen_fm_retrieval_func(
530                     'cnal',
531                     [
532                         api.FM_CNAL_RETRIEVE.app,
533                         api.FM_CNAL_RETRIEVE.method,
534                         [ obj.session.key ],
535                         false
536                     ]
537                 );
538                 try {
539                     f();
540                 } catch(E) {
541                     var error = 'Error: ' + js2JSON(E);
542                     obj.error.sdump('D_ERROR',error);
543                     throw(E);
544                 }
545             }
546         );
547
548         this.chain.push(
549             function() {
550                 var f = gen_fm_retrieval_func(
551                     'csp',
552                     [
553                         api.FM_CSP_PCRUD_SEARCH.app,
554                         api.FM_CSP_PCRUD_SEARCH.method,
555                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"csp":"id"}} ],
556                         false
557                     ]
558                 );
559                 try {
560                     f();
561                 } catch(E) {
562                     var error = 'Error: ' + js2JSON(E);
563                     obj.error.sdump('D_ERROR',error);
564                     throw(E);
565                 }
566             }
567         );
568
569         this.chain.push(
570             function() {
571                 var f = gen_fm_retrieval_func(
572                     'ahrcc',
573                     [
574                         api.FM_AHRCC_PCRUD_SEARCH.app,
575                         api.FM_AHRCC_PCRUD_SEARCH.method,
576                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"ahrcc":"label"}} ],
577                         false
578                     ]
579                 );
580                 try {
581                     f();
582                 } catch(E) {
583                     var error = 'Error: ' + js2JSON(E);
584                     obj.error.sdump('D_ERROR',error);
585                     throw(E);
586                 }
587             }
588         );
589
590
591         this.chain.push(
592             function() {
593                 var f = gen_fm_retrieval_func(
594                     'au',
595                     [
596                         api.FM_AU_RETRIEVE_VIA_SESSION.app,
597                         api.FM_AU_RETRIEVE_VIA_SESSION.method,
598                         [ obj.session.key ],
599                         false
600                     ]
601                 );
602                 try {
603                     f();
604                 } catch(E) {
605                     var error = 'Error: ' + js2JSON(E);
606                     obj.error.sdump('D_ERROR',error);
607                     throw(E);
608                 }
609                 obj.list.au = [ obj.list.au ];
610             }
611         );
612
613         this.chain.push(
614             function() {
615                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
616                 var f = gen_fm_retrieval_func(
617                     'my_asv',
618                     [
619                         api.FM_ASV_RETRIEVE_REQUIRED.app,
620                         api.FM_ASV_RETRIEVE_REQUIRED.method,
621                         [ obj.session.key ],
622                         true
623                     ]
624                 );
625                 try {
626                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
627                     f();
628                 } catch(E) {
629                     var error = 'Error: ' + js2JSON(E);
630                     obj.error.sdump('D_ERROR',error);
631                     throw(E);
632                 }
633             }
634         );
635
636         this.chain.push(
637             function() {
638                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
639                 var f = gen_fm_retrieval_func(
640                     'asv',
641                     [
642                         api.FM_ASV_RETRIEVE.app,
643                         api.FM_ASV_RETRIEVE.method,
644                         [ obj.session.key ],
645                         true
646                     ]
647                 );
648                 try {
649                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
650                     f();
651                 } catch(E) {
652                     var error = 'Error: ' + js2JSON(E);
653                     obj.error.sdump('D_ERROR',error);
654                     throw(E);
655                 }
656             }
657         );
658
659         obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
660
661         for (var i in this._fm_objects) {
662             this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
663         }
664
665         // The previous org_tree call returned a tree, not a list or hash.
666         this.chain.push(
667             function () {
668                 obj.tree.aou = obj.list.aou;
669                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
670                 for (var i = 0; i < obj.list.aou.length; i++) {
671                     var c = obj.list.aou[i].children();
672                     if (!c) c = [];
673                     c = c.sort(
674                         function( a, b ) {
675                             if (a.shortname() < b.shortname()) return -1;
676                             if (a.shortname() > b.shortname()) return 1;
677                             return 0;
678                         }
679                     );
680                     obj.list.aou[i].children( c );
681                 }
682                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
683                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
684             }
685         );
686
687         // The previous pgt call returned a tree, not a list or hash.
688         this.chain.push(
689             function () {
690                 obj.tree.pgt = obj.list.pgt;
691                 obj.list.pgt = util.fm_utils.flatten_ou_branch( obj.tree.pgt );
692                 obj.hash.pgt = util.functional.convert_object_list_to_hash( obj.list.pgt );
693             }
694         );
695
696         // Do these after we get the user object
697
698         this.chain.push(
699             function() {
700                 try {
701                     var robj = obj.network.simple_request('FM_AOUS_RETRIEVE',[ obj.session.key, obj.list.au[0].ws_ou() ]);
702                     if (typeof robj.ilsevent != 'undefined') throw(robj);
703                     obj.hash.aous = robj;
704                     obj.data_progress('Retrieved org unit settings. ');
705                 } catch(E) {
706                     var error = 'Error: ' + js2JSON(E);
707                     obj.error.sdump('D_ERROR',error);
708                     throw(E);
709                 }
710             }
711         );
712
713         this.chain.push(
714
715             function() {
716
717                 gen_fm_retrieval_func('my_aou', 
718                     [ 
719                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
720                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
721                         [ obj.session.key, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
722                         true
723                     ]
724                 )();
725             }
726         );
727
728         this.chain.push(
729
730             function () {
731
732                 gen_fm_retrieval_func( 'my_actsc', 
733                     [ 
734                         api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
735                         api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
736                         [ obj.session.key, obj.list.au[0].ws_ou() ],
737                         true
738                     ]
739                 )();
740             }
741         );
742
743         this.chain.push(
744
745             function () {
746
747                 gen_fm_retrieval_func( 'my_asc', 
748                     [ 
749                         api.FM_ASC_RETRIEVE_VIA_AOU.app,
750                         api.FM_ASC_RETRIEVE_VIA_AOU.method,
751                         [ obj.session.key, obj.list.au[0].ws_ou() ],
752                         true
753                     ]
754                 )();
755             }
756         );
757
758
759         this.chain.push(
760             function() {
761                 var f = gen_fm_retrieval_func(
762                     'cnct',
763                     [
764                         api.FM_CNCT_RETRIEVE.app,
765                         api.FM_CNCT_RETRIEVE.method,
766                         [ obj.list.au[0].ws_ou() ], 
767                         false
768                     ]
769                 );
770                 try {
771                     f();
772                 } catch(E) {
773                     var error = 'Error: ' + js2JSON(E);
774                     obj.error.sdump('D_ERROR',error);
775                     throw(E);
776                 }
777             }
778         );
779
780         this.chain.push(
781             function() {
782                 var f = gen_fm_retrieval_func(
783                     'my_cnct',
784                     [
785                         api.FM_CNCT_RETRIEVE.app,
786                         api.FM_CNCT_RETRIEVE.method,
787                         [ obj.list.au[0].ws_ou() ], 
788                         false
789                     ]
790                 );
791                 try {
792                     f();
793                 } catch(E) {
794                     var error = 'Error: ' + js2JSON(E);
795                     obj.error.sdump('D_ERROR',error);
796                     throw(E);
797                 }
798             }
799         );
800
801
802         this.chain.push(
803             function() {
804                 var f = gen_fm_retrieval_func(
805                     'acpl',
806                     [
807                         api.FM_ACPL_RETRIEVE.app,
808                         api.FM_ACPL_RETRIEVE.method,
809                         [ obj.list.au[0].ws_ou() ],
810                         false
811                     ]
812                 );
813                 try {
814                     f();
815                 } catch(E) {
816                     var error = 'Error: ' + js2JSON(E);
817                     obj.error.sdump('D_ERROR',error);
818                     throw(E);
819                 }
820             }
821         );
822
823         this.chain.push(
824             function() {
825                 var f = gen_fm_retrieval_func(
826                     'cbt',
827                     [
828                         api.FM_CBT_RETRIEVE.app,
829                         api.FM_CBT_RETRIEVE.method,
830                         [ obj.session.key, obj.list.au[0].ws_ou() ],
831                         false
832                     ]
833                 );
834                 try {
835                     f();
836                 } catch(E) {
837                     var error = 'Error: ' + js2JSON(E);
838                     obj.error.sdump('D_ERROR',error);
839                     throw(E);
840                 }
841             }
842         );
843
844         if (typeof this.on_complete == 'function') {
845
846             this.chain.push( this.on_complete );
847         }
848         JSAN.use('util.exec'); this.exec = new util.exec();
849         this.exec.on_error = function(E) { 
850         
851             if (typeof obj.on_error == 'function') {
852                 return obj.on_error(E); /* false breaks chain */
853             } else {
854                 alert('oops: ' + E ); 
855                 return false; /* break chain */
856             }
857
858         }
859
860         this.exec.chain( this.chain );
861
862     }
863 }
864
865 dump('exiting OpenILS/data.js\n');