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