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 if (!this.session.disconnect()) {
62 if (onerror) onerror(this.session);
69 retrieve : function ( fm_class /* Fieldmapper class hint */, id /* Fieldmapper object primary key value */, opts /* Option hash */) {
71 var req_hash = dojo.mixin(
73 { method : 'open-ils.pcrud.retrieve.' + fm_class,
74 params : [ this.auth(), id ]
78 if (!opts.async && !opts.timeout) req_hash.timeout = 10;
81 var req = this.session.request( req_hash );
85 req.onerror = function (r) { throw js2JSON(r); };
88 req.oncomplete = function (r) { r.result = r.recv(); _pcrud.last_result = r.result; };
92 if (req_hash.timeout) return req.recv().content();
96 retrieveAll : function ( fm_class /* Fieldmapper class hint */, opts /* Option hash */) {
97 var pkey = fieldmapper[fm_class].Identifier;
101 if (opts.order_by) order_by.order_by = opts.order_by;
102 if (opts.select) order_by.select = opts.select;
104 var method = 'open-ils.pcrud.search.' + fm_class;
105 if(!opts.streaming) method += '.atomic';
108 search[pkey] = { '!=' : null };
110 var req_hash = dojo.mixin(
113 params : [ this.auth(), search, order_by ]
117 if (!opts.async && !opts.timeout) req_hash.timeout = 10;
120 var req = this.session.request( req_hash );
123 req.onerror = function (r) { throw js2JSON(r); };
126 req.oncomplete = function (r) { r.result = r.recv(); _pcrud.last_result = r.result; };
130 if (req_hash.timeout) return req.recv().content();
134 search : function ( fm_class /* Fieldmapper class hint */, search /* Fieldmapper query object */, opts /* Option hash */) {
137 if (opts.order_by) order_by.order_by = opts.order_by;
138 if (opts.select) order_by.select = opts.select;
140 var method = 'open-ils.pcrud.search.' + fm_class;
141 if(!opts.streaming) method += '.atomic';
143 var req_hash = dojo.mixin(
146 params : [ this.auth(), search, order_by ]
150 if (!opts.async && !opts.timeout) req_hash.timeout = 10;
153 var req = this.session.request( req_hash );
156 req.onerror = function (r) { throw js2JSON(r); };
159 req.oncomplete = function (r) { r.result = r.recv(); _pcrud.last_result = r.result; };
163 if (req_hash.timeout) return req.recv().content();
167 _CUD : function ( method /* 'create' or 'update' or 'delete' */, list /* Fieldmapper object */, opts /* Option hash */) {
170 if (dojo.isArray(list)) {
171 if (list.classname) list = [ list ];
176 if (!this.connected) this.connect();
180 function _CUD_recursive ( obj_list, pos, final_complete, final_error ) {
181 var obj = obj_list[pos];
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); }
188 var req = _pcrud.session.request( req_hash );
189 req._final_complete = final_complete;
190 req._final_error = final_error;
192 if (++pos == obj_list.length) {
193 req.oncomplete = function (r) {
195 _pcrud.session.request({
196 method : 'open-ils.pcrud.transaction.commit',
199 onerror : function (r) {
201 throw 'Transaction commit error';
203 oncomplete : function (r) {
205 if ( res && res.content() ) {
206 _auto_CUD_recursive( list, 0 );
209 throw 'Transaction commit error';
214 if (r._final_complete) r._final_complete(r);
218 req.onerror = function (r) {
219 if (r._final_error) r._final_error(r);
225 req._obj_list = obj_list;
226 req.oncomplete = function (r) {
228 if ( res && res.content() ) {
229 _CUD_recursive( r._obj_list, r._pos, r._final_complete );
232 throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
240 var f_complete = opts.oncomplete;
241 var f_error = opts.onerror;
243 this.session.request({
244 method : 'open-ils.pcrud.transaction.begin',
247 onerror : function (r) {
249 throw 'Transaction begin error';
251 oncomplete : function (r) {
253 if ( res && res.content() ) {
254 _CUD_recursive( list, 0, f_complete, f_error );
257 throw 'Transaction begin error';
263 create : function ( list, opts ) {
264 this._CUD( 'create', list, opts );
267 update : function ( list, opts ) {
268 this._CUD( 'update', list, opts );
271 delete : function ( list, opts ) {
272 this._CUD( 'delete', list, opts );
275 apply : function ( list, opts ) {
276 this._auto_CUD( list, opts );
279 _auto_CUD : function ( list /* Fieldmapper object */, opts /* Option hash */) {
283 if (dojo.isArray(list)) {
284 if (list.classname) list = [ list ];
289 if (!this.connected) this.connect();
293 function _auto_CUD_recursive ( obj_list, pos, final_complete, final_error ) {
294 var obj = obj_list[pos];
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';
303 method : 'open-ils.pcrud.' + method + '.' + obj.classname,
305 params : [ _pcrud.auth(), obj ],
306 onerror : final_error || function (r) { _pcrud.disconnect(); throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj); }
309 var req = _pcrud.session.request( req_hash );
310 req._final_complete = final_complete;
311 req._final_error = final_error;
313 if (++pos == obj_list.length) {
314 req.oncomplete = function (r) {
316 _pcrud.session.request({
317 method : 'open-ils.pcrud.transaction.commit',
320 onerror : function (r) {
322 throw 'Transaction commit error';
324 oncomplete : function (r) {
326 if ( res && res.content() ) {
327 _auto_CUD_recursive( list, 0 );
330 throw 'Transaction commit error';
335 if (r._final_complete) r._final_complete(r);
339 req.onerror = function (r) {
340 if (r._final_error) r._final_error(r);
346 req._obj_list = obj_list;
347 req.oncomplete = function (r) {
349 if ( res && res.content() ) {
350 _auto_CUD_recursive( r._obj_list, r._pos, r._final_complete, r._final_error );
353 throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj);
361 var f_complete = opts.oncomplete;
362 var f_error = opts.onerror;
364 this.session.request({
365 method : 'open-ils.pcrud.transaction.begin',
368 onerror : function (r) {
370 throw 'Transaction begin error';
372 oncomplete : function (r) {
374 if ( res && res.content() ) {
375 _auto_CUD_recursive( list, 0, f_complete, f_error );
378 throw 'Transaction begin error';