]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
some offline print templates
[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 = [ 'offline', 'items', 'holds', 'patrons' ];
98                         obj.print_list_templates = { 
99                                 'item_status' : {
100                                         'type' : 'items',
101                                         'header' : 'The following items are examined:<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                                 'offline_checkout' : {
118                                         'type' : 'offline',
119                                         'header' : 'Patron %OFFLINE_PATRON_BARCODE%\r\nYou checked out the following items:<hr/><ol>',
120                                         'line_item' : '<li>Barcode: %OFFLINE_BARCODE%\r\nDue: %OFFLINE_DUE_DATE%\r\n',
121                                         'footer' : '</ol><hr />%TODAY%',
122                                 },
123                                 'checkin' : {
124                                         'type' : 'items',
125                                         'header' : 'You checked in the following items:<hr/><ol>',
126                                         'line_item' : '<li>%title%\r\nBarcode: %barcode%  Call Number: %call_number%\r\n',
127                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\n',
128                                 }, 
129                                 'offline_checkin' : {
130                                         'type' : 'offline',
131                                         'header' : 'You checked in the following items:<hr/><ol>',
132                                         'line_item' : '<li>Barcode: %OFFLINE_BARCODE%\r\n',
133                                         'footer' : '</ol><hr />%TODAY%',
134                                 },
135                                 'offline_items_used' : {
136                                         'type' : 'offline',
137                                         'header' : 'You marked the following in-house items used:<hr/><ol>',
138                                         'line_item' : '<li>Barcode: %OFFLINE_BARCODE%\r\nUses: %COUNT%',
139                                         'footer' : '</ol><hr />%TODAY%',
140                                 },
141                                 'holds' : {
142                                         'type' : 'holds',
143                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!\r\nYou have the following titles on hold:<hr/><ol>',
144                                         'line_item' : '<li>%title%\r\n',
145                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY%\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%',
146                                 } 
147                         }; 
148
149                         obj.stash( 'print_list_templates', 'print_list_types' );
150                 }
151         },
152
153         'network_retrieve' : function() {
154                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
155                 var obj = this;
156
157                 try { obj.print_list_defaults(); } catch(E) { alert(E); }
158
159                 JSAN.use('util.file');
160                 JSAN.use('util.functional');
161                 JSAN.use('util.fm_utils');
162
163                 function gen_fm_retrieval_func(classname,data) {
164                         var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
165                         return function () {
166                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
167
168                                 function convert() {
169                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
170                                         try {
171                                                 if (obj.list[classname].constructor.name == 'Array') {
172                                                         obj.hash[classname] = 
173                                                                 util.functional.convert_object_list_to_hash(
174                                                                         obj.list[classname]
175                                                                 );
176                                                 }
177                                         } catch(E) {
178
179                                                 obj.error.sdump('D_ERROR',E + '\n');
180                                         }
181
182                                 }
183
184                                 try {
185                                         var level = obj.error.sdump_levels.D_SES_RESULT;
186                                         if (classname == 'aou' || classname == 'my_aou')
187                                                 obj.error.sdump_levels.D_SES_RESULT = false;
188                                         obj.list[classname] = obj.network.request( app, method, params);
189                                         obj.error.sdump_levels.D_SES_RESULT = level;
190                                         convert();
191                                         // if cacheable, store an offline copy
192                                         /* FIXME -- we're going to revisit caching and do it differently
193                                         if (cacheable) {
194                                                 var file = new util.file( classname );
195                                                 file.set_object( obj.list[classname] );
196                                         }
197                                         */
198
199                                 } catch(E) {
200                                         // if cacheable, try offline
201                                         if (cacheable) {
202                                                 /* FIXME -- we're going to revisit caching and do it differently
203                                                 try {
204                                                         var file = new util.file( classname );
205                                                         obj.list[classname] = file.get_object(); file.close();
206                                                         convert();
207                                                 } catch(E) {
208                                                         throw(E);
209                                                 }
210                                                 */
211                                                 throw(E); // for now
212                                         } else {
213                                                 throw(E); // for now
214                                         }
215                                 }
216                         }
217                 }
218
219                 this.chain = [];
220
221                 this.chain.push(
222                         function() {
223                                 var f = gen_fm_retrieval_func(
224                                         'au',
225                                         [
226                                                 api.FM_AU_RETRIEVE_VIA_SESSION.app,
227                                                 api.FM_AU_RETRIEVE_VIA_SESSION.method,
228                                                 [ obj.session ],
229                                                 false
230                                         ]
231                                 );
232                                 try {
233                                         f();
234                                 } catch(E) {
235                                         var error = 'Error: ' + js2JSON(E);
236                                         obj.error.sdump('D_ERROR',error);
237                                         alert(error);
238                                         throw(E);
239                                 }
240                                 obj.list.au = [ obj.list.au ];
241                         }
242                 );
243
244                 this.chain.push(
245                         function() {
246                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
247                                 var f = gen_fm_retrieval_func(
248                                         'my_asv',
249                                         [
250                                                 api.FM_ASV_RETRIEVE_REQUIRED.app,
251                                                 api.FM_ASV_RETRIEVE_REQUIRED.method,
252                                                 [ obj.session ],
253                                                 true
254                                         ]
255                                 );
256                                 try {
257                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
258                                         f();
259                                 } catch(E) {
260                                         var error = 'Error: ' + js2JSON(E);
261                                         obj.error.sdump('D_ERROR',error);
262                                         alert(error);
263                                         throw(E);
264                                 }
265                         }
266                 );
267
268                 this.chain.push(
269                         function() {
270                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
271                                 var f = gen_fm_retrieval_func(
272                                         'asv',
273                                         [
274                                                 api.FM_ASV_RETRIEVE.app,
275                                                 api.FM_ASV_RETRIEVE.method,
276                                                 [ obj.session ],
277                                                 true
278                                         ]
279                                 );
280                                 try {
281                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
282                                         f();
283                                 } catch(E) {
284                                         var error = 'Error: ' + js2JSON(E);
285                                         obj.error.sdump('D_ERROR',error);
286                                         alert(error);
287                                         throw(E);
288                                 }
289                         }
290                 );
291
292                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
293
294                 for (var i in this._fm_objects) {
295                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
296                 }
297
298                 // The previous org_tree call returned a tree, not a list or hash.
299                 this.chain.push(
300                         function () {
301                                 obj.tree.aou = obj.list.aou;
302                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
303                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
304                         }
305                 );
306
307                 // Do this after we get the user object
308                 this.chain.push(
309
310                         function() {
311
312                                 gen_fm_retrieval_func('my_aou', 
313                                         [ 
314                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
315                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
316                                                 [ obj.session, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
317                                                 true
318                                         ]
319                                 )();
320                         }
321                 );
322
323                 this.chain.push(
324
325                         function () {
326
327                                 gen_fm_retrieval_func( 'my_actsc', 
328                                         [ 
329                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
330                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
331                                                 [ obj.session, obj.list.au[0].ws_ou() ],
332                                                 true
333                                         ]
334                                 )();
335                         }
336                 );
337
338                 this.chain.push(
339
340                         function () {
341
342                                 gen_fm_retrieval_func( 'my_asc', 
343                                         [ 
344                                                 api.FM_ASC_RETRIEVE_VIA_AOU.app,
345                                                 api.FM_ASC_RETRIEVE_VIA_AOU.method,
346                                                 [ obj.session, obj.list.au[0].ws_ou() ],
347                                                 true
348                                         ]
349                                 )();
350                         }
351                 );
352
353
354                 this.chain.push(
355                         function() {
356                                 var f = gen_fm_retrieval_func(
357                                         'cnct',
358                                         [
359                                                 api.FM_CNCT_RETRIEVE.app,
360                                                 api.FM_CNCT_RETRIEVE.method,
361                                                 [ obj.list.au[0].ws_ou() ], 
362                                                 false
363                                         ]
364                                 );
365                                 try {
366                                         f();
367                                 } catch(E) {
368                                         var error = 'Error: ' + js2JSON(E);
369                                         obj.error.sdump('D_ERROR',error);
370                                         alert(error);
371                                         throw(E);
372                                 }
373                         }
374                 );
375
376
377                 if (typeof this.on_complete == 'function') {
378
379                         this.chain.push( this.on_complete );
380                 }
381                 JSAN.use('util.exec'); this.exec = new util.exec();
382                 this.exec.on_error = function(E) { 
383                 
384                         if (typeof obj.on_error == 'function') {
385                                 obj.on_error();
386                         } else {
387                                 alert('oops: ' + E ); 
388                         }
389
390                         return false; /* break chain */
391                 }
392
393                 this.exec.chain( this.chain );
394
395         }
396 }
397
398 dump('exiting OpenILS/data.js\n');