default error handler typo
[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("OpenSRF");
30
31         dojo.declare( "fieldmapper.Fieldmapper", null, {
32
33                 constructor : function (initArray) {
34                         if (initArray) {
35                                 if (dojo.isArray(initArray)) {
36                                         this.a = initArray;
37                                 } else {
38                                         this.a = [];
39                                 }
40                         }
41                 },
42
43                 _isfieldmapper : true,
44                 fm_classes : fmclasses,
45
46                 clone : function() {
47                         var obj = new this.constructor();
48
49                         for( var i in this.a ) {
50                                 var thing = this.a[i];
51                                 if(thing == null) continue;
52
53                                 if( thing._isfieldmapper ) {
54                                         obj.a[i] = thing.clone();
55                                 } else {
56
57                                         if(instanceOf(thing, Array)) {
58                                                 obj.a[i] = new Array();
59
60                                                 for( var j in thing ) {
61
62                                                         if( thing[j]._isfieldmapper )
63                                                                 obj.a[i][j] = thing[j].clone();
64                                                         else
65                                                                 obj.a[i][j] = thing[j];
66                                                 }
67                                         } else {
68                                                 obj.a[i] = thing;
69                                         }
70                                 }
71                         }
72                         return obj;
73                 },
74
75                 isnew : function(n) { if(arguments.length == 1) this.a[0] =n; return this.a[0]; },
76                 ischanged : function(n) { if(arguments.length == 1) this.a[1] =n; return this.a[1]; },
77                 isdeleted : function(n) { if(arguments.length == 1) this.a[2] =n; return this.a[2]; }
78         });
79
80         fieldmapper._request = function ( meth, staff, params ) {
81                 var ses = OpenSRF.CachedClientSession( meth[0] );
82                 if (!ses) return null;
83
84                 var result = null;
85                 var args = {};
86
87                 if (dojo.isArray(params)) {
88                         args.params = params;
89                 } else {
90
91                         if (dojo.isObject(params)) {
92                                 args = params;
93                         } else {
94                                 args.params = arguments.splice(1, arguments.length - 1);
95                         }
96
97                 }
98
99         if (!args.async && !args.timeout) args.timeout = 10;
100
101                 if (!args.onerror) {
102                         args.onerror = function (r) {
103                                 throw 'Error encountered! ' + r;
104                         }
105                 }
106
107                 if (!args.oncomplete) {
108                         args.oncomplete = function (r) {
109                                 var x = r.recv();
110                                 if (x) result = x.content();
111                         }
112                 }
113
114                 args.method = meth[1];
115                 if (staff && meth[2]) args.method += '.staff';
116
117                 ses.request(args).send();
118
119                 return result;
120         };
121
122         fieldmapper.standardRequest = function (meth, params) { return fieldmapper._request(meth, false, params) };
123         fieldmapper.Fieldmapper.prototype.standardRequest = fieldmapper.standardRequest;
124
125         fieldmapper.staffRequest = function (meth, params) { return fieldmapper._request(meth, true, params) };
126         fieldmapper.Fieldmapper.prototype.staffRequest = fieldmapper.staffRequest;
127
128         for( var cl in fmclasses ) {
129                 dojo.provide( cl );
130                 dojo.declare( cl , fieldmapper.Fieldmapper, {
131                         constructor : function () {
132                                 if (!this.a) this.a = [];
133                                 this.classname = this.declaredClass;
134                                 this._fields = fmclasses[this.classname];
135                                 for( var pos = 0; pos <  this._fields.length; pos++ ) {
136                                         var p = parseInt(pos) + 3;
137                                         var f = this._fields[pos];
138                                         this[f]=new Function('n', 'if(arguments.length==1)this.a['+p+']=n;return this.a['+p+'];');
139                                 }
140                         }
141                 });
142                 fieldmapper[cl] = window[cl]; // alias into place
143
144         }
145
146         fieldmapper.OpenSRF = {};
147
148         /*      Methods are defined as [ service, method, have_staff ]
149                 An optional 3rd component is when a method is followed by true, such methods
150                 have a staff counterpart and should have ".staff" appended to the method 
151                 before the method is called when in XUL mode */
152         fieldmapper.OpenSRF.methods = {
153                 SEARCH_MRS : ['open-ils.search','open-ils.search.metabib.multiclass',true],
154                 SEARCH_RS : ['open-ils.search','open-ils.search.biblio.multiclass',true],
155                 SEARCH_MRS_QUERY : ['open-ils.search','open-ils.search.metabib.multiclass.query',true],
156                 SEARCH_RS_QUERY : ['open-ils.search','open-ils.search.biblio.multiclass.query',true],
157                 FETCH_SEARCH_RIDS : ['open-ils.search','open-ils.search.biblio.record.class.search',true],
158                 FETCH_MRMODS : ['open-ils.search','open-ils.search.biblio.metarecord.mods_slim.retrieve'],
159                 FETCH_MODS_FROM_COPY : ['open-ils.search','open-ils.search.biblio.mods_from_copy'],
160                 FETCH_MR_COPY_COUNTS : ['open-ils.search','open-ils.search.biblio.metarecord.copy_count',true],
161                 FETCH_RIDS : ['open-ils.search','open-ils.search.biblio.metarecord_to_records',true],
162                 FETCH_RMODS : ['open-ils.search','open-ils.search.biblio.record.mods_slim.retrieve'],
163                 FETCH_R_COPY_COUNTS : ['open-ils.search','open-ils.search.biblio.record.copy_count',true],
164                 FETCH_FLESHED_USER : ['open-ils.actor','open-ils.actor.user.fleshed.retrieve'],
165                 FETCH_SESSION : ['open-ils.auth','open-ils.auth.session.retrieve'],
166                 LOGIN_INIT : ['open-ils.auth','open-ils.auth.authenticate.init'],
167                 LOGIN_COMPLETE : ['open-ils.auth','open-ils.auth.authenticate.complete'],
168                 LOGIN_DELETE : ['open-ils.auth','open-ils.auth.session.delete'],
169                 FETCH_USER_PREFS : ['open-ils.actor','open-ils.actor.patron.settings.retrieve'], 
170                 UPDATE_USER_PREFS : ['open-ils.actor','open-ils.actor.patron.settings.update'], 
171                 FETCH_COPY_STATUSES : ['open-ils.search','open-ils.search.config.copy_status.retrieve.all'],
172                 FETCH_COPY_COUNTS_SUMMARY : ['open-ils.search','open-ils.search.biblio.copy_counts.summary.retrieve'],
173                 FETCH_MARC_HTML : ['open-ils.search','open-ils.search.biblio.record.html'],
174                 FETCH_CHECKED_OUT_SUM : ['open-ils.actor','open-ils.actor.user.checked_out'],
175                 FETCH_HOLDS : ['open-ils.circ','open-ils.circ.holds.retrieve'],
176                 FETCH_FINES_SUMMARY : ['open-ils.actor','open-ils.actor.user.fines.summary'],
177                 FETCH_TRANSACTIONS : ['open-ils.actor','open-ils.actor.user.transactions.have_charge.fleshed'],
178                 FETCH_MONEY_BILLING : ['open-ils.circ','open-ils.circ.money.billing.retrieve.all'],
179                 FETCH_CROSSREF : ['open-ils.search','open-ils.search.authority.crossref'],
180                 FETCH_CROSSREF_BATCH : ['open-ils.search','open-ils.search.authority.crossref.batch'],
181                 CREATE_HOLD : ['open-ils.circ','open-ils.circ.holds.create'],
182                 CREATE_HOLD_OVERRIDE : ['open-ils.circ','open-ils.circ.holds.create.override'],
183                 CANCEL_HOLD : ['open-ils.circ','open-ils.circ.hold.cancel'],
184                 UPDATE_USERNAME : ['open-ils.actor','open-ils.actor.user.username.update'],
185                 UPDATE_PASSWORD : ['open-ils.actor','open-ils.actor.user.password.update'],
186                 UPDATE_EMAIL : ['open-ils.actor','open-ils.actor.user.email.update'],
187                 RENEW_CIRC : ['open-ils.circ','open-ils.circ.renew'],
188                 CHECK_SPELL : ['open-ils.search','open-ils.search.spellcheck'],
189                 FETCH_REVIEWS : ['open-ils.search','open-ils.search.added_content.review.retrieve.all'],
190                 FETCH_TOC : ['open-ils.search','open-ils.search.added_content.toc.retrieve'],
191                 FETCH_ACONT_SUMMARY : ['open-ils.search','open-ils.search.added_content.summary.retrieve'],
192                 FETCH_USER_BYBARCODE : ['open-ils.actor','open-ils.actor.user.fleshed.retrieve_by_barcode'],
193                 FETCH_ADV_MARC_MRIDS : ['open-ils.search','open-ils.search.biblio.marc',true],
194                 FETCH_ADV_ISBN_RIDS : ['open-ils.search','open-ils.search.biblio.isbn'],
195                 FETCH_ADV_ISSN_RIDS : ['open-ils.search','open-ils.search.biblio.issn'],
196                 FETCH_ADV_TCN_RIDS : ['open-ils.search','open-ils.search.biblio.tcn'],
197                 FETCH_CNBROWSE : ['open-ils.search','open-ils.search.callnumber.browse'],
198                 FETCH_CONTAINERS : ['open-ils.actor','open-ils.actor.container.retrieve_by_class'],
199                 FETCH_CONTAINERS : ['open-ils.actor','open-ils.actor.container.retrieve_by_class'],
200                 CREATE_CONTAINER : ['open-ils.actor','open-ils.actor.container.create'],
201                 DELETE_CONTAINER : ['open-ils.actor','open-ils.actor.container.full_delete'],
202                 CREATE_CONTAINER_ITEM : ['open-ils.actor','open-ils.actor.container.item.create'],
203                 DELETE_CONTAINER_ITEM : ['open-ils.actor','open-ils.actor.container.item.delete'],
204                 FLESH_CONTAINER : ['open-ils.actor','open-ils.actor.container.flesh'],
205                 FLESH_PUBLIC_CONTAINER : ['open-ils.actor','open-ils.actor.container.public.flesh'],
206                 UPDATE_CONTAINER : ['open-ils.actor','open-ils.actor.container.update'],
207                 FETCH_COPY : ['open-ils.search','open-ils.search.asset.copy.retrieve'],
208                 FETCH_FLESHED_COPY : ['open-ils.search','open-ils.search.asset.copy.fleshed2.retrieve'],
209                 CHECK_HOLD_POSSIBLE : ['open-ils.circ','open-ils.circ.title_hold.is_possible'],
210                 UPDATE_HOLD : ['open-ils.circ','open-ils.circ.hold.update'],
211                 FETCH_COPIES_FROM_VOLUME : ['open-ils.search','open-ils.search.asset.copy.retrieve_by_cn_label',true],
212                 FETCH_VOLUME_BY_INFO : ['open-ils.search','open-ils.search.call_number.retrieve_by_info'], /* XXX staff method? */
213                 FETCH_VOLUME : ['open-ils.search','open-ils.search.asset.call_number.retrieve'],
214                 FETCH_COPY_LOCATIONS : ['open-ils.circ','open-ils.circ.copy_location.retrieve.all'],
215                 FETCH_COPY_NOTES : ['open-ils.circ','open-ils.circ.copy_note.retrieve.all'],
216                 FETCH_COPY_STAT_CATS : ['open-ils.circ','open-ils.circ.asset.stat_cat_entries.fleshed.retrieve_by_copy'],
217                 FETCH_LIT_FORMS : ['open-ils.search','open-ils.search.biblio.lit_form_map.retrieve.all'],
218                 FETCH_ITEM_FORMS : ['open-ils.search','open-ils.search.biblio.item_form_map.retrieve.all'],
219                 FETCH_ITEM_TYPES : ['open-ils.search','open-ils.search.biblio.item_type_map.retrieve.all'],
220                 FETCH_AUDIENCES : ['open-ils.search','open-ils.search.biblio.audience_map.retrieve.all'],
221                 FETCH_HOLD_STATUS : ['open-ils.circ','open-ils.circ.hold.status.retrieve'],
222                 FETCH_NON_CAT_CIRCS : ['open-ils.circ','open-ils.circ.open_non_cataloged_circulation.user'],
223                 FETCH_NON_CAT_CIRC : ['open-ils.circ','open-ils.circ.non_cataloged_circulation.retrieve'],
224                 FETCH_NON_CAT_TYPES : ['open-ils.circ','open-ils.circ.non_cat_types.retrieve.all'],
225                 FETCH_BRE : ['open-ils.search','open-ils.search.biblio.record_entry.slim.retrieve'],
226                 CHECK_USERNAME : ['open-ils.actor','open-ils.actor.username.exists'],
227                 FETCH_CIRC_BY_ID : ['open-ils.circ','open-ils.circ.retrieve'],
228                 FETCH_MR_DESCRIPTORS : ['open-ils.search','open-ils.search.metabib.record_to_descriptors'],
229                 FETCH_HIGHEST_PERM_ORG : ['open-ils.actor','open-ils.actor.user.perm.highest_org.batch'],
230                 FETCH_USER_NOTES : ['open-ils.actor','open-ils.actor.note.retrieve.all'],
231                 FETCH_ORG_BY_SHORTNAME : ['open-ils.actor','open-ils.actor.org_unit.retrieve_by_shorname'],
232                 FETCH_BIB_ID_BY_BARCODE : ['open-ils.search','open-ils.search.bib_id.by_barcode'],
233                 FETCH_ORG_SETTING : ['open-ils.actor','open-ils.actor.ou_setting.ancestor_default']
234         };
235
236 }
237
238
239