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, /* timestamp. used internally */
20 "_last_fetch_sort": null, /* dijit sort object. used internally */
21 "_flattener_url": "/opac/extras/flattener",
23 /* Everything between here and the constructor can be specified in
24 * the constructor's args object. */
33 "sortFieldReMap": null,
35 "constructor": function(/* object */ args) {
36 dojo.mixin(this, args);
37 this._current_items = {};
40 /* turn dojo-style sort into flattener-style sort */
41 "_prepare_sort": function(dsort) {
42 if (!dsort || !dsort.length)
43 return this.baseSort || this.defaultSort || [];
45 return (this.baseSort || []).concat(
49 o[d.attribute] = d.descending ? "desc" : "asc";
56 "_remap_sort": function(prepared_sort) {
57 if (this.sortFieldReMap) {
58 return prepared_sort.map(
61 if (typeof exp == "object") {
65 var newkey = (key in this.sortFieldReMap) ?
66 this.sortFieldReMap[key] : key;
71 return (exp in this.sortFieldReMap) ?
72 this.sortFieldReMap[exp] : exp;
82 "_build_flattener_params": function(req) {
85 "ses": openils.User.authtoken
88 /* If we're asked for a specific identity, we don't use
89 * any query or sort/count/start (sort/limit/offset). */
90 if ("identity" in req) {
92 where[this.fmIdentifier] = req.identity;
94 params.where = dojo.toJson(where);
96 params.where = dojo.toJson(req.query);
99 "sort": this._remap_sort(this._prepare_sort(req.sort))
102 if (!req.queryOptions.all) {
104 (!isNaN(req.count) && req.count != Infinity) ?
105 req.count : this.limit;
108 (!isNaN(req.start) && req.start != Infinity) ?
109 req.start : this.offset;
112 if (req.queryOptions.columns)
113 params.columns = req.queryOptions.columns;
114 if (req.queryOptions.labels)
115 params.labels = req.queryOptions.labels;
117 params.slo = dojo.toJson(slo);
121 params.key = this.mapKey;
123 params.map = dojo.toJson(this.mapClause);
126 // for (var key in params)
127 // console.debug("flattener param " + key + " -> " + params[key]);
132 "_display_attributes": function() {
135 return openils.Util.objectProperties(this.mapClause).filter(
136 function(key) { return self.mapClause[key].display; }
140 "_get_map_key": function() {
141 //console.debug("mapClause: " + dojo.toJson(this.mapClause));
142 this.mapKey = fieldmapper.standardRequest(
144 "open-ils.fielder.flattened_search.prepare"], {
145 "params": [openils.User.authtoken, this.fmClass,
152 "_on_http_error": function(response, ioArgs, req, retry_method) {
153 if (response.status == 402) { /* 'Payment Required' stands
155 if (this._retried_map_key_already) {
156 var e = new FlattenerStoreError(
157 "Server won't cache flattener map?"
159 if (typeof req.onError == "function")
160 req.onError.call(callback_scope, e);
164 this._retried_map_key_already = true;
167 return this[retry_method](req);
172 "_fetch_prepare": function(req) {
173 req.queryOptions = req.queryOptions || {};
174 req.abort = function() { console.warn("[unimplemented] abort()"); };
176 /* If we were asked to fetch without any sort order specified (as
177 * will happen when coming from fetchToPrint(), try to use the
178 * last cached sort order, if any. */
179 req.sort = req.sort || this._last_fetch_sort;
180 this._last_fetch_sort = req.sort;
185 return this._build_flattener_params(req);
188 "_fetch_execute": function(params,handle_as,mime_type,onload,onerror) {
190 "url": this._flattener_url,
192 "handleAs": handle_as,
194 "preventCache": true,
195 "headers": {"Accept": mime_type},
201 /* *** Nonstandard but public API - Please think hard about doing
202 * things the Dojo Way whenever possible before extending the API
205 /* fetchToPrint() acts like a lot like fetch(), but doesn't call
206 * onBegin or onComplete. */
207 "fetchToPrint": function(req) {
208 var callback_scope = req.scope || dojo.global;
212 post_params = this._fetch_prepare(req);
214 if (typeof req.onError == "function")
215 req.onError.call(callback_scope, E);
220 var process_fetch_all = dojo.hitch(
221 this, function(text) {
222 this._retried_map_key_already = false;
224 if (typeof req.onComplete == "function")
225 req.onComplete.call(callback_scope, text, req);
229 var process_error = dojo.hitch(
230 this, function(response, ioArgs) {
231 this._on_http_error(response, ioArgs, req, "fetchToPrint");
246 /* *** Begin dojo.data.api.Read methods *** */
248 "getValue": function(
250 /* string */ attribute,
251 /* anything */ defaultValue) {
252 //console.log("getValue(" + lazy(item) + ", " + attribute + ", " + defaultValue + ")")
253 if (!this.isItem(item))
254 throw new FlattenerStoreError("getValue(): bad item " + item);
255 else if (typeof attribute != "string")
256 throw new FlattenerStoreError("getValue(): bad attribute");
258 var value = item[attribute];
259 return (typeof value == "undefined") ? defaultValue : value;
262 "getValues": function(/* object */ item, /* string */ attribute) {
263 //console.log("getValues(" + item + ", " + attribute + ")");
264 if (!this.isItem(item) || typeof attribute != "string")
265 throw new FlattenerStoreError("bad arguments");
267 var result = this.getValue(item, attribute, []);
268 return dojo.isArray(result) ? result : [result];
271 "getAttributes": function(/* object */ item) {
272 //console.log("getAttributes(" + item + ")");
273 if (!this.isItem(item))
274 throw new FlattenerStoreError("getAttributes(): bad args");
276 return this._display_attributes();
279 "hasAttribute": function(/* object */ item, /* string */ attribute) {
280 //console.log("hasAttribute(" + item + ", " + attribute + ")");
281 if (!this.isItem(item) || typeof attribute != "string") {
282 throw new FlattenerStoreError("hasAttribute(): bad args");
284 return dojo.indexOf(this._display_attributes(), attribute) > -1;
288 "containsValue": function(
290 /* string */ attribute,
291 /* anything */ value) {
292 //console.log("containsValue(" + item + ", " + attribute + ", " + value + ")");
293 if (!this.isItem(item) || typeof attribute != "string")
294 throw new FlattenerStoreError("bad data");
297 dojo.indexOf(this.getValues(item, attribute), value) >= -1
301 "isItem": function(/* anything */ something) {
302 //console.log("isItem(" + lazy(something) + ")");
303 if (typeof something != "object" || something === null)
306 var fields = this._display_attributes();
308 for (var i = 0; i < fields.length; i++) {
310 if (!(cur in something))
316 "isItemLoaded": function(/* anything */ something) {
317 /* XXX if 'something' is not an item at all, are we just supposed
318 * to return false or throw an exception? */
319 return this.isItem(something) && (
320 something[this.fmIdentifier] in this._current_items
324 "close": function(/* object */ request) { /* no-op */ return; },
326 "getLabel": function(/* object */ item) {
327 console.warn("[unimplemented] getLabel()");
330 "getLabelAttributes": function(/* object */ item) {
331 console.warn("[unimplemented] getLabelAttributes()");
334 "loadItem": function(/* object */ keywordArgs) {
335 if (!keywordArgs.force && this.isItemLoaded(keywordArgs.item))
338 keywordArgs.identity = this.getIdentity(keywordArgs.item);
339 return this.fetchItemByIdentity(keywordArgs);
342 "fetch": function(/* request-object */ req) {
343 // Respect the following properties of the *req* object:
345 // query a dojo-style query, which will need modest
346 // translation for our server-side service
348 // onBegin a callback that takes the number of items
349 // that this call to fetch() *could* have
350 // returned, with a higher limit. We do
352 // onItem a callback that takes each item as we get it
353 // onComplete a callback that takes the list of items
354 // after they're all fetched
357 var callback_scope = req.scope || dojo.global;
361 post_params = this._fetch_prepare(req);
363 if (typeof req.onError == "function")
364 req.onError.call(callback_scope, E);
369 var process_fetch = function(obj, when) {
370 if (when < self._last_fetch) /* Stale response. Discard. */
373 self._retried_map_key_already = false;
375 /* The following is apparently the "right" way to call onBegin,
376 * and is very necessary (at least in Dojo 1.3.3) to get
377 * the Grid's fetch-more-when-I-need-it logic to work
378 * correctly. *grumble* crummy documentation *snarl!*
380 if (typeof req.onBegin == "function") {
381 /* We lie to onBegin like this because we don't know how
382 * many more rows we might be able to fetch if the
383 * user keeps scrolling. Once we get a number of
384 * results that is less than the limit we asked for,
385 * we stop exaggerating, and the grid is smart enough to
386 * know we're at the end and it does the right thing. */
387 var might_be_a_lie = req.start;
388 if (obj.length >= req.count)
389 might_be_a_lie += obj.length + req.count;
391 might_be_a_lie += obj.length;
393 req.onBegin.call(callback_scope, might_be_a_lie, req);
399 /* Cache items internally. */
400 self._current_items[item[self.fmIdentifier]] = item;
402 if (typeof req.onItem == "function")
403 req.onItem.call(callback_scope, item, req);
407 if (typeof req.onComplete == "function")
408 req.onComplete.call(callback_scope, obj, req);
411 var process_error = dojo.hitch(
412 this, function(response, ioArgs) {
413 this._on_http_error(response, ioArgs, req, "fetch");
417 var fetch_time = this._last_fetch = (new Date().getTime());
423 function(obj) { process_fetch(obj, fetch_time); },
430 /* *** Begin dojo.data.api.Identity methods *** */
432 "getIdentity": function(/* object */ item) {
433 if (!this.isItem(item))
434 throw new FlattenerStoreError("not an item");
436 return item[this.fmIdentifier];
439 "getIdentityAttributes": function(/* object */ item) {
440 // console.log("getIdentityAttributes(" + item + ")");
441 return [this.fmIdentifier];
444 "fetchItemByIdentity": function(/* object */ keywordArgs) {
445 var callback_scope = keywordArgs.scope || dojo.global;
446 var identity = keywordArgs.identity;
448 if (typeof identity == "undefined")
449 throw new FlattenerStoreError(
450 "fetchItemByIdentity() needs identity in keywordArgs"
453 /* First of force's two implications:
454 * fetch even if already loaded. */
455 if (this._current_items[identity] && !keywordArgs.force) {
456 keywordArgs.onItem.call(
457 callback_scope, this._current_items[identity]
465 post_params = this._fetch_prepare(keywordArgs);
467 if (typeof keywordArgs.onError == "function")
468 keywordArgs.onError.call(callback_scope, E);
473 var process_fetch_one = dojo.hitch(
474 this, function(obj, when) {
475 if (when < this._last_fetch) /* Stale response. Discard. */
478 if (dojo.isArray(obj)) {
479 if (obj.length <= 1) {
480 obj = obj.pop() || null; /* safe enough */
481 /* Second of force's two implications: call setValue
482 * ourselves. Makes a DataGrid update. */
483 if (keywordArgs.force && obj &&
484 (origitem = this._current_items[identity])) {
485 for (var prop in origitem)
486 this.setValue(origitem, prop, obj[prop]);
488 if (keywordArgs.onItem)
489 keywordArgs.onItem.call(callback_scope, obj);
491 var e = new FlattenerStoreError("Too many results");
492 if (keywordArgs.onError)
493 keywordArgs.onError.call(callback_scope, e);
498 var e = new FlattenerStoreError("Bad response");
499 if (keywordArgs.onError)
500 keywordArgs.onError.call(callback_scope, e);
507 var process_error = dojo.hitch(
508 this, function(response, ioArgs) {
510 response, ioArgs, keywordArgs, "fetchItemByIdentity"
515 var fetch_time = this._last_fetch = (new Date().getTime());
521 function(obj) { process_fetch_one(obj, fetch_time); },
526 /* dojo.data.api.Write - only very partially implemented, because
527 * for FlattenerGrid, the intended client of this store, we don't
528 * need most of the methods. */
530 "deleteItem": function(item) {
531 //console.log("deleteItem()");
533 var identity = this.getIdentity(item);
534 delete this._current_items[identity]; /* safe even if missing */
539 "setValue": function(item, attribute, value) {
540 /* Silently do nothing when setValue()'s caller wants to change
541 * the identifier. They must be confused anyway. */
542 if (attribute == this.fmIdentifier)
545 var old_value = dojo.clone(item[attribute]);
547 item[attribute] = dojo.clone(value);
548 this.onSet(item, attribute, old_value, value);
551 "setValues": function(item, attribute, values) {
552 console.warn("[unimplemented] setValues()"); /* unneeded */
555 "newItem": function(keywordArgs, parentInfo) {
556 console.warn("[unimplemented] newItem()"); /* unneeded */
559 "unsetAttribute": function() {
560 console.warn("[unimplemented] unsetAttribute()"); /* unneeded */
564 console.warn("[unimplemented] save()"); /* unneeded */
567 "revert": function() {
568 console.warn("[unimplemented] revert()"); /* unneeded */
571 "isDirty": function() { /* I /think/ this will be ok for our purposes */
572 console.info("[stub] isDirty() will always return false");
577 /* dojo.data.api.Notification - Keep these no-op methods because
578 * clients will dojo.connect() to them. */
580 "onNew" : function(item) { /* no-op */ },
581 "onDelete" : function(item) { /* no-op */ },
582 "onSet": function(item, attr, oldval, newval) { /* no-op */ },
584 /* *** Classes implementing any Dojo APIs do this to list which
585 * APIs they're implementing. *** */
587 "getFeatures": function() {
589 "dojo.data.api.Read": true,
590 "dojo.data.api.Identity": true,
591 "dojo.data.api.Notification": true,
592 "dojo.data.api.Write": true /* well, only partly */