1 /* ---------------------------------------------------------------------------
2 * Copyright (C) 2008 Equinox Software, Inc
3 * Mike Rylander <miker@esilibrary.com>
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.
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 * ---------------------------------------------------------------------------
17 if(!dojo._hasResource["openils.PermaCrud"]) {
19 dojo._hasResource["openils.PermaCrud"] = true;
20 dojo.provide("openils.PermaCrud");
21 dojo.require("fieldmapper.Fieldmapper");
22 dojo.require("openils.User");
24 dojo.declare('openils.PermaCrud', null, {
29 authoritative : false,
31 constructor : function ( kwargs ) {
32 kwargs = kwargs || {};
34 this.authtoken = kwargs.authtoken;
35 this.authoritative = kwargs.authoritative;
39 new OpenSRF.ClientSession('open-ils.pcrud');
43 this.session.state == OSRF_APP_SESSION_CONNECTED
44 ) this.connected = true;
47 auth : function (token) {
48 if (token) this.authtoken = token;
49 return this.authtoken || openils.User.authtoken;
52 connect : function ( onerror ) {
53 if (!this.connected && !this.session.connect()) {
54 this.connected = false;
55 if (onerror) onerror(this.session);
58 this.connected = true;
62 disconnect : function ( onerror ) {
63 this.connected = false;
65 // disconnect returns nothing, which is null, which is not true, cause the following to always run ... arg.
66 if (!this.session.disconnect()) {
67 if (onerror) onerror(this.session);
72 _session_request : function ( args /* hash */, commitOnComplete /* set to true, else no */ ) {
75 var endstyle = 'rollback';
76 var aopts = dojo.mixin({}, args);
78 if (commitOnComplete) endstyle = 'commit';
80 if (me.authoritative) {
81 if (!me.connected) me.connect();
82 if (args.timeout && !args.oncomplete && !args.onresponse) { // pure sync call
83 args.oncomplete = function (r) {
84 me.session.request('open-ils.pcrud.transaction.' + endstyle, me.auth());
85 me.session.disconnect();
88 } else if (args.oncomplete) { // there's an oncomplete, fire that, and then end the transaction
89 var orig_oncomplete = args.oncomplete;
90 args.oncomplete = function (r) {
93 ret = orig_oncomplete(r);
95 me.session.request('open-ils.pcrud.transaction.' + endstyle, me.auth());
96 me.session.disconnect();
102 me.session.request('open-ils.pcrud.transaction.begin', me.auth());
104 return me.session.request( args );
107 retrieve : function ( fm_class /* Fieldmapper class hint */, id /* Fieldmapper object primary key value */, opts /* Option hash */) {
109 var req_hash = dojo.mixin(
111 { method : 'open-ils.pcrud.retrieve.' + fm_class,
112 params : [ this.auth(), id ]
116 if (!opts.async && !opts.timeout) req_hash.timeout = 10;
119 var req = this._session_request( req_hash );
122 req.onerror = function (r) { throw js2JSON(r); };
124 // if it's an async call and the user does not care about
125 // the responses, pull them off the network and discard them
126 if (!req_hash.timeout && !req.oncomplete)
127 req.oncomplete = function (r) { while(r.recv()){}; };
131 // for synchronous calls with no handlers, return the first received value
132 if (req_hash.timeout && !opts.oncomplete && !opts.onresponse) {
133 var resp = req.recv();
134 if(resp) return resp.content();
141 retrieveAll : function ( fm_class /* Fieldmapper class hint */, opts /* Option hash */) {
142 var pkey = fieldmapper[fm_class].Identifier;
146 if (opts.order_by) order_by.order_by = opts.order_by;
147 if (opts.select) order_by.select = opts.select;
148 if (opts.limit) order_by.limit = opts.limit;
149 if (opts.offset) order_by.offset = opts.offset;
150 if (opts.join) order_by.join = opts.join;
151 if (opts.flesh) order_by.flesh = opts.flesh;
152 if (opts.flesh_fields) order_by.flesh_fields = opts.flesh_fields;
154 var method = 'open-ils.pcrud.search.' + fm_class;
155 if(!opts.streaming) method += '.atomic';
158 search[pkey] = { '!=' : null };
160 var req_hash = dojo.mixin(
163 params : [ this.auth(), search, order_by ]
167 if (!opts.async && !opts.timeout) req_hash.timeout = 10;
170 var req = this._session_request( req_hash );
173 req.onerror = function (r) { throw js2JSON(r); };
175 // if it's an async call and the user does not care about
176 // the responses, pull them off the network and discard them
177 if (!req_hash.timeout && !req.oncomplete)
178 req.oncomplete = function (r) { while(r.recv()){}; };
182 // for synchronous calls with no handlers, return the first received value
183 if (req_hash.timeout && !opts.oncomplete && !opts.onresponse) {
184 var resp = req.recv();
185 if(resp) return resp.content();
192 search : function ( fm_class /* Fieldmapper class hint */, search /* Fieldmapper query object */, opts /* Option hash */) {
193 var return_type = 'search';
196 if (opts.order_by) order_by.order_by = opts.order_by;
197 if (opts.select) order_by.select = opts.select;
198 if (opts.limit) order_by.limit = opts.limit;
199 if (opts.offset) order_by.offset = opts.offset;
200 if (opts.join) order_by.join = opts.join;
201 if (opts.flesh) order_by.flesh = opts.flesh;
202 if (opts.flesh_fields) order_by.flesh_fields = opts.flesh_fields;
203 if (opts.id_list) return_type = 'id_list';
205 var method = 'open-ils.pcrud.' + return_type + '.' + fm_class;
206 if(!opts.streaming) method += '.atomic';
208 var req_hash = dojo.mixin(
211 params : [ this.auth(), search, order_by ]
215 if (!opts.async && !opts.timeout) req_hash.timeout = 10;
218 var req = this._session_request( req_hash );
221 req.onerror = function (r) { throw js2JSON(r); };
223 // if it's an async call and the user does not care about
224 // the responses, pull them off the network and discard them
225 if (!req_hash.timeout && !req.oncomplete)
226 req.oncomplete = function (r) { while(r.recv()){}; };
230 // for synchronous calls with no handlers, return the first received value
231 if (req_hash.timeout && !opts.oncomplete && !opts.onresponse) {
232 var resp = req.recv();
233 if(resp) return resp.content();
240 _CUD : function ( method /* 'create' or 'update' or 'delete' */, list /* Fieldmapper object */, opts /* Option hash */) {
243 if (dojo.isArray(list)) {
244 if (list.classname) list = [ list ];
249 if (!this.connected) this.connect();
252 var _return_list = [];
254 function _CUD_recursive ( obj_list, pos, final_complete, final_error ) {
255 var obj = obj_list[pos];
257 method : 'open-ils.pcrud.' + method + '.' + obj.classname,
258 params : [ _pcrud.auth(), obj ],
259 onerror : final_error || function (r) { _pcrud.disconnect(); throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj); }
262 var req = _pcrud.session.request( req_hash );
263 req._final_complete = final_complete;
264 req._final_error = final_error;
266 if (++pos == obj_list.length) {
267 req.oncomplete = function (r) {
270 if ( res && res.content() ) {
271 _return_list.push( res.content() );
272 _pcrud.session.request({
273 method : 'open-ils.pcrud.transaction.commit',
275 params : [ _pcrud.auth() ],
276 onerror : function (r) {
278 if (req._final_error) req._final_error(r)
279 else throw 'Transaction commit error';
281 oncomplete : function (r) {
283 if ( res && res.content() ) {
284 if(req._final_complete)
285 req._final_complete(req, _return_list);
289 if (req._final_error) req._final_error(r)
290 else throw 'Transaction commit error';
296 if (req._final_error) req._final_error(r)
297 else throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
301 req.onerror = function (r) {
303 if (req._final_error) req._final_error(r);
304 else throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
309 req._obj_list = obj_list;
310 req.oncomplete = function (r) {
312 if ( res && res.content() ) {
313 _return_list.push( res.content() );
314 _CUD_recursive( r._obj_list, r._pos, req._final_complete, req._final_error );
317 if (req._final_error) req._final_error(r);
318 else throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
321 req.onerror = function (r) {
323 if (req._final_error) req._final_error(r);
324 throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
331 var f_complete = opts.oncomplete;
332 var f_error = opts.onerror;
334 this.session.request({
335 method : 'open-ils.pcrud.transaction.begin',
337 params : [ _pcrud.auth() ],
338 onerror : function (r) {
340 throw 'Transaction begin error';
342 oncomplete : function (r) {
344 if ( res && res.content() ) {
345 _CUD_recursive( list, 0, f_complete, f_error );
348 throw 'Transaction begin error';
357 create : function ( list, opts ) {
358 return this._CUD( 'create', list, opts );
361 update : function ( list, opts ) {
362 var id_list = this._CUD( 'update', list, opts );
365 for (var idx = 0; idx < id_list.length; idx++) {
367 this.retrieve( list[idx].classname, id_list[idx] )
375 * 'delete' is a reserved keyword in JavaScript and can't be used
376 * in browsers like IE or Chrome, so we define a safe synonym
377 * NOTE: delete() is now removed -- use eliminate instead
379 delete : function ( list, opts ) {
380 return this._CUD( 'delete', list, opts );
384 eliminate: function ( list, opts ) {
385 return this._CUD( 'delete', list, opts );
388 apply : function ( list, opts ) {
389 this._auto_CUD( list, opts );
392 _auto_CUD : function ( list /* Fieldmapper object */, opts /* Option hash */) {
396 if (dojo.isArray(list)) {
397 if (list.classname) list = [ list ];
402 if (!this.connected) this.connect();
405 var _return_list = [];
407 function _auto_CUD_recursive ( obj_list, pos, final_complete, final_error ) {
408 var obj = obj_list[pos];
411 if (obj.ischanged()) method = 'update';
412 if (obj.isnew()) method = 'create';
413 if (obj.isdeleted()) method = 'delete';
415 return _auto_CUD_recursive(obj_list, pos+1, final_complete, final_error);
419 method : 'open-ils.pcrud.' + method + '.' + obj.classname,
421 params : [ _pcrud.auth(), obj ],
422 onerror : final_error || function (r) { _pcrud.disconnect(); throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj); }
425 var req = _pcrud.session.request( req_hash );
426 req._final_complete = final_complete;
427 req._final_error = final_error;
429 if (++pos == obj_list.length) {
430 req.oncomplete = function (r) {
433 if ( res && res.content() ) {
434 _return_list.push( res.content() );
435 _pcrud.session.request({
436 method : 'open-ils.pcrud.transaction.commit',
438 params : [ _pcrud.auth() ],
439 onerror : function (r) {
441 throw 'Transaction commit error';
443 oncomplete : function (r) {
445 if ( res && res.content() ) {
446 if (req._final_complete)
447 req._final_complete(req, _return_list);
451 if (req._final_error) req._final_error(r);
452 else throw 'Transaction commit error';
458 if (req._final_error) req._final_error(r)
459 else throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj);
463 req.onerror = function (r) {
465 if (req._final_error) req._final_error(r);
470 req._obj_list = obj_list;
471 req.oncomplete = function (r) {
473 if ( res && res.content() ) {
474 _return_list.push( res.content() );
475 _auto_CUD_recursive( r._obj_list, r._pos, req._final_complete, req._final_error );
478 if (req._final_error) req._final_error(r);
479 else throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj);
487 var f_complete = opts.oncomplete;
488 var f_error = opts.onerror;
490 this.session.request({
491 method : 'open-ils.pcrud.transaction.begin',
493 params : [ _pcrud.auth() ],
494 onerror : function (r) {
496 throw 'Transaction begin error';
498 oncomplete : function (r) {
500 if ( res && res.content() ) {
501 _auto_CUD_recursive( list, 0, f_complete, f_error );
504 throw 'Transaction begin error';