From 2b1e861295bb11b49d32267f5c74bc443a09f55d Mon Sep 17 00:00:00 2001
From: "j.goderbauer" <j.goderbauer@adito.de>
Date: Thu, 20 Dec 2018 10:26:50 +0100
Subject: [PATCH] [Projekt: Entwicklung - Neon][TicketNr.: 1029627][Preview
 Kontakt: Standard-Kommunikationsadresse]

---
 entity/Org_entity/Org_entity.aod              |  5 ++
 .../entityfields/image/onValueChange.js       | 12 +---
 .../standard_email_comm/onValueChange.js      | 12 +---
 .../standard_phone_comm/onValueChange.js      | 12 +---
 .../recordcontainers/db/onDBUpdate.js         | 65 ++++---------------
 process/Comm_lib/process.js                   | 21 +++---
 process/Entity_lib/process.js                 | 58 ++++++++++++++++-
 7 files changed, 95 insertions(+), 90 deletions(-)

diff --git a/entity/Org_entity/Org_entity.aod b/entity/Org_entity/Org_entity.aod
index d577deb94b..034ec431fc 100644
--- a/entity/Org_entity/Org_entity.aod
+++ b/entity/Org_entity/Org_entity.aod
@@ -378,6 +378,11 @@
       <title>standard email</title>
       <outgoingField>OrgCommEmail_dfo</outgoingField>
       <onValueChange>%aditoprj%/entity/Org_entity/entityfields/standard_email_comm/onValueChange.js</onValueChange>
+      <onValueChangeTypes>
+        <element>MASK</element>
+        <element>PROCESS</element>
+        <element>RECORD</element>
+      </onValueChangeTypes>
     </entityField>
     <entityField>
       <name>STANDARD_PHONE_COMM</name>
diff --git a/entity/Org_entity/entityfields/image/onValueChange.js b/entity/Org_entity/entityfields/image/onValueChange.js
index 368310478f..f7fffb124f 100644
--- a/entity/Org_entity/entityfields/image/onValueChange.js
+++ b/entity/Org_entity/entityfields/image/onValueChange.js
@@ -1,12 +1,4 @@
-import("system.vars");
-import("system.neon");
+import("Entity_lib");
 
 // TODO: also there is currently no good way to do updates with fields not connected to the record container. Workaround: imagevariable and update in onDBUpdate Process
-if (vars.get("$field.IMAGE"))
-{
-    vars.set("$image.changedImage", true);
-}
-else
-{
-    vars.set("$image.changedImage", "deleted");
-}
+FieldChanges.setChange("$field.IMAGE");
\ No newline at end of file
diff --git a/entity/Org_entity/entityfields/standard_email_comm/onValueChange.js b/entity/Org_entity/entityfields/standard_email_comm/onValueChange.js
index 99ef5ee31e..4572302a6f 100644
--- a/entity/Org_entity/entityfields/standard_email_comm/onValueChange.js
+++ b/entity/Org_entity/entityfields/standard_email_comm/onValueChange.js
@@ -1,12 +1,4 @@
-import("system.vars");
-import("system.neon");
+import("Entity_lib");
 
 // TODO: also there is currently no good way to do updates with fields not connected to the record container. Workaround: imagevariable and update in onDBUpdate Process
-if (vars.get("$field.STANDARD_EMAIL_COMM"))
-{
-    vars.set("$image.changedStandardEmail", true);
-}
-else
-{
-    vars.set("$image.changedStandardEmail", "deleted");
-}
+FieldChanges.setChange("$field.STANDARD_EMAIL_COMM");
\ No newline at end of file
diff --git a/entity/Org_entity/entityfields/standard_phone_comm/onValueChange.js b/entity/Org_entity/entityfields/standard_phone_comm/onValueChange.js
index f62e387af1..fc6615da8d 100644
--- a/entity/Org_entity/entityfields/standard_phone_comm/onValueChange.js
+++ b/entity/Org_entity/entityfields/standard_phone_comm/onValueChange.js
@@ -1,12 +1,4 @@
-import("system.vars");
-import("system.neon");
+import("Entity_lib");
 
 // TODO: also there is currently no good way to do updates with fields not connected to the record container. Workaround: imagevariable and update in onDBUpdate Process
-if (vars.get("$field.STANDARD_PHONE_COMM"))
-{
-    vars.set("$image.changedStandardPhone", true);
-}
-else
-{
-    vars.set("$image.changedStandardPhone", "deleted");
-}
+FieldChanges.setChange("$field.STANDARD_PHONE_COMM");
\ No newline at end of file
diff --git a/entity/Org_entity/recordcontainers/db/onDBUpdate.js b/entity/Org_entity/recordcontainers/db/onDBUpdate.js
index 62f1267913..b0047f43f0 100644
--- a/entity/Org_entity/recordcontainers/db/onDBUpdate.js
+++ b/entity/Org_entity/recordcontainers/db/onDBUpdate.js
@@ -1,59 +1,22 @@
 import("system.vars");
 import("Org_lib");
 import("Comm_lib");
+import("Entity_lib");
 
 // TODO: this is a workaround for missing possibility to react on changes of fields not connected to record Contqainer
-if (vars.exists("$image.changedImage"))
-{
-    if (vars.get("$image.changedImage"))
-    {
-        var imageData = vars.getString("$field.IMAGE");
-        if (vars.get("$image.changedImage") != "deleted")
-        {
-            OrgUtils.setImage(vars.get("$field.ORGID"), imageData);
-        }
-        else
-        {
-            OrgUtils.removeImage(vars.get("$field.ORGID"));
-        }
-    }
-}
+FieldChanges.assimilateChangeAndDispose("$field.IMAGE", function(state, value){
+    if (state == FieldChanges.STATE_CHANGED())
+        OrgUtils.setImage(vars.get("$field.ORGID"), value);
+    else
+        OrgUtils.removeImage(vars.get("$field.ORGID"));
+});
 
-var uid = vars.get("$sys.uid");
-
-vars.set("$image.changedImage", false);
-
-if (vars.exists("$image.changedStandardEmail"))
-{
-    if (vars.get("$image.changedStandardEmail"))
-    {
-        if (vars.get("$image.changedStandardEmail") != "deleted")
-        {
-            CommUtil.setStandardMail(uid, vars.get("$field.STANDARD_EMAIL_COMM"));
-        }
-        else
-        {
-            //TODO: implement
-        }
-    }
-}
 
-vars.set("$image.changedStandardEmail", false);
-
-
-if (vars.exists("$image.changedStandardPhone"))
-{
-    if (vars.get("$image.changedStandardPhone"))
-    {
-        if (vars.get("$image.changedStandardPhone") != "deleted")
-        {
-            CommUtil.setStandardMail(uid, vars.get("$field.STANDARD_PHONE_COMM"));
-        }
-        else
-        {
-            //TODO: implement
-        }
-    }
-}
+var uid = vars.get("$sys.uid");
+FieldChanges.assimilateChangeAndDispose("$field.STANDARD_EMAIL_COMM", function(state, value){
+    CommUtil.setStandardMail(uid, value);
+});
 
-vars.set("$image.changedStandardPhone", false);
+FieldChanges.assimilateChangeAndDispose("$field.STANDARD_PHONE_COMM", function(state, value){
+    CommUtil.setStandardPhone(uid, value);
+});
diff --git a/process/Comm_lib/process.js b/process/Comm_lib/process.js
index 18cc2d1ab7..854e3aaace 100644
--- a/process/Comm_lib/process.js
+++ b/process/Comm_lib/process.js
@@ -34,13 +34,14 @@ CommUtil.getMediumIdsByCategory = function (pCategory)
  * sets the standard address for COMM entries for a specific category;
  * does not verify if the COMMID you provide has the given category
  * @param {String} pAffectedRowId a refencial ID whoes COMMs should be modified (a RELATIONID)
- * @param {String} pNewStandardCommId COMMID of the new STANDARD comm-entry
+ * @param {String} pNewStandardCommId COMMID of the new STANDARD comm-entry; can be an empty string if no new standard adress shall be set but the old standard removed
  * @param {String} pCategory value of the keyword "COMM.MEDIUM" custom.category; e.g. "PHONE"; the STANDARD of this category is set
  * @return {null} currently returns always null; reserved for future
  */
 CommUtil.setStandardForCategory = function(pAffectedRowId, pNewStandardCommId, pCategory)
 {
-    if (!pAffectedRowId || !pNewStandardCommId || !pCategory)
+    //pNewStandardCommId can be an empty string if the standard has to be removed
+    if (!pAffectedRowId || pNewStandardCommId == undefined || !pCategory)
         return null;
     var mediumIds = CommUtil.getMediumIdsByCategory(pCategory);
     if (mediumIds.length == 0)//none found? since mediumIds affects the update later there is no reason to continue
@@ -58,12 +59,16 @@ CommUtil.setStandardForCategory = function(pAffectedRowId, pNewStandardCommId, p
     cond.and("MEDIUM_ID in (" + mediumIds.join(", ") + ")");
     statements.push(["COMM", cols, types, ["0", timestamp, login], cond.build()]);
     
-    //set the new standard comm-record
-    cond.clear();
-    //check commid, relId and medium to prevent data-inconsistency when bad function params are passed by (e.g commid of a different category)
-    cond.andPrepare("COMM.COMMID", pNewStandardCommId).andPrepare("COMM.RELATION_ID", pAffectedRowId);
-    cond.and("MEDIUM_ID in (" + mediumIds.join(", ") + ")");
-    statements.push(["COMM", cols, types, ["1", timestamp, login], cond.build()]);
+    //pNewStandardCommId can be an empty string if the standard has to only be removed
+    if (pNewStandardCommId != "")
+    {
+        //set the new standard comm-record
+        cond.clear();
+        //check commid, relId and medium to prevent data-inconsistency when bad function params are passed by (e.g commid of a different category)
+        cond.andPrepare("COMM.COMMID", pNewStandardCommId).andPrepare("COMM.RELATION_ID", pAffectedRowId);
+        cond.and("MEDIUM_ID in (" + mediumIds.join(", ") + ")");
+        statements.push(["COMM", cols, types, ["1", timestamp, login], cond.build()]);
+    }
     
     count = db.updates(statements);
     return null;
diff --git a/process/Entity_lib/process.js b/process/Entity_lib/process.js
index d807207b4b..591abc0ab9 100644
--- a/process/Entity_lib/process.js
+++ b/process/Entity_lib/process.js
@@ -51,4 +51,60 @@ ProcessHandlingUtils.initialParamToResult = function(pParamVarName)
         }
     }
     
-}
\ No newline at end of file
+}
+
+//TODO: comment
+function FieldChanges(){}
+
+FieldChanges.STATE_NO_CHANGE = function (){
+    return "NO_CHANGE"
+    };
+FieldChanges.STATE_CHANGED = function (){
+    return "CHANGED"
+    };
+FieldChanges.STATE_DELETED = function (){
+    return "DELETED"
+    };
+
+FieldChanges.assimilateChangeAndDispose = function (pFieldName, pAssimilatorFn)
+{
+    var allChanges = FieldChanges._getStorage();
+    if (allChanges[pFieldName] == undefined)
+        return null;
+
+    var res = null;
+    if (allChanges[pFieldName].state != FieldChanges.STATE_NO_CHANGE())
+    {
+        res = pAssimilatorFn.call(this, allChanges[pFieldName].state, vars.get(pFieldName));
+        allChanges[pFieldName].state = FieldChanges.STATE_NO_CHANGE();
+        FieldChanges._setStorage(allChanges);
+    }
+    return res;
+};
+
+FieldChanges.setChange = function(pFieldName)
+{
+    var allChanges = FieldChanges._getStorage();
+    if (allChanges[pFieldName] == undefined)
+        allChanges[pFieldName] = {};
+    
+    var value = vars.get(pFieldName);
+    if (!value)
+        allChanges[pFieldName].state = FieldChanges.STATE_DELETED();
+    else
+        allChanges[pFieldName].state = FieldChanges.STATE_CHANGED();
+    
+    FieldChanges._setStorage(allChanges);
+};
+
+FieldChanges._getStorage = function()
+{
+    if (!vars.exists("$image.FieldChanges"))
+        return {};
+    return vars.get("$image.FieldChanges");
+};
+
+FieldChanges._setStorage = function(pAllChanges)
+{
+    return vars.set("$image.FieldChanges", pAllChanges);
+};
\ No newline at end of file
-- 
GitLab