1 | define(["dojo", "dojox", "dojo/io/script", "dojo/data/util/sorter"], function(dojo, dojox) { |
---|
2 | |
---|
3 | dojo.declare("dojox.data.SnapLogicStore", null, { |
---|
4 | Parts: { |
---|
5 | DATA: "data", |
---|
6 | COUNT: "count" |
---|
7 | }, |
---|
8 | |
---|
9 | url: "", |
---|
10 | |
---|
11 | constructor: function(/* Object */args){ |
---|
12 | // summary: |
---|
13 | // Initialize a SnapLogicStore object. |
---|
14 | // args: |
---|
15 | // An object that contains properties for initializing the new data store object. The |
---|
16 | // following properties are understood: |
---|
17 | // url: |
---|
18 | // A URL to the SnapLogic pipeline's output routed through PipeToHttp. Typically, this |
---|
19 | // will look like "http://<server-host>:<port>/pipe/<pipeline-url>/<pipeline-output-view>". |
---|
20 | // parameters: |
---|
21 | // An object whose properties define parameters to the pipeline. The values of these |
---|
22 | // properties will be sent to the pipeline as parameters when it run. |
---|
23 | // |
---|
24 | if(args.url){ |
---|
25 | this.url = args.url; |
---|
26 | } |
---|
27 | this._parameters = args.parameters; |
---|
28 | }, |
---|
29 | |
---|
30 | _assertIsItem: function(/* item */item){ |
---|
31 | // summary: |
---|
32 | // This function tests whether the item passed in is indeed an item in the store. |
---|
33 | // item: |
---|
34 | // The item to test for being contained by the store. |
---|
35 | if(!this.isItem(item)){ |
---|
36 | throw new Error("dojox.data.SnapLogicStore: a function was passed an item argument that was not an item"); |
---|
37 | } |
---|
38 | }, |
---|
39 | |
---|
40 | _assertIsAttribute: function(/* attribute-name-string */ attribute){ |
---|
41 | // summary: |
---|
42 | // This function tests whether the item passed in is indeed a valid 'attribute' like type for the store. |
---|
43 | // attribute: |
---|
44 | // The attribute to test for being contained by the store. |
---|
45 | if(typeof attribute !== "string"){ |
---|
46 | throw new Error("dojox.data.SnapLogicStore: a function was passed an attribute argument that was not an attribute name string"); |
---|
47 | } |
---|
48 | }, |
---|
49 | |
---|
50 | getFeatures: function(){ |
---|
51 | // summary: |
---|
52 | // See dojo.data.api.Read.getFeatures() |
---|
53 | return { |
---|
54 | 'dojo.data.api.Read': true |
---|
55 | }; |
---|
56 | }, |
---|
57 | |
---|
58 | getValue: function(item, attribute, defaultValue){ |
---|
59 | // summary: |
---|
60 | // See dojo.data.api.Read.getValue() |
---|
61 | this._assertIsItem(item); |
---|
62 | this._assertIsAttribute(attribute); |
---|
63 | var i = dojo.indexOf(item.attributes, attribute); |
---|
64 | if(i !== -1){ |
---|
65 | return item.values[i]; |
---|
66 | } |
---|
67 | return defaultValue; |
---|
68 | }, |
---|
69 | |
---|
70 | getAttributes: function(item){ |
---|
71 | // summary: |
---|
72 | // See dojo.data.api.Read.getAttributes() |
---|
73 | this._assertIsItem(item); |
---|
74 | return item.attributes; |
---|
75 | }, |
---|
76 | |
---|
77 | hasAttribute: function(item, attribute){ |
---|
78 | // summary: |
---|
79 | // See dojo.data.api.Read.hasAttributes() |
---|
80 | this._assertIsItem(item); |
---|
81 | this._assertIsAttribute(attribute); |
---|
82 | for(var i = 0; i < item.attributes.length; ++i){ |
---|
83 | if(attribute == item.attributes[i]){ |
---|
84 | return true; |
---|
85 | } |
---|
86 | } |
---|
87 | return false; |
---|
88 | }, |
---|
89 | |
---|
90 | isItemLoaded: function(item){ |
---|
91 | // summary: |
---|
92 | // See dojo.data.api.Read.isItemLoaded() |
---|
93 | return this.isItem(item); // Boolean |
---|
94 | }, |
---|
95 | |
---|
96 | loadItem: function(keywordArgs){ |
---|
97 | // summary: |
---|
98 | // See dojo.data.api.Read.loadItem() |
---|
99 | }, |
---|
100 | |
---|
101 | getLabel: function(item){ |
---|
102 | // summary: |
---|
103 | // See dojo.data.api.Read.getLabel() |
---|
104 | return undefined; |
---|
105 | }, |
---|
106 | |
---|
107 | getLabelAttributes: function(item){ |
---|
108 | // summary: |
---|
109 | // See dojo.data.api.Read.getLabelAttributes() |
---|
110 | return null; |
---|
111 | }, |
---|
112 | |
---|
113 | containsValue: function(item, attribute, value){ |
---|
114 | // summary: |
---|
115 | // See dojo.data.api.Read.containsValue() |
---|
116 | return this.getValue(item, attribute) === value; // Boolean |
---|
117 | }, |
---|
118 | |
---|
119 | getValues: function(item, attribute){ |
---|
120 | // summary: |
---|
121 | // See dojo.data.api.Read.getValue() |
---|
122 | this._assertIsItem(item); |
---|
123 | this._assertIsAttribute(attribute); |
---|
124 | var i = dojo.indexOf(item.attributes, attribute); |
---|
125 | if(i !== -1){ |
---|
126 | return [item.values[i]]; // Array |
---|
127 | } |
---|
128 | return []; |
---|
129 | }, |
---|
130 | |
---|
131 | isItem: function(item){ |
---|
132 | // summary: |
---|
133 | // See dojo.data.api.Read.isItem() |
---|
134 | if(item && item._store === this){ |
---|
135 | return true; |
---|
136 | } |
---|
137 | return false; |
---|
138 | }, |
---|
139 | |
---|
140 | close: function(request){ |
---|
141 | // summary: |
---|
142 | // See dojo.data.api.Read.close() |
---|
143 | }, |
---|
144 | |
---|
145 | _fetchHandler: function(/* Object */request){ |
---|
146 | // summary: |
---|
147 | // Process data retrieved via fetch and send it back to requester. |
---|
148 | // response: |
---|
149 | // The data returend from the I/O transport. In the normal case, it will be an array of result rows |
---|
150 | // from the pipeline. In the special case for record count optimization, response will be an array |
---|
151 | // with a single element containing the total pipeline result row count. See fetch() for details |
---|
152 | // on this optimization. |
---|
153 | |
---|
154 | var scope = request.scope || dojo.global; |
---|
155 | |
---|
156 | if(request.onBegin){ |
---|
157 | // Check for the record count optimization |
---|
158 | request.onBegin.call(scope, request._countResponse[0], request); |
---|
159 | } |
---|
160 | |
---|
161 | if(request.onItem || request.onComplete){ |
---|
162 | var response = request._dataResponse; |
---|
163 | |
---|
164 | if(!response.length){ |
---|
165 | request.onError.call(scope, |
---|
166 | new Error("dojox.data.SnapLogicStore: invalid response of length 0"), |
---|
167 | request); |
---|
168 | return; |
---|
169 | }else if(request.query != 'record count'){ |
---|
170 | //If this was not a record count request, the first element returned will contain |
---|
171 | //the field names. |
---|
172 | var field_names = response.shift(); |
---|
173 | |
---|
174 | var items = []; |
---|
175 | for(var i = 0; i < response.length; ++i){ |
---|
176 | if(request._aborted){ |
---|
177 | break; |
---|
178 | } |
---|
179 | |
---|
180 | items.push({attributes: field_names, values: response[i], _store: this}); |
---|
181 | } |
---|
182 | |
---|
183 | if(request.sort && !request._aborted){ |
---|
184 | items.sort(dojo.data.util.sorter.createSortFunction(request.sort, self)); |
---|
185 | } |
---|
186 | }else{ |
---|
187 | //This is a record count request, so manually set the field names. |
---|
188 | items = [({attributes: ['count'], values: response, _store: this})]; |
---|
189 | } |
---|
190 | |
---|
191 | if(request.onItem){ |
---|
192 | for(var i = 0; i < items.length; ++i){ |
---|
193 | if(request._aborted){ |
---|
194 | break; |
---|
195 | } |
---|
196 | request.onItem.call(scope, items[i], request); |
---|
197 | } |
---|
198 | items = null; |
---|
199 | } |
---|
200 | |
---|
201 | if(request.onComplete && !request._aborted){ |
---|
202 | request.onComplete.call(scope, items, request); |
---|
203 | } |
---|
204 | } |
---|
205 | }, |
---|
206 | |
---|
207 | _partHandler: function(/* Object */request, /* String */part, /* Object */response){ |
---|
208 | // summary: |
---|
209 | // Handle the individual replies for both data and length requests. |
---|
210 | // request: |
---|
211 | // The request/handle object used with the original fetch() call. |
---|
212 | // part: |
---|
213 | // A value indicating which request this handler call is for (this.Parts). |
---|
214 | // response: |
---|
215 | // Response received from the underlying IO transport. |
---|
216 | |
---|
217 | if(response instanceof Error){ |
---|
218 | if(part == this.Parts.DATA){ |
---|
219 | request._dataHandle = null; |
---|
220 | }else{ |
---|
221 | request._countHandle = null; |
---|
222 | } |
---|
223 | request._aborted = true; |
---|
224 | if(request.onError){ |
---|
225 | request.onError.call(request.scope, response, request); |
---|
226 | } |
---|
227 | }else{ |
---|
228 | if(request._aborted){ |
---|
229 | return; |
---|
230 | } |
---|
231 | if(part == this.Parts.DATA){ |
---|
232 | request._dataResponse = response; |
---|
233 | }else{ |
---|
234 | request._countResponse = response; |
---|
235 | } |
---|
236 | if((!request._dataHandle || request._dataResponse !== null) && |
---|
237 | (!request._countHandle || request._countResponse !== null)){ |
---|
238 | this._fetchHandler(request); |
---|
239 | } |
---|
240 | } |
---|
241 | }, |
---|
242 | |
---|
243 | fetch: function(/* Object */request){ |
---|
244 | // summary: |
---|
245 | // See dojo.data.api.Read.close() |
---|
246 | // request: |
---|
247 | // See dojo.data.api.Read.close() for generic interface. |
---|
248 | // |
---|
249 | // In addition to the standard Read API fetch support, this store supports an optimization for |
---|
250 | // for retrieving the total count of records in the Pipeline without retrieving the data. To |
---|
251 | // use this optimization, simply provide an onBegin handler without an onItem or onComplete handler. |
---|
252 | |
---|
253 | request._countResponse = null; |
---|
254 | request._dataResponse = null; |
---|
255 | request._aborted = false; |
---|
256 | request.abort = function(){ |
---|
257 | if(!request._aborted){ |
---|
258 | request._aborted = true; |
---|
259 | if(request._dataHandle && request._dataHandle.cancel){ |
---|
260 | request._dataHandle.cancel(); |
---|
261 | } |
---|
262 | if(request._countHandle && request._countHandle.cancel){ |
---|
263 | request._countHandle.cancel(); |
---|
264 | } |
---|
265 | } |
---|
266 | }; |
---|
267 | |
---|
268 | // Only make the call for data if onItem or onComplete is used. Otherwise, onBegin will only |
---|
269 | // require the total row count. |
---|
270 | if(request.onItem || request.onComplete){ |
---|
271 | var content = this._parameters || {}; |
---|
272 | if(request.start){ |
---|
273 | if(request.start < 0){ |
---|
274 | throw new Error("dojox.data.SnapLogicStore: request start value must be 0 or greater"); |
---|
275 | } |
---|
276 | content['sn.start'] = request.start + 1; |
---|
277 | } |
---|
278 | if(request.count){ |
---|
279 | if(request.count < 0){ |
---|
280 | throw new Error("dojox.data.SnapLogicStore: request count value 0 or greater"); |
---|
281 | } |
---|
282 | content['sn.limit'] = request.count; |
---|
283 | } |
---|
284 | |
---|
285 | content['sn.content_type'] = 'application/javascript'; |
---|
286 | |
---|
287 | var store = this; |
---|
288 | var handler = function(response, ioArgs){ |
---|
289 | if(response instanceof Error){ |
---|
290 | store._fetchHandler(response, request); |
---|
291 | } |
---|
292 | }; |
---|
293 | |
---|
294 | var getArgs = { |
---|
295 | url: this.url, |
---|
296 | content: content, |
---|
297 | // preventCache: true, |
---|
298 | timeout: 60000, //Starting a pipeline can take a long time. |
---|
299 | callbackParamName: "sn.stream_header", |
---|
300 | handle: dojo.hitch(this, "_partHandler", request, this.Parts.DATA) |
---|
301 | }; |
---|
302 | |
---|
303 | request._dataHandle = dojo.io.script.get(getArgs); |
---|
304 | } |
---|
305 | |
---|
306 | if(request.onBegin){ |
---|
307 | var content = {}; |
---|
308 | content['sn.count'] = 'records'; |
---|
309 | content['sn.content_type'] = 'application/javascript'; |
---|
310 | |
---|
311 | var getArgs = { |
---|
312 | url: this.url, |
---|
313 | content: content, |
---|
314 | timeout: 60000, |
---|
315 | callbackParamName: "sn.stream_header", |
---|
316 | handle: dojo.hitch(this, "_partHandler", request, this.Parts.COUNT) |
---|
317 | }; |
---|
318 | |
---|
319 | request._countHandle = dojo.io.script.get(getArgs); |
---|
320 | } |
---|
321 | |
---|
322 | return request; // Object |
---|
323 | } |
---|
324 | }); |
---|
325 | |
---|
326 | return dojox.data.SnapLogicStore; |
---|
327 | }); |
---|
328 | |
---|