a3077d533fa48dd90fd1881215a3ae56ebbf5b69
[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
160                         JSAN.use('util.exec');
161                         util.exec.chain( this.chain );
162                 }
163         },
164
165         'stash' : function () {
166                 try {
167                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
168                         var data_cache=new OpenILS( );
169                         for (var i = 0; i < arguments.length; i++) {
170                                 this.error.sdump('D_DATA','stashing ' + arguments[i] + ' : ' + this[arguments[i]] + '\n');
171                                 data_cache.wrappedJSObject.OpenILS.prototype.data[arguments[i]] = this[arguments[i]];
172                         }
173                 } catch(E) {
174                         this.error.sdump('D_ERROR','Error in OpenILS.data.stash(): ' + js2JSON(E) );
175                 }
176         },
177
178         '_debug_stash' : function() {
179                 try {
180                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
181                         var data_cache=new OpenILS( );
182                         for (var i in data_cache.wrappedJSObject.OpenILS.prototype.data) {
183                                 dump('_debug_stash ' + i + '\n');
184                         }
185                 } catch(E) {
186                         this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
187                 }
188         },
189
190         '_fm_objects' : {
191
192                 'pgt' : [ 'open-ils.actor', 'open-ils.actor.groups.retrieve', [], true ],
193                 'cit' : [ 'open-ils.actor', 'open-ils.actor.user.ident_types.retrieve', [], true ],
194                 'cst' : [ 'open-ils.actor', 'open-ils.actor.standings.retrieve', [], true ],
195                 'acpl' : [ 'open-ils.search', 'open-ils.search.config.copy_location.retrieve.all', [], true ],
196                 'ccs' : [ 'open-ils.search', 'open-ils.search.config.copy_status.retrieve.all', [], true ],
197                 'aou' : [ 'open-ils.actor', 'open-ils.actor.org_tree.retrieve', [], true ],
198                 'aout' : [ 'open-ils.actor', 'open-ils.actor.org_types.retrieve', [], true ]    
199         }
200
201 }
202
203 dump('exiting OpenILS/data.js\n');