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