From d1b0acbb6f6a451410d3f92ab85e677200bb7ca3 Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Thu, 6 Dec 2018 15:59:29 +0100
Subject: [PATCH] Produuct Lookup for Org

---
 aliasDefinition/Data_alias/Data_alias.aod     | 39 +++++-----
 entity/Org_entity/Org_entity.aod              |  4 +-
 .../{orgid_param => relationid_param}/code.js |  2 +-
 entity/Product_entity/Product_entity.aod      | 57 +++++++++++---
 .../entityfields/org_id/valueProcess.js       | 11 ---
 .../relation_id/linkedContextProcess.js       | 14 ++++
 .../entityfields/relation_id/valueProcess.js  |  9 +++
 .../recordcontainers/db/fromClauseProcess.js  |  3 +
 .../ProductPreview_view.aod                   |  4 +-
 others/db_changes/struct/create_product.xml   |  2 +-
 process/Relation_lib/Relation_lib.aod         |  6 ++
 process/Relation_lib/process.js               | 77 +++++++++++++++++++
 process/Sql_lib/process.js                    | 24 +++---
 13 files changed, 193 insertions(+), 59 deletions(-)
 rename entity/Org_entity/entityfields/orgproduct_dfo/children/{orgid_param => relationid_param}/code.js (52%)
 delete mode 100644 entity/Product_entity/entityfields/org_id/valueProcess.js
 create mode 100644 entity/Product_entity/entityfields/relation_id/linkedContextProcess.js
 create mode 100644 entity/Product_entity/entityfields/relation_id/valueProcess.js
 create mode 100644 entity/Product_entity/recordcontainers/db/fromClauseProcess.js
 create mode 100644 process/Relation_lib/Relation_lib.aod
 create mode 100644 process/Relation_lib/process.js

diff --git a/aliasDefinition/Data_alias/Data_alias.aod b/aliasDefinition/Data_alias/Data_alias.aod
index fc1d8d175f..0182f22693 100644
--- a/aliasDefinition/Data_alias/Data_alias.aod
+++ b/aliasDefinition/Data_alias/Data_alias.aod
@@ -214,9 +214,9 @@
                 <columnType v="1" />
                 <size v="36" />
                 <scale v="0" />
-                <notNull v="true" />
+                <notNull v="false" />
                 <isUnique v="false" />
-                <index v="true" />
+                <index v="false" />
                 <title></title>
                 <description></description>
               </entityFieldDb>
@@ -1394,19 +1394,6 @@
               <syncIds></syncIds>
             </auditSyncConfig>
             <entityFields>
-              <entityFieldDb>
-                <name>ORG_ID</name>
-                <dbName></dbName>
-                <primaryKey v="false" />
-                <columnType v="1" />
-                <size v="36" />
-                <scale v="0" />
-                <notNull v="false" />
-                <isUnique v="false" />
-                <index v="false" />
-                <title></title>
-                <description></description>
-              </entityFieldDb>
               <entityFieldDb>
                 <name>UNIT</name>
                 <dbName></dbName>
@@ -1576,6 +1563,20 @@
                 <title></title>
                 <description></description>
               </entityFieldDb>
+              <entityFieldDb>
+                <name>RELATION_ID</name>
+                <dbName></dbName>
+                <primaryKey v="false" />
+                <columnType v="1" />
+                <size v="36" />
+                <scale v="0" />
+                <notNull v="false" />
+                <isUnique v="false" />
+                <index v="false" />
+                <documentation></documentation>
+                <title></title>
+                <description></description>
+              </entityFieldDb>
             </entityFields>
           </entityDb>
           <entityDb>
@@ -1765,7 +1766,7 @@
                 <columnType v="4" />
                 <size v="10" />
                 <scale v="0" />
-                <notNull v="false" />
+                <notNull v="true" />
                 <isUnique v="false" />
                 <index v="false" />
                 <title></title>
@@ -2535,7 +2536,7 @@
                 <scale v="0" />
                 <notNull v="false" />
                 <isUnique v="false" />
-                <index v="true" />
+                <index v="false" />
                 <documentation></documentation>
                 <title></title>
                 <description></description>
@@ -2645,9 +2646,9 @@
                 <columnType v="1" />
                 <size v="36" />
                 <scale v="0" />
-                <notNull v="true" />
+                <notNull v="false" />
                 <isUnique v="false" />
-                <index v="true" />
+                <index v="false" />
                 <title></title>
                 <description></description>
               </entityFieldDb>
diff --git a/entity/Org_entity/Org_entity.aod b/entity/Org_entity/Org_entity.aod
index 0b483c0f19..112ead0c4c 100644
--- a/entity/Org_entity/Org_entity.aod
+++ b/entity/Org_entity/Org_entity.aod
@@ -374,8 +374,8 @@
       </dependency>
       <children>
         <entityParameter>
-          <name>OrgId_param</name>
-          <code>%aditoprj%/entity/Org_entity/entityfields/orgproduct_dfo/children/orgid_param/code.js</code>
+          <name>relationId_param</name>
+          <code>%aditoprj%/entity/Org_entity/entityfields/orgproduct_dfo/children/relationid_param/code.js</code>
         </entityParameter>
       </children>
     </entityOutgoingField>
diff --git a/entity/Org_entity/entityfields/orgproduct_dfo/children/orgid_param/code.js b/entity/Org_entity/entityfields/orgproduct_dfo/children/relationid_param/code.js
similarity index 52%
rename from entity/Org_entity/entityfields/orgproduct_dfo/children/orgid_param/code.js
rename to entity/Org_entity/entityfields/orgproduct_dfo/children/relationid_param/code.js
index 43a0099996..aa257b8b78 100644
--- a/entity/Org_entity/entityfields/orgproduct_dfo/children/orgid_param/code.js
+++ b/entity/Org_entity/entityfields/orgproduct_dfo/children/relationid_param/code.js
@@ -1,4 +1,4 @@
 import("system.result");
 import("system.vars");
 
-result.string(vars.get("$field.ORGID"));
\ No newline at end of file
+result.string(vars.get("$field.RELATIONID"));
\ No newline at end of file
diff --git a/entity/Product_entity/Product_entity.aod b/entity/Product_entity/Product_entity.aod
index e73c1a6a95..73e5daa061 100644
--- a/entity/Product_entity/Product_entity.aod
+++ b/entity/Product_entity/Product_entity.aod
@@ -63,13 +63,6 @@
       <contentType>NUMBER</contentType>
       <outputFormat>#,##0.##</outputFormat>
     </entityField>
-    <entityField>
-      <name>ORG_ID</name>
-      <title>Developer</title>
-      <tableName>PRODUCT</tableName>
-      <columnName>ORG_ID</columnName>
-      <valueProcess>%aditoprj%/entity/Product_entity/entityfields/org_id/valueProcess.js</valueProcess>
-    </entityField>
     <entityField>
       <name>PRODUCTCODE</name>
       <documentation>%aditoprj%/entity/Product_entity/entityfields/productcode/documentation.adoc</documentation>
@@ -167,7 +160,7 @@
       </dependencies>
     </entityIncomingField>
     <entityParameter>
-      <name>OrgId_param</name>
+      <name>relationId_param</name>
       <expose v="true" />
       <triggerRecalculation v="true" />
       <mandatory v="false" />
@@ -278,21 +271,51 @@
         </entityParameter>
       </children>
     </entityOutgoingField>
+    <entityField>
+      <name>RELATION_ID</name>
+      <title>Developer</title>
+      <tableName>PRODUCT</tableName>
+      <columnName>RELATION_ID</columnName>
+      <linkedContextProcess>%aditoprj%/entity/Product_entity/entityfields/relation_id/linkedContextProcess.js</linkedContextProcess>
+      <outgoingField>ProductOrg_dfo</outgoingField>
+      <valueProcess>%aditoprj%/entity/Product_entity/entityfields/relation_id/valueProcess.js</valueProcess>
+    </entityField>
+    <entityField>
+      <name>RELATION_ORG_ID</name>
+    </entityField>
+    <entityField>
+      <name>RELATION_PERS_ID</name>
+    </entityField>
   </entityFields>
   <recordContainers>
     <dbRecordContainer>
       <name>db</name>
       <alias>Data_alias</alias>
       <maximumDbRows v="0" />
+      <fromClauseProcess>%aditoprj%/entity/Product_entity/recordcontainers/db/fromClauseProcess.js</fromClauseProcess>
       <onDBDelete>%aditoprj%/entity/Product_entity/recordcontainers/db/onDBDelete.js</onDBDelete>
       <linkInformation>
         <linkInformation>
-          <name>c1012dc4-2126-433f-aacd-ce8f25e96f42</name>
+          <name>219bb2ab-3c6f-475f-a2b4-0d3e48b16b8f</name>
           <tableName>PRODUCT</tableName>
           <primaryKey>PRODUCTID</primaryKey>
           <isUIDTable v="true" />
           <readonly v="false" />
         </linkInformation>
+        <linkInformation>
+          <name>478598a4-3130-4b0c-ba8d-14b67f003ee5</name>
+          <tableName>RELATION</tableName>
+          <primaryKey>RELATIONID</primaryKey>
+          <isUIDTable v="false" />
+          <readonly v="true" />
+        </linkInformation>
+        <linkInformation>
+          <name>3800cc08-4ab5-4b9a-a200-993c990b7906</name>
+          <tableName>ORG</tableName>
+          <primaryKey>ORGID</primaryKey>
+          <isUIDTable v="false" />
+          <readonly v="true" />
+        </linkInformation>
       </linkInformation>
       <recordFieldMappings>
         <dbRecordFieldMapping>
@@ -316,8 +339,8 @@
           <value>PRODUCT.MINSTOCK</value>
         </dbRecordFieldMapping>
         <dbRecordFieldMapping>
-          <name>ORG_ID.value</name>
-          <value>PRODUCT.ORG_ID</value>
+          <name>RELATION_ID.value</name>
+          <value>PRODUCT.RELATION_ID</value>
         </dbRecordFieldMapping>
         <dbRecordFieldMapping>
           <name>PRODUCTCODE.value</name>
@@ -351,6 +374,18 @@
           <name>USER_NEW.value</name>
           <value>PRODUCT.USER_NEW</value>
         </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>RELATION_ORG_ID.value</name>
+          <value>RELATION.ORG_ID</value>
+        </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>RELATION_PERS_ID.value</name>
+          <value>RELATION.PERS_ID</value>
+        </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>RELATION_ID.displayValue</name>
+          <value>ORG.NAME</value>
+        </dbRecordFieldMapping>
       </recordFieldMappings>
     </dbRecordContainer>
   </recordContainers>
diff --git a/entity/Product_entity/entityfields/org_id/valueProcess.js b/entity/Product_entity/entityfields/org_id/valueProcess.js
deleted file mode 100644
index de32e15c56..0000000000
--- a/entity/Product_entity/entityfields/org_id/valueProcess.js
+++ /dev/null
@@ -1,11 +0,0 @@
-import("system.vars");
-import("system.result");
-import("system.neon");
-
-if(vars.get("$sys.operatingstate") == neon.OPERATINGSTATE_NEW)
-{
-    if(vars.exists("$param.OrgId_param") && vars.get("$param.OrgId_param") != null)
-        result.string(vars.getString("$param.OrgId_param"));    
-}
-else
-    result.string(vars.get("$this.value"));    
diff --git a/entity/Product_entity/entityfields/relation_id/linkedContextProcess.js b/entity/Product_entity/entityfields/relation_id/linkedContextProcess.js
new file mode 100644
index 0000000000..bd698d1aca
--- /dev/null
+++ b/entity/Product_entity/entityfields/relation_id/linkedContextProcess.js
@@ -0,0 +1,14 @@
+import("system.result");
+import("system.vars");
+import("Relation_lib")
+
+switch (RelationUtils.getRelationTypeByPersOrg(vars.get("$field.RELATION_PERS_ID"), vars.get("$field.RELATION_ORG_ID")))
+{
+    case 1: // Org
+        result.string("Org_context")
+        break;
+    case 2: // private Pers
+    case 3: // Pers
+        result.string("Pers_context")
+    default:
+}
\ No newline at end of file
diff --git a/entity/Product_entity/entityfields/relation_id/valueProcess.js b/entity/Product_entity/entityfields/relation_id/valueProcess.js
new file mode 100644
index 0000000000..7e309f23c0
--- /dev/null
+++ b/entity/Product_entity/entityfields/relation_id/valueProcess.js
@@ -0,0 +1,9 @@
+import("system.vars");
+import("system.result");
+import("system.neon");
+
+if(vars.get("$sys.operatingstate") == neon.OPERATINGSTATE_NEW)
+{
+    if(vars.exists("$param.relationId_param") && vars.get("$param.relationId_param"))
+        result.string(vars.getString("$param.relationId_param"));    
+}
diff --git a/entity/Product_entity/recordcontainers/db/fromClauseProcess.js b/entity/Product_entity/recordcontainers/db/fromClauseProcess.js
new file mode 100644
index 0000000000..7bfbd0635a
--- /dev/null
+++ b/entity/Product_entity/recordcontainers/db/fromClauseProcess.js
@@ -0,0 +1,3 @@
+import("system.result");
+
+result.string("PRODUCT left join RELATION on RELATIONID = RELATION_ID left join ORG on ORGID = ORG_ID");
\ No newline at end of file
diff --git a/neonView/ProductPreview_view/ProductPreview_view.aod b/neonView/ProductPreview_view/ProductPreview_view.aod
index c1f1fa217d..03fea1d033 100644
--- a/neonView/ProductPreview_view/ProductPreview_view.aod
+++ b/neonView/ProductPreview_view/ProductPreview_view.aod
@@ -25,8 +25,8 @@
           <entityField>PRODUCTNAME</entityField>
         </entityFieldLink>
         <entityFieldLink>
-          <name>7074979a-a2fc-4a57-98ad-3d8e98fde37a</name>
-          <entityField>ORG_ID</entityField>
+          <name>bd8e7e33-6fc6-48fe-b9be-931383f4d859</name>
+          <entityField>RELATION_ID</entityField>
         </entityFieldLink>
         <entityFieldLink>
           <name>a5a3ecee-059d-4bf2-9654-abe3efd58510</name>
diff --git a/others/db_changes/struct/create_product.xml b/others/db_changes/struct/create_product.xml
index 685984f4d1..03301c76b7 100644
--- a/others/db_changes/struct/create_product.xml
+++ b/others/db_changes/struct/create_product.xml
@@ -8,7 +8,7 @@
 		<column name="ADVERTISING" type="CHAR(1)"/>          
 		<column name="GROUPCODEID" type="INTEGER"/>
 		<column name="MINSTOCK" type="NUMERIC(14,2)"/>
-		<column name="ORG_ID" type="CHAR(36)"/>
+		<column name="RELATION_ID" type="CHAR(36)"/>
 		<column name="PRODUCTCODE" type="VARCHAR(50)">
                     <constraints  nullable="false" unique="true" uniqueConstraintName="UNIQUE_PRODUCT_PRODUCTCODE"/>
                 </column>
diff --git a/process/Relation_lib/Relation_lib.aod b/process/Relation_lib/Relation_lib.aod
new file mode 100644
index 0000000000..3871a31e6a
--- /dev/null
+++ b/process/Relation_lib/Relation_lib.aod
@@ -0,0 +1,6 @@
+<?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.1.7" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/process/1.1.7">
+  <name>Relation_lib</name>
+  <majorModelMode>DISTRIBUTED</majorModelMode>
+  <process>%aditoprj%/process/Relation_lib/process.js</process>
+</process>
diff --git a/process/Relation_lib/process.js b/process/Relation_lib/process.js
new file mode 100644
index 0000000000..6b6e3829fe
--- /dev/null
+++ b/process/Relation_lib/process.js
@@ -0,0 +1,77 @@
+import("system.db");
+import("Sql_lib");
+
+/**
+ * a static Utility class for relations
+ * 
+ * Do not create an instance of this!
+ * @class
+ */
+function RelationUtils() {}
+
+/**
+ * get the type of relation for a relationId <br>
+ * If you already have persId and orgId from the RELATION table, use getRelationTypeByPersOrg() <br>
+ * @param {String} pRelationId
+ * <br>
+ * @return {String} 0 if relationId not found <br>
+ *                  1 if organisation <br>
+ *                  2 if privat person <br>
+ *                  3 if person of an organisation <br>
+ */
+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"));
+    
+    if (relationData[0]) 
+    {
+        return this.getRelationTypeByPersOrg(relationData[1], relationData[2]);
+    }
+    else
+    {
+        return 0;
+    }
+}
+
+/**
+ * Get the type of relation. <br>
+ * 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 getRelationTypeByRelation, <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
+ *  <br>
+ * @return {String} <br>1 if organisation <br>
+ *                      2 if privat person <br>
+ *                      3 if person of an organisation <br>
+ */
+RelationUtils.getRelationTypeByPersOrg = function(pPersId, pOrgId)
+{
+    if (!pPersId)
+    {
+        return 1;  // Organisation da PERS_ID leer
+    }
+    else
+    {
+        if (pOrgId.replace(/\s/g,"") == "0" )
+        {
+            return 2; // Privatperson da PERS_ID nicht leer und ORG_ID.trim() = '0'
+        }
+        else
+        {
+            return 3; // Person einer Organisation da PERS_ID nicht leer und ORG_ID nicht '0'
+        }
+    }
+}
\ No newline at end of file
diff --git a/process/Sql_lib/process.js b/process/Sql_lib/process.js
index 6d270c9c0d..56217a2538 100644
--- a/process/Sql_lib/process.js
+++ b/process/Sql_lib/process.js
@@ -38,7 +38,7 @@ function SqlCondition(alias) {
  * See Examples!
  * 
  * @param {String} [alias=the current alias] the database alias where the condition shall be executed later (important for column types of preparedStatements)
- * @return {Object} the new SqlCondition-object
+ * @return {SqlCondition} the new SqlCondition-object
  * 
  * @example 
  * vars mySelect = SqlCondition.begin(alias)
@@ -61,7 +61,7 @@ SqlCondition.begin = function(alias) {
 /**
  * append with SQL-and; no paranthesize of existing conditions is done
  * @param {String} cond the condition string which shall be appended
- * @return {Object} current SqlCondition-object
+ * @return {SqlCondition} current SqlCondition-object
  */
 SqlCondition.prototype.and = function(cond) {
     if (!cond)
@@ -84,7 +84,7 @@ SqlCondition.prototype.and = function(cond) {
  *                              for (...) {
  *                                  cond.andPrepare("SALESPROJECT_CLASSIFICATION.TYPE", entry, "# <> ?")
  *                              }
- * @return {Object} current SqlCondition-object
+ * @return {SqlCondition} current SqlCondition-object
  */
 SqlCondition.prototype.andPrepare = function(field, value, cond, fieldType) {
     cond = this._prepare(field, value, cond, fieldType);
@@ -94,7 +94,7 @@ SqlCondition.prototype.andPrepare = function(field, value, cond, fieldType) {
 /**
  * append with SQL-or; Also paranthesize the existing conditions
  * @param {String} cond the condition string which shall be appended
- * @return {Object} current SqlCondition-object
+ * @return {SqlCondition} current SqlCondition-object
  */
 SqlCondition.prototype.or = function(cond) {
     if (!cond)
@@ -117,9 +117,9 @@ SqlCondition.prototype.or = function(cond) {
 /**
  * append another condition with SQL-or; Also paranthesize the existing conditions
  * 
- * @param {Object} cond the condition which shall be appended
+ * @param {SqlCondition} cond the condition which shall be appended
  * @param {String} [alternativeCond=""] condition if the given SqlCondition has none
- * @return {Object} current SqlCondition-object
+ * @return {SqlCondition} current SqlCondition-object
  */
 SqlCondition.prototype.orSqlCondition = function(cond, alternativeCond) {
     this.or(" ( " + cond.toString(alternativeCond) + " ) ");
@@ -132,9 +132,9 @@ SqlCondition.prototype.orSqlCondition = function(cond, alternativeCond) {
 /**
  * append another condition with SQL-and
  * 
- * @param {Object} cond the condition which shall be appended
+ * @param {SqlCondition} cond the condition which shall be appended
  * @param {String} [alternativeCond=""] condition if the given SqlCondition has none
- * @return {Object} current SqlCondition-object
+ * @return {SqlCondition} current SqlCondition-object
  */
 SqlCondition.prototype.andSqlCondition = function(cond, alternativeCond) {
     this.and(" ( " + cond.toString(alternativeCond) + " ) ");
@@ -156,7 +156,7 @@ SqlCondition.prototype.andSqlCondition = function(cond, alternativeCond) {
  *                              for (...) {
  *                                  cond.andPrepare("SALESPROJECT_CLASSIFICATION.TYPE", entry, "# <> ?")
  *                              }
- * @return {Object} current SqlCondition-object
+ * @return {SqlCondition} current SqlCondition-object
  */
 SqlCondition.prototype.orPrepare = function(field, value, cond, fieldType) {
     cond = this._prepare(field, value, cond, fieldType);
@@ -175,7 +175,7 @@ SqlCondition.prototype.orPrepare = function(field, value, cond, fieldType) {
  *                              for (...) {
  *                                  cond.andPrepare("SALESPROJECT_CLASSIFICATION.TYPE", entry, "# <> ?")
  *                              }
- * @return {Object} current SqlCondition-object
+ * @return {SqlCondition} current SqlCondition-object
  */
 SqlCondition.prototype.andPrepareVars = function(field, variable, cond, fieldType) {
     variable = this._checkVars(variable)
@@ -197,7 +197,7 @@ SqlCondition.prototype.andPrepareVars = function(field, variable, cond, fieldTyp
  *                              for (...) {
  *                                  cond.andPrepare("SALESPROJECT_CLASSIFICATION.TYPE", entry, "# <> ?")
  *                              }
- * @return {Object} current SqlCondition-object
+ * @return {SqlCondition} current SqlCondition-object
  */
 SqlCondition.prototype.orPrepareVars = function(field, variable, cond, fieldType) {
     variable = this._checkVars(variable)
@@ -222,7 +222,7 @@ SqlCondition.prototype.toString = function(alternativeCond) {
 /**
  * ready to use string; does contain a where keyword at the beginning
  * @param {String} [alternativeCond=""] condition that is returned when nothing has been appended.
- * @return {String} concatenated SQL-condition; empty string if nothing has been appended or - if passed - the alternativeCond
+ * @return {SqlCondition} concatenated SQL-condition; empty string if nothing has been appended or - if passed - the alternativeCond
  */
 SqlCondition.prototype.toWhereString = function(alternativeCond) {
     var cond = this.toString(alternativeCond);
-- 
GitLab