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