Don't feed big G to the objects hanging off of G if we don't have to. But can I...
[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 () {
136
137                 if (typeof this.on_complete == 'function') {
138
139                         this.chain.push( this.on_complete );
140                 }
141
142                 JSAN.use('util.exec');
143                 util.exec.chain( this.chain );
144         },
145
146         '_fm_objects' : {
147
148                 'pgt' : [ 'open-ils.actor', 'open-ils.actor.groups.retrieve', [], true ],
149                 'cit' : [ 'open-ils.actor', 'open-ils.actor.user.ident_types.retrieve', [], true ],
150                 'cst' : [ 'open-ils.actor', 'open-ils.actor.standings.retrieve', [], true ],
151                 'acpl' : [ 'open-ils.search', 'open-ils.search.config.copy_location.retrieve.all', [], true ],
152                 'ccs' : [ 'open-ils.search', 'open-ils.search.config.copy_status.retrieve.all', [], true ],
153                 'aou' : [ 'open-ils.actor', 'open-ils.actor.org_tree.retrieve', [], true ],
154                 'aout' : [ 'open-ils.actor', 'open-ils.actor.org_types.retrieve', [], true ]    
155         }
156
157 }
158
159 dump('exiting OpenILS/data.js\n');