From dcac4aa4ec8380a89c0470ce0ccf4eba2903740e Mon Sep 17 00:00:00 2001
From: Sascha Schmidt <s.schmidt@adito.de>
Date: Fri, 22 Oct 2021 13:31:37 +0200
Subject: [PATCH] [Projekt: xRM-ContactManagement][TicketNr.:
 2001249][Kopfanrede in Kontakt wird falsch angezeigt]

---
 entity/Person_entity/Person_entity.aod        |  7 +++
 entity/Person_entity/contentTitleProcess.js   | 14 +-----
 .../full_name_fieldgroup/valueProcess.js      | 13 +----
 .../contenttitle.value/expression.js          |  4 ++
 process/Contact_lib/process.js                | 47 +++++++++++++++++++
 5 files changed, 60 insertions(+), 25 deletions(-)
 create mode 100644 entity/Person_entity/recordcontainers/db/recordfieldmappings/contenttitle.value/expression.js

diff --git a/entity/Person_entity/Person_entity.aod b/entity/Person_entity/Person_entity.aod
index 9d13160611..b28b37030c 100644
--- a/entity/Person_entity/Person_entity.aod
+++ b/entity/Person_entity/Person_entity.aod
@@ -1389,6 +1389,9 @@
       <name>LETTERSALUTATION</name>
       <title>Lettersalutation</title>
     </entityField>
+    <entityField>
+      <name>contenttitle</name>
+    </entityField>
   </entityFields>
   <recordContainers>
     <dbRecordContainer>
@@ -1697,6 +1700,10 @@
           <name>LETTERSALUTATION.value</name>
           <recordfield>CONTACT.LETTERSALUTATION</recordfield>
         </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>contenttitle.value</name>
+          <expression>%aditoprj%/entity/Person_entity/recordcontainers/db/recordfieldmappings/contenttitle.value/expression.js</expression>
+        </dbRecordFieldMapping>
       </recordFieldMappings>
       <linkInformation>
         <linkInformation>
diff --git a/entity/Person_entity/contentTitleProcess.js b/entity/Person_entity/contentTitleProcess.js
index 68305e82bf..4377860159 100644
--- a/entity/Person_entity/contentTitleProcess.js
+++ b/entity/Person_entity/contentTitleProcess.js
@@ -1,16 +1,4 @@
 import("system.vars");
 import("system.result");
-import("Util_lib");
-import("Contact_lib");
 
-//do not use "FULL_NAME_fieldGroup" here since the field group must not include the orgname
-var contact = new Contact();
-contact.salutation = vars.get("$field.SALUTATION");
-contact.title = vars.get("$field.TITLE");
-contact.firstname = vars.get("$field.FIRSTNAME");
-contact.middlename = vars.get("$field.MIDDLENAME");
-contact.lastname = vars.get("$field.LASTNAME");
-contact.organisationName = vars.get("$field.ORGANISATION_NAME");
-
-var renderer = new ContactTitleRenderer(contact, null);
-result.string(renderer.asString());
\ No newline at end of file
+result.string(vars.get("$field.contenttitle"));
\ No newline at end of file
diff --git a/entity/Person_entity/entityfields/full_name_fieldgroup/valueProcess.js b/entity/Person_entity/entityfields/full_name_fieldgroup/valueProcess.js
index 778c203ff1..07e52e7b7e 100644
--- a/entity/Person_entity/entityfields/full_name_fieldgroup/valueProcess.js
+++ b/entity/Person_entity/entityfields/full_name_fieldgroup/valueProcess.js
@@ -1,15 +1,4 @@
 import("system.vars");
 import("system.result");
-import("Util_lib");
-import("Contact_lib");
 
-//no orgname here since the org-field is in the card-template as separate field
-var contact = new Contact();
-contact.salutation = vars.get("$field.SALUTATION");
-contact.title = vars.get("$field.TITLE");
-contact.firstname = vars.get("$field.FIRSTNAME");
-contact.middlename = vars.get("$field.MIDDLENAME");
-contact.lastname = vars.get("$field.LASTNAME");
-
-var renderer = new ContactTitleRenderer(contact, null);
-result.string(renderer.asString());
\ No newline at end of file
+result.string(vars.get("$field.contenttitle"));
\ No newline at end of file
diff --git a/entity/Person_entity/recordcontainers/db/recordfieldmappings/contenttitle.value/expression.js b/entity/Person_entity/recordcontainers/db/recordfieldmappings/contenttitle.value/expression.js
new file mode 100644
index 0000000000..89a6fc8ad6
--- /dev/null
+++ b/entity/Person_entity/recordcontainers/db/recordfieldmappings/contenttitle.value/expression.js
@@ -0,0 +1,4 @@
+import("Contact_lib");
+import("system.result");
+
+result.string(ContactUtils.getContactSalutationSubSql(true));
\ No newline at end of file
diff --git a/process/Contact_lib/process.js b/process/Contact_lib/process.js
index ef75d00d32..e62bcfe34e 100644
--- a/process/Contact_lib/process.js
+++ b/process/Contact_lib/process.js
@@ -798,6 +798,53 @@ ContactUtils.isDeletable = function (pCurrentContext, pContactId, pPersonId)
         .validate();
 }
 
+/**
+ * Gives the contenttitle in right order from salutaion as SubSQL
+ * 
+ * @param {Boolean} pGetHeadline if you want SALUTATION.HEADLINE as COLUMN
+ * @param {Boolean} pGetLetterSalutation if you want SALUTATION.LETTERSALUTATION as COLUMN
+ * 
+ * @return {String} Subsql for DB.expression
+ */
+ContactUtils.getContactSalutationSubSql = function(pGetHeadline, pGetLetterSalutation)
+{
+    var column;
+    if(pGetHeadline)
+    {
+        column = "SALUTATION.HEADLINE";
+    }
+    if(pGetLetterSalutation)
+    {
+        column = "SALUTATION.LETTERSALUTATION";
+    }
+    var sqlHelper = new SqlMaskingUtils();
+    var personSelect = "";
+    var orgSelect = column;
+    var saltuationPlaceholders = {"'{fn}'": "PERSON.FIRSTNAME", "'{ln}'": "PERSON.LASTNAME", "'{ti}'": "PERSON.TITLE"};
+    Object.keys(saltuationPlaceholders).forEach(function(placeholder){
+        let persSelectFront = "REPLACE(";
+        let persSelectBack = ", " + placeholder + ", " + sqlHelper.isNull(saltuationPlaceholders[placeholder], "''")  + ")";
+        if(!personSelect)
+        {
+            personSelect = persSelectFront + column + persSelectBack;
+        }
+        else
+        {
+            personSelect = persSelectFront + personSelect + persSelectBack;
+        }
+    });
+    var saltuation = newSelect("case when CONTACT.PERSON_ID is null then " + orgSelect + " else " + personSelect + "end")
+                        .from("SALUTATION")
+                        .where("SALUTATION.ISOLANGUAGE = CONTACT.ISOLANGUAGE")
+                        .and(newWhere("SALUTATION.SEX = PERSON.GENDER").or("SALUTATION.SEX is null"))
+                        .and(newWhere("SALUTATION.TITLE = PERSON.TITLE").or("SALUTATION.TITLE is null"))
+                        .and(newWhere("SALUTATION.SALUTATION = PERSON.SALUTATION").or("SALUTATION.SALUTATION is null"))
+                        .orderBy("SALUTATION.SEX desc, SALUTATION.TITLE desc");
+    saltuation = saltuation.toString();         
+    saltuation = saltuation + " " + sqlHelper.limit(1);
+    return saltuation;
+}
+
 /**
  * object for handling of a single contact
  * provides static- and instance-functions
-- 
GitLab