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