]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/fieldmapper/Fieldmapper.js
added some more explicit error handling calls for easier debugging
[Evergreen.git] / Open-ILS / web / js / dojo / fieldmapper / Fieldmapper.js
1 /*
2 # ---------------------------------------------------------------------------
3 # Copyright (C) 2008  Georgia Public Library Service / Equinox Software, Inc
4 # Mike Rylander <miker@esilibrary.com>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # ---------------------------------------------------------------------------
16 */
17
18 if(!dojo._hasResource["fieldmapper.Fieldmapper"]){
19
20 /* generate fieldmapper javascript classes.  This expects a global variable
21         called 'fmclasses' to be fleshed with the classes we need to build */
22
23         function FMEX(message) { this.message = message; }
24         FMEX.toString = function() { return "FieldmapperException: " + this.message + "\n"; }
25
26
27         dojo._hasResource["fieldmapper.Fieldmapper"] = true;
28         dojo.provide("fieldmapper.Fieldmapper");
29         dojo.require("DojoSRF");
30         dojo.require("fieldmapper.fmall", true);
31
32         dojo.declare( "fieldmapper.Fieldmapper", null, {
33
34                 constructor : function (initArray) {
35                         if (initArray) {
36                                 if (dojo.isArray(initArray)) {
37                                         this.a = initArray;
38                                 } else {
39                                         this.a = [];
40                                 }
41                         }
42                 },
43
44                 _isfieldmapper : true,
45                 fm_classes : fmclasses,
46
47                 clone : function() {
48                         var obj = new this.constructor();
49
50                         for( var i in this.a ) {
51                                 var thing = this.a[i];
52                                 if(thing == null) continue;
53
54                                 if( thing._isfieldmapper ) {
55                                         obj.a[i] = thing.clone();
56                                 } else {
57
58                                         if(instanceOf(thing, Array)) {
59                                                 obj.a[i] = new Array();
60
61                                                 for( var j in thing ) {
62
63                                                         if( thing[j]._isfieldmapper )
64                                                                 obj.a[i][j] = thing[j].clone();
65                                                         else
66                                                                 obj.a[i][j] = thing[j];
67                                                 }
68                                         } else {
69                                                 obj.a[i] = thing;
70                                         }
71                                 }
72                         }
73                         return obj;
74                 },
75
76                 isnew : function(n) { if(arguments.length == 1) this.a[0] =n; return this.a[0]; },
77                 ischanged : function(n) { if(arguments.length == 1) this.a[1] =n; return this.a[1]; },
78                 isdeleted : function(n) { if(arguments.length == 1) this.a[2] =n; return this.a[2]; }
79         });
80
81         fieldmapper._request = function ( meth, staff, params ) {
82                 var ses = OpenSRF.CachedClientSession( meth[0] );
83                 if (!ses) return null;
84
85                 var result = null;
86                 var args = {};
87
88                 if (dojo.isArray(params)) {
89                         args.params = params;
90                 } else {
91
92                         if (dojo.isObject(params)) {
93                                 args = params;
94                         } else {
95                 args.params = [].splice.call(arguments, 1, arguments.length - 1);
96                         }
97
98                 }
99
100         if (!args.async && !args.timeout) args.timeout = 10;
101
102         if(!args.onmethoderror) {
103             args.onmethoderror = function(r, stat, stat_text) {
104                 throw new Error('Method error: ' + r.stat + ' : ' + stat_text);
105             }
106         }
107
108         if(!args.ontransporterror) {
109             args.ontransporterror = function(xreq) {
110                 throw new Error('Transport error status=' + xreq.status);
111             }
112         }
113
114                 if (!args.onerror) {
115                         args.onerror = function (r) {
116                                 throw new Error('Request error encountered! ' + r);
117                         }
118                 }
119
120                 if (!args.oncomplete) {
121                         args.oncomplete = function (r) {
122                                 var x = r.recv();
123                                 if (x) result = x.content();
124                         }
125                 }
126
127                 args.method = meth[1];
128                 if (staff && meth[2]) args.method += '.staff';
129
130                 ses.request(args).send();
131
132                 return result;
133         };
134
135         fieldmapper.standardRequest = function (meth, params) { return fieldmapper._request(meth, false, params) };
136         fieldmapper.Fieldmapper.prototype.standardRequest = fieldmapper.standardRequest;
137
138         fieldmapper.staffRequest = function (meth, params) { return fieldmapper._request(meth, true, params) };
139         fieldmapper.Fieldmapper.prototype.staffRequest = fieldmapper.staffRequest;
140
141         for( var cl in fmclasses ) {
142                 dojo.provide( cl );
143                 dojo.declare( cl , fieldmapper.Fieldmapper, {
144                         constructor : function () {
145                                 if (!this.a) this.a = [];
146                                 this.classname = this.declaredClass;
147                                 this.Identifier = 'id';
148                                 this._fields = fmclasses[this.classname];
149                                 for( var pos = 0; pos <  this._fields.length; pos++ ) {
150                                         var p = parseInt(pos) + 3;
151                                         var f = this._fields[pos];
152                                         this[f]=new Function('n', 'if(arguments.length==1)this.a['+p+']=n;return this.a['+p+'];');
153                                 }
154                         }
155                 });
156                 fieldmapper[cl] = window[cl]; // alias into place
157
158         }
159
160         fieldmapper.ppl.Identifier = 'code';
161         fieldmapper.ccm.Identifier = 'code';
162         fieldmapper.cvrfm.Identifier = 'code';
163         fieldmapper.clm.Identifier = 'code';
164         fieldmapper.cam.Identifier = 'code';
165         fieldmapper.cifm.Identifier = 'code';
166         fieldmapper.citm.Identifier = 'code';
167         fieldmapper.cblvl.Identifier = 'code';
168         fieldmapper.clfm.Identifier = 'code';
169         fieldmapper.mous.Identifier = 'usr';
170         fieldmapper.moucs.Identifier = 'usr';
171         fieldmapper.mucs.Identifier = 'usr';
172         fieldmapper.mus.Identifier = 'usr';
173         fieldmapper.rxbt.Identifier = 'xact';
174         fieldmapper.rxpt.Identifier = 'xact';
175         fieldmapper.cxt.Identifier = 'name';
176         fieldmapper.amtr.Identifier = 'matchpoint';
177         fieldmapper.chmt.Identifier = 'matchpoint';
178         fieldmapper.ccmt.Identifier = 'matchpoint';
179         fieldmapper.ccmrs.Identifier = 'matchpoint';
180
181         fieldmapper.OpenSRF = {};
182
183         /*      Methods are defined as [ service, method, have_staff ]
184                 An optional 3rd component is when a method is followed by true, such methods
185                 have a staff counterpart and should have ".staff" appended to the method 
186                 before the method is called when in XUL mode */
187         fieldmapper.OpenSRF.methods = {
188                 SEARCH_MRS : ['open-ils.search','open-ils.search.metabib.multiclass',true],
189                 SEARCH_RS : ['open-ils.search','open-ils.search.biblio.multiclass',true],
190                 SEARCH_MRS_QUERY : ['open-ils.search','open-ils.search.metabib.multiclass.query',true],
191                 SEARCH_RS_QUERY : ['open-ils.search','open-ils.search.biblio.multiclass.query',true],
192                 FETCH_SEARCH_RIDS : ['open-ils.search','open-ils.search.biblio.record.class.search',true],
193                 FETCH_MRMODS : ['open-ils.search','open-ils.search.biblio.metarecord.mods_slim.retrieve'],
194                 FETCH_MODS_FROM_COPY : ['open-ils.search','open-ils.search.biblio.mods_from_copy'],
195                 FETCH_MR_COPY_COUNTS : ['open-ils.search','open-ils.search.biblio.metarecord.copy_count',true],
196                 FETCH_RIDS : ['open-ils.search','open-ils.search.biblio.metarecord_to_records',true],
197                 FETCH_RMODS : ['open-ils.search','open-ils.search.biblio.record.mods_slim.retrieve'],
198                 FETCH_R_COPY_COUNTS : ['open-ils.search','open-ils.search.biblio.record.copy_count',true],
199                 FETCH_FLESHED_USER : ['open-ils.actor','open-ils.actor.user.fleshed.retrieve'],
200                 FETCH_SESSION : ['open-ils.auth','open-ils.auth.session.retrieve'],
201                 LOGIN_INIT : ['open-ils.auth','open-ils.auth.authenticate.init'],
202                 LOGIN_COMPLETE : ['open-ils.auth','open-ils.auth.authenticate.complete'],
203                 LOGIN_DELETE : ['open-ils.auth','open-ils.auth.session.delete'],
204                 FETCH_USER_PREFS : ['open-ils.actor','open-ils.actor.patron.settings.retrieve'], 
205                 UPDATE_USER_PREFS : ['open-ils.actor','open-ils.actor.patron.settings.update'], 
206                 FETCH_COPY_STATUSES : ['open-ils.search','open-ils.search.config.copy_status.retrieve.all'],
207                 FETCH_COPY_COUNTS_SUMMARY : ['open-ils.search','open-ils.search.biblio.copy_counts.summary.retrieve'],
208                 FETCH_MARC_HTML : ['open-ils.search','open-ils.search.biblio.record.html'],
209                 FETCH_CHECKED_OUT_SUM : ['open-ils.actor','open-ils.actor.user.checked_out'],
210                 FETCH_HOLDS : ['open-ils.circ','open-ils.circ.holds.retrieve'],
211                 FETCH_FINES_SUMMARY : ['open-ils.actor','open-ils.actor.user.fines.summary'],
212                 FETCH_TRANSACTIONS : ['open-ils.actor','open-ils.actor.user.transactions.have_charge.fleshed'],
213                 FETCH_MONEY_BILLING : ['open-ils.circ','open-ils.circ.money.billing.retrieve.all'],
214                 FETCH_CROSSREF : ['open-ils.search','open-ils.search.authority.crossref'],
215                 FETCH_CROSSREF_BATCH : ['open-ils.search','open-ils.search.authority.crossref.batch'],
216                 CREATE_HOLD : ['open-ils.circ','open-ils.circ.holds.create'],
217                 CREATE_HOLD_OVERRIDE : ['open-ils.circ','open-ils.circ.holds.create.override'],
218                 CANCEL_HOLD : ['open-ils.circ','open-ils.circ.hold.cancel'],
219                 UPDATE_USERNAME : ['open-ils.actor','open-ils.actor.user.username.update'],
220                 UPDATE_PASSWORD : ['open-ils.actor','open-ils.actor.user.password.update'],
221                 UPDATE_EMAIL : ['open-ils.actor','open-ils.actor.user.email.update'],
222                 RENEW_CIRC : ['open-ils.circ','open-ils.circ.renew'],
223                 CHECK_SPELL : ['open-ils.search','open-ils.search.spellcheck'],
224                 FETCH_REVIEWS : ['open-ils.search','open-ils.search.added_content.review.retrieve.all'],
225                 FETCH_TOC : ['open-ils.search','open-ils.search.added_content.toc.retrieve'],
226                 FETCH_ACONT_SUMMARY : ['open-ils.search','open-ils.search.added_content.summary.retrieve'],
227                 FETCH_USER_BYBARCODE : ['open-ils.actor','open-ils.actor.user.fleshed.retrieve_by_barcode'],
228                 FETCH_ADV_MARC_MRIDS : ['open-ils.search','open-ils.search.biblio.marc',true],
229                 FETCH_ADV_ISBN_RIDS : ['open-ils.search','open-ils.search.biblio.isbn'],
230                 FETCH_ADV_ISSN_RIDS : ['open-ils.search','open-ils.search.biblio.issn'],
231                 FETCH_ADV_TCN_RIDS : ['open-ils.search','open-ils.search.biblio.tcn'],
232                 FETCH_CNBROWSE : ['open-ils.search','open-ils.search.callnumber.browse'],
233                 FETCH_CONTAINERS : ['open-ils.actor','open-ils.actor.container.retrieve_by_class'],
234                 FETCH_CONTAINERS : ['open-ils.actor','open-ils.actor.container.retrieve_by_class'],
235                 CREATE_CONTAINER : ['open-ils.actor','open-ils.actor.container.create'],
236                 DELETE_CONTAINER : ['open-ils.actor','open-ils.actor.container.full_delete'],
237                 CREATE_CONTAINER_ITEM : ['open-ils.actor','open-ils.actor.container.item.create'],
238                 DELETE_CONTAINER_ITEM : ['open-ils.actor','open-ils.actor.container.item.delete'],
239                 FLESH_CONTAINER : ['open-ils.actor','open-ils.actor.container.flesh'],
240                 FLESH_PUBLIC_CONTAINER : ['open-ils.actor','open-ils.actor.container.public.flesh'],
241                 UPDATE_CONTAINER : ['open-ils.actor','open-ils.actor.container.update'],
242                 FETCH_COPY : ['open-ils.search','open-ils.search.asset.copy.retrieve'],
243                 FETCH_FLESHED_COPY : ['open-ils.search','open-ils.search.asset.copy.fleshed2.retrieve'],
244                 CHECK_HOLD_POSSIBLE : ['open-ils.circ','open-ils.circ.title_hold.is_possible'],
245                 UPDATE_HOLD : ['open-ils.circ','open-ils.circ.hold.update'],
246                 FETCH_COPIES_FROM_VOLUME : ['open-ils.search','open-ils.search.asset.copy.retrieve_by_cn_label',true],
247                 FETCH_VOLUME_BY_INFO : ['open-ils.search','open-ils.search.call_number.retrieve_by_info'], /* XXX staff method? */
248                 FETCH_VOLUME : ['open-ils.search','open-ils.search.asset.call_number.retrieve'],
249                 FETCH_COPY_LOCATIONS : ['open-ils.circ','open-ils.circ.copy_location.retrieve.all'],
250                 FETCH_COPY_NOTES : ['open-ils.circ','open-ils.circ.copy_note.retrieve.all'],
251                 FETCH_COPY_STAT_CATS : ['open-ils.circ','open-ils.circ.asset.stat_cat_entries.fleshed.retrieve_by_copy'],
252                 FETCH_LIT_FORMS : ['open-ils.search','open-ils.search.biblio.lit_form_map.retrieve.all'],
253                 FETCH_ITEM_FORMS : ['open-ils.search','open-ils.search.biblio.item_form_map.retrieve.all'],
254                 FETCH_ITEM_TYPES : ['open-ils.search','open-ils.search.biblio.item_type_map.retrieve.all'],
255                 FETCH_AUDIENCES : ['open-ils.search','open-ils.search.biblio.audience_map.retrieve.all'],
256                 FETCH_HOLD_STATUS : ['open-ils.circ','open-ils.circ.hold.status.retrieve'],
257                 FETCH_NON_CAT_CIRCS : ['open-ils.circ','open-ils.circ.open_non_cataloged_circulation.user'],
258                 FETCH_NON_CAT_CIRC : ['open-ils.circ','open-ils.circ.non_cataloged_circulation.retrieve'],
259                 FETCH_NON_CAT_TYPES : ['open-ils.circ','open-ils.circ.non_cat_types.retrieve.all'],
260                 FETCH_BRE : ['open-ils.search','open-ils.search.biblio.record_entry.slim.retrieve'],
261                 CHECK_USERNAME : ['open-ils.actor','open-ils.actor.username.exists'],
262                 FETCH_CIRC_BY_ID : ['open-ils.circ','open-ils.circ.retrieve'],
263                 FETCH_MR_DESCRIPTORS : ['open-ils.search','open-ils.search.metabib.record_to_descriptors'],
264                 FETCH_HIGHEST_PERM_ORG : ['open-ils.actor','open-ils.actor.user.perm.highest_org.batch'],
265                 FETCH_USER_NOTES : ['open-ils.actor','open-ils.actor.note.retrieve.all'],
266                 FETCH_ORG_BY_SHORTNAME : ['open-ils.actor','open-ils.actor.org_unit.retrieve_by_shorname'],
267                 FETCH_BIB_ID_BY_BARCODE : ['open-ils.search','open-ils.search.bib_id.by_barcode'],
268                 FETCH_ORG_SETTING : ['open-ils.actor','open-ils.actor.ou_setting.ancestor_default']
269         };
270
271 }
272
273
274