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