]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
another great renaming, and an un-naming
[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                 var obj = this;
96
97                 JSAN.use('util.file');
98                 JSAN.use('util.functional');
99                 JSAN.use('util.fm_utils');
100
101                 function gen_fm_retrieval_func(classname,data) {
102                         var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
103                         return function () {
104
105                                 function convert() {
106                                         try {
107                                                 if (obj.list[classname].constructor.name == 'Array') {
108                                                         obj.hash[classname] = 
109                                                                 util.functional.convert_object_list_to_hash(
110                                                                         obj.list[classname]
111                                                                 );
112                                                 }
113                                         } catch(E) {
114
115                                                 obj.error.sdump('D_ERROR',E + '\n');
116                                         }
117
118                                 }
119
120                                 try {
121                                         obj.list[classname] = obj.network.request( app, method, params);
122                                         convert();
123                                         // if cacheable, store an offline copy
124                                         if (cacheable) {
125                                                 var file = new util.file( classname );
126                                                 file.set_object( obj.list[classname] );
127                                         }
128
129                                 } catch(E) {
130                                         // if cacheable, try offline
131                                         if (cacheable) {
132                                                 try {
133                                                         var file = new util.file( classname );
134                                                         obj.list[classname] = file.get_object();
135                                                         convert();
136                                                 } catch(E) {
137                                                         throw(E);
138                                                 }
139                                         } else {
140                                                 throw(E); // for now
141                                         }
142                                 }
143                         }
144                 }
145
146                 this.chain = [];
147
148                 this.chain.push(
149                         function() {
150                                 var f = gen_fm_retrieval_func(
151                                         'au',
152                                         [
153                                                 api.FM_AU_RETRIEVE_VIA_SESSION.app,
154                                                 api.FM_AU_RETRIEVE_VIA_SESSION.method,
155                                                 [ obj.session ],
156                                                 false
157                                         ]
158                                 );
159                                 try {
160                                         f();
161                                 } catch(E) {
162                                         var error = 'Error: ' + js2JSON(E);
163                                         obj.error.sdump('D_ERROR',error);
164                                         alert(error);
165                                         throw(E);
166                                 }
167                                 obj.list.au = [ obj.list.au ];
168                         }
169                 );
170
171                 this.chain.push(
172                         function() {
173                                 var f = gen_fm_retrieval_func(
174                                         'asv',
175                                         [
176                                                 api.FM_ASV_RETRIEVE_REQUIRED.app,
177                                                 api.FM_ASV_RETRIEVE_REQUIRED.method,
178                                                 [ obj.session ],
179                                                 true
180                                         ]
181                                 );
182                                 try {
183                                         f();
184                                 } catch(E) {
185                                         var error = 'Error: ' + js2JSON(E);
186                                         obj.error.sdump('D_ERROR',error);
187                                         alert(error);
188                                         throw(E);
189                                 }
190                         }
191                 );
192
193
194                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
195
196                 for (var i in this._fm_objects) {
197                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
198                 }
199
200                 // The previous org_tree call returned a tree, not a list or hash.
201                 this.chain.push(
202                         function () {
203                                 obj.tree.aou = obj.list.aou;
204                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
205                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
206                         }
207                 );
208
209                 this.chain.push(
210                         gen_fm_retrieval_func('my_aou', 
211                                 [ 
212                                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
213                                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
214                                         [ obj.session ],
215                                         true
216                                 ]
217                         )
218                 );
219
220                 // Do this after we get the user object
221                 this.chain.push(
222
223                         function () {
224
225                                 gen_fm_retrieval_func( 'my_actsc', 
226                                         [ 
227                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
228                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
229                                                 [ obj.session, obj.list.au[0].home_ou() ],
230                                                 true
231                                         ]
232                                 )();
233                         }
234                 );
235
236                 if (typeof this.on_complete == 'function') {
237
238                         this.chain.push( this.on_complete );
239                 }
240                 JSAN.use('util.exec'); this.exec = new util.exec();
241                 this.exec.on_error = function(E) { 
242                 
243                         if (typeof obj.on_error == 'function') {
244                                 obj.on_error();
245                         } else {
246                                 alert('oops: ' + E ); 
247                         }
248
249                         return false; /* break chain */
250                 }
251
252                 this.exec.chain( this.chain );
253
254         }
255 }
256
257 dump('exiting OpenILS/data.js\n');