1 if (!dojo._hasResource["openils.FlattenerStore"]) {
2 dojo._hasResource["openils.FlattenerStore"] = true;
4 dojo.provide("openils.FlattenerStore");
6 dojo.require("DojoSRF");
7 dojo.require("openils.User");
8 dojo.require("openils.Util");
10 /* An exception class specific to openils.FlattenerStore */
11 function FlattenerStoreError(message) { this.message = message; }
12 FlattenerStoreError.prototype.toString = function() {
13 return "openils.FlattenerStore: " + this.message;
17 "openils.FlattenerStore", null, {
19 "_last_fetch": null, /* used internally */
20 "_flattener_url": "/opac/extras/flattener",
22 /* Everything between here and the constructor can be specified in
23 * the constructor's args object. */
32 "sortFieldReMap": null,
34 "constructor": function(/* object */ args) {
35 dojo.mixin(this, args);
36 this._current_items = {};
39 /* turn dojo-style sort into flattener-style sort */
40 "_prepare_sort": function(dsort) {
41 if (!dsort || !dsort.length)
42 return this.baseSort || this.defaultSort || [];
44 return (this.baseSort || []).concat(
48 o[d.attribute] = d.descending ? "desc" : "asc";
55 "_remap_sort": function(prepared_sort) {
56 if (this.sortFieldReMap) {
57 return prepared_sort.map(
60 if (typeof exp == "object") {
64 var newkey = (key in this.sortFieldReMap) ?
65 this.sortFieldReMap[key] : key;
70 return (exp in this.sortFieldReMap) ?
71 this.sortFieldReMap[exp] : exp;
81 "_build_flattener_params": function(req) {
84 "ses": openils.User.authtoken
87 /* If we're asked for a specific identity, we don't use
88 * any query or sort/count/start (sort/limit/offset). */
89 if ("identity" in req) {
91 where[this.fmIdentifier] = req.identity;
93 params.where = dojo.toJson(where);
95 params.where = dojo.toJson(req.query);
98 "sort": this._remap_sort(this._prepare_sort(req.sort))
101 if (!req.queryOptions.all) {
103 (!isNaN(req.count) && req.count != Infinity) ?
104 req.count : this.limit;
107 (!isNaN(req.start) && req.start != Infinity) ?
108 req.start : this.offset;
111 if (req.queryOptions.columns)
112 params.columns = req.queryOptions.columns;
113 if (req.queryOptions.labels)
114 params.labels = req.queryOptions.labels;
116 params.slo = dojo.toJson(slo);
120 params.key = this.mapKey;
122 params.map = dojo.toJson(this.mapClause);
125 // for (var key in params)
126 // console.debug("flattener param " + key + " -> " + params[key]);
131 "_display_attributes": function() {
134 return openils.Util.objectProperties(this.mapClause).filter(
135 function(key) { return self.mapClause[key].display; }
139 "_get_map_key": function() {
140 //console.debug("mapClause: " + dojo.toJson(this.mapClause));
141 this.mapKey = fieldmapper.standardRequest(
143 "open-ils.fielder.flattened_search.prepare"], {
144 "params": [openils.User.authtoken, this.fmClass,
151 "_on_http_error": function(response, ioArgs, req, retry_method) {
152 if (response.status == 402) { /* 'Payment Required' stands
154 if (this._retried_map_key_already) {
155 var e = new FlattenerStoreError(
156 "Server won't cache flattener map?"
158 if (typeof req.onError == "function")
159 req.onError.call(callback_scope, e);
163 this._retried_map_key_already = true;
166 return this[retry_method](req);
171 "_fetch_prepare": function(req) {
172 req.queryOptions = req.queryOptions || {};
173 req.abort = function() { console.warn("[unimplemented] abort()"); };
178 return this._build_flattener_params(req);
181 "_fetch_execute": function(params,handle_as,mime_type,onload,onerror) {
183 "url": this._flattener_url,
185 "handleAs": handle_as,
187 "preventCache": true,
188 "headers": {"Accept": mime_type},
194 /* *** Nonstandard but public API - Please think hard about doing
195 * things the Dojo Way whenever possible before extending the API
198 /* fetchToPrint() acts like a lot like fetch(), but doesn't call
199 * onBegin or onComplete. */
200 "fetchToPrint": function(req) {
201 var callback_scope = req.scope || dojo.global;
205 post_params = this._fetch_prepare(req);
207 if (typeof req.onError == "function")
208 req.onError.call(callback_scope, E);
213 var process_fetch_all = dojo.hitch(
214 this, function(text) {
215 this._retried_map_key_already = false;
217 if (typeof req.onComplete == "function")
218 req.onComplete.call(callback_scope, text, req);
222 var process_error = dojo.hitch(
223 this, function(response, ioArgs) {
224 this._on_http_error(response, ioArgs, req, "fetchToPrint");
239 /* *** Begin dojo.data.api.Read methods *** */
241 "getValue": function(
243 /* string */ attribute,
244 /* anything */ defaultValue) {
245 //console.log("getValue(" + lazy(item) + ", " + attribute + ", " + defaultValue + ")")
246 if (!this.isItem(item))
247 throw new FlattenerStoreError("getValue(): bad item " + item);
248 else if (typeof attribute != "string")
249 throw new FlattenerStoreError("getValue(): bad attribute");
251 var value = item[attribute];
252 return (typeof value == "undefined") ? defaultValue : value;
255 "getValues": function(/* object */ item, /* string */ attribute) {
256 //console.log("getValues(" + item + ", " + attribute + ")");
257 if (!this.isItem(item) || typeof attribute != "string")
258 throw new FlattenerStoreError("bad arguments");
260 var result = this.getValue(item, attribute, []);
261 return dojo.isArray(result) ? result : [result];
264 "getAttributes": function(/* object */ item) {
265 //console.log("getAttributes(" + item + ")");
266 if (!this.isItem(item))
267 throw new FlattenerStoreError("getAttributes(): bad args");
269 return this._display_attributes();
272 "hasAttribute": function(/* object */ item, /* string */ attribute) {
273 //console.log("hasAttribute(" + item + ", " + attribute + ")");
274 if (!this.isItem(item) || typeof attribute != "string") {
275 throw new FlattenerStoreError("hasAttribute(): bad args");
277 return dojo.indexOf(this._display_attributes(), attribute) > -1;
281 "containsValue": function(
283 /* string */ attribute,
284 /* anything */ value) {
285 //console.log("containsValue(" + item + ", " + attribute + ", " + value + ")");
286 if (!this.isItem(item) || typeof attribute != "string")
287 throw new FlattenerStoreError("bad data");
290 dojo.indexOf(this.getValues(item, attribute), value) >= -1
294 "isItem": function(/* anything */ something) {
295 //console.log("isItem(" + lazy(something) + ")");
296 if (typeof something != "object" || something === null)
299 var fields = this._display_attributes();
301 for (var i = 0; i < fields.length; i++) {
303 if (!(cur in something))
309 "isItemLoaded": function(/* anything */ something) {
310 /* XXX if 'something' is not an item at all, are we just supposed
311 * to return false or throw an exception? */
312 return this.isItem(something) && (
313 something[this.fmIdentifier] in this._current_items
317 "close": function(/* object */ request) { /* no-op */ return; },
319 "getLabel": function(/* object */ item) {
320 console.warn("[unimplemented] getLabel()");
323 "getLabelAttributes": function(/* object */ item) {
324 console.warn("[unimplemented] getLabelAttributes()");
327 "loadItem": function(/* object */ keywordArgs) {
328 if (!keywordArgs.force && this.isItemLoaded(keywordArgs.item))
331 keywordArgs.identity = this.getIdentity(keywordArgs.item);
332 return this.fetchItemByIdentity(keywordArgs);
335 "fetch": function(/* request-object */ req) {
336 // Respect the following properties of the *req* object:
338 // query a dojo-style query, which will need modest
339 // translation for our server-side service
341 // onBegin a callback that takes the number of items
342 // that this call to fetch() *could* have
343 // returned, with a higher limit. We do
345 // onItem a callback that takes each item as we get it
346 // onComplete a callback that takes the list of items
347 // after they're all fetched
350 var callback_scope = req.scope || dojo.global;
354 post_params = this._fetch_prepare(req);
356 if (typeof req.onError == "function")
357 req.onError.call(callback_scope, E);
362 var process_fetch = function(obj, when) {
363 if (when < self._last_fetch) /* Stale response. Discard. */
366 self._retried_map_key_already = false;
368 /* The following is apparently the "right" way to call onBegin,
369 * and is very necessary (at least in Dojo 1.3.3) to get
370 * the Grid's fetch-more-when-I-need-it logic to work
371 * correctly. *grumble* crummy documentation *snarl!*
373 if (typeof req.onBegin == "function") {
374 /* We lie to onBegin like this because we don't know how
375 * many more rows we might be able to fetch if the
376 * user keeps scrolling. Once we get a number of
377 * results that is less than the limit we asked for,
378 * we stop exaggerating, and the grid is smart enough to
379 * know we're at the end and it does the right thing. */
380 var might_be_a_lie = req.start;
381 if (obj.length >= req.count)
382 might_be_a_lie += obj.length + req.count;
384 might_be_a_lie += obj.length;
386 req.onBegin.call(callback_scope, might_be_a_lie, req);
392 /* Cache items internally. */
393 self._current_items[item[self.fmIdentifier]] = item;
395 if (typeof req.onItem == "function")
396 req.onItem.call(callback_scope, item, req);
400 if (typeof req.onComplete == "function")
401 req.onComplete.call(callback_scope, obj, req);
404 var process_error = dojo.hitch(
405 this, function(response, ioArgs) {
406 this._on_http_error(response, ioArgs, req, "fetch");
410 var fetch_time = this._last_fetch = (new Date().getTime());
416 function(obj) { process_fetch(obj, fetch_time); },
423 /* *** Begin dojo.data.api.Identity methods *** */
425 "getIdentity": function(/* object */ item) {
426 if (!this.isItem(item))
427 throw new FlattenerStoreError("not an item");
429 return item[this.fmIdentifier];
432 "getIdentityAttributes": function(/* object */ item) {
433 // console.log("getIdentityAttributes(" + item + ")");
434 return [this.fmIdentifier];
437 "fetchItemByIdentity": function(/* object */ keywordArgs) {
438 var callback_scope = keywordArgs.scope || dojo.global;
439 var identity = keywordArgs.identity;
441 if (typeof identity == "undefined")
442 throw new FlattenerStoreError(
443 "fetchItemByIdentity() needs identity in keywordArgs"
446 /* First of force's two implications:
447 * fetch even if already loaded. */
448 if (this._current_items[identity] && !keywordArgs.force) {
449 keywordArgs.onItem.call(
450 callback_scope, this._current_items[identity]
458 post_params = this._fetch_prepare(keywordArgs);
460 if (typeof keywordArgs.onError == "function")
461 keywordArgs.onError.call(callback_scope, E);
466 var process_fetch_one = dojo.hitch(
467 this, function(obj, when) {
468 if (when < this._last_fetch) /* Stale response. Discard. */
471 if (dojo.isArray(obj)) {
472 if (obj.length <= 1) {
473 obj = obj.pop() || null; /* safe enough */
474 /* Second of force's two implications: call setValue
475 * ourselves. Makes a DataGrid update. */
476 if (keywordArgs.force && obj &&
477 (origitem = this._current_items[identity])) {
478 for (var prop in origitem)
479 this.setValue(origitem, prop, obj[prop]);
481 if (keywordArgs.onItem)
482 keywordArgs.onItem.call(callback_scope, obj);
484 var e = new FlattenerStoreError("Too many results");
485 if (keywordArgs.onError)
486 keywordArgs.onError.call(callback_scope, e);
491 var e = new FlattenerStoreError("Bad response");
492 if (keywordArgs.onError)
493 keywordArgs.onError.call(callback_scope, e);
500 var process_error = dojo.hitch(
501 this, function(response, ioArgs) {
503 response, ioArgs, keywordArgs, "fetchItemByIdentity"
508 var fetch_time = this._last_fetch = (new Date().getTime());
514 function(obj) { process_fetch_one(obj, fetch_time); },
519 /* dojo.data.api.Write - only very partially implemented, because
520 * for FlattenerGrid, the intended client of this store, we don't
521 * need most of the methods. */
523 "deleteItem": function(item) {
524 //console.log("deleteItem()");
526 var identity = this.getIdentity(item);
527 delete this._current_items[identity]; /* safe even if missing */
532 "setValue": function(item, attribute, value) {
533 /* Silently do nothing when setValue()'s caller wants to change
534 * the identifier. They must be confused anyway. */
535 if (attribute == this.fmIdentifier)
538 var old_value = dojo.clone(item[attribute]);
540 item[attribute] = dojo.clone(value);
541 this.onSet(item, attribute, old_value, value);
544 "setValues": function(item, attribute, values) {
545 console.warn("[unimplemented] setValues()"); /* unneeded */
548 "newItem": function(keywordArgs, parentInfo) {
549 console.warn("[unimplemented] newItem()"); /* unneeded */
552 "unsetAttribute": function() {
553 console.warn("[unimplemented] unsetAttribute()"); /* unneeded */
557 console.warn("[unimplemented] save()"); /* unneeded */
560 "revert": function() {
561 console.warn("[unimplemented] revert()"); /* unneeded */
564 "isDirty": function() { /* I /think/ this will be ok for our purposes */
565 console.info("[stub] isDirty() will always return false");
570 /* dojo.data.api.Notification - Keep these no-op methods because
571 * clients will dojo.connect() to them. */
573 "onNew" : function(item) { /* no-op */ },
574 "onDelete" : function(item) { /* no-op */ },
575 "onSet": function(item, attr, oldval, newval) { /* no-op */ },
577 /* *** Classes implementing any Dojo APIs do this to list which
578 * APIs they're implementing. *** */
580 "getFeatures": function() {
582 "dojo.data.api.Read": true,
583 "dojo.data.api.Identity": true,
584 "dojo.data.api.Notification": true,
585 "dojo.data.api.Write": true /* well, only partly */