aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/data/SnapLogicStore.js
blob: 101ab91979949318efaafd9697b04b94056869a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
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){
		//	summary: 
		//		See dojo.data.api.Read.getValue()
		this._assertIsItem(item);
		this._assertIsAttribute(attribute);
		i = dojo.indexOf(item.attributes, attribute);
		if(i !== -1){
			return item.values[i];
		}
		return undefined;
	},

	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);
		i = dojo.indexOf(item.attributes, attribute);
		if(i !== -1){
			return [item.values[i]];	// Array
		}
		return undefined;
	},

	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){
			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.
				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
	}
});


}