]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
For Hold Cancel option, offer dialog for Cancel Reason and Cancel Note
[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' ? ' = ' + (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.authoritative',[ 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                                 'renew' : {
273                                         'type' : 'items',
274                                         'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have renewed 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                                 'checkout' : {
279                                         'type' : 'items',
280                                         'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou checked out the following items:<hr/><ol>',
281                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
282                                         'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
283                                 }, 
284                                 'offline_checkout' : {
285                                         'type' : 'offline_checkout',
286                                         'header' : 'Patron %patron_barcode%<br/>\r\nYou checked out the following items:<hr/><ol>',
287                                         'line_item' : '<li>Barcode: %barcode%<br/>\r\nDue: %due_date%\r\n',
288                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
289                                 },
290                                 'checkin' : {
291                                         'type' : 'items',
292                                         'header' : 'You checked in the following items:<hr/><ol>',
293                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%  Call Number: %call_number%\r\n',
294                                         'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
295                                 }, 
296                                 'bill_payment' : {
297                                         'type' : 'payment',
298                                         '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>',
299                                         'line_item' : 'Bill #%bill_id%  %last_billing_type% Received: $%payment%<br />%barcode% %title%<br /><br />',
300                                         'footer' : '</blockquote> </p> <hr />%SHORTNAME% %TODAY_TRIM%<br/> <br/> '
301                                 },
302                                 'bills_historical' : {
303                                         'type' : 'bills',
304                                         'header' : 'Welcome to %LIBRARY%!<br/>You had 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                                 'bills_current' : {
309                                         'type' : 'bills',
310                                         'header' : 'Welcome to %LIBRARY%!<br/>You have the following bills:<hr/><ol>',
311                                         '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/>',
312                                         'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
313                                 },
314                                 'bills_main_view' : {
315                                         'type' : 'bills',
316                                         'header' : 'Welcome to %LIBRARY%!<br/>You have the following bills:<hr/><ol>',
317                                         'line_item' : '<dt><b>Bill #%id%</b> %title%</dt> <dd> <table width="100%"> <tr valign="top"><td>Date:</td><td>%xact_start%</td></tr> <tr valign="top"><td>Type:</td><td>%xact_type%</td></tr> <tr valign="top"><td>Last Billing:</td><td>%last_billing_type%<br/>%last_billing_note%</td></tr> <tr valign="top"><td>Total Billed:</td><td>$%total_owed%</td></tr> <tr valign="top"><td>Last Payment:</td><td>%last_payment_type%<br/>%last_payment_note%</td></tr> <tr valign="top"><td>Total Paid:</td><td>$%total_paid%</td></tr> <tr valign="top"><td><b>Balance:</b></td><td><b>$%balance_owed%</b></td></tr> </table><br/>',
318                                         'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
319                                 },
320                                 'offline_checkin' : {
321                                         'type' : 'offline_checkin',
322                                         'header' : 'You checked in the following items:<hr/><ol>',
323                                         'line_item' : '<li>Barcode: %barcode%\r\n',
324                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
325                                 },
326                                 'offline_renew' : {
327                                         'type' : 'offline_renew',
328                                         'header' : 'You renewed the following items:<hr/><ol>',
329                                         'line_item' : '<li>Barcode: %barcode%\r\n',
330                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
331                                 },
332                                 'offline_inhouse_use' : {
333                                         'type' : 'offline_inhouse_use',
334                                         'header' : 'You marked the following in-house items used:<hr/><ol>',
335                                         'line_item' : '<li>Barcode: %barcode%\r\nUses: %count%',
336                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
337                                 },
338                                 'in_house_use' : {
339                                         'type' : 'items',
340                                         'header' : 'You marked the following in-house items used:<hr/><ol>',
341                                         'line_item' : '<li>Barcode: %barcode%\r\nUses: %uses%\r\n<br />%alert_message%',
342                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
343                                 },
344                                 'holds' : {
345                                         'type' : 'holds',
346                                         'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have the following titles on hold:<hr/><ol>',
347                                         'line_item' : '<li>%title%\r\n',
348                                         'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
349                                 },
350                 'hold_slip' : {
351                                         'type' : 'holds',
352                                         'header' : 'This item needs to be routed to <b>%route_to%</b>:<br/>\r\nBarcode: %item_barcode%<br/>\r\nTitle: %item_title%<br/>\r\n<br/>\r\n%hold_for_msg%<br/>\r\nBarcode: %PATRON_BARCODE%<br/>\r\nNotify by phone: %notify_by_phone%<br/>\r\nNotify by email: %notify_by_email%<br/>\r\n',
353                     'line_item' : '%formatted_note%<br/>\r\n',
354                     'footer' : '<br/>\r\nRequest date: %request_date%<br/>\r\nSlip Date: %TODAY_TRIM%<br/>\r\nPrinted by %STAFF_FIRSTNAME% at %SHORTNAME%<br/>\r\n<br/>\r\n'
355                 },
356                 'transit_slip' : {
357                                         'type' : 'transits',
358                                         'header' : 'This item needs to be routed to <b>%route_to%</b>:<br/>\r\n%route_to_org_fullname%<br/>\r\n%street1%<br/>\r\n%street2%<br/>\r\n%city_state_zip%<br/>\r\n<br/>\r\n',
359                     'line_item' : 'Barcode: %item_barcode%<br/>\r\nTitle: %item_title%<br/>\r\nAuthor: %item_author%<br>\r\n<br/>\r\n',
360                     'footer' : 'Slip Date: %TODAY_TRIM%<br/>\r\nPrinted by %STAFF_FIRSTNAME% at %SHORTNAME%<br/>\r\n<br/>\r\n'
361                 },
362                 'hold_transit_slip' : {
363                                         'type' : 'transits',
364                                         'header' : 'This item needs to be routed to <b>%route_to%</b>:<br/>\r\n%route_to_org_fullname%<br/>\r\n%street1%<br/>\r\n%street2%<br/>\r\n%city_state_zip%<br/>\r\n<br/>\r\nBarcode: %item_barcode%<br/>\r\nTitle: %item_title%<br/>\r\nAuthor: %item_author%<br>\r\n<br/>\r\n%hold_for_msg%<br/>\r\nBarcode: %PATRON_BARCODE%<br/>\r\nNotify by phone: %notify_by_phone%<br/>\r\nNotify by email: %notify_by_email%<br/>\r\n',
365                     'line_item' : '%formatted_note%<br/>\r\n',
366                     'footer' : '<br/>\r\nRequest date: %request_date%<br/>\r\nSlip Date: %TODAY_TRIM%<br/>\r\nPrinted by %STAFF_FIRSTNAME% at %SHORTNAME%<br/>\r\n<br/>\r\n'
367                 }
368                         }; 
369
370                         obj.stash( 'print_list_templates', 'print_list_types' );
371                 }
372         },
373
374         'network_retrieve' : function() {
375                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
376                 var obj = this;
377
378
379                 JSAN.use('util.file'); var file = new util.file('print_list_templates');
380                 obj.print_list_defaults();
381                 obj.data_progress('Default print templates set. ');
382                 if (file._file.exists()) {
383                         try {
384                                 var x = file.get_object();
385                                 if (x) {
386                                         for (var i in x) {
387                                                 obj.print_list_templates[i] = x[i];
388                                         }
389                                         obj.stash('print_list_templates');
390                                         obj.data_progress('Saved print templates 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('global_font_adjust');
399                 if (file._file.exists()) {
400                         try {
401                                 var x = file.get_object();
402                                 if (x) {
403                                         obj.global_font_adjust = x;
404                                         obj.stash('global_font_adjust');
405                                         obj.data_progress('Saved font settings retrieved from file. ');
406                                 }
407                         } catch(E) {
408                                 alert(E);
409                         }
410                 }
411                 file.close();
412
413                 JSAN.use('util.file'); var file = new util.file('no_sound');
414                 if (file._file.exists()) {
415                         try {
416                                 var x = file.get_content();
417                                 if (x) {
418                                         obj.no_sound = x;
419                                         obj.stash('no_sound');
420                                         obj.data_progress('Saved sound settings retrieved from file. ');
421                                 }
422                         } catch(E) {
423                                 alert(E);
424                         }
425                 }
426                 file.close();
427
428                 JSAN.use('util.file'); var file = new util.file('print_strategy');
429                 if (file._file.exists()) {
430                         try {
431                                 var x = file.get_content();
432                                 if (x) {
433                                         obj.print_strategy = x;
434                                         obj.stash('print_strategy');
435                                         obj.data_progress('Print strategy retrieved from file. ');
436                                 }
437                         } catch(E) {
438                                 alert(E);
439                         }
440                 }
441                 file.close();
442
443                 JSAN.use('util.functional');
444                 JSAN.use('util.fm_utils');
445
446                 function gen_fm_retrieval_func(classname,data) {
447                         var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
448                         return function () {
449                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
450
451                                 function convert() {
452                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
453                                         try {
454                                                 if (obj.list[classname].constructor.name == 'Array') {
455                                                         obj.hash[classname] = 
456                                                                 util.functional.convert_object_list_to_hash(
457                                                                         obj.list[classname]
458                                                                 );
459                                                 }
460                                         } catch(E) {
461
462                                                 obj.error.sdump('D_ERROR',E + '\n');
463                                         }
464
465                                 }
466
467                                 try {
468                                         var level = obj.error.sdump_levels.D_SES_RESULT;
469                                         if (classname == 'aou' || classname == 'my_aou')
470                                                 obj.error.sdump_levels.D_SES_RESULT = false;
471                                         var robj = obj.network.request( app, method, params);
472                                         if (robj != null && typeof robj.ilsevent != 'undefined') {
473                                                 obj.error.standard_unexpected_error_alert('The staff client failed to retrieve expected data from this call, "' + method + '"',robj);
474                                                 throw(robj);
475                                         }
476                                         obj.list[classname] = robj == null ? [] : robj;
477                                         obj.error.sdump_levels.D_SES_RESULT = level;
478                                         convert();
479                                         obj.data_progress('Retrieved list for ' + classname + ' objects. ');
480
481                                 } catch(E) {
482                                         // if cacheable, try offline
483                                         if (cacheable) {
484                                                 /* FIXME -- we're going to revisit caching and do it differently
485                                                 try {
486                                                         var file = new util.file( classname );
487                                                         obj.list[classname] = file.get_object(); file.close();
488                                                         convert();
489                                                 } catch(E) {
490                                                         throw(E);
491                                                 }
492                                                 */
493                                                 throw(E); // for now
494                                         } else {
495                                                 throw(E); // for now
496                                         }
497                                 }
498                         }
499                 }
500
501                 this.chain = [];
502
503                 this.chain.push(
504                         function() {
505                                 try {
506                                         var robj = obj.network.simple_request('CIRC_MODIFIER_LIST',[]);
507                                         if (typeof robj.ilsevent != 'undefined') throw(robj);
508                                         obj.list.circ_modifier = robj;
509                                         obj.data_progress('Retrieved circ modifier list. ');
510                                 } catch(E) {
511                                         var error = 'Error: ' + js2JSON(E);
512                                         obj.error.sdump('D_ERROR',error);
513                                         throw(E);
514                                 }
515                         }
516                 );
517
518                 this.chain.push(
519                         function() {
520                                 var f = gen_fm_retrieval_func(
521                                         'cnal',
522                                         [
523                                                 api.FM_CNAL_RETRIEVE.app,
524                                                 api.FM_CNAL_RETRIEVE.method,
525                                                 [ obj.session.key ],
526                                                 false
527                                         ]
528                                 );
529                                 try {
530                                         f();
531                                 } catch(E) {
532                                         var error = 'Error: ' + js2JSON(E);
533                                         obj.error.sdump('D_ERROR',error);
534                                         throw(E);
535                                 }
536                         }
537                 );
538
539                 this.chain.push(
540                         function() {
541                                 var f = gen_fm_retrieval_func(
542                                         'csp',
543                                         [
544                                                 api.FM_CSP_PCRUD_SEARCH.app,
545                                                 api.FM_CSP_PCRUD_SEARCH.method,
546                                                 [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"csp":"id"}} ],
547                                                 false
548                                         ]
549                                 );
550                                 try {
551                                         f();
552                                 } catch(E) {
553                                         var error = 'Error: ' + js2JSON(E);
554                                         obj.error.sdump('D_ERROR',error);
555                                         throw(E);
556                                 }
557                         }
558                 );
559
560                 this.chain.push(
561                         function() {
562                                 var f = gen_fm_retrieval_func(
563                                         'ahrcc',
564                                         [
565                                                 api.FM_AHRCC_PCRUD_SEARCH.app,
566                                                 api.FM_AHRCC_PCRUD_SEARCH.method,
567                                                 [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"ahrcc":"label"}} ],
568                                                 false
569                                         ]
570                                 );
571                                 try {
572                                         f();
573                                 } catch(E) {
574                                         var error = 'Error: ' + js2JSON(E);
575                                         obj.error.sdump('D_ERROR',error);
576                                         throw(E);
577                                 }
578                         }
579                 );
580
581
582                 this.chain.push(
583                         function() {
584                                 var f = gen_fm_retrieval_func(
585                                         'au',
586                                         [
587                                                 api.FM_AU_RETRIEVE_VIA_SESSION.app,
588                                                 api.FM_AU_RETRIEVE_VIA_SESSION.method,
589                                                 [ obj.session.key ],
590                                                 false
591                                         ]
592                                 );
593                                 try {
594                                         f();
595                                 } catch(E) {
596                                         var error = 'Error: ' + js2JSON(E);
597                                         obj.error.sdump('D_ERROR',error);
598                                         throw(E);
599                                 }
600                                 obj.list.au = [ obj.list.au ];
601                         }
602                 );
603
604                 this.chain.push(
605                         function() {
606                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
607                                 var f = gen_fm_retrieval_func(
608                                         'my_asv',
609                                         [
610                                                 api.FM_ASV_RETRIEVE_REQUIRED.app,
611                                                 api.FM_ASV_RETRIEVE_REQUIRED.method,
612                                                 [ obj.session.key ],
613                                                 true
614                                         ]
615                                 );
616                                 try {
617                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
618                                         f();
619                                 } catch(E) {
620                                         var error = 'Error: ' + js2JSON(E);
621                                         obj.error.sdump('D_ERROR',error);
622                                         throw(E);
623                                 }
624                         }
625                 );
626
627                 this.chain.push(
628                         function() {
629                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
630                                 var f = gen_fm_retrieval_func(
631                                         'asv',
632                                         [
633                                                 api.FM_ASV_RETRIEVE.app,
634                                                 api.FM_ASV_RETRIEVE.method,
635                                                 [ obj.session.key ],
636                                                 true
637                                         ]
638                                 );
639                                 try {
640                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
641                                         f();
642                                 } catch(E) {
643                                         var error = 'Error: ' + js2JSON(E);
644                                         obj.error.sdump('D_ERROR',error);
645                                         throw(E);
646                                 }
647                         }
648                 );
649
650                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
651
652                 for (var i in this._fm_objects) {
653                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
654                 }
655
656                 // The previous org_tree call returned a tree, not a list or hash.
657                 this.chain.push(
658                         function () {
659                                 obj.tree.aou = obj.list.aou;
660                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
661                                 for (var i = 0; i < obj.list.aou.length; i++) {
662                                         var c = obj.list.aou[i].children();
663                                         if (!c) c = [];
664                                         c = c.sort(
665                                                 function( a, b ) {
666                                                         if (a.shortname() < b.shortname()) return -1;
667                                                         if (a.shortname() > b.shortname()) return 1;
668                                                         return 0;
669                                                 }
670                                         );
671                                         obj.list.aou[i].children( c );
672                                 }
673                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
674                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
675                         }
676                 );
677
678                 // The previous pgt call returned a tree, not a list or hash.
679                 this.chain.push(
680                         function () {
681                                 obj.tree.pgt = obj.list.pgt;
682                                 obj.list.pgt = util.fm_utils.flatten_ou_branch( obj.tree.pgt );
683                                 obj.hash.pgt = util.functional.convert_object_list_to_hash( obj.list.pgt );
684                         }
685                 );
686
687                 // Do these after we get the user object
688
689                 this.chain.push(
690                         function() {
691                                 try {
692                                         var robj = obj.network.simple_request('FM_AOUS_RETRIEVE',[ obj.session.key, obj.list.au[0].ws_ou() ]);
693                                         if (typeof robj.ilsevent != 'undefined') throw(robj);
694                                         obj.hash.aous = robj;
695                                         obj.data_progress('Retrieved org unit settings. ');
696                                 } catch(E) {
697                                         var error = 'Error: ' + js2JSON(E);
698                                         obj.error.sdump('D_ERROR',error);
699                                         throw(E);
700                                 }
701                         }
702                 );
703
704                 this.chain.push(
705
706                         function() {
707
708                                 gen_fm_retrieval_func('my_aou', 
709                                         [ 
710                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
711                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
712                                                 [ obj.session.key, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
713                                                 true
714                                         ]
715                                 )();
716                         }
717                 );
718
719                 this.chain.push(
720
721                         function () {
722
723                                 gen_fm_retrieval_func( 'my_actsc', 
724                                         [ 
725                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
726                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
727                                                 [ obj.session.key, obj.list.au[0].ws_ou() ],
728                                                 true
729                                         ]
730                                 )();
731                         }
732                 );
733
734                 this.chain.push(
735
736                         function () {
737
738                                 gen_fm_retrieval_func( 'my_asc', 
739                                         [ 
740                                                 api.FM_ASC_RETRIEVE_VIA_AOU.app,
741                                                 api.FM_ASC_RETRIEVE_VIA_AOU.method,
742                                                 [ obj.session.key, obj.list.au[0].ws_ou() ],
743                                                 true
744                                         ]
745                                 )();
746                         }
747                 );
748
749
750                 this.chain.push(
751                         function() {
752                                 var f = gen_fm_retrieval_func(
753                                         'cnct',
754                                         [
755                                                 api.FM_CNCT_RETRIEVE.app,
756                                                 api.FM_CNCT_RETRIEVE.method,
757                                                 [ obj.list.au[0].ws_ou() ], 
758                                                 false
759                                         ]
760                                 );
761                                 try {
762                                         f();
763                                 } catch(E) {
764                                         var error = 'Error: ' + js2JSON(E);
765                                         obj.error.sdump('D_ERROR',error);
766                                         throw(E);
767                                 }
768                         }
769                 );
770
771                 this.chain.push(
772                         function() {
773                                 var f = gen_fm_retrieval_func(
774                                         'my_cnct',
775                                         [
776                                                 api.FM_CNCT_RETRIEVE.app,
777                                                 api.FM_CNCT_RETRIEVE.method,
778                                                 [ obj.list.au[0].ws_ou() ], 
779                                                 false
780                                         ]
781                                 );
782                                 try {
783                                         f();
784                                 } catch(E) {
785                                         var error = 'Error: ' + js2JSON(E);
786                                         obj.error.sdump('D_ERROR',error);
787                                         throw(E);
788                                 }
789                         }
790                 );
791
792
793                 this.chain.push(
794                         function() {
795                                 var f = gen_fm_retrieval_func(
796                                         'acpl',
797                                         [
798                                                 api.FM_ACPL_RETRIEVE.app,
799                                                 api.FM_ACPL_RETRIEVE.method,
800                                                 [ obj.list.au[0].ws_ou() ],
801                                                 false
802                                         ]
803                                 );
804                                 try {
805                                         f();
806                                 } catch(E) {
807                                         var error = 'Error: ' + js2JSON(E);
808                                         obj.error.sdump('D_ERROR',error);
809                                         throw(E);
810                                 }
811                         }
812                 );
813
814                 this.chain.push(
815                         function() {
816                                 var f = gen_fm_retrieval_func(
817                                         'cbt',
818                                         [
819                                                 api.FM_CBT_RETRIEVE.app,
820                                                 api.FM_CBT_RETRIEVE.method,
821                                                 [ obj.session.key, obj.list.au[0].ws_ou() ],
822                                                 false
823                                         ]
824                                 );
825                                 try {
826                                         f();
827                                 } catch(E) {
828                                         var error = 'Error: ' + js2JSON(E);
829                                         obj.error.sdump('D_ERROR',error);
830                                         throw(E);
831                                 }
832                         }
833                 );
834
835                 if (typeof this.on_complete == 'function') {
836
837                         this.chain.push( this.on_complete );
838                 }
839                 JSAN.use('util.exec'); this.exec = new util.exec();
840                 this.exec.on_error = function(E) { 
841                 
842                         if (typeof obj.on_error == 'function') {
843                                 return obj.on_error(E); /* false breaks chain */
844                         } else {
845                                 alert('oops: ' + E ); 
846                             return false; /* break chain */
847                         }
848
849                 }
850
851                 this.exec.chain( this.chain );
852
853         }
854 }
855
856 dump('exiting OpenILS/data.js\n');