[480] | 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 | */ |
---|
[517] | 24 | |
---|
[480] | 25 | define([ |
---|
[517] | 26 | 'jquery', 'utils' |
---|
| 27 | ], function($, utils) { |
---|
[480] | 28 | |
---|
[517] | 29 | /** |
---|
| 30 | * The wpsPayload module is using the |
---|
| 31 | * [Hogan.js]{@link http://twitter.github.io/hogan.js/} templating engine to |
---|
| 32 | * generate XML requests to be sent to a WPS Server. |
---|
| 33 | * In the ZOO-Client API, the Hogan.js templates have to be compiled before |
---|
| 34 | * you can use them from you application. Please refer to the ZOO-Client |
---|
[781] | 35 | * installation documentation for more information. |
---|
[517] | 36 | * |
---|
| 37 | * @module wpsPayload |
---|
| 38 | * @requires payloads |
---|
| 39 | * @requires jquery |
---|
| 40 | * @requires utils |
---|
| 41 | */ |
---|
[480] | 42 | |
---|
| 43 | return { |
---|
[517] | 44 | |
---|
| 45 | /** @exports wpsPayload */ |
---|
[480] | 46 | |
---|
[517] | 47 | /** |
---|
| 48 | * The getPayload function uses the mustache |
---|
| 49 | * templates and the parameters provided to generate a valid WPS XML |
---|
| 50 | * request. |
---|
| 51 | * |
---|
| 52 | * @static |
---|
| 53 | * @param {Object} params - The object representing the request. |
---|
| 54 | * @returns {string} - The corresponding XML request |
---|
| 55 | * @example |
---|
| 56 | * // GetCapabilities |
---|
| 57 | * var request_params = { |
---|
| 58 | * request: 'GetCapabilities', |
---|
| 59 | * language: 'en-US' |
---|
| 60 | * }; |
---|
| 61 | * console.wpsPayload.getPayload(request_params)); |
---|
| 62 | * @example |
---|
| 63 | * // DescribeProcess with Identifier value set to "all". |
---|
| 64 | * var request_params = { |
---|
| 65 | * request: 'DescribeProcess', |
---|
| 66 | * identifier: ["all"] |
---|
| 67 | * }; |
---|
| 68 | * console.log(wpsPayload.getPayload(request_params)); |
---|
| 69 | * @example |
---|
| 70 | * // |
---|
| 71 | * var request_params = { |
---|
| 72 | * request: 'Execute', |
---|
| 73 | * Identifier: "Buffer", |
---|
| 74 | * DataInputs: [{"identifier":"InputPolygon","href":"http://features.org/toto.xml","mimeType":"text/xml"}], |
---|
| 75 | * DataOutputs: [{"identifier":"Result","mimeType":"application/json"}], |
---|
| 76 | * language: 'en-US' |
---|
| 77 | * }; |
---|
| 78 | * console.log(wpsPayload.getPayload(request_params)); |
---|
| 79 | */ |
---|
[480] | 80 | getPayload: function(params) { |
---|
| 81 | if (params.request == 'DescribeProcess') { |
---|
| 82 | return this.getPayload_DescribeProcess(params); |
---|
| 83 | } else if (params.request == 'GetCapabilities') { |
---|
| 84 | return this.getPayload_GetCapabilities(params); |
---|
| 85 | } else if (params.request == 'Execute') { |
---|
| 86 | return this.getPayload_Execute(params); |
---|
[719] | 87 | } else if (params.request == 'Dismiss') { |
---|
| 88 | return this.getPayload_Dismiss(params); |
---|
[480] | 89 | } else { |
---|
| 90 | console.log("#### UNKNOWN REQUEST ####"); |
---|
| 91 | } |
---|
| 92 | }, |
---|
[517] | 93 | |
---|
| 94 | /** |
---|
| 95 | * The getPayload_GetCapabilities function is used to generate a valid |
---|
| 96 | * WPS XML GetCapabilities request using the |
---|
| 97 | * [payload_GetCapabilities.mustache]{@link http://zoo-project.org/trac/browser/trunk/zoo-project/zoo-client/lib/tpl/payload_GetCapabilities.mustache} |
---|
| 98 | * template. |
---|
| 99 | * |
---|
| 100 | * @static |
---|
| 101 | * @param {Object} params - The object representing the request. |
---|
| 102 | * @returns {string} - The corresponding XML request |
---|
| 103 | * @example |
---|
| 104 | * // log the XML request in console |
---|
| 105 | * var request_params = { |
---|
| 106 | * language: 'en-US' |
---|
| 107 | * }; |
---|
| 108 | * console.log(wpsPayload.getPayload_GetCapabilities(request_params)); |
---|
| 109 | */ |
---|
| 110 | getPayload_GetCapabilities: function(params) { |
---|
[719] | 111 | var id="payload_GetCapabilities"; |
---|
| 112 | if(params.version=="2.0.0") |
---|
| 113 | id+="2"; |
---|
| 114 | return templates[id].render(params); |
---|
[517] | 115 | }, |
---|
[480] | 116 | |
---|
[517] | 117 | /** |
---|
| 118 | * The getPayload_DescribeProcess function is used to generate a valid |
---|
| 119 | * WPS XML DescribeProcess request using the |
---|
| 120 | * [payload_DescribeProcess.mustache]{@link http://zoo-project.org/trac/browser/trunk/zoo-project/zoo-client/lib/tpl/payload_DescribeProcess.mustache} |
---|
| 121 | * template. |
---|
| 122 | * |
---|
| 123 | * @static |
---|
| 124 | * @param {Object} params - The object representing the request. |
---|
| 125 | * @returns {string} - The corresponding XML request |
---|
| 126 | * @example |
---|
| 127 | * // log the XML request in console |
---|
| 128 | * var request_params = { |
---|
| 129 | * Identifier: ["Buffer","Centroid"], |
---|
| 130 | * language: 'en-US' |
---|
| 131 | * }; |
---|
| 132 | * console.log(wpsPayload.getPayload_DescribeProcess(request_params)); |
---|
| 133 | */ |
---|
[480] | 134 | getPayload_DescribeProcess: function(params) { |
---|
[719] | 135 | var id="payload_DescribeProcess"; |
---|
| 136 | if(params.version=="2.0.0") |
---|
| 137 | id+="2"; |
---|
[480] | 138 | if (params.Identifier) { |
---|
| 139 | if ($.isArray(params.Identifier)) { |
---|
[719] | 140 | return templates[id].render({identifiers: params.Identifier,language: params.language}); |
---|
[480] | 141 | } |
---|
| 142 | else { |
---|
[719] | 143 | return templates[id].render({identifiers: [params.Identifier],language: params.language}); |
---|
[480] | 144 | } |
---|
| 145 | } |
---|
| 146 | // TODO: no Identifier |
---|
| 147 | }, |
---|
| 148 | |
---|
[517] | 149 | /** |
---|
[719] | 150 | * The getPayload_Dismiss function is used to generate a valid |
---|
| 151 | * WPS XML Dimiss request using the |
---|
| 152 | * [payload_Dismiss.mustache]{@link http://zoo-project.org/trac/browser/trunk/zoo-project/zoo-client/lib/tpl/payload_Dismiss.mustache} |
---|
| 153 | * template. |
---|
| 154 | * |
---|
| 155 | * @static |
---|
| 156 | * @param {Object} params - The object representing the request. |
---|
| 157 | * @returns {string} - The corresponding XML request |
---|
| 158 | * @example |
---|
| 159 | * // log the XML request in console |
---|
| 160 | * var request_params = { |
---|
| 161 | * jobId: ["XXXX","XXX"] |
---|
| 162 | * }; |
---|
| 163 | * console.log(wpsPayload.getPayload_DescribeProcess(request_params)); |
---|
| 164 | */ |
---|
| 165 | getPayload_Dismiss: function(params) { |
---|
| 166 | var id="payload_Dismiss"; |
---|
| 167 | params.version="2.0.0"; |
---|
| 168 | if (params.jobid) { |
---|
| 169 | if ($.isArray(params.jobid)) { |
---|
| 170 | return templates[id].render({jobid: params.jobid}); |
---|
| 171 | } |
---|
| 172 | else { |
---|
| 173 | return templates[id].render({jobid: [params.jobid]}); |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | // TODO: no Identifier |
---|
| 177 | }, |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | /** |
---|
[517] | 181 | * The getPayload_Execute function is used to generate a valid WPS XML |
---|
| 182 | * Excute request using the |
---|
| 183 | * [payload_Execute.mustache]{@link http://zoo-project.org/trac/browser/trunk/zoo-project/zoo-client/lib/tpl/payload_Execute.mustache} |
---|
| 184 | * template. |
---|
| 185 | * |
---|
| 186 | * @static |
---|
| 187 | * @param {Object} params - The object representing the request. |
---|
| 188 | * @returns {string} - The corresponding XML request |
---|
| 189 | * @example |
---|
| 190 | * // log the XML request in console |
---|
| 191 | * var request_params = { |
---|
| 192 | * Identifier: "Buffer", |
---|
| 193 | * DataInputs: [{"identifier":"InputPolygon","href":"http://features.org/toto.xml","mimeType":"text/xml"}], |
---|
| 194 | * DataOutputs: [{"identifier":"Result","mimeType":"application/json"}], |
---|
| 195 | * language: 'en-US' |
---|
| 196 | * }; |
---|
| 197 | * console.log(wpsPayload.getPayload_Execute(request_params)); |
---|
| 198 | */ |
---|
[480] | 199 | getPayload_Execute: function(params) { |
---|
[719] | 200 | var id="payload_Execute"; |
---|
| 201 | if(params.version=="2.0.0") |
---|
| 202 | id+="2"; |
---|
[480] | 203 | if (params.DataInputs) { |
---|
[517] | 204 | for (var i = 0; i < params.DataInputs.length; i++) { |
---|
| 205 | /** |
---|
| 206 | * Define inputs type depending on presence of mimeType, |
---|
| 207 | * dataType and crs or dimension for ComplexData, |
---|
| 208 | * LiteralData and BoundingBox data respectively |
---|
| 209 | */ |
---|
| 210 | var hasType=false; |
---|
| 211 | var lp={"data":"literal","mime":"complex"}; |
---|
| 212 | for(j in lp){ |
---|
| 213 | if (params.DataInputs[i][j+"Type"]) { |
---|
| 214 | params.DataInputs[i]['is_'+lp[j]] = true; |
---|
| 215 | params.DataInputs[i].type=lp[j]; |
---|
| 216 | if(j=="mime"){ |
---|
| 217 | params.DataInputs[i].is_XML=(params.DataInputs[i][j+"Type"]=="text/xml"); |
---|
| 218 | if(!params.DataInputs[i].is_XML){ |
---|
| 219 | var tmp=params.DataInputs[i][j+"Type"].split(";"); |
---|
| 220 | params.DataInputs[i].is_XML=(tmp[0]=="text/xml"); |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | hasType=true; |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | if(!hasType){ |
---|
| 227 | if (params.DataInputs[i]["type"]=="bbox" || |
---|
| 228 | params.DataInputs[i]["dimension"] || |
---|
| 229 | params.DataInputs[i]["crs"]){ |
---|
[480] | 230 | |
---|
[517] | 231 | params.DataInputs[i]['is_bbox'] = true; |
---|
| 232 | params.DataInputs[i].type='bbox'; |
---|
| 233 | hasType=true; |
---|
| 234 | |
---|
| 235 | } |
---|
| 236 | if(!hasType){ |
---|
| 237 | params.DataInputs[i]['is_literal'] = true; |
---|
| 238 | params.DataInputs[i].type = "literal"; |
---|
| 239 | } |
---|
| 240 | } |
---|
[480] | 241 | /* |
---|
[517] | 242 | * Set some default values and flags. |
---|
| 243 | */ |
---|
| 244 | if (params.DataInputs[i].type == 'bbox') { |
---|
| 245 | if (!params.DataInputs[i].crs) { |
---|
| 246 | params.DataInputs[i].crs = "EPSG:4326"; |
---|
[480] | 247 | } |
---|
| 248 | if (!params.DataInputs[i].dimension) { |
---|
[517] | 249 | params.DataInputs[i].dimension = 2; |
---|
[480] | 250 | } |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | // Complex data from payload callback. |
---|
| 254 | if (params.DataInputs[i].complexPayload_callback) { |
---|
[517] | 255 | params.DataInputs[i].value = window[params.DataInputs[i].complexPayload_callback]; |
---|
[480] | 256 | } |
---|
| 257 | |
---|
| 258 | // Complex data from reference. |
---|
| 259 | if (params.DataInputs[i].href) { |
---|
| 260 | params.DataInputs[i].is_reference = true; |
---|
| 261 | //params.DataInputs[i].href = utils.encodeXML(params.DataInputs[i].href); |
---|
| 262 | if (params.DataInputs[i].method == 'POST') { |
---|
| 263 | params.DataInputs[i].is_post = true; |
---|
| 264 | } else { |
---|
| 265 | params.DataInputs[i].is_get = true; |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | else { |
---|
| 269 | // Complex data, embeded |
---|
| 270 | } |
---|
| 271 | } // for i loop |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | //console.log("==== OUTPUTS ===="); |
---|
| 275 | if (params.DataOutputs || params.storeExecuteResponse || params.status || params.lineage) { |
---|
| 276 | |
---|
| 277 | for (var i = 0; i < params.DataOutputs.length; i++) { |
---|
| 278 | //console.log(params.DataOutputs[i]); |
---|
| 279 | |
---|
| 280 | if (params.DataOutputs[i].type) { |
---|
| 281 | params.DataOutputs[i]['is_'+params.DataOutputs[i].type] = true; |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | } |
---|
| 285 | |
---|
[719] | 286 | return templates[id].render(params); |
---|
[480] | 287 | }, |
---|
| 288 | |
---|
| 289 | |
---|
| 290 | }; |
---|
| 291 | |
---|
| 292 | }); |
---|