]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
liberal use of PrivilegeManager. shotgun debugging :) Setting the data stash happens...
[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                                         if (cacheable) {
128                                                 var file = new util.file( classname );
129                                                 file.set_object( obj.list[classname] );
130                                         }
131
132                                 } catch(E) {
133                                         // if cacheable, try offline
134                                         if (cacheable) {
135                                                 try {
136                                                         var file = new util.file( classname );
137                                                         obj.list[classname] = file.get_object();
138                                                         convert();
139                                                 } catch(E) {
140                                                         throw(E);
141                                                 }
142                                         } else {
143                                                 throw(E); // for now
144                                         }
145                                 }
146                         }
147                 }
148
149                 this.chain = [];
150
151                 this.chain.push(
152                         function() {
153                                 var f = gen_fm_retrieval_func(
154                                         'au',
155                                         [
156                                                 api.FM_AU_RETRIEVE_VIA_SESSION.app,
157                                                 api.FM_AU_RETRIEVE_VIA_SESSION.method,
158                                                 [ obj.session ],
159                                                 false
160                                         ]
161                                 );
162                                 try {
163                                         f();
164                                 } catch(E) {
165                                         var error = 'Error: ' + js2JSON(E);
166                                         obj.error.sdump('D_ERROR',error);
167                                         alert(error);
168                                         throw(E);
169                                 }
170                                 obj.list.au = [ obj.list.au ];
171                         }
172                 );
173
174                 this.chain.push(
175                         function() {
176                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
177                                 var f = gen_fm_retrieval_func(
178                                         'asv',
179                                         [
180                                                 api.FM_ASV_RETRIEVE_REQUIRED.app,
181                                                 api.FM_ASV_RETRIEVE_REQUIRED.method,
182                                                 [ obj.session ],
183                                                 true
184                                         ]
185                                 );
186                                 try {
187                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
188                                         f();
189                                 } catch(E) {
190                                         var error = 'Error: ' + js2JSON(E);
191                                         obj.error.sdump('D_ERROR',error);
192                                         alert(error);
193                                         throw(E);
194                                 }
195                         }
196                 );
197
198
199                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
200
201                 for (var i in this._fm_objects) {
202                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
203                 }
204
205                 // The previous org_tree call returned a tree, not a list or hash.
206                 this.chain.push(
207                         function () {
208                                 obj.tree.aou = obj.list.aou;
209                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
210                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
211                         }
212                 );
213
214                 this.chain.push(
215                         gen_fm_retrieval_func('my_aou', 
216                                 [ 
217                                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
218                                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
219                                         [ obj.session ],
220                                         true
221                                 ]
222                         )
223                 );
224
225                 // Do this after we get the user object
226                 this.chain.push(
227
228                         function () {
229
230                                 gen_fm_retrieval_func( 'my_actsc', 
231                                         [ 
232                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
233                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
234                                                 [ obj.session, obj.list.au[0].home_ou() ],
235                                                 true
236                                         ]
237                                 )();
238                         }
239                 );
240
241                 if (typeof this.on_complete == 'function') {
242
243                         this.chain.push( this.on_complete );
244                 }
245                 JSAN.use('util.exec'); this.exec = new util.exec();
246                 this.exec.on_error = function(E) { 
247                 
248                         if (typeof obj.on_error == 'function') {
249                                 obj.on_error();
250                         } else {
251                                 alert('oops: ' + E ); 
252                         }
253
254                         return false; /* break chain */
255                 }
256
257                 this.exec.chain( this.chain );
258
259         }
260 }
261
262 dump('exiting OpenILS/data.js\n');