for now.. we still need to move the receipt templates to org settings
[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         'init' : function (params) {
21
22                 try {
23                         if (params && params.via == 'stash') {  
24                                 this.stash_retrieve();
25                         } else {
26                                 this.network_retrieve();
27                         }
28                 
29                 } catch(E) {
30                         this.error.sdump('D_ERROR','Error in OpenILS.data.init('
31                                 +js2JSON(params)+'): ' + js2JSON(E) );
32                 }
33
34
35         },
36
37         'stash' : function () {
38                 try {
39                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
40                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
41                         var data_cache=new OpenILS( );
42                         for (var i = 0; i < arguments.length; i++) {
43                                 this.error.sdump('D_DATA','stashing ' + arguments[i] + ' : ' + this[arguments[i]] + '\n');
44                                 data_cache.wrappedJSObject.OpenILS.prototype.data[arguments[i]] = this[arguments[i]];
45                         }
46                 } catch(E) {
47                         this.error.sdump('D_ERROR','Error in OpenILS.data.stash(): ' + js2JSON(E) );
48                 }
49         },
50
51         '_debug_stash' : function() {
52                 try {
53                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
54                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
55                         var data_cache=new OpenILS( );
56                         for (var i in data_cache.wrappedJSObject.OpenILS.prototype.data) {
57                                 dump('_debug_stash ' + i + '\n');
58                         }
59                 } catch(E) {
60                         this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
61                 }
62         },
63
64         '_fm_objects' : {
65
66                 'pgt' : [ api.FM_PGT_RETRIEVE.app, api.FM_PGT_RETRIEVE.method, [], true ],
67                 'cit' : [ api.FM_CIT_RETRIEVE.app, api.FM_CIT_RETRIEVE.method, [], true ],
68                 'cst' : [ api.FM_CST_RETRIEVE.app, api.FM_CST_RETRIEVE.method, [], true ],
69                 'acpl' : [ api.FM_ACPL_RETRIEVE.app, api.FM_ACPL_RETRIEVE.method, [], true ],
70                 'ccs' : [ api.FM_CCS_RETRIEVE.app, api.FM_CCS_RETRIEVE.method, [], true ],
71                 'aou' : [ api.FM_AOU_RETRIEVE.app, api.FM_AOU_RETRIEVE.method, [], true ],
72                 'aout' : [ api.FM_AOUT_RETRIEVE.app, api.FM_AOUT_RETRIEVE.method, [], true ]    
73         },
74
75         'stash_retrieve' : function() {
76                 try {
77                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
78                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
79                         var data_cache=new OpenILS( );
80                         var dc = data_cache.wrappedJSObject.OpenILS.prototype.data;
81                         for (var i in dc) {
82                                 this.error.sdump('D_DATA','Retrieving ' + i + ' : ' + dc[i] + '\n');
83                                 this[i] = dc[i];
84                         }
85                         if (typeof this.on_complete == 'function') {
86
87                                 this.on_complete();
88                         }
89                 } catch(E) {
90                         this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
91                 }
92         },
93
94         'print_list_defaults' : function() {
95                 var obj = this;
96                 if (typeof obj.print_list_templates == 'undefined') {
97                         obj.print_list_types = [ 'items', 'holds', 'patrons' ];
98                         obj.print_list_templates = { 
99                                 'item_status' : {
100                                         'type' : 'items',
101                                         'header' : 'You following items were checked:<hr/><ol>',
102                                         'line_item' : '<li>%title%\r\nBarcode: %barcode%\r\n',
103                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\n',
104                                 }, 
105                                 'items_out' : {
106                                         'type' : 'items',
107                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!\r\nYou have the following items:<hr/><ol>',
108                                         'line_item' : '<li>%title%\r\nBarcode: %barcode% Due: %due_date%\r\n',
109                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%',
110                                 }, 
111                                 'checkout' : {
112                                         'type' : 'items',
113                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!\r\nYou checked out the following items:<hr/><ol>',
114                                         'line_item' : '<li>%title%\r\nBarcode: %barcode% Due: %due_date%\r\n',
115                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%',
116                                 }, 
117                                 'checkin' : {
118                                         'type' : 'items',
119                                         'header' : 'You checked in the following items:<hr/><ol>',
120                                         'line_item' : '<li>%title%\r\nBarcode: %barcode%  Call Number: %call_number%\r\n',
121                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\n',
122                                 }, 
123                                 'holds' : {
124                                         'type' : 'holds',
125                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!\r\nYou have the following titles on hold:<hr/><ol>',
126                                         'line_item' : '<li>%title%\r\n',
127                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%',
128                                 } 
129                         }; 
130
131                         obj.stash( 'print_list_templates', 'print_list_types' );
132                 }
133         },
134
135         'network_retrieve' : function() {
136                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
137                 var obj = this;
138
139                 try { obj.print_list_defaults(); } catch(E) { alert(E); }
140
141                 JSAN.use('util.file');
142                 JSAN.use('util.functional');
143                 JSAN.use('util.fm_utils');
144
145                 function gen_fm_retrieval_func(classname,data) {
146                         var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
147                         return function () {
148                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
149
150                                 function convert() {
151                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
152                                         try {
153                                                 if (obj.list[classname].constructor.name == 'Array') {
154                                                         obj.hash[classname] = 
155                                                                 util.functional.convert_object_list_to_hash(
156                                                                         obj.list[classname]
157                                                                 );
158                                                 }
159                                         } catch(E) {
160
161                                                 obj.error.sdump('D_ERROR',E + '\n');
162                                         }
163
164                                 }
165
166                                 try {
167                                         var level = obj.error.sdump_levels.D_SES_RESULT;
168                                         if (classname == 'aou' || classname == 'my_aou')
169                                                 obj.error.sdump_levels.D_SES_RESULT = false;
170                                         obj.list[classname] = obj.network.request( app, method, params);
171                                         obj.error.sdump_levels.D_SES_RESULT = level;
172                                         convert();
173                                         // if cacheable, store an offline copy
174                                         /* FIXME -- we're going to revisit caching and do it differently
175                                         if (cacheable) {
176                                                 var file = new util.file( classname );
177                                                 file.set_object( obj.list[classname] );
178                                         }
179                                         */
180
181                                 } catch(E) {
182                                         // if cacheable, try offline
183                                         if (cacheable) {
184                                                 /* FIXME -- we're going to revisit caching and do it differently
185                                                 try {
186                                                         var file = new util.file( classname );
187                                                         obj.list[classname] = file.get_object();
188                                                         convert();
189                                                 } catch(E) {
190                                                         throw(E);
191                                                 }
192                                                 */
193                                                 throw(E); // for now
194                                         } else {
195                                                 throw(E); // for now
196                                         }
197                                 }
198                         }
199                 }
200
201                 this.chain = [];
202
203                 this.chain.push(
204                         function() {
205                                 var f = gen_fm_retrieval_func(
206                                         'au',
207                                         [
208                                                 api.FM_AU_RETRIEVE_VIA_SESSION.app,
209                                                 api.FM_AU_RETRIEVE_VIA_SESSION.method,
210                                                 [ obj.session ],
211                                                 false
212                                         ]
213                                 );
214                                 try {
215                                         f();
216                                 } catch(E) {
217                                         var error = 'Error: ' + js2JSON(E);
218                                         obj.error.sdump('D_ERROR',error);
219                                         alert(error);
220                                         throw(E);
221                                 }
222                                 obj.list.au = [ obj.list.au ];
223                         }
224                 );
225
226                 this.chain.push(
227                         function() {
228                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
229                                 var f = gen_fm_retrieval_func(
230                                         'asv',
231                                         [
232                                                 api.FM_ASV_RETRIEVE_REQUIRED.app,
233                                                 api.FM_ASV_RETRIEVE_REQUIRED.method,
234                                                 [ obj.session ],
235                                                 true
236                                         ]
237                                 );
238                                 try {
239                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
240                                         f();
241                                 } catch(E) {
242                                         var error = 'Error: ' + js2JSON(E);
243                                         obj.error.sdump('D_ERROR',error);
244                                         alert(error);
245                                         throw(E);
246                                 }
247                         }
248                 );
249
250
251                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
252
253                 for (var i in this._fm_objects) {
254                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
255                 }
256
257                 // The previous org_tree call returned a tree, not a list or hash.
258                 this.chain.push(
259                         function () {
260                                 obj.tree.aou = obj.list.aou;
261                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
262                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
263                         }
264                 );
265
266                 // Do this after we get the user object
267                 this.chain.push(
268
269                         function() {
270
271                                 gen_fm_retrieval_func('my_aou', 
272                                         [ 
273                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
274                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
275                                                 [ obj.session, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
276                                                 true
277                                         ]
278                                 )();
279                         }
280                 );
281
282                 this.chain.push(
283
284                         function () {
285
286                                 gen_fm_retrieval_func( 'my_actsc', 
287                                         [ 
288                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
289                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
290                                                 [ obj.session, obj.list.au[0].ws_ou() ],
291                                                 true
292                                         ]
293                                 )();
294                         }
295                 );
296
297                 this.chain.push(
298
299                         function () {
300
301                                 gen_fm_retrieval_func( 'my_asc', 
302                                         [ 
303                                                 api.FM_ASC_RETRIEVE_VIA_AOU.app,
304                                                 api.FM_ASC_RETRIEVE_VIA_AOU.method,
305                                                 [ obj.session, obj.list.au[0].ws_ou() ],
306                                                 true
307                                         ]
308                                 )();
309                         }
310                 );
311
312
313                 this.chain.push(
314                         function() {
315                                 var f = gen_fm_retrieval_func(
316                                         'cnct',
317                                         [
318                                                 api.FM_CNCT_RETRIEVE.app,
319                                                 api.FM_CNCT_RETRIEVE.method,
320                                                 [ obj.list.au[0].ws_ou() ], 
321                                                 false
322                                         ]
323                                 );
324                                 try {
325                                         f();
326                                 } catch(E) {
327                                         var error = 'Error: ' + js2JSON(E);
328                                         obj.error.sdump('D_ERROR',error);
329                                         alert(error);
330                                         throw(E);
331                                 }
332                         }
333                 );
334
335
336                 if (typeof this.on_complete == 'function') {
337
338                         this.chain.push( this.on_complete );
339                 }
340                 JSAN.use('util.exec'); this.exec = new util.exec();
341                 this.exec.on_error = function(E) { 
342                 
343                         if (typeof obj.on_error == 'function') {
344                                 obj.on_error();
345                         } else {
346                                 alert('oops: ' + E ); 
347                         }
348
349                         return false; /* break chain */
350                 }
351
352                 this.exec.chain( this.chain );
353
354         }
355 }
356
357 dump('exiting OpenILS/data.js\n');