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 version {String} The WPS version |
---|
76 | */ |
---|
77 | this.version = params.version?params.version:"1.0.0"; |
---|
78 | /** |
---|
79 | * @access public |
---|
80 | * @memberof ZooProcess# |
---|
81 | * @var language {String} The language to be used to request the WPS Server |
---|
82 | * @default "en-US" |
---|
83 | */ |
---|
84 | this.language = params.language?params.language:"en-US"; |
---|
85 | /** |
---|
86 | * @access public |
---|
87 | * @memberof ZooProcess# |
---|
88 | * @var statusLocation {Object} An object to store the statusLocation |
---|
89 | * URLs when running request including both the storeExecuteResponse and |
---|
90 | * the status parameters set to true. |
---|
91 | */ |
---|
92 | this.statusLocation = {}; |
---|
93 | /** |
---|
94 | * @access public |
---|
95 | * @memberof ZooProcess# |
---|
96 | * @var launched {Object} An object to store the running asynchrone services. |
---|
97 | */ |
---|
98 | this.launched = {}; |
---|
99 | /** |
---|
100 | * @access public |
---|
101 | * @memberof ZooProcess# |
---|
102 | * @var terminated {Object} An object to store the finished services. |
---|
103 | */ |
---|
104 | this.terminated = {}; |
---|
105 | /** |
---|
106 | * @access public |
---|
107 | * @memberof ZooProcess# |
---|
108 | * @var percent {Object} An object to store the percentage of completude of services. |
---|
109 | */ |
---|
110 | this.percent = {}; |
---|
111 | /** |
---|
112 | * @access public |
---|
113 | * @memberof ZooProcess# |
---|
114 | * @var delay {Integer} The time (in milliseconds) between each polling requests. |
---|
115 | * @default 2000 |
---|
116 | */ |
---|
117 | this.delay = params.delay || 2000; |
---|
118 | |
---|
119 | /** |
---|
120 | * The getCapabilities method run the GetCapabilities request by calling {@link ZooProcess#request}. |
---|
121 | * |
---|
122 | * @method getCapabilities |
---|
123 | * @memberof ZooProcess# |
---|
124 | * @param {Object} params The parameter for the request and callback functions to call on success or |
---|
125 | * on failure. |
---|
126 | * @example |
---|
127 | * // Log the array of available processes in console |
---|
128 | * myZooObject.getCapabilities({ |
---|
129 | * type: 'POST', |
---|
130 | * success: function(data){ |
---|
131 | * console.log(data["Capabilities"]["ProcessOfferings"]["Process"]); |
---|
132 | * } |
---|
133 | * }); |
---|
134 | */ |
---|
135 | this.getCapabilities = function(params) { |
---|
136 | var closure = this; |
---|
137 | |
---|
138 | if (!params.hasOwnProperty('type')) { |
---|
139 | params.type = 'GET'; |
---|
140 | } |
---|
141 | |
---|
142 | var zoo_request_params = { |
---|
143 | request: 'GetCapabilities', |
---|
144 | service: 'WPS', |
---|
145 | version: (params.hasOwnProperty('version')?params.version:closure.version), |
---|
146 | } |
---|
147 | |
---|
148 | this.request(zoo_request_params, params.success, params.error, params.type); |
---|
149 | }; |
---|
150 | |
---|
151 | /** |
---|
152 | * The describeProcess method run the DescribeProcess request by calling {@link ZooProcess#request}. |
---|
153 | * |
---|
154 | * @method describeProcess |
---|
155 | * @memberof ZooProcess# |
---|
156 | * @param {Object} params |
---|
157 | * @example |
---|
158 | * // Log x2js representation of all available services in console |
---|
159 | * myZooObject.describeProcess({ |
---|
160 | * type: 'POST', |
---|
161 | * identifier: "all" |
---|
162 | * success: function(data){ |
---|
163 | * console.log(data); |
---|
164 | * } |
---|
165 | * }); |
---|
166 | */ |
---|
167 | this.describeProcess = function(params) { |
---|
168 | var closure = this; |
---|
169 | |
---|
170 | if (!params.hasOwnProperty('type')) { |
---|
171 | params.type = 'GET'; |
---|
172 | } |
---|
173 | |
---|
174 | var zoo_request_params = { |
---|
175 | Identifier: params.identifier, |
---|
176 | request: 'DescribeProcess', |
---|
177 | service: 'WPS', |
---|
178 | version: (params.hasOwnProperty('version')?params.version:closure.version), |
---|
179 | } |
---|
180 | |
---|
181 | this.request(zoo_request_params, params.success, params.error, params.type); |
---|
182 | }; |
---|
183 | |
---|
184 | /** |
---|
185 | * The convertParams method convert parameters for Execute requests |
---|
186 | * |
---|
187 | * @method convertParams |
---|
188 | * @memberof ZooProcess# |
---|
189 | * @param {Object} params The original object |
---|
190 | * @returns {Object} The converted object |
---|
191 | */ |
---|
192 | this.convertParams = function(params){ |
---|
193 | var closure = this; |
---|
194 | if(closure.debug){ |
---|
195 | console.log("======== Execute "+params.identifier); |
---|
196 | console.log(params); |
---|
197 | } |
---|
198 | |
---|
199 | if (!params.hasOwnProperty('type')) { |
---|
200 | params.type = 'GET'; |
---|
201 | } |
---|
202 | |
---|
203 | var zoo_request_params = { |
---|
204 | request: 'Execute', |
---|
205 | service: 'WPS', |
---|
206 | version: (params.hasOwnProperty('version')?params.version:closure.version), |
---|
207 | Identifier: params.identifier, |
---|
208 | DataInputs: params.dataInputs ? params.dataInputs : '', |
---|
209 | DataOutputs: params.dataOutputs ? params.dataOutputs : '', |
---|
210 | } |
---|
211 | |
---|
212 | console.log(zoo_request_params); |
---|
213 | |
---|
214 | if (params.hasOwnProperty('responseDocument')) { |
---|
215 | zoo_request_params.ResponseDocument = params.responseDocument; |
---|
216 | } |
---|
217 | if (params.hasOwnProperty('mode')) { |
---|
218 | zoo_request_params.mode = params.mode; |
---|
219 | } |
---|
220 | if (params.hasOwnProperty('storeExecuteResponse') && params.storeExecuteResponse) { |
---|
221 | zoo_request_params.storeExecuteResponse = 'true'; |
---|
222 | } |
---|
223 | if (params.hasOwnProperty('status') && params.status) { |
---|
224 | zoo_request_params.status = 'true'; |
---|
225 | } |
---|
226 | if (params.hasOwnProperty('lineage') && params.lineage) { |
---|
227 | zoo_request_params.lineage = 'true'; |
---|
228 | } |
---|
229 | return zoo_request_params; |
---|
230 | }; |
---|
231 | |
---|
232 | /** |
---|
233 | * The buildRequest method is building the object expected by |
---|
234 | * [jQuery.ajax]{@link http://api.jquery.com/jquery.ajax/}. |
---|
235 | * In case of GET request, it will use {@link ZooProcess#getQueryString}. |
---|
236 | * In case of POST request, it will use {@link module:wpsPayload} getPayload. |
---|
237 | * |
---|
238 | * @method buildRequest |
---|
239 | * @memberof ZooProcess# |
---|
240 | * @param {Object} params the request parameters |
---|
241 | * @param {String} type the request method ("GET" or "POST") |
---|
242 | * @returns {Object} The expected object to give as input for the |
---|
243 | * [jQuery.ajax]{@link http://api.jquery.com/jquery.ajax/} function. |
---|
244 | */ |
---|
245 | this.buildRequest = function(params,type){ |
---|
246 | var closure = this; |
---|
247 | if(closure.debug){ |
---|
248 | console.log('======== REQUEST method='+type); |
---|
249 | console.log(params); |
---|
250 | } |
---|
251 | var url = this.url; |
---|
252 | var payload; |
---|
253 | var headers; |
---|
254 | if(params.hasOwnProperty('DataOutputs')) |
---|
255 | for(var i=0;i<params.DataOutputs.length;i++) |
---|
256 | if(params.DataOutputs[i].type==="raw"){ |
---|
257 | params["RawDataOutput"]=params.DataOutputs[i]; |
---|
258 | break; |
---|
259 | } |
---|
260 | |
---|
261 | params["language"]=this.language; |
---|
262 | if (type == 'GET') { |
---|
263 | url += '?' + this.getQueryString(params); |
---|
264 | } else if (type == 'POST') { |
---|
265 | payload = wpsPayload.getPayload(params); |
---|
266 | if(closure.debug){ |
---|
267 | console.log("======== POST PAYLOAD ========"); |
---|
268 | console.log(payload); |
---|
269 | console.log(params); |
---|
270 | } |
---|
271 | |
---|
272 | headers = { |
---|
273 | "Content-Type": "text/xml" |
---|
274 | }; |
---|
275 | } |
---|
276 | |
---|
277 | if(closure.debug){ |
---|
278 | console.log("ajax url: "+url); |
---|
279 | } |
---|
280 | return {"url":url,"headers": headers,"data": payload,"type":type}; |
---|
281 | }; |
---|
282 | |
---|
283 | /** |
---|
284 | * The getRequest method call the {@link ZooProcess#buildRequest} method |
---|
285 | * by giving the {@link ZooProcess#convertParams} result as first |
---|
286 | * argument and the detected type (default is 'GET') defined in params. |
---|
287 | * |
---|
288 | * @method getRequest |
---|
289 | * @memberof ZooProcess# |
---|
290 | * @params {Object} params The request parameters |
---|
291 | */ |
---|
292 | this.getRequest = function(params){ |
---|
293 | var closure = this; |
---|
294 | var type = 'GET'; |
---|
295 | if(params.hasOwnProperty("type")) |
---|
296 | type=params["type"]; |
---|
297 | return closure.buildRequest(closure.convertParams(params),type); |
---|
298 | }; |
---|
299 | |
---|
300 | /** |
---|
301 | * The execute method run the Execute request by calling {@link ZooProcess#request} |
---|
302 | * with the params converted by {@link ZooProcess#convertParams}. |
---|
303 | * |
---|
304 | * @method execute |
---|
305 | * @memberof ZooProcess# |
---|
306 | * @param {Object} param Parameters |
---|
307 | * @example |
---|
308 | * myZooObject.execute({ |
---|
309 | * identifier: "Buffer", |
---|
310 | * dataInputs: [{"identifier":"InputPolygon","href":"http://features.org/toto.xml","mimeType":"text/xml"}], |
---|
311 | * dataOutputs: [{"identifier":"Result","mimeType":"application/json","type":"raw"}], |
---|
312 | * type: 'POST', |
---|
313 | * success: function(data) { |
---|
314 | * console.log(data); |
---|
315 | * } |
---|
316 | * }); |
---|
317 | */ |
---|
318 | this.execute = function(params) { |
---|
319 | var closure = this; |
---|
320 | this.request(closure.convertParams(params), params.success, params.error, params.type); |
---|
321 | }; |
---|
322 | |
---|
323 | |
---|
324 | /** |
---|
325 | * The request method call {@link ZooProcess#buildRequest} method to |
---|
326 | * to build parameters to give to |
---|
327 | * [jQuery.ajax]{@link http://api.jquery.com/jquery.ajax/}. |
---|
328 | * If the request succeed and if the content-type of the response is |
---|
329 | * "text/xml" then xml2json is called on the resulting data and passed |
---|
330 | * to the onSuccess callback function given in parameter. In case the |
---|
331 | * request failed, the WPS Exception Repport will be parsed with |
---|
332 | * xml2json and given as parameter to the onError callback function. |
---|
333 | * |
---|
334 | * @method request |
---|
335 | * @memberof ZooProcess# |
---|
336 | * @param {Object} params The object used as parameter for |
---|
337 | * [jQuery.ajax]{@link http://api.jquery.com/jquery.ajax/} |
---|
338 | * @param {Function} onSuccess The callback function called if the request succeed |
---|
339 | * @param {Function} onError The callback function called if the request failed |
---|
340 | * @param {String} type The request method ('GET' or 'POST') |
---|
341 | */ |
---|
342 | this.request = function(params, onSuccess, onError, type) { |
---|
343 | var closure = this; |
---|
344 | |
---|
345 | var obj; |
---|
346 | obj=closure.buildRequest(params,type); |
---|
347 | $.ajax(obj) |
---|
348 | .always( |
---|
349 | function() { |
---|
350 | //console.log("ALWAYS"); |
---|
351 | } |
---|
352 | ) |
---|
353 | .fail( |
---|
354 | function(jqXHR, textStatus, errorThrown) { |
---|
355 | if(closure.debug){ |
---|
356 | console.log("======== ERROR ========"); |
---|
357 | } |
---|
358 | var robj=_x2js.xml2json( jqXHR.responseXML ); |
---|
359 | if(closure.debug){ |
---|
360 | console.log(robj); |
---|
361 | } |
---|
362 | if(onError) |
---|
363 | onError(robj); |
---|
364 | } |
---|
365 | ) |
---|
366 | .done( |
---|
367 | function(data, textStatus, jqXHR) { |
---|
368 | if(closure.debug){ |
---|
369 | console.log("======== SUCCESS ========2"); |
---|
370 | console.log(data); |
---|
371 | } |
---|
372 | var ctype=jqXHR.getResponseHeader("Content-Type").split(";")[0]; |
---|
373 | if( ctype=="text/xml" ) |
---|
374 | { |
---|
375 | var tmpD=data; |
---|
376 | data = _x2js.xml2json( data ); |
---|
377 | data._origin=tmpD; |
---|
378 | } |
---|
379 | var launched; |
---|
380 | var version=(params.version?params.version:closure.version); |
---|
381 | if(version=="1.0.0"){ |
---|
382 | if (params.storeExecuteResponse == 'true' && params.status == 'true') { |
---|
383 | launched = closure.parseStatusLocation(data); |
---|
384 | closure.statusLocation[launched.sid] = launched.statusLocation; |
---|
385 | |
---|
386 | if ( launched.hasOwnProperty('sid') && |
---|
387 | !closure.launched.hasOwnProperty(launched.sid)) { |
---|
388 | closure.launched[launched.sid] = launched.statusLocation; |
---|
389 | } |
---|
390 | } |
---|
391 | } |
---|
392 | else{ |
---|
393 | if (params.mode == 'async') { |
---|
394 | launched = closure.parseJobID(data); |
---|
395 | closure.statusLocation[launched.sid] = closure.url+"?request=GetStatus&service=WPS&version=2.0.0&JobID="+launched.jobid; |
---|
396 | if ( launched.hasOwnProperty('sid') && |
---|
397 | !closure.launched.hasOwnProperty(launched.sid)) { |
---|
398 | closure.launched[launched.sid] = launched.jobid; |
---|
399 | } |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | if(onSuccess) |
---|
404 | onSuccess(data, launched); |
---|
405 | }); |
---|
406 | }; |
---|
407 | |
---|
408 | /** |
---|
409 | * The watch method should be used from the success callback function |
---|
410 | * passed to {@link ZooProcess#execute} when both status and |
---|
411 | * storeExecuteResponse parameters are set to 'true', so when the |
---|
412 | * service should be called asynchronously. This function is |
---|
413 | * responsible for polling the WPS server until the service end (success |
---|
414 | * or failure). It call the {@link ZooProcess#getStatus} method every |
---|
415 | * {@link ZooProcess#delay} milliseconds. |
---|
416 | * |
---|
417 | * @method watch |
---|
418 | * @memberof ZooProcess# |
---|
419 | * @param {Integer} sid The identifier of the running service |
---|
420 | * @param {Object} handlers The callback function definitions |
---|
421 | * (onPercentCompleted, onProcessSucceeded, onError) |
---|
422 | * @example |
---|
423 | * zoo.execute({ |
---|
424 | * identifier: 'MyIdentifier', |
---|
425 | * type: 'POST', |
---|
426 | * dataInputs: myInputs, |
---|
427 | * dataOutputs: myOupts, |
---|
428 | * storeExecuteResponse: true, |
---|
429 | * status: true, |
---|
430 | * success: function(data, launched) { |
---|
431 | * zoo.watch(launched.sid, { |
---|
432 | * onPercentCompleted: function(data) { |
---|
433 | * console.log("**** PercentCompleted ****"); |
---|
434 | * console.log(data); |
---|
435 | * progress.text(data.text+' : '+(data.percentCompleted)+'%'); |
---|
436 | * }, |
---|
437 | * onProcessSucceeded: function(data) { |
---|
438 | * progress.css('width', (100)+'%'); |
---|
439 | * progress.text(data.text+' : '+(100)+'%'); |
---|
440 | * if (data.result.ExecuteResponse.ProcessOutputs) { |
---|
441 | * console.log("**** onSuccess ****"); |
---|
442 | * console.log(data.result); |
---|
443 | * } |
---|
444 | * }, |
---|
445 | * onError: function(data) { |
---|
446 | * console.log("**** onError ****"); |
---|
447 | * console.log(data); |
---|
448 | * }, |
---|
449 | * }); |
---|
450 | * }, |
---|
451 | * error: function(data) { |
---|
452 | * console.log("**** ERROR ****"); |
---|
453 | * console.log(data); |
---|
454 | * notify("Execute asynchrone failed", 'danger'); |
---|
455 | * } |
---|
456 | * }); |
---|
457 | */ |
---|
458 | this.watch = function(sid, handlers) { |
---|
459 | //onPercentCompleted, onProcessSucceeded, onError |
---|
460 | var closure = this; |
---|
461 | if(closure.debug){ |
---|
462 | console.log("WATCH: "+sid); |
---|
463 | } |
---|
464 | |
---|
465 | function onSuccess(data) { |
---|
466 | if(closure.debug){ |
---|
467 | console.log("++++ getStatus SUCCESS "+sid); |
---|
468 | console.log(data); |
---|
469 | } |
---|
470 | |
---|
471 | if(data.StatusInfo || data.Result){ |
---|
472 | if (data.Result) { |
---|
473 | |
---|
474 | var ret = { |
---|
475 | sid: sid, |
---|
476 | text: "", |
---|
477 | result: data |
---|
478 | }; |
---|
479 | |
---|
480 | if (handlers.onProcessSucceeded instanceof Function) { |
---|
481 | handlers.onProcessSucceeded(ret); |
---|
482 | } |
---|
483 | |
---|
484 | return; |
---|
485 | } |
---|
486 | if (data.StatusInfo.Status == "Running") { |
---|
487 | if(closure.debug){ |
---|
488 | console.log("#### ProcessStarted"); |
---|
489 | } |
---|
490 | |
---|
491 | var message=""; |
---|
492 | for(index=0;index<data._origin.childNodes[0].childNodes.length;index++){ |
---|
493 | if(data._origin.childNodes[0].childNodes[index].nodeType==8){ |
---|
494 | message=data._origin.childNodes[0].childNodes[index].textContent; |
---|
495 | } |
---|
496 | } |
---|
497 | var ret = { |
---|
498 | sid: sid, |
---|
499 | percentCompleted: (data.StatusInfo.PercentCompleted?data.StatusInfo.PercentCompleted:0), |
---|
500 | text: message, |
---|
501 | creationTime: "", |
---|
502 | }; |
---|
503 | |
---|
504 | if (handlers.onPercentCompleted instanceof Function) { |
---|
505 | handlers.onPercentCompleted(ret); |
---|
506 | } |
---|
507 | } |
---|
508 | else if (data.StatusInfo.Status == "Succeeded") { |
---|
509 | if(closure.debug){ |
---|
510 | console.log("#### ProcessSucceeded"); |
---|
511 | } |
---|
512 | |
---|
513 | var text = ""; |
---|
514 | closure.terminated[sid] = true; |
---|
515 | |
---|
516 | ret = { |
---|
517 | sid: sid, |
---|
518 | text: text, |
---|
519 | result: data |
---|
520 | }; |
---|
521 | |
---|
522 | closure.getResult(sid, onSuccess, onError); |
---|
523 | } |
---|
524 | else { |
---|
525 | if(closure.debug){ |
---|
526 | console.log("#### UNHANDLED EXCEPTION"); |
---|
527 | } |
---|
528 | closure.terminated[sid] = true; |
---|
529 | ret = { |
---|
530 | sid: sid, |
---|
531 | code: 'BAD', |
---|
532 | text: 'UNHANDLED EXCEPTION' |
---|
533 | }; |
---|
534 | |
---|
535 | //closure.emit('exception', ret); |
---|
536 | if (handlers.onError instanceof Function) { |
---|
537 | handlers.onError(ret); |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | return |
---|
542 | } |
---|
543 | |
---|
544 | if (data.ExecuteResponse.Status.ProcessAccepted) { |
---|
545 | var ret = { |
---|
546 | sid: sid, |
---|
547 | percentCompleted: 0, |
---|
548 | text: data.ExecuteResponse.Status.ProcessAccepted.__text, |
---|
549 | creationTime: data.ExecuteResponse.Status._creationTime, |
---|
550 | }; |
---|
551 | |
---|
552 | closure.percent[sid] = ret.percentCompleted; |
---|
553 | //closure.emit('percent', ret); |
---|
554 | |
---|
555 | if (handlers.onPercentCompleted instanceof Function) { |
---|
556 | handlers.onPercentCompleted(ret); |
---|
557 | } |
---|
558 | |
---|
559 | } |
---|
560 | else if (data.ExecuteResponse.Status.ProcessStarted) { |
---|
561 | if(closure.debug){ |
---|
562 | console.log("#### ProcessStarted"); |
---|
563 | } |
---|
564 | |
---|
565 | var ret = { |
---|
566 | sid: sid, |
---|
567 | percentCompleted: data.ExecuteResponse.Status.ProcessStarted._percentCompleted, |
---|
568 | text: data.ExecuteResponse.Status.ProcessStarted.__text, |
---|
569 | creationTime: data.ExecuteResponse.Status._creationTime, |
---|
570 | }; |
---|
571 | |
---|
572 | closure.percent[sid] = ret.percentCompleted; |
---|
573 | //closure.emit('percent', ret); |
---|
574 | |
---|
575 | if (handlers.onPercentCompleted instanceof Function) { |
---|
576 | handlers.onPercentCompleted(ret); |
---|
577 | } |
---|
578 | } |
---|
579 | else if (data.ExecuteResponse.Status.ProcessSucceeded) { |
---|
580 | if(closure.debug){ |
---|
581 | console.log("#### ProcessSucceeded"); |
---|
582 | } |
---|
583 | |
---|
584 | var text = data.ExecuteResponse.Status.ProcessSucceeded.__text; |
---|
585 | closure.terminated[sid] = true; |
---|
586 | |
---|
587 | ret = { |
---|
588 | sid: sid, |
---|
589 | text: text, |
---|
590 | result: data |
---|
591 | }; |
---|
592 | |
---|
593 | //closure.emit('success', ret); |
---|
594 | if (handlers.onProcessSucceeded instanceof Function) { |
---|
595 | handlers.onProcessSucceeded(ret); |
---|
596 | } |
---|
597 | } |
---|
598 | else { |
---|
599 | if(closure.debug){ |
---|
600 | console.log("#### UNHANDLED EXCEPTION"); |
---|
601 | } |
---|
602 | closure.terminated[sid] = true; |
---|
603 | ret = { |
---|
604 | sid: sid, |
---|
605 | code: 'BAD', |
---|
606 | text: 'UNHANDLED EXCEPTION' |
---|
607 | }; |
---|
608 | |
---|
609 | //closure.emit('exception', ret); |
---|
610 | if (handlers.onError instanceof Function) { |
---|
611 | handlers.onError(ret); |
---|
612 | } |
---|
613 | } |
---|
614 | } |
---|
615 | |
---|
616 | function onError(data) { |
---|
617 | if(closure.debug){ |
---|
618 | console.log("++++ getStatus ERROR "+sid); |
---|
619 | console.log(data); |
---|
620 | } |
---|
621 | } |
---|
622 | |
---|
623 | function ping(sid) { |
---|
624 | if(closure.debug){ |
---|
625 | console.log("PING: "+sid); |
---|
626 | } |
---|
627 | |
---|
628 | closure.getStatus(sid, onSuccess, onError); |
---|
629 | if (closure.terminated[sid]) { |
---|
630 | if(closure.debug){ |
---|
631 | console.log("++++ getStatus TERMINATED "+sid); |
---|
632 | } |
---|
633 | } |
---|
634 | else if (!closure.percent.hasOwnProperty(sid) || closure.percent[sid]<100) { |
---|
635 | setTimeout( function() { |
---|
636 | ping(sid); |
---|
637 | }, closure.delay); |
---|
638 | } else { |
---|
639 | if(closure.debug){ |
---|
640 | console.log(closure.percent); |
---|
641 | } |
---|
642 | } |
---|
643 | } |
---|
644 | |
---|
645 | ping(sid); |
---|
646 | }; |
---|
647 | |
---|
648 | /** |
---|
649 | * The getStatus method call |
---|
650 | * [jQuery.ajax]{@link http://api.jquery.com/jquery.ajax/} to fecth the |
---|
651 | * ExecuteResponse document which contains a Status node and |
---|
652 | * potentially the result (when the asynch service end). This method is |
---|
653 | * used by {@link ZooProcess#watch} to get the ongoing status of |
---|
654 | * services called asynchronously. |
---|
655 | * |
---|
656 | * @method getStatus |
---|
657 | * @memberof ZooProcess# |
---|
658 | * @param {Integer} sid Service Identifier |
---|
659 | * @param {Function} onSuccess callback |
---|
660 | * @param {Function} onError callback |
---|
661 | */ |
---|
662 | this.getStatus = function(sid, onSuccess, onError) { |
---|
663 | var closure = this; |
---|
664 | if(closure.debug){ |
---|
665 | console.log("GET STATUS: "+sid); |
---|
666 | } |
---|
667 | if (closure.terminated[sid]) { |
---|
668 | if(closure.debug){ |
---|
669 | console.log("DEBUG TERMINATED"); |
---|
670 | } |
---|
671 | return; |
---|
672 | } |
---|
673 | if (!closure.launched[sid]) { |
---|
674 | if(closure.debug){ |
---|
675 | console.log("DEBUG LAUNCHED"); |
---|
676 | } |
---|
677 | return; |
---|
678 | } |
---|
679 | |
---|
680 | $.ajax({ |
---|
681 | url: closure.statusLocation[sid]+"×tamp="++(new Date()).getTime() |
---|
682 | }) |
---|
683 | .fail( |
---|
684 | function(jqXHR, textStatus, errorThrown) { |
---|
685 | if(closure.debug){ |
---|
686 | console.log("======== ERROR ========"); |
---|
687 | } |
---|
688 | var robj=_x2js.xml2json( jqXHR.responseXML ); |
---|
689 | if(closure.debug){ |
---|
690 | console.log(robj); |
---|
691 | } |
---|
692 | if(onError) |
---|
693 | onError(robj); |
---|
694 | } |
---|
695 | ) |
---|
696 | .done( |
---|
697 | function(data, textStatus, jqXHR) { |
---|
698 | if(closure.debug){ |
---|
699 | console.log("======== SUCCESS ========2"); |
---|
700 | console.log(data); |
---|
701 | } |
---|
702 | var ctype=jqXHR.getResponseHeader("Content-Type").split(";")[0]; |
---|
703 | if( ctype=="text/xml" ){ |
---|
704 | var tmpD=data; |
---|
705 | data = _x2js.xml2json( data ); |
---|
706 | data._origin=tmpD; |
---|
707 | } |
---|
708 | if(onSuccess) |
---|
709 | onSuccess(data); |
---|
710 | }); |
---|
711 | }; |
---|
712 | |
---|
713 | /** |
---|
714 | * The getResult method is used by {@link ZooProcess#watch} to get the |
---|
715 | * final result of services called asynchronously. |
---|
716 | * |
---|
717 | * @method getResult |
---|
718 | * @memberof ZooProcess# |
---|
719 | * @param {Integer} sid Service Identifier |
---|
720 | * @param {Function} onSuccess callback |
---|
721 | * @param {Function} onError callback |
---|
722 | */ |
---|
723 | this.getResult = function(sid, onSuccess, onError) { |
---|
724 | var closure = this; |
---|
725 | if(closure.debug){ |
---|
726 | console.log("GET STATUS: "+sid); |
---|
727 | console.log(closure.statusLocation[sid].replace(/GetStatus/g,"GetResult")); |
---|
728 | } |
---|
729 | $.ajax({ |
---|
730 | url: closure.statusLocation[sid].replace(/GetStatus/g,"GetResult") |
---|
731 | }) |
---|
732 | .fail( |
---|
733 | function(jqXHR, textStatus, errorThrown) { |
---|
734 | if(closure.debug){ |
---|
735 | console.log("======== ERROR ========"); |
---|
736 | } |
---|
737 | var robj=_x2js.xml2json( jqXHR.responseXML ); |
---|
738 | if(closure.debug){ |
---|
739 | console.log(robj); |
---|
740 | } |
---|
741 | if(onError) |
---|
742 | onError(robj); |
---|
743 | } |
---|
744 | ) |
---|
745 | .done( |
---|
746 | function(data, textStatus, jqXHR) { |
---|
747 | if(closure.debug){ |
---|
748 | console.log("======== SUCCESS ========2"); |
---|
749 | console.log(data); |
---|
750 | } |
---|
751 | var ctype=jqXHR.getResponseHeader("Content-Type").split(";")[0]; |
---|
752 | if( ctype=="text/xml" ){ |
---|
753 | var tmpD=data; |
---|
754 | data = _x2js.xml2json( data ); |
---|
755 | data._origin=tmpD; |
---|
756 | } |
---|
757 | if(onSuccess) |
---|
758 | onSuccess(data); |
---|
759 | }); |
---|
760 | }; |
---|
761 | |
---|
762 | /** |
---|
763 | * The getQueryString method generate a KVP GET request which can be |
---|
764 | * used to request a WPS server. |
---|
765 | * |
---|
766 | * @method getQueryString |
---|
767 | * @memberof ZooProcess# |
---|
768 | * @param {Object} params The WPS requests parameters |
---|
769 | * @returns {String} The GET WPS request |
---|
770 | * @example |
---|
771 | * // Log GetCapabilities GET request in console |
---|
772 | * var request_params = { |
---|
773 | * request: 'GetCapabilities', |
---|
774 | * service: 'WPS', |
---|
775 | * version: '1.0.0', |
---|
776 | * language; 'en-US' |
---|
777 | * } |
---|
778 | * console.log(myZooObject.getQueryString(request_params)); |
---|
779 | */ |
---|
780 | this.getQueryString = function(params) { |
---|
781 | var closure = this; |
---|
782 | var ret = ''; |
---|
783 | |
---|
784 | serializeInputs = function(obj) { |
---|
785 | if(closure.debug){ |
---|
786 | console.log("SERIALIZE dataInputs"); |
---|
787 | console.log(obj); |
---|
788 | } |
---|
789 | var lt=$.type(obj); |
---|
790 | if(lt === "string") { |
---|
791 | return obj; |
---|
792 | } |
---|
793 | var str = []; |
---|
794 | for(var p in obj){ |
---|
795 | if(lt === "array"){ |
---|
796 | if(obj[p].hasOwnProperty("href")) |
---|
797 | str.push(obj[p]["identifier"] + "=Reference"); |
---|
798 | else |
---|
799 | str.push(obj[p]["identifier"] + "=" + obj[p]["value"]); |
---|
800 | for(var q in obj[p]){ |
---|
801 | if(q!="identifier" && q!="value" && q!="href") |
---|
802 | str.push("@" + q + "=" + obj[p][q]); |
---|
803 | else |
---|
804 | if(q=="href") |
---|
805 | str.push("@xlink:" + q + "=" + encodeURIComponent(obj[p][q])); |
---|
806 | } |
---|
807 | str.push(";"); |
---|
808 | } |
---|
809 | else |
---|
810 | if (obj.hasOwnProperty(p)) { |
---|
811 | if(p=="href") |
---|
812 | str.push(p + "=" + encodeURIComponent(obj[p])); |
---|
813 | else |
---|
814 | str.push(p + "=" + obj[p]); |
---|
815 | } |
---|
816 | } |
---|
817 | return str.join(""); |
---|
818 | } |
---|
819 | |
---|
820 | serializeOutputs = function(obj) { |
---|
821 | if(closure.debug){ |
---|
822 | console.log("SERIALIZE dataOutputs"); |
---|
823 | console.log(obj); |
---|
824 | } |
---|
825 | var lt=$.type(obj); |
---|
826 | if(lt === "string") { |
---|
827 | return obj; |
---|
828 | } |
---|
829 | var str = []; |
---|
830 | for(var p in obj){ |
---|
831 | str.push(obj[p]["identifier"]); |
---|
832 | for(var q in obj[p]){ |
---|
833 | if(q!="identifier" && q!="type") |
---|
834 | str.push("@" + q + "=" + obj[p][q]); |
---|
835 | } |
---|
836 | str.push(";"); |
---|
837 | } |
---|
838 | return str.join(""); |
---|
839 | } |
---|
840 | |
---|
841 | var responseDocument = params.ResponseDocument; |
---|
842 | var tmp_params = {}; |
---|
843 | |
---|
844 | var objectKeys = Object.keys || function (obj) { |
---|
845 | var keys = []; |
---|
846 | for (var key in obj) keys.push(key); |
---|
847 | return keys; |
---|
848 | }; |
---|
849 | |
---|
850 | var skip = { |
---|
851 | 'DataInputs': true, |
---|
852 | 'DataOutputs': true, |
---|
853 | 'ResponseDocument': true, |
---|
854 | 'RawDataOutput': true, |
---|
855 | } |
---|
856 | var keys = objectKeys(params); |
---|
857 | for (var i = 0; i < keys.length; i++) { |
---|
858 | var key = keys[i]; |
---|
859 | if (skip.hasOwnProperty(key) && skip[key]) { |
---|
860 | continue; |
---|
861 | } |
---|
862 | if (params.hasOwnProperty(key)) { |
---|
863 | tmp_params[key] = params[key]; |
---|
864 | } |
---|
865 | } |
---|
866 | ret = qs.stringify(tmp_params); |
---|
867 | |
---|
868 | //req_options.path = req_options.path.replace("&DataInputs=sid%3D", "&DataInputs=sid=") |
---|
869 | if (params.hasOwnProperty('DataInputs')) { |
---|
870 | //var dataInputs = params.DataInputs; |
---|
871 | var dataInputs = serializeInputs(params.DataInputs); |
---|
872 | if(closure.debug){ |
---|
873 | console.log("dataInputs: "+dataInputs); |
---|
874 | } |
---|
875 | ret += '&DataInputs=' + dataInputs; |
---|
876 | } |
---|
877 | |
---|
878 | if (params.hasOwnProperty('DataOutputs')) { |
---|
879 | var dataOutputs = serializeOutputs(params.DataOutputs); |
---|
880 | if(closure.debug){ |
---|
881 | console.log("dataOutputs: "+dataOutputs); |
---|
882 | } |
---|
883 | if(dataOutputs!=""){ |
---|
884 | var displayInputs=true; |
---|
885 | for(var i=0;i<params.DataOutputs.length;i++) |
---|
886 | if(params.DataOutputs[i].type==="raw"){ |
---|
887 | ret += '&RawDataOutput=' + dataOutputs; |
---|
888 | displayInputs=false; |
---|
889 | break; |
---|
890 | } |
---|
891 | if(displayInputs) |
---|
892 | ret += '&ResponseDocument=' + dataOutputs; |
---|
893 | } |
---|
894 | }else{ |
---|
895 | if (params.hasOwnProperty('RawDataOutput')) { |
---|
896 | ret+="&RawDataOutput="+params['RawDataOutput']+";"; |
---|
897 | }else{ |
---|
898 | if (params.hasOwnProperty('ResponseDocument')) { |
---|
899 | var lt=$.type(params['ResponseDocument']); |
---|
900 | if(lt === "string") { |
---|
901 | ret+="&ResponseDocument="+params['ResponseDocument']+";"; |
---|
902 | }else{ |
---|
903 | var tmp_ret=serializeOutputs(params['ResponseDocument']); |
---|
904 | ret+="&ResponseDocument="+tmp; |
---|
905 | } |
---|
906 | } |
---|
907 | } |
---|
908 | } |
---|
909 | |
---|
910 | return ret; |
---|
911 | }; |
---|
912 | |
---|
913 | /** |
---|
914 | * The parseStatusLocation method parse the statusLocation and return an |
---|
915 | * object with sid and statusLocation attributes which contains |
---|
916 | * respectively: a unique identifier named sid and the statusLocation |
---|
917 | * value returned by the WPS server. |
---|
918 | * |
---|
919 | * @method parseStatusLocation |
---|
920 | * @memberof ZooProcess# |
---|
921 | * @param {Object} data The XML response parsed by x2js.xml2json |
---|
922 | * @returns {Object} The result is an object with sid and statusLocation |
---|
923 | */ |
---|
924 | this.parseStatusLocation = function(data) { |
---|
925 | var closure = this; |
---|
926 | |
---|
927 | if (statusLocation = data.ExecuteResponse._statusLocation) { |
---|
928 | if(closure.debug){ |
---|
929 | console.log("statusLocation: "+statusLocation); |
---|
930 | } |
---|
931 | |
---|
932 | var lsid=0; |
---|
933 | for(i in closure.statusLocation) |
---|
934 | lsid++; |
---|
935 | |
---|
936 | return {sid: lsid, statusLocation: statusLocation}; |
---|
937 | } |
---|
938 | }; |
---|
939 | |
---|
940 | /** |
---|
941 | * The parseJobID method parse the JobID and return an |
---|
942 | * object with sid and the JobID attributes which contains |
---|
943 | * respectively: a unique identifier named sid and the JobID |
---|
944 | * value returned by the WPS server. |
---|
945 | * |
---|
946 | * @method parseJobID |
---|
947 | * @memberof ZooProcess# |
---|
948 | * @param {Object} data The XML response parsed by x2js.xml2json |
---|
949 | * @returns {Object} The result is an object with sid and jobID |
---|
950 | */ |
---|
951 | this.parseJobID = function(data) { |
---|
952 | var closure = this; |
---|
953 | |
---|
954 | if (jobID = data.StatusInfo.JobID) { |
---|
955 | |
---|
956 | var lsid=0; |
---|
957 | for(i in closure.statusLocation) |
---|
958 | lsid++; |
---|
959 | |
---|
960 | return {sid: lsid, jobid: jobID}; |
---|
961 | } |
---|
962 | }; |
---|
963 | |
---|
964 | /** |
---|
965 | * The dismiss method run the Dismiss request by calling {@link ZooProcess#request}. |
---|
966 | * |
---|
967 | * @method dismiss |
---|
968 | * @memberof ZooProcess# |
---|
969 | * @param {Object} params |
---|
970 | * @example |
---|
971 | * // Log x2js representation of all available services in console |
---|
972 | * myZooObject.dismiss({ |
---|
973 | * type: 'POST', |
---|
974 | * jobid: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" |
---|
975 | * success: function(data){ |
---|
976 | * console.log(data); |
---|
977 | * } |
---|
978 | * }); |
---|
979 | */ |
---|
980 | this.dismiss = function(params) { |
---|
981 | var closure = this; |
---|
982 | |
---|
983 | if (!params.hasOwnProperty('type')) { |
---|
984 | params.type = 'GET'; |
---|
985 | } |
---|
986 | |
---|
987 | var zoo_request_params = { |
---|
988 | Identifier: params.identifier, |
---|
989 | request: 'Dismiss', |
---|
990 | service: 'WPS', |
---|
991 | jobid: params.jobid, |
---|
992 | version: "2.0.0", |
---|
993 | } |
---|
994 | |
---|
995 | this.request(zoo_request_params, params.success, params.error, params.type); |
---|
996 | }; |
---|
997 | |
---|
998 | }; |
---|
999 | |
---|
1000 | |
---|
1001 | return ZooProcess; |
---|
1002 | |
---|
1003 | }); |
---|