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