1 | /** |
---|
2 | * Author : Samuel Souk aloun |
---|
3 | * |
---|
4 | * Copyright (c) 2014 GeoLabs SARL |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
7 | * of this software and associated documentation files (the "Software"), to deal |
---|
8 | * in the Software without restriction, including without limitation the rights |
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
10 | * copies of the Software, and to permit persons to whom the Software is |
---|
11 | * furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
22 | * THE SOFTWARE. |
---|
23 | */ |
---|
24 | |
---|
25 | define([ |
---|
26 | 'xml2json', 'queryString', 'wpsPayload', 'utils' |
---|
27 | ], function(X2JS, qs, wpsPayload, utils) { |
---|
28 | |
---|
29 | /** |
---|
30 | * The ZooProcess Class |
---|
31 | * @constructs ZooProcess |
---|
32 | * @param {Object} params Parameters |
---|
33 | * @example |
---|
34 | * var myZooObject = new ZooProcess({ |
---|
35 | * url: "http://localhost/cgi-bin/zoo_loader.cgi", |
---|
36 | * delay: 2500 |
---|
37 | * }); |
---|
38 | */ |
---|
39 | var ZooProcess = function(params) { |
---|
40 | |
---|
41 | /** |
---|
42 | * Object configuring the xml2json use. |
---|
43 | * |
---|
44 | * @access private |
---|
45 | * @memberof ZooProcess# |
---|
46 | * @var _x2js {x2js} |
---|
47 | */ |
---|
48 | var _x2js = new X2JS({ |
---|
49 | arrayAccessFormPaths: [ |
---|
50 | 'ProcessDescriptions.ProcessDescription.DataInputs.Input', |
---|
51 | 'ProcessDescriptions.ProcessDescription.DataInputs.Input.ComplexData.Supported.Format', |
---|
52 | 'ProcessDescriptions.ProcessDescription.ProcessOutputs.Output', |
---|
53 | 'ProcessDescriptions.ProcessDescription.ProcessOutputs.Output.ComplexOutput.Supported.Format', |
---|
54 | 'Capabilities.ServiceIdentification.Keywords' |
---|
55 | ], |
---|
56 | }); |
---|
57 | |
---|
58 | |
---|
59 | /** |
---|
60 | * @access public |
---|
61 | * @memberof ZooProcess# |
---|
62 | * @var debug {Boolean} true if verbose messages should be displayed on the console |
---|
63 | * @default false |
---|
64 | */ |
---|
65 | this.debug = false; |
---|
66 | /** |
---|
67 | * @access public |
---|
68 | * @memberof ZooProcess# |
---|
69 | * @var url {String} The WPS Server URL |
---|
70 | */ |
---|
71 | this.url = params.url; |
---|
72 | /** |
---|
73 | * @access public |
---|
74 | * @memberof ZooProcess# |
---|
75 | * @var language {String} The language to be used to request the WPS Server |
---|
76 | * @default "en-US" |
---|
77 | */ |
---|
78 | this.language = params.language?params.language:"en-US"; |
---|
79 | /** |
---|
80 | * @access public |
---|
81 | * @memberof ZooProcess# |
---|
82 | * @var statusLocation {Object} An object to store the statusLocation |
---|
83 | * URLs when running request including both the storeExecuteResponse and |
---|
84 | * the status parameters set to true. |
---|
85 | */ |
---|
86 | this.statusLocation = {}; |
---|
87 | /** |
---|
88 | * @access public |
---|
89 | * @memberof ZooProcess# |
---|
90 | * @var launched {Object} An object to store the running asynchrone services. |
---|
91 | */ |
---|
92 | this.launched = {}; |
---|
93 | /** |
---|
94 | * @access public |
---|
95 | * @memberof ZooProcess# |
---|
96 | * @var terminated {Object} An object to store the finished services. |
---|
97 | */ |
---|
98 | this.terminated = {}; |
---|
99 | /** |
---|
100 | * @access public |
---|
101 | * @memberof ZooProcess# |
---|
102 | * @var percent {Object} An object to store the percentage of completude of services. |
---|
103 | */ |
---|
104 | this.percent = {}; |
---|
105 | /** |
---|
106 | * @access public |
---|
107 | * @memberof ZooProcess# |
---|
108 | * @var delay {Integer} The time (in milliseconds) between each polling requests. |
---|
109 | * @default 2000 |
---|
110 | */ |
---|
111 | this.delay = params.delay || 2000; |
---|
112 | |
---|
113 | /** |
---|
114 | * The getCapabilities method run the GetCapabilities request by calling {@link ZooProcess#request}. |
---|
115 | * |
---|
116 | * @method getCapabilities |
---|
117 | * @memberof ZooProcess# |
---|
118 | * @param {Object} params The parameter for the request and callback functions to call on success or |
---|
119 | * on failure. |
---|
120 | * @example |
---|
121 | * // Log the array of available processes in console |
---|
122 | * myZooObject.getCapabilities({ |
---|
123 | * type: 'POST', |
---|
124 | * success: function(data){ |
---|
125 | * console.log(data["Capabilities"]["ProcessOfferings"]["Process"]); |
---|
126 | * } |
---|
127 | * }); |
---|
128 | */ |
---|
129 | this.getCapabilities = function(params) { |
---|
130 | var closure = this; |
---|
131 | |
---|
132 | if (!params.hasOwnProperty('type')) { |
---|
133 | params.type = 'GET'; |
---|
134 | } |
---|
135 | |
---|
136 | var zoo_request_params = { |
---|
137 | request: 'GetCapabilities', |
---|
138 | service: 'WPS', |
---|
139 | version: '1.0.0', |
---|
140 | } |
---|
141 | |
---|
142 | this.request(zoo_request_params, params.success, params.error, params.type); |
---|
143 | }; |
---|
144 | |
---|
145 | /** |
---|
146 | * The describeProcess method run the DescribeProcess request by calling {@link ZooProcess#request}. |
---|
147 | * |
---|
148 | * @method describeProcess |
---|
149 | * @memberof ZooProcess# |
---|
150 | * @param {Object} params |
---|
151 | * @example |
---|
152 | * // Log x2js representation of all available services in console |
---|
153 | * myZooObject.describeProcess({ |
---|
154 | * type: 'POST', |
---|
155 | * identifier: "all" |
---|
156 | * success: function(data){ |
---|
157 | * console.log(data); |
---|
158 | * } |
---|
159 | * }); |
---|
160 | */ |
---|
161 | this.describeProcess = function(params) { |
---|
162 | var closure = this; |
---|
163 | |
---|
164 | if (!params.hasOwnProperty('type')) { |
---|
165 | params.type = 'GET'; |
---|
166 | } |
---|
167 | |
---|
168 | var zoo_request_params = { |
---|
169 | Identifier: params.identifier, |
---|
170 | request: 'DescribeProcess', |
---|
171 | service: 'WPS', |
---|
172 | version: '1.0.0', |
---|
173 | } |
---|
174 | |
---|
175 | this.request(zoo_request_params, params.success, params.error, params.type); |
---|
176 | }; |
---|
177 | |
---|
178 | /** |
---|
179 | * The convertParams method convert parameters for Execute requests |
---|
180 | * |
---|
181 | * @method convertParams |
---|
182 | * @memberof ZooProcess# |
---|
183 | * @param {Object} params The original object |
---|
184 | * @returns {Object} The converted object |
---|
185 | */ |
---|
186 | this.convertParams = function(params){ |
---|
187 | var closure = this; |
---|
188 | if(closure.debug){ |
---|
189 | console.log("======== Execute "+params.identifier); |
---|
190 | console.log(params); |
---|
191 | } |
---|
192 | |
---|
193 | if (!params.hasOwnProperty('type')) { |
---|
194 | params.type = 'GET'; |
---|
195 | } |
---|
196 | |
---|
197 | var zoo_request_params = { |
---|
198 | request: 'Execute', |
---|
199 | service: 'WPS', |
---|
200 | version: '1.0.0', |
---|
201 | Identifier: params.identifier, |
---|
202 | DataInputs: params.dataInputs ? params.dataInputs : '', |
---|
203 | DataOutputs: params.dataOutputs ? params.dataOutputs : '', |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | if (params.hasOwnProperty('responseDocument')) { |
---|
208 | zoo_request_params.ResponseDocument = params.responseDocument; |
---|
209 | } |
---|
210 | if (params.hasOwnProperty('storeExecuteResponse') && params.storeExecuteResponse) { |
---|
211 | zoo_request_params.storeExecuteResponse = 'true'; |
---|
212 | } |
---|
213 | if (params.hasOwnProperty('status') && params.status) { |
---|
214 | zoo_request_params.status = 'true'; |
---|
215 | } |
---|
216 | if (params.hasOwnProperty('lineage') && params.lineage) { |
---|
217 | zoo_request_params.lineage = 'true'; |
---|
218 | } |
---|
219 | return zoo_request_params; |
---|
220 | }; |
---|
221 | |
---|
222 | /** |
---|
223 | * The buildRequest method is building the object expected by |
---|
224 | * [jQuery.ajax]{@link http://api.jquery.com/jquery.ajax/}. |
---|
225 | * In case of GET request, it will use {@link ZooProcess#getQueryString}. |
---|
226 | * In case of POST request, it will use {@link module:wpsPayload} getPayload. |
---|
227 | * |
---|
228 | * @method buildRequest |
---|
229 | * @memberof ZooProcess# |
---|
230 | * @param {Object} params the request parameters |
---|
231 | * @param {String} type the request method ("GET" or "POST") |
---|
232 | * @returns {Object} The expected object to give as input for the |
---|
233 | * [jQuery.ajax]{@link http://api.jquery.com/jquery.ajax/} function. |
---|
234 | */ |
---|
235 | this.buildRequest = function(params,type){ |
---|
236 | var closure = this; |
---|
237 | if(closure.debug){ |
---|
238 | console.log('======== REQUEST method='+type); |
---|
239 | console.log(params); |
---|
240 | } |
---|
241 | var url = this.url; |
---|
242 | var payload; |
---|
243 | var headers; |
---|
244 | if(params.hasOwnProperty('DataOutputs')) |
---|
245 | for(var i=0;i<params.DataOutputs.length;i++) |
---|
246 | if(params.DataOutputs[i].type==="raw"){ |
---|
247 | params["RawDataOutput"]=params.DataOutputs[i]; |
---|
248 | break; |
---|
249 | } |
---|
250 | |
---|
251 | params["language"]=this.language; |
---|
252 | if (type == 'GET') { |
---|
253 | url += '?' + this.getQueryString(params); |
---|
254 | } else if (type == 'POST') { |
---|
255 | payload = wpsPayload.getPayload(params); |
---|
256 | if(closure.debug){ |
---|
257 | console.log("======== POST PAYLOAD ========"); |
---|
258 | console.log(payload); |
---|
259 | } |
---|
260 | |
---|
261 | headers = { |
---|
262 | "Content-Type": "text/xml" |
---|
263 | }; |
---|
264 | } |
---|
265 | |
---|
266 | if(closure.debug){ |
---|
267 | console.log("ajax url: "+url); |
---|
268 | } |
---|
269 | return {"url":url,"headers": headers,"data": payload,"type":type}; |
---|
270 | }; |
---|
271 | |
---|
272 | /** |
---|
273 | * The getRequest method call the {@link ZooProcess#buildRequest} method |
---|
274 | * by giving the {@link ZooProcess#convertParams} result as first |
---|
275 | * argument and the detected type (default is 'GET') defined in params. |
---|
276 | * |
---|
277 | * @method getRequest |
---|
278 | * @memberof ZooProcess# |
---|
279 | * @params {Object} params The request parameters |
---|
280 | */ |
---|
281 | this.getRequest = function(params){ |
---|
282 | var closure = this; |
---|
283 | var type = 'GET'; |
---|
284 | if(params.hasOwnProperty("type")) |
---|
285 | type=params["type"]; |
---|
286 | return closure.buildRequest(closure.convertParams(params),type); |
---|
287 | }; |
---|
288 | |
---|
289 | /** |
---|
290 | * The execute method run the Execute request by calling {@link ZooProcess#request} |
---|
291 | * with the params converted by {@link ZooProcess#convertParams}. |
---|
292 | * |
---|
293 | * @method execute |
---|
294 | * @memberof ZooProcess# |
---|
295 | * @param {Object} param Parameters |
---|
296 | * @example |
---|
297 | * myZooObject.execute({ |
---|
298 | * identifier: "Buffer", |
---|
299 | * dataInputs: [{"identifier":"InputPolygon","href":"http://features.org/toto.xml","mimeType":"text/xml"}], |
---|
300 | * dataOutputs: [{"identifier":"Result","mimeType":"application/json","type":"raw"}], |
---|
301 | * type: 'POST', |
---|
302 | * success: function(data) { |
---|
303 | * console.log(data); |
---|
304 | * } |
---|
305 | * }); |
---|
306 | */ |
---|
307 | this.execute = function(params) { |
---|
308 | var closure = this; |
---|
309 | this.request(closure.convertParams(params), params.success, params.error, params.type); |
---|
310 | }; |
---|
311 | |
---|
312 | |
---|
313 | /** |
---|
314 | * The request method call {@link ZooProcess#buildRequest} method to |
---|
315 | * to build parameters to give to |
---|
316 | * [jQuery.ajax]{@link http://api.jquery.com/jquery.ajax/}. |
---|
317 | * If the request succeed and if the content-type of the response is |
---|
318 | * "text/xml" then xml2json is called on the resulting data and passed |
---|
319 | * to the onSuccess callback function given in parameter. In case the |
---|
320 | * request failed, the WPS Exception Repport will be parsed with |
---|
321 | * xml2json and given as parameter to the onError callback function. |
---|
322 | * |
---|
323 | * @method request |
---|
324 | * @memberof ZooProcess# |
---|
325 | * @param {Object} params The object used as parameter for |
---|
326 | * [jQuery.ajax]{@link http://api.jquery.com/jquery.ajax/} |
---|
327 | * @param {Function} onSuccess The callback function called if the request succeed |
---|
328 | * @param {Function} onError The callback function called if the request failed |
---|
329 | * @param {String} type The request method ('GET' or 'POST') |
---|
330 | */ |
---|
331 | this.request = function(params, onSuccess, onError, type) { |
---|
332 | var closure = this; |
---|
333 | |
---|
334 | var obj; |
---|
335 | obj=closure.buildRequest(params,type); |
---|
336 | $.ajax(obj) |
---|
337 | .always( |
---|
338 | function() { |
---|
339 | //console.log("ALWAYS"); |
---|
340 | } |
---|
341 | ) |
---|
342 | .fail( |
---|
343 | function(jqXHR, textStatus, errorThrown) { |
---|
344 | if(closure.debug){ |
---|
345 | console.log("======== ERROR ========"); |
---|
346 | } |
---|
347 | var robj=_x2js.xml2json( jqXHR.responseXML ); |
---|
348 | if(closure.debug){ |
---|
349 | console.log(robj); |
---|
350 | } |
---|
351 | if(onError) |
---|
352 | onError(robj); |
---|
353 | } |
---|
354 | ) |
---|
355 | .done( |
---|
356 | function(data, textStatus, jqXHR) { |
---|
357 | if(closure.debug){ |
---|
358 | console.log("======== SUCCESS ========2"); |
---|
359 | console.log(data); |
---|
360 | } |
---|
361 | var ctype=jqXHR.getResponseHeader("Content-Type").split(";")[0]; |
---|
362 | if( ctype=="text/xml" ) |
---|
363 | { |
---|
364 | var tmpD=data; |
---|
365 | data = _x2js.xml2json( data ); |
---|
366 | data._origin=tmpD; |
---|
367 | } |
---|
368 | var launched; |
---|
369 | |
---|
370 | if (params.storeExecuteResponse == 'true' && params.status == 'true') { |
---|
371 | launched = closure.parseStatusLocation(data); |
---|
372 | closure.statusLocation[launched.sid] = launched.statusLocation; |
---|
373 | |
---|
374 | if ( launched.hasOwnProperty('sid') && |
---|
375 | !closure.launched.hasOwnProperty(launched.sid)) { |
---|
376 | closure.launched[launched.sid] = launched.statusLocation; |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|
380 | if(onSuccess) |
---|
381 | onSuccess(data, launched); |
---|
382 | }); |
---|
383 | }; |
---|
384 | |
---|
385 | /** |
---|
386 | * The watch method should be used from the success callback function |
---|
387 | * passed to {@link ZooProcess#execute} when both status and |
---|
388 | * storeExecuteResponse parameters are set to 'true', so when the |
---|
389 | * service should be called asynchronously. This function is |
---|
390 | * responsible for polling the WPS server until the service end (success |
---|
391 | * or failure). It call the {@link ZooProcess#getStatus} method every |
---|
392 | * {@link ZooProcess#delay} milliseconds. |
---|
393 | * |
---|
394 | * @method watch |
---|
395 | * @memberof ZooProcess# |
---|
396 | * @param {Integer} sid The identifier of the running service |
---|
397 | * @param {Object} handlers The callback function definitions |
---|
398 | * (onPercentCompleted, onProcessSucceeded, onError) |
---|
399 | * @example |
---|
400 | * zoo.execute({ |
---|
401 | * identifier: 'MyIdentifier', |
---|
402 | * type: 'POST', |
---|
403 | * dataInputs: myInputs, |
---|
404 | * dataOutputs: myOupts, |
---|
405 | * storeExecuteResponse: true, |
---|
406 | * status: true, |
---|
407 | * success: function(data, launched) { |
---|
408 | * zoo.watch(launched.sid, { |
---|
409 | * onPercentCompleted: function(data) { |
---|
410 | * console.log("**** PercentCompleted ****"); |
---|
411 | * console.log(data); |
---|
412 | * progress.text(data.text+' : '+(data.percentCompleted)+'%'); |
---|
413 | * }, |
---|
414 | * onProcessSucceeded: function(data) { |
---|
415 | * progress.css('width', (100)+'%'); |
---|
416 | * progress.text(data.text+' : '+(100)+'%'); |
---|
417 | * if (data.result.ExecuteResponse.ProcessOutputs) { |
---|
418 | * console.log("**** onSuccess ****"); |
---|
419 | * console.log(data.result); |
---|
420 | * } |
---|
421 | * }, |
---|
422 | * onError: function(data) { |
---|
423 | * console.log("**** onError ****"); |
---|
424 | * console.log(data); |
---|
425 | * }, |
---|
426 | * }); |
---|
427 | * }, |
---|
428 | * error: function(data) { |
---|
429 | * console.log("**** ERROR ****"); |
---|
430 | * console.log(data); |
---|
431 | * notify("Execute asynchrone failed", 'danger'); |
---|
432 | * } |
---|
433 | * }); |
---|
434 | */ |
---|
435 | this.watch = function(sid, handlers) { |
---|
436 | //onPercentCompleted, onProcessSucceeded, onError |
---|
437 | var closure = this; |
---|
438 | if(closure.debug){ |
---|
439 | console.log("WATCH: "+sid); |
---|
440 | } |
---|
441 | |
---|
442 | function onSuccess(data) { |
---|
443 | if(closure.debug){ |
---|
444 | console.log("++++ getStatus SUCCESS "+sid); |
---|
445 | console.log(data); |
---|
446 | } |
---|
447 | |
---|
448 | if (data.ExecuteResponse.Status.ProcessAccepted) { |
---|
449 | var ret = { |
---|
450 | sid: sid, |
---|
451 | percentCompleted: 0, |
---|
452 | text: data.ExecuteResponse.Status.ProcessAccepted.__text, |
---|
453 | creationTime: data.ExecuteResponse.Status._creationTime, |
---|
454 | }; |
---|
455 | |
---|
456 | closure.percent[sid] = ret.percentCompleted; |
---|
457 | //closure.emit('percent', ret); |
---|
458 | |
---|
459 | if (handlers.onPercentCompleted instanceof Function) { |
---|
460 | handlers.onPercentCompleted(ret); |
---|
461 | } |
---|
462 | |
---|
463 | } |
---|
464 | else if (data.ExecuteResponse.Status.ProcessStarted) { |
---|
465 | if(closure.debug){ |
---|
466 | console.log("#### ProcessStarted"); |
---|
467 | } |
---|
468 | |
---|
469 | var ret = { |
---|
470 | sid: sid, |
---|
471 | percentCompleted: data.ExecuteResponse.Status.ProcessStarted._percentCompleted, |
---|
472 | text: data.ExecuteResponse.Status.ProcessStarted.__text, |
---|
473 | creationTime: data.ExecuteResponse.Status._creationTime, |
---|
474 | }; |
---|
475 | |
---|
476 | closure.percent[sid] = ret.percentCompleted; |
---|
477 | //closure.emit('percent', ret); |
---|
478 | |
---|
479 | if (handlers.onPercentCompleted instanceof Function) { |
---|
480 | handlers.onPercentCompleted(ret); |
---|
481 | } |
---|
482 | } |
---|
483 | else if (data.ExecuteResponse.Status.ProcessSucceeded) { |
---|
484 | if(closure.debug){ |
---|
485 | console.log("#### ProcessSucceeded"); |
---|
486 | } |
---|
487 | |
---|
488 | var text = data.ExecuteResponse.Status.ProcessSucceeded.__text; |
---|
489 | closure.terminated[sid] = true; |
---|
490 | |
---|
491 | ret = { |
---|
492 | sid: sid, |
---|
493 | text: text, |
---|
494 | result: data |
---|
495 | }; |
---|
496 | |
---|
497 | //closure.emit('success', ret); |
---|
498 | if (handlers.onProcessSucceeded instanceof Function) { |
---|
499 | handlers.onProcessSucceeded(ret); |
---|
500 | } |
---|
501 | } |
---|
502 | else { |
---|
503 | if(closure.debug){ |
---|
504 | console.log("#### UNHANDLED EXCEPTION"); |
---|
505 | } |
---|
506 | closure.terminated[sid] = true; |
---|
507 | ret = { |
---|
508 | sid: sid, |
---|
509 | code: 'BAD', |
---|
510 | text: 'UNHANDLED EXCEPTION' |
---|
511 | }; |
---|
512 | |
---|
513 | //closure.emit('exception', ret); |
---|
514 | if (handlers.onError instanceof Function) { |
---|
515 | handlers.onError(ret); |
---|
516 | } |
---|
517 | } |
---|
518 | } |
---|
519 | |
---|
520 | function onError(data) { |
---|
521 | if(closure.debug){ |
---|
522 | console.log("++++ getStatus ERROR "+sid); |
---|
523 | console.log(data); |
---|
524 | } |
---|
525 | } |
---|
526 | |
---|
527 | function ping(sid) { |
---|
528 | if(closure.debug){ |
---|
529 | console.log("PING: "+sid); |
---|
530 | } |
---|
531 | closure.getStatus(sid, onSuccess, onError); |
---|
532 | if (closure.terminated[sid]) { |
---|
533 | if(closure.debug){ |
---|
534 | console.log("++++ getStatus TERMINATED "+sid); |
---|
535 | } |
---|
536 | } |
---|
537 | else if (!closure.percent.hasOwnProperty(sid) || closure.percent[sid]<100) { |
---|
538 | setTimeout( function() { |
---|
539 | ping(sid); |
---|
540 | }, closure.delay); |
---|
541 | } else { |
---|
542 | if(closure.debug){ |
---|
543 | console.log(closure.percent); |
---|
544 | } |
---|
545 | } |
---|
546 | } |
---|
547 | |
---|
548 | ping(sid); |
---|
549 | }; |
---|
550 | |
---|
551 | /** |
---|
552 | * The getStatus method call |
---|
553 | * [jQuery.ajax]{@link http://api.jquery.com/jquery.ajax/} to fecth the |
---|
554 | * ExecuteResponse document which contains a Status node and |
---|
555 | * potentially the result (when the asynch service end). This method is |
---|
556 | * used by {@link ZooProcess#watch} to get the ongoing status of |
---|
557 | * services called asynchronously. |
---|
558 | * |
---|
559 | * @method getStatus |
---|
560 | * @memberof ZooProcess# |
---|
561 | * @param {Integer} sid Service Identifier |
---|
562 | * @param {Function} onSuccess callback |
---|
563 | * @param {Function} onError callback |
---|
564 | */ |
---|
565 | this.getStatus = function(sid, onSuccess, onError) { |
---|
566 | var closure = this; |
---|
567 | if(closure.debug){ |
---|
568 | console.log("GET STATUS: "+sid); |
---|
569 | } |
---|
570 | if (closure.terminated[sid]) { |
---|
571 | if(closure.debug){ |
---|
572 | console.log("DEBUG TERMINATED"); |
---|
573 | } |
---|
574 | return; |
---|
575 | } |
---|
576 | if (!closure.launched[sid]) { |
---|
577 | if(closure.debug){ |
---|
578 | console.log("DEBUG LAUNCHED"); |
---|
579 | } |
---|
580 | return; |
---|
581 | } |
---|
582 | |
---|
583 | $.ajax({ |
---|
584 | url: closure.statusLocation[sid] |
---|
585 | }) |
---|
586 | .fail( |
---|
587 | function(jqXHR, textStatus, errorThrown) { |
---|
588 | if(closure.debug){ |
---|
589 | console.log("======== ERROR ========"); |
---|
590 | } |
---|
591 | var robj=_x2js.xml2json( jqXHR.responseXML ); |
---|
592 | if(closure.debug){ |
---|
593 | console.log(robj); |
---|
594 | } |
---|
595 | if(onError) |
---|
596 | onError(robj); |
---|
597 | } |
---|
598 | ) |
---|
599 | .done( |
---|
600 | function(data, textStatus, jqXHR) { |
---|
601 | if(closure.debug){ |
---|
602 | console.log("======== SUCCESS ========2"); |
---|
603 | console.log(data); |
---|
604 | } |
---|
605 | var ctype=jqXHR.getResponseHeader("Content-Type").split(";")[0]; |
---|
606 | if( ctype=="text/xml" ){ |
---|
607 | var tmpD=data; |
---|
608 | data = _x2js.xml2json( data ); |
---|
609 | data._origin=tmpD; |
---|
610 | } |
---|
611 | if(onSuccess) |
---|
612 | onSuccess(data); |
---|
613 | }); |
---|
614 | }; |
---|
615 | |
---|
616 | /** |
---|
617 | * The getQueryString method generate a KVP GET request which can be |
---|
618 | * used to request a WPS server. |
---|
619 | * |
---|
620 | * @method getQueryString |
---|
621 | * @memberof ZooProcess# |
---|
622 | * @param {Object} params The WPS requests parameters |
---|
623 | * @returns {String} The GET WPS request |
---|
624 | * @example |
---|
625 | * // Log GetCapabilities GET request in console |
---|
626 | * var request_params = { |
---|
627 | * request: 'GetCapabilities', |
---|
628 | * service: 'WPS', |
---|
629 | * version: '1.0.0', |
---|
630 | * language; 'en-US' |
---|
631 | * } |
---|
632 | * console.log(myZooObject.getQueryString(request_params)); |
---|
633 | */ |
---|
634 | this.getQueryString = function(params) { |
---|
635 | var closure = this; |
---|
636 | var ret = ''; |
---|
637 | |
---|
638 | serializeInputs = function(obj) { |
---|
639 | if(closure.debug){ |
---|
640 | console.log("SERIALIZE dataInputs"); |
---|
641 | console.log(obj); |
---|
642 | } |
---|
643 | var lt=$.type(obj); |
---|
644 | if(lt === "string") { |
---|
645 | return obj; |
---|
646 | } |
---|
647 | var str = []; |
---|
648 | for(var p in obj){ |
---|
649 | if(lt === "array"){ |
---|
650 | if(obj[p].hasOwnProperty("href")) |
---|
651 | str.push(obj[p]["identifier"] + "=Reference"); |
---|
652 | else |
---|
653 | str.push(obj[p]["identifier"] + "=" + obj[p]["value"]); |
---|
654 | for(var q in obj[p]){ |
---|
655 | if(q!="identifier" && q!="value" && q!="href") |
---|
656 | str.push("@" + q + "=" + obj[p][q]); |
---|
657 | else |
---|
658 | if(q=="href") |
---|
659 | str.push("@xlink:" + q + "=" + encodeURIComponent(obj[p][q])); |
---|
660 | } |
---|
661 | str.push(";"); |
---|
662 | } |
---|
663 | else |
---|
664 | if (obj.hasOwnProperty(p)) { |
---|
665 | if(p=="href") |
---|
666 | str.push(p + "=" + encodeURIComponent(obj[p])); |
---|
667 | else |
---|
668 | str.push(p + "=" + obj[p]); |
---|
669 | } |
---|
670 | } |
---|
671 | return str.join(""); |
---|
672 | } |
---|
673 | |
---|
674 | serializeOutputs = function(obj) { |
---|
675 | if(closure.debug){ |
---|
676 | console.log("SERIALIZE dataOutputs"); |
---|
677 | console.log(obj); |
---|
678 | } |
---|
679 | var lt=$.type(obj); |
---|
680 | if(lt === "string") { |
---|
681 | return obj; |
---|
682 | } |
---|
683 | var str = []; |
---|
684 | for(var p in obj){ |
---|
685 | str.push(obj[p]["identifier"]); |
---|
686 | for(var q in obj[p]){ |
---|
687 | if(q!="identifier" && q!="type") |
---|
688 | str.push("@" + q + "=" + obj[p][q]); |
---|
689 | } |
---|
690 | str.push(";"); |
---|
691 | } |
---|
692 | return str.join(""); |
---|
693 | } |
---|
694 | |
---|
695 | var responseDocument = params.ResponseDocument; |
---|
696 | var tmp_params = {}; |
---|
697 | |
---|
698 | var objectKeys = Object.keys || function (obj) { |
---|
699 | var keys = []; |
---|
700 | for (var key in obj) keys.push(key); |
---|
701 | return keys; |
---|
702 | }; |
---|
703 | |
---|
704 | var skip = { |
---|
705 | 'DataInputs': true, |
---|
706 | 'DataOutputs': true, |
---|
707 | 'ResponseDocument': true, |
---|
708 | 'RawDataOutput': true, |
---|
709 | } |
---|
710 | var keys = objectKeys(params); |
---|
711 | for (var i = 0; i < keys.length; i++) { |
---|
712 | var key = keys[i]; |
---|
713 | if (skip.hasOwnProperty(key) && skip[key]) { |
---|
714 | continue; |
---|
715 | } |
---|
716 | if (params.hasOwnProperty(key)) { |
---|
717 | tmp_params[key] = params[key]; |
---|
718 | } |
---|
719 | } |
---|
720 | ret = qs.stringify(tmp_params); |
---|
721 | |
---|
722 | //req_options.path = req_options.path.replace("&DataInputs=sid%3D", "&DataInputs=sid=") |
---|
723 | if (params.hasOwnProperty('DataInputs')) { |
---|
724 | //var dataInputs = params.DataInputs; |
---|
725 | var dataInputs = serializeInputs(params.DataInputs); |
---|
726 | if(closure.debug){ |
---|
727 | console.log("dataInputs: "+dataInputs); |
---|
728 | } |
---|
729 | ret += '&DataInputs=' + dataInputs; |
---|
730 | } |
---|
731 | |
---|
732 | if (params.hasOwnProperty('DataOutputs')) { |
---|
733 | var dataOutputs = serializeOutputs(params.DataOutputs); |
---|
734 | if(closure.debug){ |
---|
735 | console.log("dataOutputs: "+dataOutputs); |
---|
736 | } |
---|
737 | if(dataOutputs!=""){ |
---|
738 | var displayInputs=true; |
---|
739 | for(var i=0;i<params.DataOutputs.length;i++) |
---|
740 | if(params.DataOutputs[i].type==="raw"){ |
---|
741 | ret += '&RawDataOutput=' + dataOutputs; |
---|
742 | displayInputs=false; |
---|
743 | break; |
---|
744 | } |
---|
745 | if(displayInputs) |
---|
746 | ret += '&ResponseDocument=' + dataOutputs; |
---|
747 | } |
---|
748 | }else{ |
---|
749 | if (params.hasOwnProperty('RawDataOutput')) { |
---|
750 | ret+="&RawDataOutput="+params['RawDataOutput']+";"; |
---|
751 | }else{ |
---|
752 | if (params.hasOwnProperty('ResponseDocument')) { |
---|
753 | var lt=$.type(params['ResponseDocument']); |
---|
754 | if(lt === "string") { |
---|
755 | ret+="&ResponseDocument="+params['ResponseDocument']+";"; |
---|
756 | }else{ |
---|
757 | var tmp_ret=serializeOutputs(params['ResponseDocument']); |
---|
758 | ret+="&ResponseDocument="+tmp; |
---|
759 | } |
---|
760 | } |
---|
761 | } |
---|
762 | } |
---|
763 | |
---|
764 | return ret; |
---|
765 | }; |
---|
766 | |
---|
767 | /** |
---|
768 | * The parseStatusLocation method parse the statusLocation and return an |
---|
769 | * object with sid and statusLocation attributes which contains |
---|
770 | * respectively: a unique identifier named sid and the statusLocation |
---|
771 | * value returned by the WPS server. |
---|
772 | * |
---|
773 | * @method parseStatusLocation |
---|
774 | * @memberof ZooProcess# |
---|
775 | * @param {Object} data The XML response parsed by x2js.xml2json |
---|
776 | * @returns {Object} The result is an object with sid and statusLocation |
---|
777 | */ |
---|
778 | this.parseStatusLocation = function(data) { |
---|
779 | var closure = this; |
---|
780 | |
---|
781 | if (statusLocation = data.ExecuteResponse._statusLocation) { |
---|
782 | if(closure.debug){ |
---|
783 | console.log("statusLocation: "+statusLocation); |
---|
784 | } |
---|
785 | |
---|
786 | var lsid=0; |
---|
787 | for(i in closure.statusLocation) |
---|
788 | lsid++; |
---|
789 | |
---|
790 | return {sid: lsid, statusLocation: statusLocation}; |
---|
791 | } |
---|
792 | }; |
---|
793 | }; |
---|
794 | |
---|
795 | |
---|
796 | return ZooProcess; |
---|
797 | |
---|
798 | }); |
---|