From b4d05f6e87bb100ea78d9673017f3981c040ff3b Mon Sep 17 00:00:00 2001
From: "j.goderbauer" <j.goderbauer@adito.de>
Date: Thu, 31 Oct 2019 10:50:03 +0100
Subject: [PATCH] fix: address: state was always a mandatory field

---
 entity/Address_entity/Address_entity.aod                  | 1 +
 .../entityfields/address/mandatoryProcess.js              | 3 ++-
 .../entityfields/address_ws/mandatoryProcess.js           | 3 ++-
 .../Address_entity/entityfields/city/mandatoryProcess.js  | 3 ++-
 .../entityfields/city_ws/mandatoryProcess.js              | 3 ++-
 .../entityfields/country/displayValueProcess.js           | 5 +++++
 .../Address_entity/entityfields/state/mandatoryProcess.js | 3 ++-
 .../Address_entity/entityfields/zip/mandatoryProcess.js   | 3 ++-
 .../entityfields/zip_ws/mandatoryProcess.js               | 3 ++-
 process/AddressEntity_lib/process.js                      | 7 +++++--
 process/Context_lib/process.js                            | 8 ++++++--
 process/PostalAddress_lib/process.js                      | 8 ++++----
 12 files changed, 35 insertions(+), 15 deletions(-)
 create mode 100644 entity/Address_entity/entityfields/country/displayValueProcess.js

diff --git a/entity/Address_entity/Address_entity.aod b/entity/Address_entity/Address_entity.aod
index 871854a526..8f105a3468 100644
--- a/entity/Address_entity/Address_entity.aod
+++ b/entity/Address_entity/Address_entity.aod
@@ -60,6 +60,7 @@
       <consumer>Countries</consumer>
       <mandatory v="true" />
       <valueProcess>%aditoprj%/entity/Address_entity/entityfields/country/valueProcess.js</valueProcess>
+      <displayValueProcess>%aditoprj%/entity/Address_entity/entityfields/country/displayValueProcess.js</displayValueProcess>
     </entityField>
     <entityField>
       <name>DISTRICT</name>
diff --git a/entity/Address_entity/entityfields/address/mandatoryProcess.js b/entity/Address_entity/entityfields/address/mandatoryProcess.js
index c8c795a308..c92fea5dd7 100644
--- a/entity/Address_entity/entityfields/address/mandatoryProcess.js
+++ b/entity/Address_entity/entityfields/address/mandatoryProcess.js
@@ -1,5 +1,6 @@
+import("system.vars");
 import("AddressEntity_lib");
 import("system.result");
 import("WsValidation_lib");
 
-result.string(WsValidationFieldUtils.mainFieldMandatoryProcess(WsValidationType.get().TYPE_STREET_NOMINATIM, AddressEntityValidation.isMandatoryField()));
\ No newline at end of file
+result.string(WsValidationFieldUtils.mainFieldMandatoryProcess(WsValidationType.get().TYPE_STREET_NOMINATIM, AddressEntityValidation.isMandatoryField(vars.get("$field.COUNTRY"))));
\ No newline at end of file
diff --git a/entity/Address_entity/entityfields/address_ws/mandatoryProcess.js b/entity/Address_entity/entityfields/address_ws/mandatoryProcess.js
index 3e9f416503..305a67d36c 100644
--- a/entity/Address_entity/entityfields/address_ws/mandatoryProcess.js
+++ b/entity/Address_entity/entityfields/address_ws/mandatoryProcess.js
@@ -1,5 +1,6 @@
+import("system.vars");
 import("AddressEntity_lib");
 import("WsValidation_lib");
 import("system.result");
 
-result.object(WsValidationFieldUtils.wsFieldMandatoryProcess(WsValidationType.get().TYPE_STREET_NOMINATIM, AddressEntityValidation.isMandatoryField("ADDRESS")));
\ No newline at end of file
+result.object(WsValidationFieldUtils.wsFieldMandatoryProcess(WsValidationType.get().TYPE_STREET_NOMINATIM, AddressEntityValidation.isMandatoryField(vars.get("$field.COUNTRY"), "ADDRESS")));
\ No newline at end of file
diff --git a/entity/Address_entity/entityfields/city/mandatoryProcess.js b/entity/Address_entity/entityfields/city/mandatoryProcess.js
index df67f6f85d..255d63dfb4 100644
--- a/entity/Address_entity/entityfields/city/mandatoryProcess.js
+++ b/entity/Address_entity/entityfields/city/mandatoryProcess.js
@@ -1,5 +1,6 @@
+import("system.vars");
 import("AddressEntity_lib");
 import("system.result");
 import("WsValidation_lib");
 
-result.string(WsValidationFieldUtils.mainFieldMandatoryProcess(WsValidationType.get().TYPE_CITY_NOMINATIM, AddressEntityValidation.isMandatoryField()));
\ No newline at end of file
+result.string(WsValidationFieldUtils.mainFieldMandatoryProcess(WsValidationType.get().TYPE_CITY_NOMINATIM, AddressEntityValidation.isMandatoryField(vars.get("$field.COUNTRY"))));
\ No newline at end of file
diff --git a/entity/Address_entity/entityfields/city_ws/mandatoryProcess.js b/entity/Address_entity/entityfields/city_ws/mandatoryProcess.js
index 7dba637e9d..866716ba42 100644
--- a/entity/Address_entity/entityfields/city_ws/mandatoryProcess.js
+++ b/entity/Address_entity/entityfields/city_ws/mandatoryProcess.js
@@ -1,5 +1,6 @@
+import("system.vars");
 import("AddressEntity_lib");
 import("WsValidation_lib");
 import("system.result");
 
-result.object(WsValidationFieldUtils.wsFieldMandatoryProcess(WsValidationType.get().TYPE_CITY_NOMINATIM, AddressEntityValidation.isMandatoryField("CITY")));
\ No newline at end of file
+result.object(WsValidationFieldUtils.wsFieldMandatoryProcess(WsValidationType.get().TYPE_CITY_NOMINATIM, AddressEntityValidation.isMandatoryField(vars.get("$field.COUNTRY"), "CITY")));
\ No newline at end of file
diff --git a/entity/Address_entity/entityfields/country/displayValueProcess.js b/entity/Address_entity/entityfields/country/displayValueProcess.js
new file mode 100644
index 0000000000..60deeeeaba
--- /dev/null
+++ b/entity/Address_entity/entityfields/country/displayValueProcess.js
@@ -0,0 +1,5 @@
+import("system.vars");
+import("system.result");
+import("Context_lib");
+
+result.string(ContextUtils.loadContentTitle("Countries_Entity", vars.get("$field.COUNTRY"), "ISO2Name"));
\ No newline at end of file
diff --git a/entity/Address_entity/entityfields/state/mandatoryProcess.js b/entity/Address_entity/entityfields/state/mandatoryProcess.js
index 7c898f0182..132a366dcb 100644
--- a/entity/Address_entity/entityfields/state/mandatoryProcess.js
+++ b/entity/Address_entity/entityfields/state/mandatoryProcess.js
@@ -1,5 +1,6 @@
+import("system.vars");
 import("Entity_lib");
 import("system.result");
 import("AddressEntity_lib");
 
-result.object(AddressEntityValidation.isMandatoryField());
\ No newline at end of file
+result.object(AddressEntityValidation.isMandatoryField(vars.get("$field.COUNTRY")));
\ No newline at end of file
diff --git a/entity/Address_entity/entityfields/zip/mandatoryProcess.js b/entity/Address_entity/entityfields/zip/mandatoryProcess.js
index 981aa78bf8..453956bca6 100644
--- a/entity/Address_entity/entityfields/zip/mandatoryProcess.js
+++ b/entity/Address_entity/entityfields/zip/mandatoryProcess.js
@@ -1,5 +1,6 @@
+import("system.vars");
 import("AddressEntity_lib");
 import("WsValidation_lib");
 import("system.result");
 
-result.object(WsValidationFieldUtils.mainFieldMandatoryProcess(WsValidationType.get().TYPE_ZIP_NOMINATIM, AddressEntityValidation.isMandatoryField()));
\ No newline at end of file
+result.object(WsValidationFieldUtils.mainFieldMandatoryProcess(WsValidationType.get().TYPE_ZIP_NOMINATIM, AddressEntityValidation.isMandatoryField(vars.get("$field.COUNTRY"))));
\ No newline at end of file
diff --git a/entity/Address_entity/entityfields/zip_ws/mandatoryProcess.js b/entity/Address_entity/entityfields/zip_ws/mandatoryProcess.js
index a2dfbb85c8..3146171358 100644
--- a/entity/Address_entity/entityfields/zip_ws/mandatoryProcess.js
+++ b/entity/Address_entity/entityfields/zip_ws/mandatoryProcess.js
@@ -1,5 +1,6 @@
+import("system.vars");
 import("AddressEntity_lib");
 import("WsValidation_lib");
 import("system.result");
 
-result.object(WsValidationFieldUtils.wsFieldMandatoryProcess(WsValidationType.get().TYPE_ZIP_NOMINATIM, AddressEntityValidation.isMandatoryField("ZIP")));
\ No newline at end of file
+result.object(WsValidationFieldUtils.wsFieldMandatoryProcess(WsValidationType.get().TYPE_ZIP_NOMINATIM, AddressEntityValidation.isMandatoryField(vars.get("$field.COUNTRY"), "ZIP")));
\ No newline at end of file
diff --git a/process/AddressEntity_lib/process.js b/process/AddressEntity_lib/process.js
index 61b8e5a06f..ffb472671a 100644
--- a/process/AddressEntity_lib/process.js
+++ b/process/AddressEntity_lib/process.js
@@ -14,10 +14,12 @@ function AddressEntityValidation(){}
 
 /**
  * check if the field, this function is called in is a mandatory field of the country
+ * 
+ * @param {String} pCountryCode iso code of the country of the field whoes standard values should be checked
  * @param {String} [pField=undefined] if provided use this field else use $this.name
  * @return {Boolean}
  */
-AddressEntityValidation.isMandatoryField = function(pField)
+AddressEntityValidation.isMandatoryField = function(pCountryCode, pField)
 {
     var fieldName;
     
@@ -25,7 +27,8 @@ AddressEntityValidation.isMandatoryField = function(pField)
         fieldName = pField;
     else
         fieldName = vars.get("$this.name").replace(/^\$?field\./, "");
-    var countryCode = vars.get("$field.COUNTRY");
+    //do not use vars.get of the country-field here since the process would no be triggered on a value change, instead pass it from the invoking code
+    var countryCode = pCountryCode; 
     var fieldCode = this.mapFieldToShorthand(fieldName);
     if (fieldCode == null)
         return false;
diff --git a/process/Context_lib/process.js b/process/Context_lib/process.js
index c652ab9089..7b435b4aab 100644
--- a/process/Context_lib/process.js
+++ b/process/Context_lib/process.js
@@ -95,21 +95,25 @@ ContextUtils.getContextConsumer = function(pContextId)
  * loads the contenttitle by using entities.getRow
  * @param {String} pEntity The entity name you want to load the title for
  * @param {String} pUid the uid for which to load the title
+ * @param {String} [pProvider] providername when instead of the default provider another provider shall be used
  * 
  * @return the #CONTENTTITLE or ""
  */
-ContextUtils.loadContentTitle = function(pEntity, pUid)
+ContextUtils.loadContentTitle = function(pEntity, pUid, pProvider)
 {    
     if (!pUid)
     {
         return "";
     }
-    
+
     var conf = entities.createConfigForLoadingRows()
                         .entity(pEntity)
                         .uid(pUid)
                         .fields(["#CONTENTTITLE"]);
 
+    if (pProvider)
+        conf.provider(pProvider);
+
     if (entities.getRowCount(conf) > 0)
     {
         return entities.getRow(conf)["#CONTENTTITLE"];
diff --git a/process/PostalAddress_lib/process.js b/process/PostalAddress_lib/process.js
index 9e2a28bb1c..83d7bfde4c 100644
--- a/process/PostalAddress_lib/process.js
+++ b/process/PostalAddress_lib/process.js
@@ -222,15 +222,15 @@ AddressValidationUtils._getRequiredFields = function(countryCode)
 /**
  * check if the requested field is a mandatory field
  * 
- * @param {String} countryCode
- * @param {String} fieldCode
+ * @param {String} countryCode iso code of the country
+ * @param {String} fieldCode shortform code of the field that shalle be checked; e.g. "S" for STATE
  * 
- * @return {Boolean}
+ * @return {Boolean} if the given field is a mandatory field for the specified country; if there is no definition the field is NOT mandatory
  */
 AddressValidationUtils.isMandatoryField = function(countryCode, fieldCode)
 {
     var requiredFields = this._getRequiredFields(countryCode);
-    return requiredFields == "" || requiredFields.search(fieldCode) != -1;
+    return requiredFields != "" && requiredFields.search(fieldCode) != -1;
 };
 
 /**
-- 
GitLab