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;
105 search[pkey] = { '!=' : null };
107 var req_hash = dojo.mixin(
109 { method : 'open-ils.pcrud.search.' + fm_class + '.atomic',
110 params : [ this.auth(), search, order_by ]
114 if (!opts.async && !opts.timeout) req_hash.timeout = 10;
117 var req = this.session.request( req_hash );
120 req.onerror = function (r) { throw js2JSON(r); };
123 req.oncomplete = function (r) { r.result = r.recv(); _pcrud.last_result = r.result; };
127 if (req_hash.timeout) return req.recv().content();
131 search : function ( fm_class /* Fieldmapper class hint */, search /* Fieldmapper query object */, opts /* Option hash */) {
134 if (opts.order_by) order_by.order_by = opts.order_by;
135 if (opts.select) order_by.select = opts.select;
137 var req_hash = dojo.mixin(
139 { method : 'open-ils.pcrud.search.' + fm_class + '.atomic',
140 params : [ this.auth(), search, order_by ]
144 if (!opts.async && !opts.timeout) req_hash.timeout = 10;
147 var req = this.session.request( req_hash );
150 req.onerror = function (r) { throw js2JSON(r); };
153 req.oncomplete = function (r) { r.result = r.recv(); _pcrud.last_result = r.result; };
157 if (req_hash.timeout) return req.recv().content();
161 _CUD : function ( method /* 'create' or 'update' or 'delete' */, list /* Fieldmapper object */, opts /* Option hash */) {
164 if (dojo.isArray(list)) {
165 if (list.classname) list = [ list ];
170 if (!this.connected) this.connect();
174 function _CUD_recursive ( obj_list, pos, final_complete, final_error ) {
175 var obj = obj_list[pos];
177 method : 'open-ils.pcrud.' + method + '.' + obj.classname,
178 params : [ _pcrud.auth(), obj ],
179 onerror : final_error || function (r) { _pcrud.disconnect(); throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj); }
182 var req = _pcrud.session.request( req_hash );
183 req._final_complete = final_complete;
184 req._final_error = final_error;
186 if (++pos == obj_list.length) {
187 req.oncomplete = function (r) {
189 _pcrud.session.request({
190 method : 'open-ils.pcrud.transaction.commit',
193 onerror : function (r) {
195 throw 'Transaction commit error';
197 oncomplete : function (r) {
199 if ( res && res.content() ) {
200 _auto_CUD_recursive( list, 0 );
203 throw 'Transaction commit error';
208 if (r._final_complete) r._final_complete(r);
212 req.onerror = function (r) {
213 if (r._final_error) r._final_error(r);
219 req._obj_list = obj_list;
220 req.oncomplete = function (r) {
222 if ( res && res.content() ) {
223 _CUD_recursive( r._obj_list, r._pos, r._final_complete );
226 throw '_CUD: Error creating, deleting or updating ' + js2JSON(obj);
234 var f_complete = opts.oncomplete;
235 var f_error = opts.onerror;
237 this.session.request({
238 method : 'open-ils.pcrud.transaction.begin',
241 onerror : function (r) {
243 throw 'Transaction begin error';
245 oncomplete : function (r) {
247 if ( res && res.content() ) {
248 _CUD_recursive( list, 0, f_complete, f_error );
251 throw 'Transaction begin error';
257 create : function ( list, opts ) {
258 this._CUD( 'create', list, opts );
261 update : function ( list, opts ) {
262 this._CUD( 'update', list, opts );
265 delete : function ( list, opts ) {
266 this._CUD( 'delete', list, opts );
269 apply : function ( list, opts ) {
270 this._auto_CUD( list, opts );
273 _auto_CUD : function ( list /* Fieldmapper object */, opts /* Option hash */) {
277 if (dojo.isArray(list)) {
278 if (list.classname) list = [ list ];
283 if (!this.connected) this.connect();
287 function _auto_CUD_recursive ( obj_list, pos, final_complete, final_error ) {
288 var obj = obj_list[pos];
291 if (obj.ischanged()) method = 'update';
292 if (obj.isnew()) method = 'create';
293 if (obj.isdeleted()) method = 'delete';
294 if (!method) throw 'No action detected';
297 method : 'open-ils.pcrud.' + method + '.' + obj.classname,
299 params : [ _pcrud.auth(), obj ],
300 onerror : final_error || function (r) { _pcrud.disconnect(); throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj); }
303 var req = _pcrud.session.request( req_hash );
304 req._final_complete = final_complete;
305 req._final_error = final_error;
307 if (++pos == obj_list.length) {
308 req.oncomplete = function (r) {
310 _pcrud.session.request({
311 method : 'open-ils.pcrud.transaction.commit',
314 onerror : function (r) {
316 throw 'Transaction commit error';
318 oncomplete : function (r) {
320 if ( res && res.content() ) {
321 _auto_CUD_recursive( list, 0 );
324 throw 'Transaction commit error';
329 if (r._final_complete) r._final_complete(r);
333 req.onerror = function (r) {
334 if (r._final_error) r._final_error(r);
340 req._obj_list = obj_list;
341 req.oncomplete = function (r) {
343 if ( res && res.content() ) {
344 _auto_CUD_recursive( r._obj_list, r._pos, r._final_complete, r._final_error );
347 throw '_auto_CUD: Error creating, deleting or updating ' + js2JSON(obj);
355 var f_complete = opts.oncomplete;
356 var f_error = opts.onerror;
358 this.session.request({
359 method : 'open-ils.pcrud.transaction.begin',
362 onerror : function (r) {
364 throw 'Transaction begin error';
366 oncomplete : function (r) {
368 if ( res && res.content() ) {
369 _auto_CUD_recursive( list, 0, f_complete, f_error );
372 throw 'Transaction begin error';