]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
498725d9aca256dbf88dab9d8d50618d02450653
[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                 this.chain.push(
224                         gen_fm_retrieval_func('my_aou', 
225                                 [ 
226                                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
227                                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
228                                         [ obj.session, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
229                                         true
230                                 ]
231                         )
232                 );
233
234                 // Do this after we get the user object
235                 this.chain.push(
236
237                         function () {
238
239                                 gen_fm_retrieval_func( 'my_actsc', 
240                                         [ 
241                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
242                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
243                                                 [ obj.session, obj.list.au[0].ws_ou() ],
244                                                 true
245                                         ]
246                                 )();
247                         }
248                 );
249
250                 this.chain.push(
251
252                         function () {
253
254                                 gen_fm_retrieval_func( 'my_asc', 
255                                         [ 
256                                                 api.FM_ASC_RETRIEVE_VIA_AOU.app,
257                                                 api.FM_ASC_RETRIEVE_VIA_AOU.method,
258                                                 [ obj.session, obj.list.au[0].ws_ou() ],
259                                                 true
260                                         ]
261                                 )();
262                         }
263                 );
264
265
266                 this.chain.push(
267                         function() {
268                                 var f = gen_fm_retrieval_func(
269                                         'cnct',
270                                         [
271                                                 api.FM_CNCT_RETRIEVE.app,
272                                                 api.FM_CNCT_RETRIEVE.method,
273                                                 [ obj.list.au[0].ws_ou() ], 
274                                                 false
275                                         ]
276                                 );
277                                 try {
278                                         f();
279                                 } catch(E) {
280                                         var error = 'Error: ' + js2JSON(E);
281                                         obj.error.sdump('D_ERROR',error);
282                                         alert(error);
283                                         throw(E);
284                                 }
285                         }
286                 );
287
288
289                 if (typeof this.on_complete == 'function') {
290
291                         this.chain.push( this.on_complete );
292                 }
293                 JSAN.use('util.exec'); this.exec = new util.exec();
294                 this.exec.on_error = function(E) { 
295                 
296                         if (typeof obj.on_error == 'function') {
297                                 obj.on_error();
298                         } else {
299                                 alert('oops: ' + E ); 
300                         }
301
302                         return false; /* break chain */
303                 }
304
305                 this.exec.chain( this.chain );
306
307         }
308 }
309
310 dump('exiting OpenILS/data.js\n');