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