/*############################################################################## # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ############################################################################## */ define([ "dojo/_base/declare", "dojo/_base/lang", "dojo/_base/config", "dojo/request", "dojo/request/script" ], function (declare, lang, config, request, script) { var RequestHelper = declare(null, { serverIP: null, constructor: function (args) { declare.safeMixin(this, args); this.serverIP = this.getParamFromURL("ServerIP"); }, getParamFromURL: function (key) { var value = dojo.queryToObject(dojo.doc.location.search.substr((dojo.doc.location.search.substr(0, 1) == "?" ? 1 : 0)))[key]; if (value) return value; return config[key]; }, getBaseURL: function (service) { if (!service) { service = "WsWorkunits"; } if (this.serverIP) return "http://" + this.serverIP + ":8010/" + service; return "/" + service; }, isCrossSite: function () { return this.serverIP ? true : false; }, _send: function(service, action, _params) { var params = lang.mixin({ load: function (response) { }, error: function (error) { }, event: function (evt) { } }, _params); lang.mixin(params.request, { rawxml_: true }); var handleAs = params.handleAs ? params.handleAs : "json"; var postfix = ""; if (handleAs === "json") { postfix = ".json"; } var method = params.method ? params.method : "get"; var retVal = null; if (this.isCrossSite()) { retVal = script.get(this.getBaseURL(service) + "/" + action + postfix, { query: params.request, jsonp: "jsonp" }); } else { retVal = request.post(this.getBaseURL(service) + "/" + action + postfix, { data: params.request, handleAs: handleAs }); } return retVal.then(function (response) { params.load(response); return response; }, function (error) { params.error(error); return error; }, function (event) { params.event(event); return event; }); }, send: function (service, action, params) { dojo.publish("hpcc/standbyBackgroundShow"); var handleAs = params.handleAs ? params.handleAs : "json"; return this._send(service, action, params).then(function (response) { if (handleAs == "json") { if (lang.exists("Exceptions.Source", response)) { var message = "
" + response.Exceptions.Exception[i].Message + "
"; } } dojo.publish("hpcc/brToaster", { message: message, type: "error", duration: -1 }); } } dojo.publish("hpcc/standbyBackgroundHide"); return response; }, function (error) { var message = "Unknown Error"; if (lang.exists("response.text", error)) { message = error.response.text; } else if (error.message && error.stack) { message = "" + error.stack + "
"; } dojo.publish("hpcc/brToaster", { message: message, type: "error", duration: -1 }); dojo.publish("hpcc/standbyBackgroundHide"); return error; }); }, // XML to JSON helpers --- getValue: function (domXml, tagName, knownObjectArrays) { var retVal = this.getValues(domXml, tagName, knownObjectArrays); if (retVal.length == 0) { return null; } else if (retVal.length != 1) { alert("Invalid length: " + retVal.length); } return retVal[0]; }, getValues: function (domXml, tagName, knownObjectArrays) { var retVal = []; var items = domXml.getElementsByTagName(tagName); var parentNode = items.length ? items[0].parentNode : null; // Prevent