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, {
30 constructor : function ( kwargs ) {
31 kwargs = kwargs || {};
33 this.authtoken = kwargs.authtoken;
37 new OpenSRF.ClientSession('open-ils.pcrud');
41 this.session.state == OSRF_APP_SESSION_CONNECTED
42 ) this.connected = true;
45 auth : function (token) {
46 if (token) this.authtoken = token;
47 return this.authtoken || openils.User.authtoken;
50 connect : function ( onerror ) {
51 if (!this.connected && !this.session.connect()) {
52 this.connected = false;
53 if (onerror) onerror(this.session);
56 this.connected = true;
60 disconnect : function ( onerror ) {
61 this.connected = false;
63 // disconnect returns nothing, which is null, which is not true, cause the following to always run ... arg.
64 if (!this.session.disconnect()) {
65 if (onerror) onerror(this.session);
71 retrieve : function ( fm_class /* Fieldmapper class hint */, id /* Fieldmapper object primary key value */, opts /* Option hash */) {
73 var req_hash = dojo.mixin(
75 { method : 'open-ils.pcrud.retrieve.' + fm_class,
76 params : [ this.auth(), id ]
80 if (!opts.async && !opts.timeout) req_hash.timeout = 10;
83 var req = this.session.request( req_hash );
86 req.onerror = function (r) { throw js2JSON(r); };
88 // if it's an async call and the user does not care about
89 // the responses, pull them off the network and discard them
90 if (!req_hash.timeout && !req.oncomplete)
91 req.oncomplete = function (r) { while(r.recv()){}; };
95 // for synchronous calls with no handlers, return the first received value
96 if (req_hash.timeout && !opts.oncomplete && !opts.onresponse) {
97 var resp = req.recv();
98 if(resp) return resp.content();
105 retrieveAll : function ( fm_class /* Fieldmapper class hint */, opts /* Option hash */) {
106 var pkey = fieldmapper[fm_class].Identifier;
110 if (opts.order_by) order_by.order_by = opts.order_by;
111 if (opts.select) order_by.select = opts.select;
112 if (opts.limit) order_by.limit = opts.limit;
113 if (opts.offset) order_by.offset = opts.offset;
115 var method = 'open-ils.pcrud.search.' + fm_class;
116 if(!opts.streaming) method += '.atomic';
119 search[pkey] = { '!=' : null };
121 var req_hash = dojo.mixin(
124 params : [ this.auth(), search, order_by ]
128 if (!opts.async && !opts.timeout) req_hash.timeout = 10;
131 var req = this.session.request( req_hash );
134 req.onerror = function (r) { throw js2JSON(r); };
136 // if it's an async call and the user does not care about
137 // the responses, pull them off the network and discard them
138 if (!req_hash.timeout && !req.oncomplete)
139 req.oncomplete = function (r) { while(r.recv()){}; };
143 // for synchronous calls with no handlers, return the first received value
144 if (req_hash.timeout && !opts.oncomplete && !opts.onresponse) {
145 var resp = req.recv();
146 if(resp) return resp.content();
153 search : function ( fm_class /* Fieldmapper class hint */, search /* Fieldmapper query object */, opts /* Option hash */) {
156 if (opts.order_by) order_by.order_by = opts.order_by;
157 if (opts.select) order_by.select = opts.select;
158 if (opts.limit) order_by.limit = opts.limit;
159 if (opts.offset) order_by.offset = opts.offset;
161 var method = 'open-ils.pcrud.search.' + fm_class;
162 if(!opts.streaming) method += '.atomic';
164 var req_hash = dojo.mixin(
167 params : [ this.auth(), search, order_by ]
171 if (!opts.async && !opts.timeout) req_hash.timeout = 10;
174 var req = this.session.request( req_hash );
177 req.onerror = function (r) { throw js2JSON(r); };
179 // if it's an async call and the user does not care about
180 // the responses, pull them off the network and discard them
181 if (!req_hash.timeout && !req.oncomplete)
182 req.oncomplete = function (r) { while(r.recv()){}; };
186 // for synchronous calls with no handlers, return the first received value
187 if (req_hash.timeout && !opts.oncomplete && !opts.onresponse) {
188 var resp = req.recv();
189 if(resp) return resp.content();
196 _CUD : function ( method /* 'create' or 'update' or 'delete' */, list /* Fieldmapper object */, opts /* Option hash */) {
199 if (dojo.isArray(list)) {
200 if (list.classname) list = [ list ];
205 if (!this.connected) this.connect();
208 var _return_list = [];
210 function _CUD_recursive ( obj_list, pos, final_complete, final_error ) {
211 var obj = obj_list[pos];
213 method : 'open-ils.pcrud.' + method + '.' + obj.classname,
214 params : [ _pcrud.auth(), obj ],
215 onerror : final_error || function (r) { _pcrud.disconnect(); throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj); }
218 var req = _pcrud.session.request( req_hash );
219 req._final_complete = final_complete;
220 req._final_error = final_error;
222 if (++pos == obj_list.length) {
223 req.oncomplete = function (r) {
226 if ( res && res.content() ) {
227 _return_list.push( res.content() );
228 _pcrud.session.request({
229 method : 'open-ils.pcrud.transaction.commit',
231 params : [ _pcrud.auth() ],
232 onerror : function (r) {
234 throw 'Transaction commit error';
236 oncomplete : function (r) {
238 if ( res && res.content() ) {
239 if(req._final_complete)
240 req._final_complete(req, _return_list);
244 throw 'Transaction commit error';
250 throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
254 req.onerror = function (r) {
255 if (r._final_error) r._final_error(r);
257 throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
262 req._obj_list = obj_list;
263 req.oncomplete = function (r) {
265 if ( res && res.content() ) {
266 _return_list.push( res.content() );
267 _CUD_recursive( r._obj_list, r._pos, r._final_complete );
270 throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
273 req.onerror = function (r) {
274 if (r._final_error) r._final_error(r);
276 throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
283 var f_complete = opts.oncomplete;
284 var f_error = opts.onerror;
286 this.session.request({
287 method : 'open-ils.pcrud.transaction.begin',
289 params : [ _pcrud.auth() ],
290 onerror : function (r) {
292 throw 'Transaction begin error';
294 oncomplete : function (r) {
296 if ( res && res.content() ) {
297 _CUD_recursive( list, 0, f_complete, f_error );
300 throw 'Transaction begin error';
309 create : function ( list, opts ) {
310 return this._CUD( 'create', list, opts );
313 update : function ( list, opts ) {
314 var id_list = this._CUD( 'update', list, opts );
317 for (var idx = 0; idx < id_list.length; idx++) {
319 this.retrieve( list[idx].classname, id_list[idx] )
326 delete : function ( list, opts ) {
327 return this._CUD( 'delete', list, opts );
331 * 'delete' is a reserved keyword in JavaScript and can't be used
332 * in browsers like IE or Chrome, so we define a safe synonym
334 eliminate: function ( list, opts ) {
335 return this._CUD( 'delete', list, opts );
338 apply : function ( list, opts ) {
339 this._auto_CUD( list, opts );
342 _auto_CUD : function ( list /* Fieldmapper object */, opts /* Option hash */) {
346 if (dojo.isArray(list)) {
347 if (list.classname) list = [ list ];
352 if (!this.connected) this.connect();
356 function _auto_CUD_recursive ( obj_list, pos, final_complete, final_error ) {
357 var obj = obj_list[pos];
360 if (obj.ischanged()) method = 'update';
361 if (obj.isnew()) method = 'create';
362 if (obj.isdeleted()) method = 'delete';
363 if (!method) throw 'No action detected';
366 method : 'open-ils.pcrud.' + method + '.' + obj.classname,
368 params : [ _pcrud.auth(), obj ],
369 onerror : final_error || function (r) { _pcrud.disconnect(); throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj); }
372 var req = _pcrud.session.request( req_hash );
373 req._final_complete = final_complete;
374 req._final_error = final_error;
376 if (++pos == obj_list.length) {
377 req.oncomplete = function (r) {
379 _pcrud.session.request({
380 method : 'open-ils.pcrud.transaction.commit',
382 params : [ _pcrud.auth() ],
383 onerror : function (r) {
385 throw 'Transaction commit error';
387 oncomplete : function (r) {
389 if ( res && res.content() ) {
390 _auto_CUD_recursive( list, 0 );
393 throw 'Transaction commit error';
398 if (r._final_complete) r._final_complete(r);
402 req.onerror = function (r) {
403 if (r._final_error) r._final_error(r);
409 req._obj_list = obj_list;
410 req.oncomplete = function (r) {
412 if ( res && res.content() ) {
413 _auto_CUD_recursive( r._obj_list, r._pos, r._final_complete, r._final_error );
416 throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj);
424 var f_complete = opts.oncomplete;
425 var f_error = opts.onerror;
427 this.session.request({
428 method : 'open-ils.pcrud.transaction.begin',
430 params : [ _pcrud.auth() ],
431 onerror : function (r) {
433 throw 'Transaction begin error';
435 oncomplete : function (r) {
437 if ( res && res.content() ) {
438 _auto_CUD_recursive( list, 0, f_complete, f_error );
441 throw 'Transaction begin error';