]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/evergreen/OpenILS/data.js
ce5b400797d45eb9751fb560d9bd684c46aff1b6
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / evergreen / 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
17         'init' : function (params) {
18
19                 try {
20                         if (params && params.via == 'stash') {  
21                                 this.stash_retrieve();
22                         } else {
23                                 this.network_retrieve();
24                         }
25                 
26                 } catch(E) {
27                         this.error.sdump('D_ERROR','Error in OpenILS.data.init('
28                                 +js2JSON(params)+'): ' + js2JSON(E) );
29                 }
30
31
32         },
33
34         'stash' : function () {
35                 try {
36                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
37                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
38                         var data_cache=new OpenILS( );
39                         for (var i = 0; i < arguments.length; i++) {
40                                 this.error.sdump('D_DATA','stashing ' + arguments[i] + ' : ' + this[arguments[i]] + '\n');
41                                 data_cache.wrappedJSObject.OpenILS.prototype.data[arguments[i]] = this[arguments[i]];
42                         }
43                 } catch(E) {
44                         this.error.sdump('D_ERROR','Error in OpenILS.data.stash(): ' + js2JSON(E) );
45                 }
46         },
47
48         '_debug_stash' : function() {
49                 try {
50                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
51                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
52                         var data_cache=new OpenILS( );
53                         for (var i in data_cache.wrappedJSObject.OpenILS.prototype.data) {
54                                 dump('_debug_stash ' + i + '\n');
55                         }
56                 } catch(E) {
57                         this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
58                 }
59         },
60
61         '_fm_objects' : {
62
63                 'pgt' : [ api.fm_pgt_retrieve.app, api.fm_pgt_retrieve.method, [], true ],
64                 'cit' : [ api.fm_cit_retrieve.app, api.fm_cit_retrieve.method, [], true ],
65                 'cst' : [ api.fm_cst_retrieve.app, api.fm_cst_retrieve.method, [], true ],
66                 'acpl' : [ api.fm_acpl_retrieve.app, api.fm_acpl_retrieve.method, [], true ],
67                 'ccs' : [ api.fm_ccs_retrieve.app, api.fm_ccs_retrieve.method, [], true ],
68                 'aou' : [ api.fm_aou_retrieve.app, api.fm_aou_retrieve.method, [], true ],
69                 'aout' : [ api.fm_aout_retrieve.app, api.fm_aout_retrieve.method, [], true ]    
70         },
71
72         'stash_retrieve' : function() {
73                 try {
74                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
75                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
76                         var data_cache=new OpenILS( );
77                         var dc = data_cache.wrappedJSObject.OpenILS.prototype.data;
78                         for (var i in dc) {
79                                 this.error.sdump('D_DATA','Retrieving ' + i + ' : ' + dc[i] + '\n');
80                                 this[i] = dc[i];
81                         }
82                         if (typeof this.on_complete == 'function') {
83
84                                 this.on_complete();
85                         }
86                 } catch(E) {
87                         this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
88                 }
89         },
90
91         'network_retrieve' : function() {
92                 var obj = this;
93
94                 JSAN.use('util.file');
95                 JSAN.use('util.functional');
96                 JSAN.use('util.fm_utils');
97
98                 function gen_fm_retrieval_func(classname,data) {
99                         var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
100                         return function () {
101
102                                 function convert() {
103                                         try {
104                                                 if (obj.list[classname].constructor.name == 'Array') {
105                                                         obj.hash[classname] = 
106                                                                 util.functional.convert_object_list_to_hash(
107                                                                         obj.list[classname]
108                                                                 );
109                                                 }
110                                         } catch(E) {
111
112                                                 obj.error.sdump('D_ERROR',E + '\n');
113                                         }
114
115                                 }
116
117                                 try {
118                                         obj.list[classname] = obj.network.request( app, method, params);
119                                         convert();
120                                         // if cacheable, store an offline copy
121                                         if (cacheable) {
122                                                 var file = new util.file( classname );
123                                                 file.set_object( obj.list[classname] );
124                                         }
125
126                                 } catch(E) {
127                                         // if cacheable, try offline
128                                         if (cacheable) {
129                                                 try {
130                                                         var file = new util.file( classname );
131                                                         obj.list[classname] = file.get_object();
132                                                         convert();
133                                                 } catch(E) {
134                                                         throw(E);
135                                                 }
136                                         } else {
137                                                 throw(E); // for now
138                                         }
139                                 }
140                         }
141                 }
142
143                 this.chain = [];
144
145                 this.chain.push(
146                         function() {
147                                 var f = gen_fm_retrieval_func(
148                                         'au',
149                                         [
150                                                 api.fm_au_retrieve_via_session.app,
151                                                 api.fm_au_retrieve_via_session.method,
152                                                 [ obj.session ],
153                                                 false
154                                         ]
155                                 );
156                                 try {
157                                         f();
158                                 } catch(E) {
159                                         // Probably the one thing we should not cache, so what do we do?
160                                         obj.list.au = new au();
161                                         obj.list.au.home_lib( '1' );
162                                 }
163                         }
164                 );
165
166                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
167
168                 for (var i in this._fm_objects) {
169                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
170                 }
171
172                 // The previous org_tree call returned a tree, not a list or hash.
173                 this.chain.push(
174                         function () {
175                                 obj.org_tree = obj.list.aou;
176                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.org_tree );
177                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
178                         }
179                 );
180
181                 this.chain.push(
182                         gen_fm_retrieval_func('my_aou', 
183                                 [ 
184                                         api.fm_aou_retrieve_related_via_session.app,
185                                         api.fm_aou_retrieve_related_via_session.method,
186                                         [ obj.session ],
187                                         true
188                                 ]
189                         )
190                 );
191
192                 // Do this after we get the user object
193                 this.chain.push(
194
195                         function () {
196
197                                 gen_fm_retrieval_func( 'my_actsc', 
198                                         [ 
199                                                 api.fm_actsc_retrieve_via_aou.app,
200                                                 api.fm_actsc_retrieve_via_aou.method,
201                                                 [ obj.session, obj.list.au.home_ou() ],
202                                                 true
203                                         ]
204                                 )();
205                         }
206                 );
207
208                 if (typeof this.on_complete == 'function') {
209
210                         this.chain.push( this.on_complete );
211                 }
212                 JSAN.use('util.exec'); this.exec = new util.exec();
213                 this.exec.on_error = function(E) { 
214                 
215                         if (typeof obj.on_error == 'function') {
216                                 obj.on_error();
217                         } else {
218                                 alert('oops: ' + E ); 
219                         }
220                 }
221
222                 this.exec.chain( this.chain );
223
224         }
225 }
226
227 dump('exiting OpenILS/data.js\n');