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