c6dadb875951a6433c1114ffa157e6e7a178ceb6
[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                                 }
53                                 //throw(E); // for now
54                         }
55                 }
56         }
57
58         this.chain = [];
59
60         this.chain.push(
61                 gen_fm_retrieval_func(
62                         'au',
63                         [
64                                 'open-ils.search',
65                                 'open-ils.search.actor.user.session',
66                                 [ obj.G.auth.session.key ],
67                                 true
68                         ]
69                 )
70         );
71
72         obj.G.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
73
74         for (var i in this._fm_objects) {
75                 this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
76         }
77
78         // The previous org_tree call returned a tree, not a list or hash.
79         this.chain.push(
80                 function () {
81                         obj.org_tree = obj.list.aou;
82                         obj.list.aou = util.fm_utils.flatten_ou_branch( obj.org_tree );
83                         obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
84                 }
85         );
86
87         this.chain.push(
88                 gen_fm_retrieval_func('my_aou', 
89                         [ 
90                                 'open-ils.actor', 
91                                 'open-ils.actor.org_unit.full_path.retrieve', 
92                                 [ obj.G.auth.session.key ],
93                                 true
94                         ]
95                 )
96         );
97
98         // Do this after we get the user object
99         this.chain.push(
100
101                 function () {
102
103                         gen_fm_retrieval_func( 'my_actsc', 
104                                 [ 
105                                         'open-ils.circ', 
106                                         'open-ils.circ.stat_cat.actor.retrieve.all', 
107                                         [ obj.G.auth.session.key, obj.list.au.home_ou() ],
108                                         true
109                                 ]
110                         )();
111                 }
112         );
113
114         return this;
115 };
116
117 OpenILS.data.prototype = {
118
119         'list' : {},
120         'hash' : {},
121
122         'init' : function () {
123
124                 if (typeof this.on_complete == 'function') {
125
126                         this.chain.push( this.on_complete );
127                 }
128
129                 JSAN.use('util.exec');
130                 util.exec.chain( this.chain );
131         },
132
133         '_fm_objects' : {
134
135                 'pgt' : [ 'open-ils.actor', 'open-ils.actor.groups.retrieve', [], true ],
136                 'cit' : [ 'open-ils.actor', 'open-ils.actor.user.ident_types.retrieve', [], true ],
137                 'cst' : [ 'open-ils.actor', 'open-ils.actor.standings.retrieve', [], true ],
138                 'acpl' : [ 'open-ils.search', 'open-ils.search.config.copy_location.retrieve.all', [], true ],
139                 'ccs' : [ 'open-ils.search', 'open-ils.search.config.copy_status.retrieve.all', [], true ],
140                 'aou' : [ 'open-ils.actor', 'open-ils.actor.org_tree.retrieve', [], true ],
141                 'aout' : [ 'open-ils.actor', 'open-ils.actor.org_types.retrieve', [], true ]    
142         }
143
144 }
145
146 dump('exiting OpenILS/data.js\n');