Skip to content
Snippets Groups Projects
Commit 644d0d2a authored by Johannes Hörmann's avatar Johannes Hörmann
Browse files

address webservice better error handling, disable by default

parent 6de62ff3
No related branches found
No related tags found
No related merge requests found
......@@ -37,7 +37,6 @@
<entityField>
<name>CITY</name>
<title>City</title>
<consumer>CityValidation</consumer>
<mandatoryProcess>%aditoprj%/entity/Address_entity/entityfields/city/mandatoryProcess.js</mandatoryProcess>
<displayValueProcess>%aditoprj%/entity/Address_entity/entityfields/city/displayValueProcess.js</displayValueProcess>
<onValueChange>%aditoprj%/entity/Address_entity/entityfields/city/onValueChange.js</onValueChange>
......@@ -74,9 +73,8 @@
<entityField>
<name>ZIP</name>
<title>postcode</title>
<consumer>ZipValidation</consumer>
<consumer></consumer>
<mandatoryProcess>%aditoprj%/entity/Address_entity/entityfields/zip/mandatoryProcess.js</mandatoryProcess>
<newItemsAllowed v="true" />
<displayValueProcess>%aditoprj%/entity/Address_entity/entityfields/zip/displayValueProcess.js</displayValueProcess>
<onValidation>%aditoprj%/entity/Address_entity/entityfields/zip/onValidation.js</onValidation>
<onValueChange>%aditoprj%/entity/Address_entity/entityfields/zip/onValueChange.js</onValueChange>
......
......@@ -23,5 +23,5 @@ if (fn != null)
additional.countryCode = vars.get("$param.ContactsMainCountry_param");//TODO: try to use users language first and then the companies
var res = fn.call(null, commAddr, additional);
if (res != null)
result.object(res);
result.string(res);
}
\ No newline at end of file
......@@ -2,7 +2,7 @@
<preferences xmlns="http://www.adito.de/2018/ao/Model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" VERSION="3.1.0" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/preferences/3.1.0">
<name>_____PREFERENCES_PROJECT</name>
<majorModelMode>DISTRIBUTED</majorModelMode>
<projectName>basic 2019</projectName>
<projectName>xRM-Basic2019</projectName>
<jditoMaxContentSize v="57671680" />
<calendarCategoriesEvent>
<entry>
......@@ -34,6 +34,10 @@
<indexsearchIncrementingIndexerBunchSize v="250" />
<indexsearchMaximumHits v="50000" />
<customProperties>
<customBooleanProperty>
<name>addressValidation.enable</name>
<property v="false" />
</customBooleanProperty>
<customStringProperty>
<name>addressValidation.url</name>
<property>https://services.aditosoftware.local/services/rest/ws_checkAddress</property>
......@@ -50,8 +54,14 @@
<name>addressValidation.countryParamName</name>
<property>country</property>
</customStringProperty>
<customBooleanProperty>
<name>phoneValidation.enable</name>
<description></description>
<property v="false" />
</customBooleanProperty>
<customStringProperty>
<name>phoneValidation.url</name>
<description></description>
<property>https://services.aditosoftware.local/services/rest/ws_checkPhoneNumber</property>
</customStringProperty>
<customStringProperty>
......
......@@ -49,13 +49,23 @@ WsValidationType.get = function(pKey)
function _processAddressLookup(pWsResult, pValue)
{
// add default result consisting of pValue
var defaultResult = [[{}, pValue]];
defaultResult[0][0][this.paramName] = pValue;
return JSON.parse(pWsResult)
.concat(defaultResult)
.map(function(pAddress)
var resultAddresses = [];
// if error, only add the default value else parse the body
if (!WsValidationUtils._isError(pWsResult))
{
resultAddresses = JSON.parse(pWsResult.body);
}
resultAddresses = resultAddresses.concat(defaultResult);
return resultAddresses.map(function(pAddress)
{
var data = pAddress[0];
return [
......@@ -68,12 +78,19 @@ WsValidationType.get = function(pKey)
data.region,
data.state,
data[this.paramName]]
}, this)
}, this);
}
function _processCommunicationValidation(pWsResult, pValue)
{
return pWsResult;
if (!WsValidationUtils._isError(pWsResult))
{
return pWsResult.body;
}
else
{
return pValue;
}
}
}
......@@ -108,39 +125,42 @@ WsValidationUtils.validate = function(pValue, pType, pCountry)
if (typeof pType == "string")
pType = WsValidationType.get(pType);
// get configurations for this type
var userName = project.getPreferenceValue("custom." + pType.webserviceName + ".user");
var pw = project.getPreferenceValue("custom." + pType.webserviceName + ".pw");
var url = project.getPreferenceValue("custom." + pType.webserviceName + ".url");
var countryParamName = project.getPreferenceValue("custom." + pType.webserviceName + ".countryParamName");
var actionType = "GET";
if (pValue && pType)
if (WsValidationUtils.isWsEnabled(pType))
{
// fill params
var parameters = {};
if (pCountry)
{
parameters[countryParamName] = pCountry;
}
parameters[pType.paramName] = pValue;
// call webservice
var ret = JSON.parse(net.callRestWebserviceBasicAuth(url, actionType, parameters, null, null, "text/plain", "text/plain", util.DATA_TEXT, util.DATA_TEXT, userName, pw, true));
if (ret.hasHttpSuccessStatusCode && ret.httpStatusCode == 200)
var userName = project.getPreferenceValue("custom." + pType.webserviceName + ".user");
var pw = project.getPreferenceValue("custom." + pType.webserviceName + ".pw");
var url = project.getPreferenceValue("custom." + pType.webserviceName + ".url");
var countryParamName = project.getPreferenceValue("custom." + pType.webserviceName + ".countryParamName");
var actionType = "GET";
if (pValue && pType)
{
// fill params
var parameters = {};
if (pCountry)
{
parameters[countryParamName] = pCountry;
}
parameters[pType.paramName] = pValue;
// call webservice
var ret = JSON.parse(net.callRestWebserviceBasicAuth(url, actionType, parameters, null, null, "text/plain", "text/plain", util.DATA_TEXT, util.DATA_TEXT, userName, pw, true));
// if error, log the error. But also process the result and let the callback generate the correct default value
if (WsValidationUtils._isError(ret))
{
logging.log(translate.withArguments("${WEBSERVICE_ERROR} url:%0 status:%1", [url, (ret.hasHttpSuccessStatusCode ? " StatusCode: " + ret.httpStatusCode : "")]));
logging.log("Params: " + parameters.toSource());
}
// different handling of the result per type
return pType.processResultCallback(ret.body, pValue);
}
else
{
throw new Error(translate.withArguments("${WEBSERVICE_ERROR} url:%0 status:%1", [url, (ret.hasHttpSuccessStatusCode ? " StatusCode: " + ret.httpStatusCode : "")]));
return pType.processResultCallback(ret, pValue);
}
}
return [];
return pType.processResultCallback(null, pValue);
}
/**
......@@ -169,4 +189,36 @@ WsValidationUtils.setAddressFields = function()
if (pValue)
neon.setFieldValue(pField, pValue);
}
}
/**
* check if the Webservice is enabled in the project preferences
* @param {Object} pType
*
* @return {boolean}
*/
WsValidationUtils.isWsEnabled = function(pType)
{
// get WsValidationType-Object if it is only the key
if (typeof pType == "string")
pType = WsValidationType.get(pType);
// get configurations for this type
try
{
return JSON.parse(project.getPreferenceValue("custom." + pType.webserviceName + ".enable"));
} catch (exception) {
return false;
}
}
/**
* check if the Webservice-result is valid or an error
* @param {Object} pWsReturn
*
* @return {boolean}
*/
WsValidationUtils._isError = function(pWsReturn)
{
return pWsReturn == null || !pWsReturn.hasHttpSuccessStatusCode || pWsReturn.httpStatusCode != 200;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment