/** * Author : Samuel Souk aloun * * Copyright (c) 2014 GeoLabs SARL * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ define([ 'xml2json', 'queryString', 'wpsPayload', 'utils' ], function(X2JS, qs, wpsPayload, utils) { /** * The ZooProcess Class * @constructs ZooProcess * @param {Object} params Parameters * @example * var myZooObject = new ZooProcess({ * url: "http://localhost/cgi-bin/zoo_loader.cgi", * delay: 2500 * }); */ var ZooProcess = function(params) { /** * Object configuring the xml2json use. * * @access private * @memberof ZooProcess# * @var _x2js {x2js} */ var _x2js = new X2JS({ arrayAccessFormPaths: [ 'ProcessDescriptions.ProcessDescription.DataInputs.Input', 'ProcessDescriptions.ProcessDescription.DataInputs.Input.ComplexData.Supported.Format', 'ProcessDescriptions.ProcessDescription.ProcessOutputs.Output', 'ProcessDescriptions.ProcessDescription.ProcessOutputs.Output.ComplexOutput.Supported.Format', 'Capabilities.ServiceIdentification.Keywords' ], }); /** * @access public * @memberof ZooProcess# * @var debug {Boolean} true if verbose messages should be displayed on the console * @default false */ this.debug = false; /** * @access public * @memberof ZooProcess# * @var url {String} The WPS Server URL */ this.url = params.url; /** * @access public * @memberof ZooProcess# * @var version {String} The WPS version */ this.version = params.version?params.version:"1.0.0"; /** * @access public * @memberof ZooProcess# * @var language {String} The language to be used to request the WPS Server * @default "en-US" */ this.language = params.language?params.language:"en-US"; /** * @access public * @memberof ZooProcess# * @var statusLocation {Object} An object to store the statusLocation * URLs when running request including both the storeExecuteResponse and * the status parameters set to true. */ this.statusLocation = {}; /** * @access public * @memberof ZooProcess# * @var launched {Object} An object to store the running asynchrone services. */ this.launched = {}; /** * @access public * @memberof ZooProcess# * @var terminated {Object} An object to store the finished services. */ this.terminated = {}; /** * @access public * @memberof ZooProcess# * @var percent {Object} An object to store the percentage of completude of services. */ this.percent = {}; /** * @access public * @memberof ZooProcess# * @var delay {Integer} The time (in milliseconds) between each polling requests. * @default 2000 */ this.delay = params.delay || 2000; /** * The getCapabilities method run the GetCapabilities request by calling {@link ZooProcess#request}. * * @method getCapabilities * @memberof ZooProcess# * @param {Object} params The parameter for the request and callback functions to call on success or * on failure. * @example * // Log the array of available processes in console * myZooObject.getCapabilities({ * type: 'POST', * success: function(data){ * console.log(data["Capabilities"]["ProcessOfferings"]["Process"]); * } * }); */ this.getCapabilities = function(params) { var closure = this; if (!params.hasOwnProperty('type')) { params.type = 'GET'; } var zoo_request_params = { request: 'GetCapabilities', service: 'WPS', version: (params.hasOwnProperty('version')?params.version:closure.version), } this.request(zoo_request_params, params.success, params.error, params.type); }; /** * The describeProcess method run the DescribeProcess request by calling {@link ZooProcess#request}. * * @method describeProcess * @memberof ZooProcess# * @param {Object} params * @example * // Log x2js representation of all available services in console * myZooObject.describeProcess({ * type: 'POST', * identifier: "all" * success: function(data){ * console.log(data); * } * }); */ this.describeProcess = function(params) { var closure = this; if (!params.hasOwnProperty('type')) { params.type = 'GET'; } var zoo_request_params = { Identifier: params.identifier, request: 'DescribeProcess', service: 'WPS', version: (params.hasOwnProperty('version')?params.version:closure.version), } this.request(zoo_request_params, params.success, params.error, params.type); }; /** * The convertParams method convert parameters for Execute requests * * @method convertParams * @memberof ZooProcess# * @param {Object} params The original object * @returns {Object} The converted object */ this.convertParams = function(params){ var closure = this; if(closure.debug){ console.log("======== Execute "+params.identifier); console.log(params); } if (!params.hasOwnProperty('type')) { params.type = 'GET'; } var zoo_request_params = { request: 'Execute', service: 'WPS', version: (params.hasOwnProperty('version')?params.version:closure.version), Identifier: params.identifier, DataInputs: params.dataInputs ? params.dataInputs : '', DataOutputs: params.dataOutputs ? params.dataOutputs : '', } console.log(zoo_request_params); if (params.hasOwnProperty('responseDocument')) { zoo_request_params.ResponseDocument = params.responseDocument; } if (params.hasOwnProperty('mode')) { zoo_request_params.mode = params.mode; } if (params.hasOwnProperty('storeExecuteResponse') && params.storeExecuteResponse) { zoo_request_params.storeExecuteResponse = 'true'; } if (params.hasOwnProperty('status') && params.status) { zoo_request_params.status = 'true'; } if (params.hasOwnProperty('lineage') && params.lineage) { zoo_request_params.lineage = 'true'; } return zoo_request_params; }; /** * The buildRequest method is building the object expected by * [jQuery.ajax]{@link http://api.jquery.com/jquery.ajax/}. * In case of GET request, it will use {@link ZooProcess#getQueryString}. * In case of POST request, it will use {@link module:wpsPayload} getPayload. * * @method buildRequest * @memberof ZooProcess# * @param {Object} params the request parameters * @param {String} type the request method ("GET" or "POST") * @returns {Object} The expected object to give as input for the * [jQuery.ajax]{@link http://api.jquery.com/jquery.ajax/} function. */ this.buildRequest = function(params,type){ var closure = this; if(closure.debug){ console.log('======== REQUEST method='+type); console.log(params); } var url = this.url; var payload; var headers; if(params.hasOwnProperty('DataOutputs')) for(var i=0;i