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