From c3a7a8df244c59de15592db16b1824f76c9a27e2 Mon Sep 17 00:00:00 2001 From: Johannes Hoermann <j.hoermann@adito.de> Date: Tue, 23 Apr 2019 14:59:32 +0200 Subject: [PATCH] rename AddressValidation_lib to wsValidation_lib, refactor, add phone validation --- .../organisation/affectedIds.js | 2 +- .../indexsearchgroups/person/affectedIds.js | 2 +- .../AddressValidation_entity.aod | 6 - .../children/type_param/valueProcess.js | 4 +- .../children/type_param/valueProcess.js | 2 +- .../children/type_param/valueProcess.js | 4 +- .../children/type_param/valueProcess.js | 2 +- .../recordcontainers/jdito/contentProcess.js | 4 +- .../entityfields/city/onValueChange.js | 4 +- .../entityfields/zip/onValueChange.js | 4 +- .../Communication_entity.aod | 9 - .../entityfields/addr/onValidation.js | 2 +- .../entityfields/addr/onValueChange.js | 27 +-- .../AddressList_view/AddressList_view.aod | 20 +-- .../AddressValidationLookup_view.aod | 4 - .../AdressMultiEdit_view.aod | 20 +-- .../_____PREFERENCES_PROJECT.aod | 20 +++ process/AddressValidation_lib/process.js | 141 --------------- .../WsValidation_lib.aod} | 4 +- process/WsValidation_lib/process.js | 164 ++++++++++++++++++ 20 files changed, 223 insertions(+), 222 deletions(-) delete mode 100644 process/AddressValidation_lib/process.js rename process/{AddressValidation_lib/AddressValidation_lib.aod => WsValidation_lib/WsValidation_lib.aod} (76%) create mode 100644 process/WsValidation_lib/process.js diff --git a/aliasDefinition/Data_alias/indexsearchgroups/organisation/affectedIds.js b/aliasDefinition/Data_alias/indexsearchgroups/organisation/affectedIds.js index a9593f23e2d..3d5f6ab938f 100644 --- a/aliasDefinition/Data_alias/indexsearchgroups/organisation/affectedIds.js +++ b/aliasDefinition/Data_alias/indexsearchgroups/organisation/affectedIds.js @@ -30,7 +30,7 @@ switch (tableName) break; case "COMMUNICATION": res = IndexsearchUtils.getAffectedIdValues("CONTACT_ID", infoContainer, function (id){ - return db.array(db.COLUMN, ["select ADDRESS.CONTACT_ID from COMMUNICATION where COMMUNICATIONID = ?", [ + return db.array(db.COLUMN, ["select COMMUNICATION.CONTACT_ID from COMMUNICATION where COMMUNICATIONID = ?", [ [id, SqlUtils.getSingleColumnType("COMMUNICATION", "COMMUNICATIONID")] ]]); }); diff --git a/aliasDefinition/Data_alias/indexsearchgroups/person/affectedIds.js b/aliasDefinition/Data_alias/indexsearchgroups/person/affectedIds.js index d8a51cd604e..dcec7271b09 100644 --- a/aliasDefinition/Data_alias/indexsearchgroups/person/affectedIds.js +++ b/aliasDefinition/Data_alias/indexsearchgroups/person/affectedIds.js @@ -33,7 +33,7 @@ switch (tableName) break; case "COMMUNICATION": res = IndexsearchUtils.getAffectedIdValues("CONTACT_ID", infoContainer, function (id){ - return db.array(db.COLUMN, ["select ADDRESS.CONTACT_ID from COMMUNICATION where COMMUNICATIONID = ?", [ + return db.array(db.COLUMN, ["select COMMUNICATION.CONTACT_ID from COMMUNICATION where COMMUNICATIONID = ?", [ [id, SqlUtils.getSingleColumnType("COMMUNICATION", "COMMUNICATIONID")] ]]); }); diff --git a/entity/AddressValidation_entity/AddressValidation_entity.aod b/entity/AddressValidation_entity/AddressValidation_entity.aod index 824e75c1ded..49f5ae6751b 100644 --- a/entity/AddressValidation_entity/AddressValidation_entity.aod +++ b/entity/AddressValidation_entity/AddressValidation_entity.aod @@ -17,12 +17,6 @@ <name>ZipValidaton</name> <fieldType>DEPENDENCY_IN</fieldType> <dependencies> - <entityDependency> - <name>1b2e3be5-b3be-42c5-a55f-b892fdfbf21f</name> - <entityName>Communication_entity</entityName> - <fieldName>AddressValidator</fieldName> - <isConsumer v="false" /> - </entityDependency> <entityDependency> <name>fcfcbebe-260a-4773-a874-9791b4887ae2</name> <entityName>Address_entity</entityName> diff --git a/entity/AddressValidation_entity/entityfields/cityvalidation/children/type_param/valueProcess.js b/entity/AddressValidation_entity/entityfields/cityvalidation/children/type_param/valueProcess.js index 605d65eb517..1f9eec45278 100644 --- a/entity/AddressValidation_entity/entityfields/cityvalidation/children/type_param/valueProcess.js +++ b/entity/AddressValidation_entity/entityfields/cityvalidation/children/type_param/valueProcess.js @@ -1,4 +1,4 @@ -import("AddressValidation_lib"); +import("WsValidation_lib"); import("system.result"); -result.string(AddressValidationType.get().TYPE_CITY.key); \ No newline at end of file +result.string(WsValidationType.get().TYPE_CITY.key); \ No newline at end of file diff --git a/entity/AddressValidation_entity/entityfields/cityvalidatior/children/type_param/valueProcess.js b/entity/AddressValidation_entity/entityfields/cityvalidatior/children/type_param/valueProcess.js index 605d65eb517..c930ba5500d 100644 --- a/entity/AddressValidation_entity/entityfields/cityvalidatior/children/type_param/valueProcess.js +++ b/entity/AddressValidation_entity/entityfields/cityvalidatior/children/type_param/valueProcess.js @@ -1,4 +1,4 @@ -import("AddressValidation_lib"); +import("WsValidation_lib"); import("system.result"); result.string(AddressValidationType.get().TYPE_CITY.key); \ No newline at end of file diff --git a/entity/AddressValidation_entity/entityfields/zipvalidaton/children/type_param/valueProcess.js b/entity/AddressValidation_entity/entityfields/zipvalidaton/children/type_param/valueProcess.js index e980e6dd0b8..c7eee1bb589 100644 --- a/entity/AddressValidation_entity/entityfields/zipvalidaton/children/type_param/valueProcess.js +++ b/entity/AddressValidation_entity/entityfields/zipvalidaton/children/type_param/valueProcess.js @@ -1,4 +1,4 @@ -import("AddressValidation_lib"); +import("WsValidation_lib"); import("system.result"); -result.string(AddressValidationType.get().TYPE_ZIP.key); \ No newline at end of file +result.string(WsValidationType.get().TYPE_ZIP.key); \ No newline at end of file diff --git a/entity/AddressValidation_entity/entityfields/zipvalidator/children/type_param/valueProcess.js b/entity/AddressValidation_entity/entityfields/zipvalidator/children/type_param/valueProcess.js index e980e6dd0b8..7b6b2602657 100644 --- a/entity/AddressValidation_entity/entityfields/zipvalidator/children/type_param/valueProcess.js +++ b/entity/AddressValidation_entity/entityfields/zipvalidator/children/type_param/valueProcess.js @@ -1,4 +1,4 @@ -import("AddressValidation_lib"); +import("WsValidation_lib"); import("system.result"); result.string(AddressValidationType.get().TYPE_ZIP.key); \ No newline at end of file diff --git a/entity/AddressValidation_entity/recordcontainers/jdito/contentProcess.js b/entity/AddressValidation_entity/recordcontainers/jdito/contentProcess.js index 513b5ed0150..5b14acb7d11 100644 --- a/entity/AddressValidation_entity/recordcontainers/jdito/contentProcess.js +++ b/entity/AddressValidation_entity/recordcontainers/jdito/contentProcess.js @@ -1,5 +1,5 @@ import("system.result"); -import("AddressValidation_lib"); +import("WsValidation_lib"); import("system.vars"); var type = ""; @@ -20,6 +20,6 @@ if(sysFilter.filter != null) { if (!filter && vars.exists("$param.CurrentValue_param") && vars.get("$param.CurrentValue_param")) filter = vars.get("$param.CurrentValue_param"); -var foundAddresses = AddressValidationUtils.validate(filter, type, country); +var foundAddresses = WsValidationUtils.validate(filter, type, country); result.object(foundAddresses); \ No newline at end of file diff --git a/entity/Address_entity/entityfields/city/onValueChange.js b/entity/Address_entity/entityfields/city/onValueChange.js index 29bde9d4868..5634474c9da 100644 --- a/entity/Address_entity/entityfields/city/onValueChange.js +++ b/entity/Address_entity/entityfields/city/onValueChange.js @@ -1,3 +1,3 @@ -import("AddressValidation_lib"); +import("WsValidation_lib"); -AddressValidationUtils.setFields(); \ No newline at end of file +WsValidationUtils.setAddressFields(); \ No newline at end of file diff --git a/entity/Address_entity/entityfields/zip/onValueChange.js b/entity/Address_entity/entityfields/zip/onValueChange.js index 29bde9d4868..5634474c9da 100644 --- a/entity/Address_entity/entityfields/zip/onValueChange.js +++ b/entity/Address_entity/entityfields/zip/onValueChange.js @@ -1,3 +1,3 @@ -import("AddressValidation_lib"); +import("WsValidation_lib"); -AddressValidationUtils.setFields(); \ No newline at end of file +WsValidationUtils.setAddressFields(); \ No newline at end of file diff --git a/entity/Communication_entity/Communication_entity.aod b/entity/Communication_entity/Communication_entity.aod index dc93aa2b68f..63d8b0811f3 100644 --- a/entity/Communication_entity/Communication_entity.aod +++ b/entity/Communication_entity/Communication_entity.aod @@ -208,15 +208,6 @@ Usually this is used for filtering COMMUNICATION-entries by a specified contact <name>DATE_EDIT</name> <valueProcess>%aditoprj%/entity/Communication_entity/entityfields/date_edit/valueProcess.js</valueProcess> </entityField> - <entityConsumer> - <name>AddressValidator</name> - <fieldType>DEPENDENCY_OUT</fieldType> - <dependency> - <name>dependency</name> - <entityName>AddressValidation_entity</entityName> - <fieldName>ZipValidaton</fieldName> - </dependency> - </entityConsumer> </entityFields> <recordContainers> <dbRecordContainer> diff --git a/entity/Communication_entity/entityfields/addr/onValidation.js b/entity/Communication_entity/entityfields/addr/onValidation.js index 54a9148c82d..be9753504a1 100644 --- a/entity/Communication_entity/entityfields/addr/onValidation.js +++ b/entity/Communication_entity/entityfields/addr/onValidation.js @@ -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.string(res); + result.object(res); } \ No newline at end of file diff --git a/entity/Communication_entity/entityfields/addr/onValueChange.js b/entity/Communication_entity/entityfields/addr/onValueChange.js index 61a183ab8b7..95fae13ee02 100644 --- a/entity/Communication_entity/entityfields/addr/onValueChange.js +++ b/entity/Communication_entity/entityfields/addr/onValueChange.js @@ -1,12 +1,8 @@ -import("system.util"); -import("system.net"); -import("system.vars"); -import("Keyword_lib"); -import("KeywordRegistry_basic"); +import("WsValidation_lib"); import("system.neon"); -import("PostalAddress_lib"); - -// TODO: put into AddressValidation_lib +import("KeywordRegistry_basic"); +import("Keyword_lib"); +import("system.vars"); var addr = vars.get("$this.value"); @@ -19,20 +15,9 @@ if (addr) switch (commCategory) { case "TELEPHONE": // Phone - if(addr.length > 5) // https://services.aditosoftware.local/services/rest/ws_checkPhoneNumber?Number=017666862716&Country=DE + if(addr.length > 5) { - var url = "https://services.aditosoftware.local/services/rest/ws_checkPhoneNumber"; - var actionType = "GET"; - - var ret = JSON.parse(net.callRestWebserviceBasicAuth(url, actionType, {Number:addr, Country:vars.get("$param.ContactsMainCountry_param")}, null, null, "text/plain", "text/plain", util.DATA_TEXT, util.DATA_TEXT, "Admin", "a", true)); - if (ret.hasHttpSuccessStatusCode) - { - neon.setFieldValue("$field.ADDR", ret.body); - } - else - { - // error handling - } + neon.setFieldValue("$field.ADDR", WsValidationUtils.validate(addr, WsValidationType.get().TYPE_PHONE, vars.get("$param.ContactsMainCountry_param"))); } break; default: diff --git a/neonView/AddressList_view/AddressList_view.aod b/neonView/AddressList_view/AddressList_view.aod index 3ef2a83f526..74236c8433e 100644 --- a/neonView/AddressList_view/AddressList_view.aod +++ b/neonView/AddressList_view/AddressList_view.aod @@ -22,14 +22,6 @@ <name>dfaa01a1-0b11-4f92-aa18-89f12ebb2db5</name> <entityField>COUNTRY</entityField> </neonTableColumn> - <neonTableColumn> - <name>4e5e6a4f-f43e-4861-a912-fb05e576f960</name> - <entityField>ADDRESS</entityField> - </neonTableColumn> - <neonTableColumn> - <name>935bcecf-fdd9-4e82-a53f-3c3082c85340</name> - <entityField>BUILDINGNO</entityField> - </neonTableColumn> <neonTableColumn> <name>0336ea99-c4a1-4f39-b5aa-a520a7dcbaab</name> <entityField>ZIP</entityField> @@ -38,10 +30,6 @@ <name>12727b21-0359-4430-a9c2-54eb48e2e864</name> <entityField>CITY</entityField> </neonTableColumn> - <neonTableColumn> - <name>af515eed-da89-49dc-ba3a-30958bd4fedd</name> - <entityField>DISTRICT</entityField> - </neonTableColumn> <neonTableColumn> <name>29dbb3f5-6f80-4100-aed3-334ac0b9cc48</name> <entityField>REGION</entityField> @@ -50,6 +38,14 @@ <name>aafc58cf-7dbb-42cb-b04c-d0869e58d09a</name> <entityField>STATE</entityField> </neonTableColumn> + <neonTableColumn> + <name>4e5e6a4f-f43e-4861-a912-fb05e576f960</name> + <entityField>ADDRESS</entityField> + </neonTableColumn> + <neonTableColumn> + <name>935bcecf-fdd9-4e82-a53f-3c3082c85340</name> + <entityField>BUILDINGNO</entityField> + </neonTableColumn> <neonTableColumn> <name>6af52273-25bf-4286-83cc-217aea94ad09</name> <entityField>ADDRIDENTIFIER</entityField> diff --git a/neonView/AddressValidationLookup_view/AddressValidationLookup_view.aod b/neonView/AddressValidationLookup_view/AddressValidationLookup_view.aod index a0bf44af7ce..faa59a3cf3f 100644 --- a/neonView/AddressValidationLookup_view/AddressValidationLookup_view.aod +++ b/neonView/AddressValidationLookup_view/AddressValidationLookup_view.aod @@ -24,10 +24,6 @@ <name>d32d92bf-db7d-443f-b911-6731ea89cf3b</name> <entityField>COUNTRY</entityField> </neonTableColumn> - <neonTableColumn> - <name>4035d833-ece7-4562-8acf-51c7a01b8497</name> - <entityField>DISTRICT</entityField> - </neonTableColumn> <neonTableColumn> <name>05bb9d3b-0e90-4a1a-bfe8-03b7dab6ed01</name> <entityField>REGION</entityField> diff --git a/neonView/AdressMultiEdit_view/AdressMultiEdit_view.aod b/neonView/AdressMultiEdit_view/AdressMultiEdit_view.aod index 952afaabcfe..7ff9546ade2 100644 --- a/neonView/AdressMultiEdit_view/AdressMultiEdit_view.aod +++ b/neonView/AdressMultiEdit_view/AdressMultiEdit_view.aod @@ -20,14 +20,6 @@ <name>435f10b8-b6ef-4187-bffc-fc6104a6130c</name> <entityField>COUNTRY</entityField> </neonTableColumn> - <neonTableColumn> - <name>ac71d5eb-7ce7-41a0-91aa-5594f2238788</name> - <entityField>ADDRESS</entityField> - </neonTableColumn> - <neonTableColumn> - <name>61261845-740e-4f4b-a1f8-5e6aeac2deba</name> - <entityField>BUILDINGNO</entityField> - </neonTableColumn> <neonTableColumn> <name>c1372a93-cf83-4c55-90e6-3b6b352abebf</name> <entityField>ZIP</entityField> @@ -36,10 +28,6 @@ <name>2e7ba632-36ad-4852-bcca-d75bcf3f7de1</name> <entityField>CITY</entityField> </neonTableColumn> - <neonTableColumn> - <name>e25f4140-0772-4417-85f0-41052b413c30</name> - <entityField>DISTRICT</entityField> - </neonTableColumn> <neonTableColumn> <name>b62515bf-e89d-44a9-94ff-933766657fe5</name> <entityField>REGION</entityField> @@ -48,6 +36,14 @@ <name>66ae75ca-7a5c-4cf0-b768-6c2900f7b7c6</name> <entityField>STATE</entityField> </neonTableColumn> + <neonTableColumn> + <name>ac71d5eb-7ce7-41a0-91aa-5594f2238788</name> + <entityField>ADDRESS</entityField> + </neonTableColumn> + <neonTableColumn> + <name>61261845-740e-4f4b-a1f8-5e6aeac2deba</name> + <entityField>BUILDINGNO</entityField> + </neonTableColumn> <neonTableColumn> <name>443b9d7d-dac2-40b2-bb2b-d5e0091877a9</name> <entityField>ADDRIDENTIFIER</entityField> diff --git a/preferences/_____PREFERENCES_PROJECT/_____PREFERENCES_PROJECT.aod b/preferences/_____PREFERENCES_PROJECT/_____PREFERENCES_PROJECT.aod index 50084ef3280..19af7688962 100644 --- a/preferences/_____PREFERENCES_PROJECT/_____PREFERENCES_PROJECT.aod +++ b/preferences/_____PREFERENCES_PROJECT/_____PREFERENCES_PROJECT.aod @@ -46,5 +46,25 @@ <name>addressValidation.pw</name> <property>a</property> </customStringProperty> + <customStringProperty> + <name>addressValidation.countryParamName</name> + <property>country</property> + </customStringProperty> + <customStringProperty> + <name>phoneValidation.url</name> + <property>https://services.aditosoftware.local/services/rest/ws_checkPhoneNumber</property> + </customStringProperty> + <customStringProperty> + <name>phoneValidation.user</name> + <property>Admin</property> + </customStringProperty> + <customStringProperty> + <name>phoneValidation.pw</name> + <property>a</property> + </customStringProperty> + <customStringProperty> + <name>phoneValidation.countryParamName</name> + <property>Country</property> + </customStringProperty> </customProperties> </preferences> diff --git a/process/AddressValidation_lib/process.js b/process/AddressValidation_lib/process.js deleted file mode 100644 index 9f0ab520f0b..00000000000 --- a/process/AddressValidation_lib/process.js +++ /dev/null @@ -1,141 +0,0 @@ -import("system.logging"); -import("system.project"); -import("system.vars"); -import("system.neon"); -import("system.net"); -import("system.util"); - -/** - * Class used for the address validation types. - * Do not create new instances anywhere else than inside of the get-function! - * @param {String} pKey the key-name - * @param {String} pParamName name used by the webservice - */ -function AddressValidationType(pKey, pParamName) -{ - this.key = pKey; - this.paramName = pParamName; -} - -/** - * get all possible validation types or one specific - * @param {String} [pKey=undefined] if provided, this function returns only the validation type with this key - * - * @return {Object|AddressValidationType} object containing all AddressValidationTypes or one specific AddressValidationType - */ -AddressValidationType.get = function(pKey) -{ - if (!this._cache) - this._cache = { - TYPE_ZIP: new AddressValidationType("TYPE_ZIP", "zip"), - TYPE_CITY: new AddressValidationType("TYPE_CITY", "city") - } - - if (pKey) - return this._cache[pKey]; - - return this._cache; -} - -/** - * Class containing utility functions for address validation - * do not create an instance of this - * - * @class - */ -function AddressValidationUtils() {} - -/** - * validate the value with the webservice - * @param {String} pValue to search for - * @param {AddressValidationType|String} pType type used for the search. Can be an AddressValidationType or the key of the AddressValidationType - * @param {String} pCountry needed by the webservice e.g. "DE" - * - * @return {String[][]} [UID, - * displayName, - * zip, - * city, - * country, - * district, - * region, - * state, - * value (the result. e.g. if pType was ZIP, the value is the found zip) - */ -AddressValidationUtils.validate = function(pValue, pType, pCountry) -{ - var userName = project.getPreferenceValue("custom.addressValidation.user"); - var pw = project.getPreferenceValue("custom.addressValidation.pw"); - var url = project.getPreferenceValue("custom.addressValidation.url"); - var actionType = "GET"; - - // get AddressValidationType-Object if it is only the key - if (typeof pType == "string") - pType = AddressValidationType.get(pType); - - if (pValue && pType) - { - // fill params - var parameters = {}; - if (pCountry) - { - parameters.country = 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) - { - // add default result consisting of pValue - var defaultResult = [[{}, pValue]]; - defaultResult[0][0][pType.paramName] = pValue; - - return JSON.parse(ret.body) - .concat(defaultResult) - .map(function(pAddress) - { - var data = pAddress[0]; - return [ - JSON.stringify(data), - pAddress[1], - data.zip, - data.city, - data.country, - data.district, - data.region, - data.state, - data[pType.paramName] - ] - }); - } - else - { - - // error handling - } - } - - return []; -} - -/** - * set all address-fields by the value of $this.value, which should contain an object of all values as JSON - */ -AddressValidationUtils.setFields = function() -{ - var data = JSON.parse(vars.get("$this.value")); - - _setField("$field.ZIP", data.zip); - _setField("$field.COUNTRY", data.country); - _setField("$field.CITY", data.city); - _setField("$field.DISTRICT", data.district); - _setField("$field.REGION", data.region); - _setField("$field.STATE", data.state); - - function _setField(pField, pValue) - { - if (pValue) - neon.setFieldValue(pField, pValue); - } -} \ No newline at end of file diff --git a/process/AddressValidation_lib/AddressValidation_lib.aod b/process/WsValidation_lib/WsValidation_lib.aod similarity index 76% rename from process/AddressValidation_lib/AddressValidation_lib.aod rename to process/WsValidation_lib/WsValidation_lib.aod index d9b312c9ea0..521ca5a4571 100644 --- a/process/AddressValidation_lib/AddressValidation_lib.aod +++ b/process/WsValidation_lib/WsValidation_lib.aod @@ -1,8 +1,8 @@ <?xml version="1.0" encoding="UTF-8"?> <process xmlns="http://www.adito.de/2018/ao/Model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" VERSION="1.2.1" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/process/1.2.1"> - <name>AddressValidation_lib</name> + <name>WsValidation_lib</name> <majorModelMode>DISTRIBUTED</majorModelMode> - <process>%aditoprj%/process/AddressValidation_lib/process.js</process> + <process>%aditoprj%/process/WsValidation_lib/process.js</process> <variants> <element>LIBRARY</element> </variants> diff --git a/process/WsValidation_lib/process.js b/process/WsValidation_lib/process.js new file mode 100644 index 00000000000..4cc2f2effeb --- /dev/null +++ b/process/WsValidation_lib/process.js @@ -0,0 +1,164 @@ +import("system.project"); +import("system.vars"); +import("system.neon"); +import("system.net"); +import("system.util"); + +/** + * Class used for the webservice validation types. + * + * Do not create new instances anywhere else than inside of the get-function! + * + * @param {String} pKey the key-name + * @param {String} pParamName name used by the webservice + * @param {String} pWebserviceName name of the webservice. Used to find the correct webservice configuraation + * @param {Function} pProcessResultCallback function for processing Webservice-result to the return value + */ +function WsValidationType(pKey, pParamName, pWebserviceName, pProcessResultCallback) +{ + this.key = pKey; + this.paramName = pParamName; + this.processResultCallback = pProcessResultCallback; + this.webserviceName = pWebserviceName; +} + +/** + * get all possible validation types or one specific + * @param {String} [pKey=undefined] if provided, this function returns only the validation type with this key + * + * @return {Object|WsValidationType} object containing all WsValidationTypes or one specific WsValidationType + */ +WsValidationType.get = function(pKey) +{ + if (!this._cache) + this._cache = { + // Address lookups + TYPE_ZIP: new WsValidationType("TYPE_ZIP", "zip", "addressValidation", _processAddressLookup), + TYPE_CITY: new WsValidationType("TYPE_CITY", "city", "addressValidation", _processAddressLookup), + + // Communication validation + TYPE_PHONE: new WsValidationType("TYPE_PHONE", "Number", "phoneValidation", _processCommunicationValidation) + } + + if (pKey) + return this._cache[pKey]; + + return this._cache; + + 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 data = pAddress[0]; + return [ + JSON.stringify(data), + pAddress[1], + data.zip, + data.city, + data.country, + data.district, + data.region, + data.state, + data[this.paramName]] + }) + } + + function _processCommunicationValidation(pWsResult, pValue) + { + return pWsResult; + } +} + +/** + * Class containing utility functions for validation webservices + * It can be used for lookups (like AddressValidation-entity) or onValueChange-formatting/validation like the Communication.ADDR-field. + * do not create an instance of this + * + * @class + */ +function WsValidationUtils() {} + +/** + * validate the value with the webservice + * @param {String} pValue to search for + * @param {WsValidationType|String} pType type used for the search. Can be an WsValidationType or the key of the WsValidationType + * @param {String} pCountry needed by the webservice e.g. "DE" + * + * @return {String[][]} [UID, + * displayName, + * zip, + * city, + * country, + * district, + * region, + * state, + * value (the result. e.g. if pType was ZIP, the value is the found zip) + */ +WsValidationUtils.validate = function(pValue, pType, pCountry) +{ + // get WsValidationType-Object if it is only the key + 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) + { + // 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) + { + // different handling of the result per type + return pType.processResultCallback(ret.body, pValue); + } + else + { + + // error handling + } + } + + return []; +} + +/** + * set all address-fields by the value of $this.value, which should contain an object of all values as JSON + */ +WsValidationUtils.setAddressFields = function() +{ + var data = JSON.parse(vars.get("$this.value")); + + _setField("$field.ZIP", data.zip); + _setField("$field.COUNTRY", data.country); + _setField("$field.CITY", data.city); + //_setField("$field.DISTRICT", data.district); not needed currently + _setField("$field.REGION", data.region); + _setField("$field.STATE", data.state); + + function _setField(pField, pValue) + { + if (pValue) + neon.setFieldValue(pField, pValue); + } +} \ No newline at end of file -- GitLab