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;
152 var method = 'open-ils.pcrud.search.' + fm_class;
153 if(!opts.streaming) method += '.atomic';
156 search[pkey] = { '!=' : null };
158 var req_hash = dojo.mixin(
161 params : [ this.auth(), search, order_by ]
165 if (!opts.async && !opts.timeout) req_hash.timeout = 10;
168 var req = this._session_request( req_hash );
171 req.onerror = function (r) { throw js2JSON(r); };
173 // if it's an async call and the user does not care about
174 // the responses, pull them off the network and discard them
175 if (!req_hash.timeout && !req.oncomplete)
176 req.oncomplete = function (r) { while(r.recv()){}; };
180 // for synchronous calls with no handlers, return the first received value
181 if (req_hash.timeout && !opts.oncomplete && !opts.onresponse) {
182 var resp = req.recv();
183 if(resp) return resp.content();
190 search : function ( fm_class /* Fieldmapper class hint */, search /* Fieldmapper query object */, opts /* Option hash */) {
191 var return_type = 'search';
194 if (opts.order_by) order_by.order_by = opts.order_by;
195 if (opts.select) order_by.select = opts.select;
196 if (opts.limit) order_by.limit = opts.limit;
197 if (opts.offset) order_by.offset = opts.offset;
198 if (opts.join) order_by.join = opts.join;
199 if (opts.id_list) return_type = 'id_list';
201 var method = 'open-ils.pcrud.' + return_type + '.' + fm_class;
202 if(!opts.streaming) method += '.atomic';
204 var req_hash = dojo.mixin(
207 params : [ this.auth(), search, order_by ]
211 if (!opts.async && !opts.timeout) req_hash.timeout = 10;
214 var req = this._session_request( req_hash );
217 req.onerror = function (r) { throw js2JSON(r); };
219 // if it's an async call and the user does not care about
220 // the responses, pull them off the network and discard them
221 if (!req_hash.timeout && !req.oncomplete)
222 req.oncomplete = function (r) { while(r.recv()){}; };
226 // for synchronous calls with no handlers, return the first received value
227 if (req_hash.timeout && !opts.oncomplete && !opts.onresponse) {
228 var resp = req.recv();
229 if(resp) return resp.content();
236 _CUD : function ( method /* 'create' or 'update' or 'delete' */, list /* Fieldmapper object */, opts /* Option hash */) {
239 if (dojo.isArray(list)) {
240 if (list.classname) list = [ list ];
245 if (!this.connected) this.connect();
248 var _return_list = [];
250 function _CUD_recursive ( obj_list, pos, final_complete, final_error ) {
251 var obj = obj_list[pos];
253 method : 'open-ils.pcrud.' + method + '.' + obj.classname,
254 params : [ _pcrud.auth(), obj ],
255 onerror : final_error || function (r) { _pcrud.disconnect(); throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj); }
258 var req = _pcrud.session.request( req_hash );
259 req._final_complete = final_complete;
260 req._final_error = final_error;
262 if (++pos == obj_list.length) {
263 req.oncomplete = function (r) {
266 if ( res && res.content() ) {
267 _return_list.push( res.content() );
268 _pcrud.session.request({
269 method : 'open-ils.pcrud.transaction.commit',
271 params : [ _pcrud.auth() ],
272 onerror : function (r) {
274 throw 'Transaction commit error';
276 oncomplete : function (r) {
278 if ( res && res.content() ) {
279 if(req._final_complete)
280 req._final_complete(req, _return_list);
284 throw 'Transaction commit error';
290 throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
294 req.onerror = function (r) {
295 if (r._final_error) r._final_error(r);
297 throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
302 req._obj_list = obj_list;
303 req.oncomplete = function (r) {
305 if ( res && res.content() ) {
306 _return_list.push( res.content() );
307 _CUD_recursive( r._obj_list, r._pos, r._final_complete );
310 throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
313 req.onerror = function (r) {
314 if (r._final_error) r._final_error(r);
316 throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
323 var f_complete = opts.oncomplete;
324 var f_error = opts.onerror;
326 this.session.request({
327 method : 'open-ils.pcrud.transaction.begin',
329 params : [ _pcrud.auth() ],
330 onerror : function (r) {
332 throw 'Transaction begin error';
334 oncomplete : function (r) {
336 if ( res && res.content() ) {
337 _CUD_recursive( list, 0, f_complete, f_error );
340 throw 'Transaction begin error';
349 create : function ( list, opts ) {
350 return this._CUD( 'create', list, opts );
353 update : function ( list, opts ) {
354 var id_list = this._CUD( 'update', list, opts );
357 for (var idx = 0; idx < id_list.length; idx++) {
359 this.retrieve( list[idx].classname, id_list[idx] )
367 * 'delete' is a reserved keyword in JavaScript and can't be used
368 * in browsers like IE or Chrome, so we define a safe synonym
369 * NOTE: delete() is now removed -- use eliminate instead
371 delete : function ( list, opts ) {
372 return this._CUD( 'delete', list, opts );
376 eliminate: function ( list, opts ) {
377 return this._CUD( 'delete', list, opts );
380 apply : function ( list, opts ) {
381 this._auto_CUD( list, opts );
384 _auto_CUD : function ( list /* Fieldmapper object */, opts /* Option hash */) {
388 if (dojo.isArray(list)) {
389 if (list.classname) list = [ list ];
394 if (!this.connected) this.connect();
398 function _auto_CUD_recursive ( obj_list, pos, final_complete, final_error ) {
399 var obj = obj_list[pos];
402 if (obj.ischanged()) method = 'update';
403 if (obj.isnew()) method = 'create';
404 if (obj.isdeleted()) method = 'delete';
406 return _auto_CUD_recursive(obj_list, pos+1, final_complete, final_error);
410 method : 'open-ils.pcrud.' + method + '.' + obj.classname,
412 params : [ _pcrud.auth(), obj ],
413 onerror : final_error || function (r) { _pcrud.disconnect(); throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj); }
416 var req = _pcrud.session.request( req_hash );
417 req._final_complete = final_complete;
418 req._final_error = final_error;
420 if (++pos == obj_list.length) {
421 req.oncomplete = function (r) {
423 _pcrud.session.request({
424 method : 'open-ils.pcrud.transaction.commit',
426 params : [ _pcrud.auth() ],
427 onerror : function (r) {
429 throw 'Transaction commit error';
431 oncomplete : function (r) {
433 if ( res && res.content() ) {
434 if (r._final_complete)
435 req._final_complete(req, _return_list);
439 throw 'Transaction commit error';
445 req.onerror = function (r) {
446 if (r._final_error) r._final_error(r);
452 req._obj_list = obj_list;
453 req.oncomplete = function (r) {
455 if ( res && res.content() ) {
456 _auto_CUD_recursive( r._obj_list, r._pos, r._final_complete, r._final_error );
459 throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj);
467 var f_complete = opts.oncomplete;
468 var f_error = opts.onerror;
470 this.session.request({
471 method : 'open-ils.pcrud.transaction.begin',
473 params : [ _pcrud.auth() ],
474 onerror : function (r) {
476 throw 'Transaction begin error';
478 oncomplete : function (r) {
480 if ( res && res.content() ) {
481 _auto_CUD_recursive( list, 0, f_complete, f_error );
484 throw 'Transaction begin error';