| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 | /*	Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.	Available via Academic Free License >= 2.1 OR the modified BSD license.	see: http://dojotoolkit.org/license for details*/if(!dojo._hasResource["dojox.data.SnapLogicStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.dojo._hasResource["dojox.data.SnapLogicStore"] = true;dojo.provide("dojox.data.SnapLogicStore");dojo.require("dojo.io.script");dojo.require("dojo.data.util.sorter");dojo.declare("dojox.data.SnapLogicStore", null, {	Parts: {		DATA: "data",		COUNT: "count"	},	url: "",	constructor: function(/* Object */args){		//	summary:		//		Initialize a SnapLogicStore object.		//	args:		//		An object that contains properties for initializing the new data store object. The		//		following properties are understood:		//			url:		//				A URL to the SnapLogic pipeline's output routed through PipeToHttp. Typically, this		//				will look like "http://<server-host>:<port>/pipe/<pipeline-url>/<pipeline-output-view>".		//			parameters:		//				An object whose properties define parameters to the pipeline. The values of these		//				properties will be sent to the pipeline as parameters when it run.		//		if(args.url){			this.url = args.url;		}		this._parameters = args.parameters;	},	_assertIsItem: function(/* item */item){		//	summary:		//		This function tests whether the item passed in is indeed an item in the store.		//	item:		//		The item to test for being contained by the store.		if(!this.isItem(item)){			throw new Error("dojox.data.SnapLogicStore: a function was passed an item argument that was not an item");		}	},	_assertIsAttribute: function(/* attribute-name-string */ attribute){		//	summary:		//		This function tests whether the item passed in is indeed a valid 'attribute' like type for the store.		//	attribute:		//		The attribute to test for being contained by the store.		if(typeof attribute !== "string"){			throw new Error("dojox.data.SnapLogicStore: a function was passed an attribute argument that was not an attribute name string");		}	},	getFeatures: function(){		//	summary:		//		See dojo.data.api.Read.getFeatures()		return {			'dojo.data.api.Read': true		};	},	getValue: function(item, attribute, defaultValue){		//	summary:		//		See dojo.data.api.Read.getValue()		this._assertIsItem(item);		this._assertIsAttribute(attribute);		var i = dojo.indexOf(item.attributes, attribute);		if(i !== -1){			return item.values[i];		}		return defaultValue;	},	getAttributes: function(item){		//	summary:		//		See dojo.data.api.Read.getAttributes()		this._assertIsItem(item);		return item.attributes;	},	hasAttribute: function(item, attribute){		//	summary:		//		See dojo.data.api.Read.hasAttributes()		this._assertIsItem(item);		this._assertIsAttribute(attribute);		for(var i = 0; i < item.attributes.length; ++i){			if(attribute == item.attributes[i]){				return true;			}		}		return false;	},	isItemLoaded: function(item){		 //	summary:		 //		 See dojo.data.api.Read.isItemLoaded()		 return this.isItem(item);		// Boolean	},	loadItem: function(keywordArgs){		//	summary:		//		See dojo.data.api.Read.loadItem()	},	getLabel: function(item){		//	summary:		//		See dojo.data.api.Read.getLabel()		return undefined;	},		getLabelAttributes: function(item){		//	summary:		//		See dojo.data.api.Read.getLabelAttributes()		return null;	},	containsValue: function(item, attribute, value){		//	summary:		//		See dojo.data.api.Read.containsValue()		return this.getValue(item, attribute) === value;		// Boolean	},	getValues: function(item, attribute){		//	summary:		//		See dojo.data.api.Read.getValue()		this._assertIsItem(item);		this._assertIsAttribute(attribute);		var i = dojo.indexOf(item.attributes, attribute);		if(i !== -1){			return [item.values[i]];	// Array		}		return [];	},	isItem: function(item){		//	summary:		//		See dojo.data.api.Read.isItem()		if(item && item._store === this){			return true;		}		return false;	},		close: function(request){		//	summary:		//		See dojo.data.api.Read.close()	},	_fetchHandler: function(/* Object */request){		//	summary:		//		Process data retrieved via fetch and send it back to requester.		//	response:		//		The data returend from the I/O transport. In the normal case, it will be an array of result rows		//		from the pipeline. In the special case for record count optimization, response will be an array		//		with a single element containing the total pipeline result row count. See fetch() for details		//		on this optimization.		var scope = request.scope || dojo.global;		if(request.onBegin){			// Check for the record count optimization			request.onBegin.call(scope, request._countResponse[0], request);		}				if(request.onItem || request.onComplete){			var response = request._dataResponse;			if(!response.length){				request.onError.call(scope,									 new Error("dojox.data.SnapLogicStore: invalid response of length 0"),									 request);				return;			}else if(request.query != 'record count'){				//If this was not a record count request, the first element returned will contain				//the field names.				var field_names = response.shift();								var items = [];				for(var i = 0; i < response.length; ++i){					if(request._aborted){						break;					}					items.push({attributes: field_names, values: response[i], _store: this});				}				if(request.sort && !request._aborted){					items.sort(dojo.data.util.sorter.createSortFunction(request.sort, self));				}			}else{				//This is a record count request, so manually set the field names.				items = [({attributes: ['count'], values: response, _store: this})];			}			if(request.onItem){				for(var i = 0; i < items.length; ++i){					if(request._aborted){						break;					}					request.onItem.call(scope, items[i], request);				}				items = null;			}			if(request.onComplete && !request._aborted){				request.onComplete.call(scope, items, request);			}		}	},			_partHandler: function(/* Object */request, /* String */part, /* Object */response){		//	summary:		//		Handle the individual replies for both data and length requests.		//	request:		//		The request/handle object used with the original fetch() call.		//  part:		//		A value indicating which request this handler call is for (this.Parts).		//	response:		//		Response received from the underlying IO transport.		if(response instanceof Error){			if(part == this.Parts.DATA){				request._dataHandle = null;			}else{				request._countHandle = null;			}			request._aborted = true;			if(request.onError){				request.onError.call(request.scope, response, request);			}		}else{			if(request._aborted){				return;			}			if(part == this.Parts.DATA){				request._dataResponse = response;			}else{				request._countResponse = response;			}			if((!request._dataHandle || request._dataResponse !== null) &&				(!request._countHandle || request._countResponse !== null)){				this._fetchHandler(request);			}		}	},	fetch: function(/* Object */request){		//	summary:		//		See dojo.data.api.Read.close()		//	request:		//		See dojo.data.api.Read.close() for generic interface.		//		//		In addition to the standard Read API fetch support, this store supports an optimization for		//		for retrieving the total count of records in the Pipeline without retrieving the data. To		//		use this optimization, simply provide an onBegin handler without an onItem or onComplete handler.		request._countResponse = null;		request._dataResponse = null;		request._aborted = false;		request.abort = function(){			if(!request._aborted){				request._aborted = true;				if(request._dataHandle && request._dataHandle.cancel){					request._dataHandle.cancel();				}				if(request._countHandle && request._countHandle.cancel){					request._countHandle.cancel();				}			}		};		// Only make the call for data if onItem or onComplete is used. Otherwise, onBegin will only		// require the total row count.		if(request.onItem || request.onComplete){			var content = this._parameters || {};			if(request.start){				if(request.start < 0){					throw new Error("dojox.data.SnapLogicStore: request start value must be 0 or greater");				}				content['sn.start'] = request.start + 1;			}			if(request.count){				if(request.count < 0){					throw new Error("dojox.data.SnapLogicStore: request count value 0 or greater");				}				content['sn.limit'] = request.count;			}						content['sn.content_type'] = 'application/javascript';			var store = this;			var handler = function(response, ioArgs){				if(response instanceof Error){					store._fetchHandler(response, request);				}			};			var getArgs = {				url: this.url,				content: content,				// preventCache: true,				timeout: 60000,								//Starting a pipeline can take a long time.				callbackParamName: "sn.stream_header",				handle: dojo.hitch(this, "_partHandler", request, this.Parts.DATA)			};			request._dataHandle = dojo.io.script.get(getArgs);		}				if(request.onBegin){			var content = {};			content['sn.count'] = 'records';			content['sn.content_type'] = 'application/javascript';			var getArgs = {				url: this.url,				content: content,				timeout: 60000,				callbackParamName: "sn.stream_header",				handle: dojo.hitch(this, "_partHandler", request, this.Parts.COUNT)			};			request._countHandle = dojo.io.script.get(getArgs);		}					return request;			// Object	}});}
 |