From 829938eb77fff8232efc213011dfa0ab1f03b08f Mon Sep 17 00:00:00 2001
From: "j.goderbauer" <j.goderbauer@adito.de>
Date: Mon, 3 Dec 2018 17:23:50 +0100
Subject: [PATCH] RecordContainer fix: History_entity

---
 entity/History_entity/History_entity.aod      | 69 ++++++++++++++++---
 .../recordcontainers/db/conditionProcess.js   |  9 +++
 .../recordcontainers/db/fromClauseProcess.js  |  3 +
 .../recordcontainers/db/onDBDelete.js         |  5 ++
 .../recordcontainers/db/onDBInsert.js         | 12 ++++
 5 files changed, 90 insertions(+), 8 deletions(-)
 create mode 100644 entity/History_entity/recordcontainers/db/conditionProcess.js
 create mode 100644 entity/History_entity/recordcontainers/db/fromClauseProcess.js
 create mode 100644 entity/History_entity/recordcontainers/db/onDBDelete.js
 create mode 100644 entity/History_entity/recordcontainers/db/onDBInsert.js

diff --git a/entity/History_entity/History_entity.aod b/entity/History_entity/History_entity.aod
index ee8acf6280..858f19eb53 100644
--- a/entity/History_entity/History_entity.aod
+++ b/entity/History_entity/History_entity.aod
@@ -11,6 +11,14 @@
   <conditionProcess>%aditoprj%/entity/History_entity/conditionProcess.js</conditionProcess>
   <onDBInsert>%aditoprj%/entity/History_entity/onDBInsert.js</onDBInsert>
   <onDBDelete>%aditoprj%/entity/History_entity/onDBDelete.js</onDBDelete>
+  <linkInformation>
+    <linkInformation>
+      <name>0f5254de-80c0-4861-9526-dab5144c73e1</name>
+      <tableName>HISTORY</tableName>
+      <primaryKey>HISTORYID</primaryKey>
+      <isUIDTable v="true" />
+    </linkInformation>
+  </linkInformation>
   <entityFields>
     <entityField>
       <name>DIRECTION</name>
@@ -150,12 +158,57 @@
       </dependencies>
     </entityIncomingField>
   </entityFields>
-  <linkInformation>
-    <linkInformation>
-      <name>0f5254de-80c0-4861-9526-dab5144c73e1</name>
-      <tableName>HISTORY</tableName>
-      <primaryKey>HISTORYID</primaryKey>
-      <isUIDTable v="true" />
-    </linkInformation>
-  </linkInformation>
+  <recordContainers>
+    <dbRecordContainer>
+      <name>db</name>
+      <alias>Data_alias</alias>
+      <fromClauseProcess>%aditoprj%/entity/History_entity/recordcontainers/db/fromClauseProcess.js</fromClauseProcess>
+      <conditionProcess>%aditoprj%/entity/History_entity/recordcontainers/db/conditionProcess.js</conditionProcess>
+      <onDBInsert>%aditoprj%/entity/History_entity/recordcontainers/db/onDBInsert.js</onDBInsert>
+      <onDBDelete>%aditoprj%/entity/History_entity/recordcontainers/db/onDBDelete.js</onDBDelete>
+      <linkInformation>
+        <linkInformation>
+          <name>3d146593-d7a8-463c-9805-0d607c9053e9</name>
+          <tableName>HISTORY</tableName>
+          <primaryKey>HISTORYID</primaryKey>
+          <isUIDTable v="true" />
+          <readonly v="false" />
+        </linkInformation>
+      </linkInformation>
+      <recordFieldMappings>
+        <dbRecordFieldMapping>
+          <name>DATE_NEW.value</name>
+          <value>HISTORY.DATE_NEW</value>
+        </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>HISTORYID.value</name>
+          <value>HISTORY.HISTORYID</value>
+        </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>DIRECTION.value</name>
+          <value>HISTORY.DIRECTION</value>
+        </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>INFO.value</name>
+          <value>HISTORY.INFO</value>
+        </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>MEDIUM.value</name>
+          <value>HISTORY.MEDIUM</value>
+        </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>SUBJECT.value</name>
+          <value>HISTORY.SUBJECT</value>
+        </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>ENTRYDATE.value</name>
+          <value>HISTORY.ENTRYDATE</value>
+        </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>USER_NEW.value</name>
+          <value>HISTORY.USER_NEW</value>
+        </dbRecordFieldMapping>
+      </recordFieldMappings>
+    </dbRecordContainer>
+  </recordContainers>
 </entity>
diff --git a/entity/History_entity/recordcontainers/db/conditionProcess.js b/entity/History_entity/recordcontainers/db/conditionProcess.js
new file mode 100644
index 0000000000..f98df46a08
--- /dev/null
+++ b/entity/History_entity/recordcontainers/db/conditionProcess.js
@@ -0,0 +1,9 @@
+import("system.db");
+import("system.result");
+import("Sql_lib");
+
+var cond = new SqlCondition();
+cond.andPrepareVars("HISTORYLINK.ROW_ID", "$param.RowId_param");
+
+//TODO: use a preparedCondition when available
+result.string(db.translateCondition(cond.build("1 = 1")));
\ No newline at end of file
diff --git a/entity/History_entity/recordcontainers/db/fromClauseProcess.js b/entity/History_entity/recordcontainers/db/fromClauseProcess.js
new file mode 100644
index 0000000000..4086d379af
--- /dev/null
+++ b/entity/History_entity/recordcontainers/db/fromClauseProcess.js
@@ -0,0 +1,3 @@
+import("system.result");
+
+result.string("HISTORY join HISTORYLINK on (HISTORY.HISTORYID = HISTORYLINK.HISTORY_ID)");
\ No newline at end of file
diff --git a/entity/History_entity/recordcontainers/db/onDBDelete.js b/entity/History_entity/recordcontainers/db/onDBDelete.js
new file mode 100644
index 0000000000..c054511acc
--- /dev/null
+++ b/entity/History_entity/recordcontainers/db/onDBDelete.js
@@ -0,0 +1,5 @@
+import("system.vars");
+import("system.db");
+
+db.deleteData("HISTORYLINK", "HISTORYLINK.HISTORY_ID = '" + vars.getString("$field.HISTORYID") + "'");
+
diff --git a/entity/History_entity/recordcontainers/db/onDBInsert.js b/entity/History_entity/recordcontainers/db/onDBInsert.js
new file mode 100644
index 0000000000..10db22cf04
--- /dev/null
+++ b/entity/History_entity/recordcontainers/db/onDBInsert.js
@@ -0,0 +1,12 @@
+import("system.util");
+import("system.db");
+import("system.neon");
+import("system.vars");
+
+if (vars.exists("$param.RowId_param") && vars.get("$param.RowId_param") != null )
+{   
+    var cols = ["HISTORYLINKID", "OBJECT_ID", "ROW_ID", "HISTORY_ID", "USER_NEW", "DATE_NEW"];
+    var vals = [util.getNewUUID(), 1, vars.get("$param.RowId_param"), vars.getString("$field.HISTORYID"), vars.getString("$sys.user"), vars.getString("$sys.date")]; // use new OBJECT_ID for context
+    var colTypes = db.getColumnTypes("HISTORYLINK", cols);	
+    db.insertData("HISTORYLINK", cols, colTypes, vals);
+}
\ No newline at end of file
-- 
GitLab