From f4721f3406bbd7ac9d1005723e4184ccce8db300 Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Tue, 15 Jan 2019 11:09:15 +0100
Subject: [PATCH] refactor lib and comments

---
 process/Relation_lib/process.js | 164 ++++++++++++++++++++------------
 1 file changed, 105 insertions(+), 59 deletions(-)

diff --git a/process/Relation_lib/process.js b/process/Relation_lib/process.js
index fdcced185ad..5b436de1486 100644
--- a/process/Relation_lib/process.js
+++ b/process/Relation_lib/process.js
@@ -1,7 +1,9 @@
+import("system.logging");
 import("system.vars");
 import("system.result");
 import("system.db");
 import("Sql_lib");
+import("Context_lib");
 
 /**
  * a static Utility class for relations
@@ -23,10 +25,7 @@ function RelationUtils() {}
  */
 RelationUtils.getRelationTypeByRelation = function(pRelationId)
 {
-    var relationData = db.array(db.ROW, 
-            SqlCondition.begin()
-                        .andPrepare("RELATION.RELATIONID", pRelationId)
-                        .buildSelect("select RELATIONID, PERS_ID, ORG_ID from RELATION", "1=0"));
+    var relationData = RelationUtils.getContextByRelationId(pRelationId);
     
     if (relationData[0]) 
     {
@@ -55,7 +54,8 @@ RelationUtils.getRelationTypeByRelation = function(pRelationId)
  * @param {String} pPersId selected from the RELATION table
  * @param {String} pOrgId selected from the RELATION table
  *  <br>
- * @return {Integer} <br>1 if organisation <br>
+ * @return {Integer} <br>0 if both ids are empty <br>
+ *                      1 if organisation <br>
  *                      2 if privat person <br>
  *                      3 if person of an organisation <br>
  */
@@ -63,6 +63,9 @@ RelationUtils.getRelationTypeByPersOrg = function(pPersId, pOrgId)
 {
     if (!pPersId)
     {
+        if (!pOrgId) {
+            return 0; // both are empty
+        }
         return 1;  // Organisation da PERS_ID leer
     }
     else
@@ -78,88 +81,131 @@ RelationUtils.getRelationTypeByPersOrg = function(pPersId, pOrgId)
     }
 }
 
-RelationUtils.getContextByPersOrg = function(pPersIdField, pOrgIdField)
+/**
+ * return the corresponding context of the relation
+ * 
+ * It only checks if the parameters are not empty. <br>
+ * Based on which parameter is empty / not empty it return s the type of the relation. <br>
+ *  <br>
+ * !!It does not check if the pers / org ids really exist!! <br>
+ * !!And it does not check if really any relation with this pers / org ids exist!! <br>
+ *  <br>
+ * This function is more performant than getContextByRelationId, <br>
+ * because it doesn't load something from the db. <br>
+ *  <br>
+ * It is meant to be used by entitys, where you can load pers and org with the DataRecord. <br>
+ * This saves an extra select from RELATION. <br>
+ *  <br>
+ * 
+ * @param {String} pPersId selected from the RELATION table
+ * @param {String} pOrgId selected from the RELATION table
+ * 
+ * @return {String} contextname or "" if both ids are empty
+ */
+RelationUtils.getContextByPersOrg = function(pPersId, pOrgId)
 {
-    if(!vars.get(pPersIdField) && !vars.get(pOrgIdField))
-        return "";
-    else 
+    switch (RelationUtils.getRelationTypeByPersOrg(pPersId, pOrgId))
     {
-        switch (RelationUtils.getRelationTypeByPersOrg(vars.get(pPersIdField), vars.get(pOrgIdField)))
-        {
-            case 1: // Org
-                return "Org_context";
-            case 2: // private Pers
-            case 3: // Pers
-                return "Pers_context";
-            default:
-                return "";
-        }
+        case 1: // Org
+            return ContextUtils.getContextName("Org_context");
+        case 2: // private Pers
+        case 3: // Pers
+            return ContextUtils.getContextName("Pers_context");
+        default:
+            return "";
     }
 }
 
+/**
+ * return the corresponding context of the relation <br>
+ * If you already have persId and orgId from the RELATION table, use getContextByPersOrg() <br>
+ * 
+ * @param {String} pRelationId
+ * @return {String} contextname or "" if relation not found
+ */
 RelationUtils.getContextByRelationId = function(pRelationId)
 {
-    if(!pRelationId)
-        return "";
-    else 
-    {
-        switch (RelationUtils.getRelationTypeByRelation(pRelationId))
-        {
-            case 1: // Org
-                return "Org_context";
-            case 2: // private Pers
-            case 3: // Pers
-                return "Pers_context";
-            default:
-                return "";
-        }
+    var relationData = RelationUtils.getPersOrgIds(pRelationId);
+    return RelationUtils.getContextByPersOrg(relationData[0], relationData[1])
+}
+
+/**
+ * get the pers- and org-id from a relation as array
+ * 
+ * @param {String} pRelationId
+ * @return {String[]} result as [persid, orgid] if one of them is null in the db, "" will be returned as the id.
+ */
+RelationUtils.getPersOrgIds = function(pRelationId)
+{
+    if (pRelationId) {
+        return db.array(db.ROW, 
+            SqlCondition.begin()
+                        .andPrepare("RELATION.RELATIONID", pRelationId)
+                        .buildSelect("select RELATIONID, PERS_ID, ORG_ID from RELATION", "1=0"));
     }
+    
+    
+    return ["", ""];
 }
 
-RelationUtils.getNameByPersOrg = function(pPersIdField, pOrgIdField, pPersFirstnameField, 
-                                            pPersLastnameField, pOrgnameField)
+/**
+ * get the name of the person or organisation
+ * The parameters have to be selected from the same relation.
+ * 
+ * @param {String} pPersId value of the pers_id selected from RELATION
+ * @param {String} pOrgId value of the org_id selected from RELATION
+ * @param {String} pPersFirstname value of the firstname selected from PERS
+ * @param {String} pPersLastname value of the lastname selected from PERS
+ * @param {String} pOrgname value of the name selected from ORG
+ * @param {String} [pTitle=undefined] value of the title selected from PERS. You can ommit this parameter if you do not want to append the title
+ * 
+ * @return {String} the name or ""
+ */
+RelationUtils.getNameByPersOrg = function(pPersId, pOrgId, pPersFirstname, 
+                                            pPersLastname, pOrgname, pTitle)
 {
-    if(!vars.get(pPersIdField) && !vars.get(pOrgIdField))
+    if(!pPersId && !pOrgId)
         return "";
     else 
     {
-        switch (RelationUtils.getRelationTypeByPersOrg(vars.get(pPersIdField), vars.get(pOrgIdField)))
+        switch (RelationUtils.getRelationTypeByPersOrg(pPersId, pOrgId))
         {
             case 1: // Org   
-                return vars.get(pOrgnameField);
+                return pOrgname;
             case 2: // private Pers
             case 3: // Pers
-                return vars.getString(pPersFirstnameField)
-                .concat(" " , vars.getString(pPersLastnameField));
+                var name = "";
+                
+                if (pTitle != undefined && pTitle)
+                    name = pTitle + " ";
+               
+                return name + pPersFirstname.concat(" " , pPersLastname);
             default:
                 return "";
         }
     }
 }
 
-RelationUtils.getNameByPersOrgWithRelationId = function(pRelationId)
+/**
+ * get the name of the person or organisation
+ * 
+ * @param {String} pRelationId the relation id
+ * @param {Boolean} [pTitle=false] also add the title for persons
+ * 
+ * @return {String} the name or ""
+ */
+RelationUtils.getNameByPersOrgWithRelationId = function(pRelationId, pTitle)
 {
-    
-    var data = db.array(db.ROW, SqlCondition.begin()
+    logging.log(vars.get("$sys.currententityname") + pRelationId + " ")
+    if (pRelationId) {
+        var data = db.array(db.ROW, SqlCondition.begin()
                             .andPrepare("RELATION.RELATIONID", pRelationId)
-                            .buildSelect("select RELATION.PERS_ID, RELATION.ORG_ID, PERS.FIRSTNAME, PERS.LASTNAME, ORG.NAME from RELATION RELATION join ORG on ORG.ORGID = RELATION.ORG_ID left join PERS on PERS.PERSID = RELATION.PERS_ID", "1 = 2"));                                    
+                            .buildSelect("select RELATION.PERS_ID, RELATION.ORG_ID, PERS.FIRSTNAME, PERS.LASTNAME, PERS.TITLE, ORG.NAME from RELATION RELATION join ORG on ORG.ORGID = RELATION.ORG_ID left join PERS on PERS.PERSID = RELATION.PERS_ID", "1 = 2"));                                    
  
-    if(!data[0] && !data[1])
-        return "";
-    else 
-    {
-        switch (RelationUtils.getRelationTypeByPersOrg(data[0], data[1]))
-        {
-            case 1: // Org   
-                return data[4];
-            case 2: // private Pers
-            case 3: // Pers
-                return data[2]
-                .concat(" " , data[3]);
-            default:
-                return "";
-        }
+        return RelationUtils.getNameByPersOrg(data[0], data[1], data[2], data[3], data[5], (pTitle ? data[4] : undefined));
     }
+    
+    return "";
 }
 
 /**
-- 
GitLab