]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
8aed2350285ab8f6caf192c0b9cbf9d5bd02fcea
[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                                         obj.list[classname] = obj.network.request( app, method, params);
125                                         convert();
126                                         // if cacheable, store an offline copy
127                                         /* FIXME -- we're going to revisit caching and do it differently
128                                         if (cacheable) {
129                                                 var file = new util.file( classname );
130                                                 file.set_object( obj.list[classname] );
131                                         }
132                                         */
133
134                                 } catch(E) {
135                                         // if cacheable, try offline
136                                         if (cacheable) {
137                                                 /* FIXME -- we're going to revisit caching and do it differently
138                                                 try {
139                                                         var file = new util.file( classname );
140                                                         obj.list[classname] = file.get_object();
141                                                         convert();
142                                                 } catch(E) {
143                                                         throw(E);
144                                                 }
145                                                 */
146                                                 throw(E); // for now
147                                         } else {
148                                                 throw(E); // for now
149                                         }
150                                 }
151                         }
152                 }
153
154                 this.chain = [];
155
156                 this.chain.push(
157                         function() {
158                                 var f = gen_fm_retrieval_func(
159                                         'au',
160                                         [
161                                                 api.FM_AU_RETRIEVE_VIA_SESSION.app,
162                                                 api.FM_AU_RETRIEVE_VIA_SESSION.method,
163                                                 [ obj.session ],
164                                                 false
165                                         ]
166                                 );
167                                 try {
168                                         f();
169                                 } catch(E) {
170                                         var error = 'Error: ' + js2JSON(E);
171                                         obj.error.sdump('D_ERROR',error);
172                                         alert(error);
173                                         throw(E);
174                                 }
175                                 obj.list.au = [ obj.list.au ];
176                         }
177                 );
178
179                 this.chain.push(
180                         function() {
181                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
182                                 var f = gen_fm_retrieval_func(
183                                         'asv',
184                                         [
185                                                 api.FM_ASV_RETRIEVE_REQUIRED.app,
186                                                 api.FM_ASV_RETRIEVE_REQUIRED.method,
187                                                 [ obj.session ],
188                                                 true
189                                         ]
190                                 );
191                                 try {
192                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
193                                         f();
194                                 } catch(E) {
195                                         var error = 'Error: ' + js2JSON(E);
196                                         obj.error.sdump('D_ERROR',error);
197                                         alert(error);
198                                         throw(E);
199                                 }
200                         }
201                 );
202
203
204                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
205
206                 for (var i in this._fm_objects) {
207                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
208                 }
209
210                 // The previous org_tree call returned a tree, not a list or hash.
211                 this.chain.push(
212                         function () {
213                                 obj.tree.aou = obj.list.aou;
214                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
215                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
216                         }
217                 );
218
219                 this.chain.push(
220                         gen_fm_retrieval_func('my_aou', 
221                                 [ 
222                                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
223                                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
224                                         [ obj.session ],
225                                         true
226                                 ]
227                         )
228                 );
229
230                 // Do this after we get the user object
231                 this.chain.push(
232
233                         function () {
234
235                                 gen_fm_retrieval_func( 'my_actsc', 
236                                         [ 
237                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
238                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
239                                                 [ obj.session, obj.list.au[0].home_ou() ],
240                                                 true
241                                         ]
242                                 )();
243                         }
244                 );
245
246                 this.chain.push(
247
248                         function () {
249
250                                 gen_fm_retrieval_func( 'my_asc', 
251                                         [ 
252                                                 api.FM_ASC_RETRIEVE_VIA_AOU.app,
253                                                 api.FM_ASC_RETRIEVE_VIA_AOU.method,
254                                                 [ obj.session, obj.list.au[0].home_ou() ],
255                                                 true
256                                         ]
257                                 )();
258                         }
259                 );
260
261
262                 this.chain.push(
263                         function() {
264                                 var f = gen_fm_retrieval_func(
265                                         'cnct',
266                                         [
267                                                 api.FM_CNCT_RETRIEVE.app,
268                                                 api.FM_CNCT_RETRIEVE.method,
269                                                 /* FIXME -- later, in most places where we look at the home_ou of the sessioned staff member, we'll want to use the workstation ou instead */
270                                                 [ obj.list.au[0].home_ou() ], 
271                                                 false
272                                         ]
273                                 );
274                                 try {
275                                         f();
276                                 } catch(E) {
277                                         var error = 'Error: ' + js2JSON(E);
278                                         obj.error.sdump('D_ERROR',error);
279                                         alert(error);
280                                         throw(E);
281                                 }
282                         }
283                 );
284
285
286                 if (typeof this.on_complete == 'function') {
287
288                         this.chain.push( this.on_complete );
289                 }
290                 JSAN.use('util.exec'); this.exec = new util.exec();
291                 this.exec.on_error = function(E) { 
292                 
293                         if (typeof obj.on_error == 'function') {
294                                 obj.on_error();
295                         } else {
296                                 alert('oops: ' + E ); 
297                         }
298
299                         return false; /* break chain */
300                 }
301
302                 this.exec.chain( this.chain );
303
304         }
305 }
306
307 dump('exiting OpenILS/data.js\n');