Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["dojox.data.QueryReadStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["dojox.data.QueryReadStore"] = true;
3
dojo.provide("dojox.data.QueryReadStore");
4
dojo.provide("dojox.data.QueryReadStore.InvalidItemError");
5
dojo.provide("dojox.data.QueryReadStore.InvalidAttributeError");
6
 
7
dojo.require("dojo.string");
8
dojo.require("dojo.data.util.simpleFetch");
9
 
10
dojo.declare("dojox.data.QueryReadStore", null, {
11
	/*
12
	//	summary:
13
	//		This class provides a store that is mainly intended to be used
14
	//		for loading data dynamically from the server, used i.e. for
15
	//		retreiving chunks of data from huge data stores on the server (by server-side filtering!).
16
	//		Upon calling the fetch() method of this store the data are requested from
17
	//		the server if they are not yet loaded for paging (or cached).
18
	//
19
	//		For example used for a combobox which works on lots of data. It
20
	//		can be used to retreive the data partially upon entering the
21
	//		letters "ac" it returns only items like "action", "acting", etc.
22
	//
23
	//  note:
24
	//      The field name "id" in a query is reserved for looking up data
25
	//      by id. This is necessary as before the first fetch, the store
26
	//      has no way of knowing which field the server will declare as
27
	//      identifier.
28
	//
29
	//	examples:
30
	//		// The parameter "query" contains the data that are sent to the server.
31
	//		var store = new dojox.data.QueryReadStore({url:'/search.php'});
32
	//		store.fetch({query:{name:'a'}, queryOptions:{ignoreCase:false}});
33
	//
34
	//		// Since "serverQuery" is given, it overrules and those data are
35
	//		// sent to the server.
36
	//		var store = new dojox.data.QueryReadStore({url:'/search.php'});
37
	//		store.fetch({serverQuery:{name:'a'}, queryOptions:{ignoreCase:false}});
38
	//
39
	//	todo:
40
	//		- there is a bug in the paging, when i set start:2, count:5 after an initial fetch() and doClientPaging:true
41
	//		  it returns 6 elemetns, though count=5, try it in QueryReadStore.html
42
	//		- allow configuring if the paging shall takes place on the client or the server
43
	//		- add optional caching
44
	//		- when the first query searched for "a" and the next for a subset of
45
	//		  the first, i.e. "ab" then we actually dont need a server request, if
46
	//		  we have client paging, we just need to filter the items we already have
47
	//		  that might also be tooo much logic
48
	*/
49
 
50
	url:"",
51
	requestMethod:"get",
52
	//useCache:false,
53
 
54
	// We use the name in the errors, once the name is fixed hardcode it, may be.
55
	_className:"dojox.data.QueryReadStore",
56
 
57
	// This will contain the items we have loaded from the server.
58
	// The contents of this array is optimized to satisfy all read-api requirements
59
	// and for using lesser storage, so the keys and their content need some explaination:
60
	// 		this._items[0].i - the item itself
61
	//		this._items[0].r - a reference to the store, so we can identify the item
62
	//			securly. We set this reference right after receiving the item from the
63
	//			server.
64
	_items:[],
65
 
66
	// Store the last query that triggered xhr request to the server.
67
	// So we can compare if the request changed and if we shall reload
68
	// (this also depends on other factors, such as is caching used, etc).
69
	_lastServerQuery:null,
70
 
71
 
72
	// Store a hash of the last server request. Actually I introduced this
73
	// for testing, so I can check if no unnecessary requests were issued for
74
	// client-side-paging.
75
	lastRequestHash:null,
76
 
77
	// If this is false, every request is sent to the server.
78
	// If it's true a second request with the same query will not issue another
79
	// request, but use the already returned data. This assumes that the server
80
	// does not do the paging.
81
	doClientPaging:true,
82
 
83
	// Items by identify for Identify API
84
	_itemsByIdentity:null,
85
 
86
	// Identifier used
87
	_identifier:null,
88
 
89
	_features: {'dojo.data.api.Read':true, 'dojo.data.api.Identity':true},
90
 
91
	constructor: function(/* Object */ params){
92
		dojo.mixin(this,params);
93
	},
94
 
95
	getValue: function(/* item */ item, /* attribute-name-string */ attribute, /* value? */ defaultValue){
96
		//	According to the Read API comments in getValue() and exception is
97
		//	thrown when an item is not an item or the attribute not a string!
98
		this._assertIsItem(item);
99
		if (!dojo.isString(attribute)) {
100
			throw new Error(this._className+".getValue(): Invalid attribute, string expected!");
101
		}
102
		if(!this.hasAttribute(item, attribute)){
103
			// read api says: return defaultValue "only if *item* does not have a value for *attribute*."
104
			// Is this the case here? The attribute doesn't exist, but a defaultValue, sounds reasonable.
105
			if(defaultValue){
106
				return defaultValue;
107
			}
108
			console.log(this._className+".getValue(): Item does not have the attribute '"+attribute+"'.");
109
		}
110
		return item.i[attribute];
111
	},
112
 
113
	getValues: function(/* item */ item, /* attribute-name-string */ attribute){
114
		var ret = [];
115
		if(this.hasAttribute(item, attribute)){
116
			ret.push(item.i[attribute]);
117
		}
118
		return ret;
119
	},
120
 
121
	getAttributes: function(/* item */ item){
122
		this._assertIsItem(item);
123
		var ret = [];
124
		for(var i in item.i){
125
			ret.push(i);
126
		}
127
		return ret;
128
	},
129
 
130
	hasAttribute: function(/* item */ item,	/* attribute-name-string */ attribute) {
131
		//	summary:
132
		//		See dojo.data.api.Read.hasAttribute()
133
		return this.isItem(item) && typeof item.i[attribute]!="undefined";
134
	},
135
 
136
	containsValue: function(/* item */ item, /* attribute-name-string */ attribute, /* anything */ value){
137
		var values = this.getValues(item, attribute);
138
		var len = values.length;
139
		for(var i=0; i<len; i++){
140
			if(values[i]==value){
141
				return true;
142
			}
143
		}
144
		return false;
145
	},
146
 
147
	isItem: function(/* anything */ something){
148
		// Some basic tests, that are quick and easy to do here.
149
		// >>> var store = new dojox.data.QueryReadStore({});
150
		// >>> store.isItem("");
151
		// false
152
		//
153
		// >>> var store = new dojox.data.QueryReadStore({});
154
		// >>> store.isItem({});
155
		// false
156
		//
157
		// >>> var store = new dojox.data.QueryReadStore({});
158
		// >>> store.isItem(0);
159
		// false
160
		//
161
		// >>> var store = new dojox.data.QueryReadStore({});
162
		// >>> store.isItem({name:"me", label:"me too"});
163
		// false
164
		//
165
		if(something){
166
			return typeof something.r!="undefined" && something.r==this;
167
		}
168
		return false;
169
	},
170
 
171
	isItemLoaded: function(/* anything */ something) {
172
		// Currently we dont have any state that tells if an item is loaded or not
173
		// if the item exists its also loaded.
174
		// This might change when we start working with refs inside items ...
175
		return this.isItem(something);
176
	},
177
 
178
	loadItem: function(/* object */ args){
179
		if(this.isItemLoaded(args.item)){
180
			return;
181
		}
182
		// Actually we have nothing to do here, or at least I dont know what to do here ...
183
	},
184
 
185
	fetch:function(/* Object? */ request){
186
		//	summary:
187
		//		See dojo.data.util.simpleFetch.fetch() this is just a copy and I adjusted
188
		//		only the paging, since it happens on the server if doClientPaging is
189
		//		false, thx to http://trac.dojotoolkit.org/ticket/4761 reporting this.
190
		//		Would be nice to be able to use simpleFetch() to reduce copied code,
191
		//		but i dont know how yet. Ideas please!
192
		request = request || {};
193
		if(!request.store){
194
			request.store = this;
195
		}
196
		var self = this;
197
 
198
		var _errorHandler = function(errorData, requestObject){
199
			if(requestObject.onError){
200
				var scope = requestObject.scope || dojo.global;
201
				requestObject.onError.call(scope, errorData, requestObject);
202
			}
203
		};
204
 
205
		var _fetchHandler = function(items, requestObject){
206
			var oldAbortFunction = requestObject.abort || null;
207
			var aborted = false;
208
 
209
			var startIndex = requestObject.start?requestObject.start:0;
210
			if (self.doClientPaging==false) {
211
				// For client paging we dont need no slicing of the result.
212
				startIndex = 0;
213
			}
214
			var endIndex   = requestObject.count?(startIndex + requestObject.count):items.length;
215
 
216
			requestObject.abort = function(){
217
				aborted = true;
218
				if(oldAbortFunction){
219
					oldAbortFunction.call(requestObject);
220
				}
221
			};
222
 
223
			var scope = requestObject.scope || dojo.global;
224
			if(!requestObject.store){
225
				requestObject.store = self;
226
			}
227
			if(requestObject.onBegin){
228
				requestObject.onBegin.call(scope, items.length, requestObject);
229
			}
230
			if(requestObject.sort){
231
				items.sort(dojo.data.util.sorter.createSortFunction(requestObject.sort, self));
232
			}
233
			if(requestObject.onItem){
234
				for(var i = startIndex; (i < items.length) && (i < endIndex); ++i){
235
					var item = items[i];
236
					if(!aborted){
237
						requestObject.onItem.call(scope, item, requestObject);
238
					}
239
				}
240
			}
241
			if(requestObject.onComplete && !aborted){
242
				var subset = null;
243
				if (!requestObject.onItem) {
244
					subset = items.slice(startIndex, endIndex);
245
				}
246
				requestObject.onComplete.call(scope, subset, requestObject);
247
			}
248
		};
249
		this._fetchItems(request, _fetchHandler, _errorHandler);
250
		return request;	// Object
251
	},
252
 
253
	getFeatures: function(){
254
		return this._features;
255
	},
256
 
257
	close: function(/*dojo.data.api.Request || keywordArgs || null */ request){
258
		// I have no idea if this is really needed ...
259
	},
260
 
261
	getLabel: function(/* item */ item){
262
		// Override it to return whatever the label shall be, see Read-API.
263
		return undefined;
264
	},
265
 
266
	getLabelAttributes: function(/* item */ item){
267
		return null;
268
	},
269
 
270
	_fetchItems: function(request, fetchHandler, errorHandler){
271
		//	summary:
272
		// 		The request contains the data as defined in the Read-API.
273
		// 		Additionally there is following keyword "serverQuery".
274
		//
275
		//	The *serverQuery* parameter, optional.
276
		//		This parameter contains the data that will be sent to the server.
277
		//		If this parameter is not given the parameter "query"'s
278
		//		data are sent to the server. This is done for some reasons:
279
		//		- to specify explicitly which data are sent to the server, they
280
		//		  might also be a mix of what is contained in "query", "queryOptions"
281
		//		  and the paging parameters "start" and "count" or may be even
282
		//		  completely different things.
283
		//		- don't modify the request.query data, so the interface using this
284
		//		  store can rely on unmodified data, as the combobox dijit currently
285
		//		  does it, it compares if the query has changed
286
		//		- request.query is required by the Read-API
287
		//
288
		// 		I.e. the following examples might be sent via GET:
289
		//		  fetch({query:{name:"abc"}, queryOptions:{ignoreCase:true}})
290
		//		  the URL will become:   /url.php?name=abc
291
		//
292
		//		  fetch({serverQuery:{q:"abc", c:true}, query:{name:"abc"}, queryOptions:{ignoreCase:true}})
293
		//		  the URL will become:   /url.php?q=abc&c=true
294
		//		  // The serverQuery-parameter has overruled the query-parameter
295
		//		  // but the query parameter stays untouched, but is not sent to the server!
296
		//		  // The serverQuery contains more data than the query, so they might differ!
297
		//
298
 
299
		var serverQuery = request.serverQuery || request.query || {};
300
		//Need to add start and count
301
		if(!this.doClientPaging){
302
			serverQuery.start = request.start || 0;
303
			// Count might not be sent if not given.
304
			if (request.count) {
305
				serverQuery.count = request.count;
306
			}
307
		}
308
		// Compare the last query and the current query by simply json-encoding them,
309
		// so we dont have to do any deep object compare ... is there some dojo.areObjectsEqual()???
310
		if(this.doClientPaging && this._lastServerQuery!==null &&
311
			dojo.toJson(serverQuery)==dojo.toJson(this._lastServerQuery)
312
			){
313
			fetchHandler(this._items, request);
314
		}else{
315
			var xhrFunc = this.requestMethod.toLowerCase()=="post" ? dojo.xhrPost : dojo.xhrGet;
316
			var xhrHandler = xhrFunc({url:this.url, handleAs:"json-comment-optional", content:serverQuery});
317
			xhrHandler.addCallback(dojo.hitch(this, function(data){
318
				data=this._filterResponse(data);
319
				this._items = [];
320
				// Store a ref to "this" in each item, so we can simply check if an item
321
				// really origins form here (idea is from ItemFileReadStore, I just don't know
322
				// how efficient the real storage use, garbage collection effort, etc. is).
323
				dojo.forEach(data.items,function(e){
324
					this._items.push({i:e, r:this});
325
				},this);
326
 
327
				var identifier = data.identifier;
328
				this._itemsByIdentity = {};
329
				if(identifier){
330
					this._identifier = identifier;
331
					for(i = 0; i < this._items.length; ++i){
332
						var item = this._items[i].i;
333
						var identity = item[identifier];
334
						if(!this._itemsByIdentity[identity]){
335
							this._itemsByIdentity[identity] = item;
336
						}else{
337
							throw new Error("dojo.data.QueryReadStore:  The json data as specified by: [" + this.url + "] is malformed.  Items within the list have identifier: [" + identifier + "].  Value collided: [" + identity + "]");
338
						}
339
					}
340
				}else{
341
					this._identifier = Number;
342
					for(i = 0; i < this._items.length; ++i){
343
						this._items[i].n = i;
344
					}
345
				}
346
 
347
				// TODO actually we should do the same as dojo.data.ItemFileReadStore._getItemsFromLoadedData() to sanitize
348
				// (does it really sanititze them) and store the data optimal. should we? for security reasons???
349
				fetchHandler(this._items, request);
350
			}));
351
			xhrHandler.addErrback(function(error){
352
				errorHandler(error, request);
353
			});
354
			// Generate the hash using the time in milliseconds and a randon number.
355
			// Since Math.randon() returns something like: 0.23453463, we just remove the "0."
356
			// probably just for esthetic reasons :-).
357
			this.lastRequestHash = new Date().getTime()+"-"+String(Math.random()).substring(2);
358
			this._lastServerQuery = dojo.mixin({}, serverQuery);
359
		}
360
	},
361
 
362
	_filterResponse: function(data){
363
		//	summary:
364
		//		If the data from servers needs to be processed before it can be processed by this
365
		//		store, then this function should be re-implemented in subclass. This default
366
		//		implementation just return the data unchanged.
367
		//	data:
368
		//		The data received from server
369
		return data;
370
	},
371
 
372
	_assertIsItem: function(/* item */ item){
373
		//	summary:
374
		//		It throws an error if item is not valid, so you can call it in every method that needs to
375
		//		throw an error when item is invalid.
376
		//	item:
377
		//		The item to test for being contained by the store.
378
		if(!this.isItem(item)){
379
			throw new dojox.data.QueryReadStore.InvalidItemError(this._className+": a function was passed an item argument that was not an item");
380
		}
381
	},
382
 
383
	_assertIsAttribute: function(/* attribute-name-string */ attribute){
384
		//	summary:
385
		//		This function tests whether the item passed in is indeed a valid 'attribute' like type for the store.
386
		//	attribute:
387
		//		The attribute to test for being contained by the store.
388
		if(typeof attribute !== "string"){
389
			throw new dojox.data.QueryReadStore.InvalidAttributeError(this._className+": '"+attribute+"' is not a valid attribute identifier.");
390
		}
391
	},
392
 
393
	fetchItemByIdentity: function(/* Object */ keywordArgs){
394
		//	summary:
395
		//		See dojo.data.api.Identity.fetchItemByIdentity()
396
 
397
		// See if we have already loaded the item with that id
398
		// In case there hasn't been a fetch yet, _itemsByIdentity is null
399
		// and thus a fetch will be triggered below.
400
		if(this._itemsByIdentity){
401
			var item = this._itemsByIdentity[keywordArgs.identity];
402
			if(!(item === undefined)){
403
				if(keywordArgs.onItem){
404
					var scope =  keywordArgs.scope?keywordArgs.scope:dojo.global;
405
					keywordArgs.onItem.call(scope, {i:item, r:this});
406
				}
407
				return;
408
			}
409
		}
410
 
411
		// Otherwise we need to go remote
412
		// Set up error handler
413
		var _errorHandler = function(errorData, requestObject){
414
			var scope =  keywordArgs.scope?keywordArgs.scope:dojo.global;
415
			if(keywordArgs.onError){
416
				keywordArgs.onError.call(scope, error);
417
			}
418
		};
419
 
420
		// Set up fetch handler
421
		var _fetchHandler = function(items, requestObject){
422
			var scope =  keywordArgs.scope?keywordArgs.scope:dojo.global;
423
			try{
424
				// There is supposed to be only one result
425
				var item = null;
426
				if(items && items.length == 1){
427
					item = items[0];
428
				}
429
 
430
				// If no item was found, item is still null and we'll
431
				// fire the onItem event with the null here
432
				if(keywordArgs.onItem){
433
					keywordArgs.onItem.call(scope, item);
434
				}
435
			}catch(error){
436
				if(keywordArgs.onError){
437
					keywordArgs.onError.call(scope, error);
438
				}
439
			}
440
		};
441
 
442
		// Construct query
443
		var request = {serverQuery:{id:keywordArgs.identity}};
444
 
445
		// Dispatch query
446
		this._fetchItems(request, _fetchHandler, _errorHandler);
447
	},
448
 
449
	getIdentity: function(/* item */ item){
450
		//	summary:
451
		//		See dojo.data.api.Identity.getIdentity()
452
		var identifier = null;
453
		if(this._identifier === Number){
454
			identifier = item.n; // Number
455
		}else{
456
			identifier = item.i[this._identifier];
457
		}
458
		return identifier;
459
	},
460
 
461
	getIdentityAttributes: function(/* item */ item){
462
		//	summary:
463
		//		See dojo.data.api.Identity.getIdentityAttributes()
464
		return [this._identifier];
465
	}
466
});
467
 
468
dojo.declare("dojox.data.QueryReadStore.InvalidItemError", Error, {});
469
dojo.declare("dojox.data.QueryReadStore.InvalidAttributeError", Error, {});
470
 
471
 
472
 
473
 
474
}