source: Dev/trunk/src/client/dojox/data/SnapLogicStore.js @ 529

Last change on this file since 529 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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