]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/PermaCrud.js
fixed recurse function name
[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 method = 'open-ils.pcrud.search.' + fm_class;
105             if(!opts.streaming) method += '.atomic';
106
107             var search = {};
108             search[pkey] = { '!=' : null };
109
110             var req_hash = dojo.mixin(
111                 opts, 
112                 { method : method,
113                   params : [ this.auth(), search, order_by ]
114                 }
115             );
116
117             if (!opts.async && !opts.timeout) req_hash.timeout = 10;
118
119             var _pcrud = this;
120             var req = this.session.request( req_hash );
121
122             if (!req.onerror)
123                 req.onerror = function (r) { throw js2JSON(r); };
124
125             if (!req.oncomplete)
126                 req.oncomplete = function (r) { r.result = r.recv(); _pcrud.last_result = r.result; };
127
128             req.send();
129
130             if (req_hash.timeout) return req.recv().content();
131             return req;
132         },
133
134         search : function ( fm_class /* Fieldmapper class hint */, search /* Fieldmapper query object */, opts /* Option hash */) {
135             if(!opts) opts = {};
136             var order_by = {};
137             if (opts.order_by) order_by.order_by = opts.order_by;
138             if (opts.select) order_by.select = opts.select;
139
140             var method = 'open-ils.pcrud.search.' + fm_class;
141             if(!opts.streaming) method += '.atomic';
142
143             var req_hash = dojo.mixin(
144                 opts, 
145                 { method : method,
146                   params : [ this.auth(), search, order_by ]
147                 }
148             );
149
150             if (!opts.async && !opts.timeout) req_hash.timeout = 10;
151
152             var _pcrud = this;
153             var req = this.session.request( req_hash );
154
155             if (!req.onerror)
156                 req.onerror = function (r) { throw js2JSON(r); };
157
158             if (!req.oncomplete)
159                 req.oncomplete = function (r) { r.result = r.recv(); _pcrud.last_result = r.result; };
160
161             req.send();
162
163             if (req_hash.timeout) return req.recv().content();
164             return req;
165         },
166
167         _CUD : function ( method /* 'create' or 'update' or 'delete' */, list /* Fieldmapper object */, opts /* Option hash */) {
168             if(!opts) opts = {};
169
170             if (dojo.isArray(list)) {
171                 if (list.classname) list = [ list ];
172             } else {
173                 list = [ list ];
174             }
175
176             if (!this.connected) this.connect();
177
178             var _pcrud = this;
179
180             function _CUD_recursive ( obj_list, pos, final_complete, final_error ) {
181                 var obj = obj_list[pos];
182                 var req_hash = {
183                     method : 'open-ils.pcrud.' + method + '.' + obj.classname,
184                     params : [ _pcrud.auth(), obj ],
185                     onerror : final_error || function (r) { _pcrud.disconnect(); throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj); }
186                 };
187
188                 var req = _pcrud.session.request( req_hash );
189                 req._final_complete = final_complete;
190                 req._final_error = final_error;
191
192                 if (++pos == obj_list.length) {
193                     req.oncomplete = function (r) {
194
195                         _pcrud.session.request({
196                             method : 'open-ils.pcrud.transaction.commit',
197                             timeout : 10,
198                             params : [ _pcrud.auth() ],
199                             onerror : function (r) {
200                                 _pcrud.disconnect();
201                                 throw 'Transaction commit error';
202                             },      
203                             oncomplete : function (r) {
204                                 var res = r.recv();
205                                 if ( res && res.content() ) {
206                                     _CUD_recursive( list, 0 );
207                                 } else {
208                                     _pcrud.disconnect();
209                                     throw 'Transaction commit error';
210                                 }
211                             },
212                         }).send();
213
214                         if (r._final_complete) r._final_complete(r);
215                         _pcrud.disconnect();
216                     };
217
218                     req.onerror = function (r) {
219                         if (r._final_error) r._final_error(r);
220                         _pcrud.disconnect();
221                     };
222
223                 } else {
224                     req._pos = pos;
225                     req._obj_list = obj_list;
226                     req.oncomplete = function (r) {
227                         var res = r.recv();
228                         if ( res && res.content() ) {
229                             _CUD_recursive( r._obj_list, r._pos, r._final_complete );
230                         } else {
231                             _pcrud.disconnect();
232                             throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
233                         }
234                     };
235                 }
236
237                 req.send();
238             }
239
240             var f_complete = opts.oncomplete;
241             var f_error = opts.onerror;
242
243             this.session.request({
244                 method : 'open-ils.pcrud.transaction.begin',
245                 timeout : 10,
246                 params : [ _pcrud.auth() ],
247                 onerror : function (r) {
248                     _pcrud.disconnect();
249                     throw 'Transaction begin error';
250                 },      
251                 oncomplete : function (r) {
252                     var res = r.recv();
253                     if ( res && res.content() ) {
254                         _CUD_recursive( list, 0, f_complete, f_error );
255                     } else {
256                         _pcrud.disconnect();
257                         throw 'Transaction begin error';
258                     }
259                 },
260             }).send();
261         },
262
263         create : function ( list, opts ) {
264             this._CUD( 'create', list, opts );
265         },
266
267         update : function ( list, opts ) {
268             this._CUD( 'update', list, opts );
269         },
270
271         delete : function ( list, opts ) {
272             this._CUD( 'delete', list, opts );
273         },
274
275         apply : function ( list, opts ) {
276             this._auto_CUD( list, opts );
277         },
278
279         _auto_CUD : function ( list /* Fieldmapper object */, opts /* Option hash */) {
280
281             if(!opts) opts = {};
282
283             if (dojo.isArray(list)) {
284                 if (list.classname) list = [ list ];
285             } else {
286                 list = [ list ];
287             }
288
289             if (!this.connected) this.connect();
290
291             var _pcrud = this;
292
293             function _auto_CUD_recursive ( obj_list, pos, final_complete, final_error ) {
294                 var obj = obj_list[pos];
295
296                 var method;
297                 if (obj.ischanged()) method = 'update';
298                 if (obj.isnew())     method = 'create';
299                 if (obj.isdeleted()) method = 'delete';
300                 if (!method) throw 'No action detected';
301
302                 var req_hash = {
303                     method : 'open-ils.pcrud.' + method + '.' + obj.classname,
304                     timeout : 10,
305                     params : [ _pcrud.auth(), obj ],
306                     onerror : final_error || function (r) { _pcrud.disconnect(); throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj); }
307                 };
308
309                 var req = _pcrud.session.request( req_hash );
310                 req._final_complete = final_complete;
311                 req._final_error = final_error;
312
313                 if (++pos == obj_list.length) {
314                     req.oncomplete = function (r) {
315
316                         _pcrud.session.request({
317                             method : 'open-ils.pcrud.transaction.commit',
318                             timeout : 10,
319                             params : [ _pcrud.auth() ],
320                             onerror : function (r) {
321                                 _pcrud.disconnect();
322                                 throw 'Transaction commit error';
323                             },      
324                             oncomplete : function (r) {
325                                 var res = r.recv();
326                                 if ( res && res.content() ) {
327                                     _auto_CUD_recursive( list, 0 );
328                                 } else {
329                                     _pcrud.disconnect();
330                                     throw 'Transaction commit error';
331                                 }
332                             },
333                         }).send();
334
335                         if (r._final_complete) r._final_complete(r);
336                         _pcrud.disconnect();
337                     };
338
339                     req.onerror = function (r) {
340                         if (r._final_error) r._final_error(r);
341                         _pcrud.disconnect();
342                     };
343
344                 } else {
345                     req._pos = pos;
346                     req._obj_list = obj_list;
347                     req.oncomplete = function (r) {
348                         var res = r.recv();
349                         if ( res && res.content() ) {
350                             _auto_CUD_recursive( r._obj_list, r._pos, r._final_complete, r._final_error );
351                         } else {
352                             _pcrud.disconnect();
353                             throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj);
354                         }
355                     };
356                 }
357
358                 req.send();
359             }
360
361             var f_complete = opts.oncomplete;
362             var f_error = opts.onerror;
363
364             this.session.request({
365                 method : 'open-ils.pcrud.transaction.begin',
366                 timeout : 10,
367                 params : [ _pcrud.auth() ],
368                 onerror : function (r) {
369                     _pcrud.disconnect();
370                     throw 'Transaction begin error';
371                 },      
372                 oncomplete : function (r) {
373                     var res = r.recv();
374                     if ( res && res.content() ) {
375                         _auto_CUD_recursive( list, 0, f_complete, f_error );
376                     } else {
377                         _pcrud.disconnect();
378                         throw 'Transaction begin error';
379                     }
380                 },
381             }).send();
382         }
383
384     });
385 }
386
387