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