]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/PermaCrud.js
call recv() to get the result.
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / PermaCrud.js
1 /* ---------------------------------------------------------------------------
2  * Copyright (C) 2008  Equinox Software, Inc
3  * Mike Rylander <miker@esilibrary.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * ---------------------------------------------------------------------------
15  */
16
17 if(!dojo._hasResource["openils.PermaCrud"]) {
18
19     dojo._hasResource["openils.PermaCrud"] = true;
20     dojo.provide("openils.PermaCrud");
21     dojo.require("fieldmapper.Fieldmapper");
22     dojo.require("openils.User");
23
24     dojo.declare('openils.PermaCrud', null, {
25
26         session : null,
27         authtoken : null,
28         connnected : false,
29
30         constructor : function ( kwargs ) {
31             kwargs = kwargs || {};
32
33             this.authtoken = kwargs.authtoken;
34
35             this.session =
36                 kwargs.session ||
37                 new OpenSRF.ClientSession('open-ils.pcrud');
38
39             if (
40                 this.session &&
41                 this.session.state == OSRF_APP_SESSION_CONNECTED
42             ) this.connected = true;
43         },
44
45         auth : function (token) {
46             if (token) this.authtoken = token;
47             return this.authtoken || openils.User.authtoken;
48         },
49
50         connect : function ( onerror ) {
51             if (!this.connected && !this.session.connect()) {
52                 this.connected = false;
53                 if (onerror) onerror(this.session);
54                 return false;
55             }
56             this.connected = true;
57             return true;
58         },
59
60         disconnect : function ( onerror ) {
61             if (!this.session.disconnect()) {
62                 if (onerror) onerror(this.session);
63                 return false;
64             }
65             return true;
66         },
67         
68
69         retrieve : function ( fm_class /* Fieldmapper class hint */, id /* Fieldmapper object primary key value */,  opts /* Option hash */) {
70             if(!opts) opts = {};
71             var req_hash = dojo.mixin(
72                 opts, 
73                 { method : 'open-ils.pcrud.retrieve.' + fm_class,
74                   params : [ this.auth(), id ]
75                 }
76             );
77
78             if (!opts.async && !opts.timeout) req_hash.timeout = 10;
79
80             var _pcrud = this;
81             var req = this.session.request( req_hash );
82
83
84             if (!req.onerror)
85                 req.onerror = function (r) { throw js2JSON(r); };
86
87             if (!req.oncomplete)
88                 req.oncomplete = function (r) { r.result = r.recv(); _pcrud.last_result = r.result; };
89
90             req.send();
91
92             if (req_hash.timeout) return req.recv().content();
93             return req;
94         },
95
96         retrieveAll : function ( fm_class /* Fieldmapper class hint */, opts /* Option hash */) {
97             var pkey = fieldmapper[fm_class].Identifier;
98
99             if(!opts) opts = {};
100             var order_by = {};
101             if (opts.order_by) order_by.order_by = opts.order_by;
102             if (opts.select) order_by.select = opts.select;
103
104             var search = {};
105             search[pkey] = { '!=' : null };
106
107             var req_hash = dojo.mixin(
108                 opts, 
109                 { method : 'open-ils.pcrud.search.' + fm_class + '.atomic',
110                   params : [ this.auth(), search, order_by ]
111                 }
112             );
113
114             if (!opts.async && !opts.timeout) req_hash.timeout = 10;
115
116             var _pcrud = this;
117             var req = this.session.request( req_hash );
118
119             if (!req.onerror)
120                 req.onerror = function (r) { throw js2JSON(r); };
121
122             if (!req.oncomplete)
123                 req.oncomplete = function (r) { r.result = r.recv(); _pcrud.last_result = r.result; };
124
125             req.send();
126
127             if (req_hash.timeout) return req.recv().content();
128             return req;
129         },
130
131         search : function ( fm_class /* Fieldmapper class hint */, search /* Fieldmapper query object */, opts /* Option hash */) {
132             if(!opts) opts = {};
133             var order_by = {};
134             if (opts.order_by) order_by.order_by = opts.order_by;
135             if (opts.select) order_by.select = opts.select;
136
137             var req_hash = dojo.mixin(
138                 opts, 
139                 { method : 'open-ils.pcrud.search.' + fm_class + '.atomic',
140                   params : [ this.auth(), search, order_by ]
141                 }
142             );
143
144             if (!opts.async && !opts.timeout) req_hash.timeout = 10;
145
146             var _pcrud = this;
147             var req = this.session.request( req_hash );
148
149             if (!req.onerror)
150                 req.onerror = function (r) { throw js2JSON(r); };
151
152             if (!req.oncomplete)
153                 req.oncomplete = function (r) { r.result = r.recv(); _pcrud.last_result = r.result; };
154
155             req.send();
156
157             if (req_hash.timeout) return req.recv().content();
158             return req;
159         },
160
161         _CUD : function ( method /* 'create' or 'update' or 'delete' */, list /* Fieldmapper object */, opts /* Option hash */) {
162             if(!opts) opts = {};
163
164             if (dojo.isArray(list)) {
165                 if (list.classname) list = [ list ];
166             } else {
167                 list = [ list ];
168             }
169
170             if (!this.connected) this.connect();
171
172             var _pcrud = this;
173
174             function _CUD_recursive ( obj_list, pos, final_complete, final_error ) {
175                 var obj = obj_list[pos];
176                 var req_hash = {
177                     method : 'open-ils.pcrud.' + method + '.' + obj.classname,
178                     params : [ _pcrud.auth(), obj ],
179                     onerror : final_error || function (r) { _pcrud.disconnect(); throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj); }
180                 };
181
182                 var req = _pcrud.session.request( req_hash );
183                 req._final_complete = final_complete;
184                 req._final_error = final_error;
185
186                 if (++pos == obj_list.length) {
187                     req.oncomplete = function (r) {
188
189                         _pcrud.session.request({
190                             method : 'open-ils.pcrud.transaction.commit',
191                             timeout : 10,
192                             params : [ ses ],
193                             onerror : function (r) {
194                                 _pcrud.disconnect();
195                                 throw 'Transaction commit error';
196                             },      
197                             oncomplete : function (r) {
198                                 var res = r.recv();
199                                 if ( res && res.content() ) {
200                                     _auto_CUD_recursive( list, 0 );
201                                 } else {
202                                     _pcrud.disconnect();
203                                     throw 'Transaction commit error';
204                                 }
205                             },
206                         }).send();
207
208                         if (r._final_complete) r._final_complete(r);
209                         _pcrud.disconnect();
210                     };
211
212                     req.onerror = function (r) {
213                         if (r._final_error) r._final_error(r);
214                         _pcrud.disconnect();
215                     };
216
217                 } else {
218                     req._pos = pos;
219                     req._obj_list = obj_list;
220                     req.oncomplete = function (r) {
221                         var res = r.recv();
222                         if ( res && res.content() ) {
223                             _CUD_recursive( r._obj_list, r._pos, r._final_complete );
224                         } else {
225                             _pcrud.disconnect();
226                             throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
227                         }
228                     };
229                 }
230
231                 req.send();
232             }
233
234             var f_complete = opts.oncomplete;
235             var f_error = opts.onerror;
236
237             this.session.request({
238                 method : 'open-ils.pcrud.transaction.begin',
239                 timeout : 10,
240                 params : [ ses ],
241                 onerror : function (r) {
242                     _pcrud.disconnect();
243                     throw 'Transaction begin error';
244                 },      
245                 oncomplete : function (r) {
246                     var res = r.recv();
247                     if ( res && res.content() ) {
248                         _CUD_recursive( list, 0, f_complete, f_error );
249                     } else {
250                         _pcrud.disconnect();
251                         throw 'Transaction begin error';
252                     }
253                 },
254             }).send();
255         },
256
257         create : function ( list, opts ) {
258             this._CUD( 'create', list, opts );
259         },
260
261         update : function ( list, opts ) {
262             this._CUD( 'update', list, opts );
263         },
264
265         delete : function ( list, opts ) {
266             this._CUD( 'delete', list, opts );
267         },
268
269         apply : function ( list, opts ) {
270             this._auto_CUD( list, opts );
271         },
272
273         _auto_CUD : function ( list /* Fieldmapper object */, opts /* Option hash */) {
274
275             if(!opts) opts = {};
276
277             if (dojo.isArray(list)) {
278                 if (list.classname) list = [ list ];
279             } else {
280                 list = [ list ];
281             }
282
283             if (!this.connected) this.connect();
284
285             var _pcrud = this;
286
287             function _auto_CUD_recursive ( obj_list, pos, final_complete, final_error ) {
288                 var obj = obj_list[pos];
289
290                 var method;
291                 if (obj.ischanged()) method = 'update';
292                 if (obj.isnew())     method = 'create';
293                 if (obj.isdeleted()) method = 'delete';
294                 if (!method) throw 'No action detected';
295
296                 var req_hash = {
297                     method : 'open-ils.pcrud.' + method + '.' + obj.classname,
298                     timeout : 10,
299                     params : [ _pcrud.auth(), obj ],
300                     onerror : final_error || function (r) { _pcrud.disconnect(); throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj); }
301                 };
302
303                 var req = _pcrud.session.request( req_hash );
304                 req._final_complete = final_complete;
305                 req._final_error = final_error;
306
307                 if (++pos == obj_list.length) {
308                     req.oncomplete = function (r) {
309
310                         _pcrud.session.request({
311                             method : 'open-ils.pcrud.transaction.commit',
312                             timeout : 10,
313                             params : [ ses ],
314                             onerror : function (r) {
315                                 _pcrud.disconnect();
316                                 throw 'Transaction commit error';
317                             },      
318                             oncomplete : function (r) {
319                                 var res = r.recv();
320                                 if ( res && res.content() ) {
321                                     _auto_CUD_recursive( list, 0 );
322                                 } else {
323                                     _pcrud.disconnect();
324                                     throw 'Transaction commit error';
325                                 }
326                             },
327                         }).send();
328
329                         if (r._final_complete) r._final_complete(r);
330                         _pcrud.disconnect();
331                     };
332
333                     req.onerror = function (r) {
334                         if (r._final_error) r._final_error(r);
335                         _pcrud.disconnect();
336                     };
337
338                 } else {
339                     req._pos = pos;
340                     req._obj_list = obj_list;
341                     req.oncomplete = function (r) {
342                         var res = r.recv();
343                         if ( res && res.content() ) {
344                             _auto_CUD_recursive( r._obj_list, r._pos, r._final_complete, r._final_error );
345                         } else {
346                             _pcrud.disconnect();
347                             throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj);
348                         }
349                     };
350                 }
351
352                 req.send();
353             }
354
355             var f_complete = opts.oncomplete;
356             var f_error = opts.onerror;
357
358             this.session.request({
359                 method : 'open-ils.pcrud.transaction.begin',
360                 timeout : 10,
361                 params : [ ses ],
362                 onerror : function (r) {
363                     _pcrud.disconnect();
364                     throw 'Transaction begin error';
365                 },      
366                 oncomplete : function (r) {
367                     var res = r.recv();
368                     if ( res && res.content() ) {
369                         _auto_CUD_recursive( list, 0, f_complete, f_error );
370                     } else {
371                         _pcrud.disconnect();
372                         throw 'Transaction begin error';
373                     }
374                 },
375             }).send();
376         }
377
378     });
379 }
380
381