7e8d6dd649f02734dc54070feb33f990267aa819
[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         'network_retrieve' : function() {
95                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
96                 var obj = this;
97
98                 JSAN.use('util.file');
99                 JSAN.use('util.functional');
100                 JSAN.use('util.fm_utils');
101
102                 function gen_fm_retrieval_func(classname,data) {
103                         var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
104                         return function () {
105                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
106
107                                 function convert() {
108                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
109                                         try {
110                                                 if (obj.list[classname].constructor.name == 'Array') {
111                                                         obj.hash[classname] = 
112                                                                 util.functional.convert_object_list_to_hash(
113                                                                         obj.list[classname]
114                                                                 );
115                                                 }
116                                         } catch(E) {
117
118                                                 obj.error.sdump('D_ERROR',E + '\n');
119                                         }
120
121                                 }
122
123                                 try {
124                                         var level = obj.error.sdump_levels.D_SES_RESULT;
125                                         if (classname == 'aou' || classname == 'my_aou')
126                                                 obj.error.sdump_levels.D_SES_RESULT = false;
127                                         obj.list[classname] = obj.network.request( app, method, params);
128                                         obj.error.sdump_levels.D_SES_RESULT = level;
129                                         convert();
130                                         // if cacheable, store an offline copy
131                                         /* FIXME -- we're going to revisit caching and do it differently
132                                         if (cacheable) {
133                                                 var file = new util.file( classname );
134                                                 file.set_object( obj.list[classname] );
135                                         }
136                                         */
137
138                                 } catch(E) {
139                                         // if cacheable, try offline
140                                         if (cacheable) {
141                                                 /* FIXME -- we're going to revisit caching and do it differently
142                                                 try {
143                                                         var file = new util.file( classname );
144                                                         obj.list[classname] = file.get_object();
145                                                         convert();
146                                                 } catch(E) {
147                                                         throw(E);
148                                                 }
149                                                 */
150                                                 throw(E); // for now
151                                         } else {
152                                                 throw(E); // for now
153                                         }
154                                 }
155                         }
156                 }
157
158                 this.chain = [];
159
160                 this.chain.push(
161                         function() {
162                                 var f = gen_fm_retrieval_func(
163                                         'au',
164                                         [
165                                                 api.FM_AU_RETRIEVE_VIA_SESSION.app,
166                                                 api.FM_AU_RETRIEVE_VIA_SESSION.method,
167                                                 [ obj.session ],
168                                                 false
169                                         ]
170                                 );
171                                 try {
172                                         f();
173                                 } catch(E) {
174                                         var error = 'Error: ' + js2JSON(E);
175                                         obj.error.sdump('D_ERROR',error);
176                                         alert(error);
177                                         throw(E);
178                                 }
179                                 obj.list.au = [ obj.list.au ];
180                         }
181                 );
182
183                 this.chain.push(
184                         function() {
185                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
186                                 var f = gen_fm_retrieval_func(
187                                         'asv',
188                                         [
189                                                 api.FM_ASV_RETRIEVE_REQUIRED.app,
190                                                 api.FM_ASV_RETRIEVE_REQUIRED.method,
191                                                 [ obj.session ],
192                                                 true
193                                         ]
194                                 );
195                                 try {
196                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
197                                         f();
198                                 } catch(E) {
199                                         var error = 'Error: ' + js2JSON(E);
200                                         obj.error.sdump('D_ERROR',error);
201                                         alert(error);
202                                         throw(E);
203                                 }
204                         }
205                 );
206
207
208                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
209
210                 for (var i in this._fm_objects) {
211                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
212                 }
213
214                 // The previous org_tree call returned a tree, not a list or hash.
215                 this.chain.push(
216                         function () {
217                                 obj.tree.aou = obj.list.aou;
218                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
219                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
220                         }
221                 );
222
223                 // Do this after we get the user object
224                 this.chain.push(
225
226                         function() {
227
228                                 gen_fm_retrieval_func('my_aou', 
229                                         [ 
230                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
231                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
232                                                 [ obj.session, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
233                                                 true
234                                         ]
235                                 )();
236                         }
237                 );
238
239                 this.chain.push(
240
241                         function () {
242
243                                 gen_fm_retrieval_func( 'my_actsc', 
244                                         [ 
245                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
246                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
247                                                 [ obj.session, obj.list.au[0].ws_ou() ],
248                                                 true
249                                         ]
250                                 )();
251                         }
252                 );
253
254                 this.chain.push(
255
256                         function () {
257
258                                 gen_fm_retrieval_func( 'my_asc', 
259                                         [ 
260                                                 api.FM_ASC_RETRIEVE_VIA_AOU.app,
261                                                 api.FM_ASC_RETRIEVE_VIA_AOU.method,
262                                                 [ obj.session, obj.list.au[0].ws_ou() ],
263                                                 true
264                                         ]
265                                 )();
266                         }
267                 );
268
269
270                 this.chain.push(
271                         function() {
272                                 var f = gen_fm_retrieval_func(
273                                         'cnct',
274                                         [
275                                                 api.FM_CNCT_RETRIEVE.app,
276                                                 api.FM_CNCT_RETRIEVE.method,
277                                                 [ obj.list.au[0].ws_ou() ], 
278                                                 false
279                                         ]
280                                 );
281                                 try {
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
293                 if (typeof this.on_complete == 'function') {
294
295                         this.chain.push( this.on_complete );
296                 }
297                 JSAN.use('util.exec'); this.exec = new util.exec();
298                 this.exec.on_error = function(E) { 
299                 
300                         if (typeof obj.on_error == 'function') {
301                                 obj.on_error();
302                         } else {
303                                 alert('oops: ' + E ); 
304                         }
305
306                         return false; /* break chain */
307                 }
308
309                 this.exec.chain( this.chain );
310
311         }
312 }
313
314 dump('exiting OpenILS/data.js\n');