From 8ab08a1d5100f0705b05527aa22301dcb6e817c1 Mon Sep 17 00:00:00 2001
From: "j.goderbauer" <j.goderbauer@adito.de>
Date: Wed, 16 Sep 2020 15:09:01 +0200
Subject: [PATCH] Member_entity: District and MSTeams: insert, edit, delete

---
 .../recordcontainers/db/conditionProcess.js     |  6 ++++--
 entity/Member_entity/Member_entity.aod          | 17 ++++++++++++++++-
 entity/Member_entity/documentation.adoc         | 13 ++++++++++++-
 .../entityfields/recordtype/documentation.adoc  |  1 +
 entity/Member_entity/grantDeleteProcess.js      |  5 +++++
 entity/Member_entity/grantUpdateProcess.js      |  5 +++++
 .../recordtype.value/expression.js              |  4 ++++
 .../recordcontainers/jdito/contentProcess.js    |  5 +++--
 .../recordcontainers/jdito/onDelete.js          | 16 ++++++++++++++++
 .../recordcontainers/jdito/onInsert.js          |  1 +
 .../recordcontainers/jdito/onUpdate.js          |  1 +
 .../Salesproject_entity/Salesproject_entity.aod |  2 +-
 neonContext/Member/Member.aod                   |  7 +++++++
 13 files changed, 76 insertions(+), 7 deletions(-)
 create mode 100644 entity/Member_entity/entityfields/recordtype/documentation.adoc
 create mode 100644 entity/Member_entity/grantDeleteProcess.js
 create mode 100644 entity/Member_entity/grantUpdateProcess.js
 create mode 100644 entity/Member_entity/recordcontainers/db/recordfieldmappings/recordtype.value/expression.js
 create mode 100644 entity/Member_entity/recordcontainers/jdito/onDelete.js
 create mode 100644 entity/Member_entity/recordcontainers/jdito/onInsert.js
 create mode 100644 entity/Member_entity/recordcontainers/jdito/onUpdate.js

diff --git a/entity/DistrictResponsible_entity/recordcontainers/db/conditionProcess.js b/entity/DistrictResponsible_entity/recordcontainers/db/conditionProcess.js
index 1942674d0c..d611896490 100644
--- a/entity/DistrictResponsible_entity/recordcontainers/db/conditionProcess.js
+++ b/entity/DistrictResponsible_entity/recordcontainers/db/conditionProcess.js
@@ -14,9 +14,11 @@ if (districtId)
 else if(orgUid) 
 {
     var orgDistrictIds = vars.get("$param.CollectedDistrictIds_param");
-    if (orgDistrictIds){
+    if (orgDistrictIds)
+    {
         orgDistrictIds = JSON.parse(orgDistrictIds);
-    cond.and("DISTRICTRESPONSIBLE.DISTRICT_ID", orgDistrictIds, SqlBuilder.IN());
+        if (orgDistrictIds.length > 0)
+            cond.and("DISTRICTRESPONSIBLE.DISTRICT_ID", orgDistrictIds, SqlBuilder.IN());
     }
 }
 
diff --git a/entity/Member_entity/Member_entity.aod b/entity/Member_entity/Member_entity.aod
index 854bfe8f3e..b0538033f6 100644
--- a/entity/Member_entity/Member_entity.aod
+++ b/entity/Member_entity/Member_entity.aod
@@ -7,6 +7,8 @@
   <siblings>
     <element>MSTTeam_entity</element>
   </siblings>
+  <grantUpdateProcess>%aditoprj%/entity/Member_entity/grantUpdateProcess.js</grantUpdateProcess>
+  <grantDeleteProcess>%aditoprj%/entity/Member_entity/grantDeleteProcess.js</grantDeleteProcess>
   <contentTitleProcess>%aditoprj%/entity/Member_entity/contentTitleProcess.js</contentTitleProcess>
   <iconId>VAADIN:FILE_SOUND</iconId>
   <titlePlural>${SALESPROJECT_MEMBER}</titlePlural>
@@ -43,7 +45,6 @@
     <entityField>
       <name>ORGANISATION_NAME</name>
       <title>Company</title>
-      <state>READONLY</state>
     </entityField>
     <entityField>
       <name>ORG_ORGID</name>
@@ -304,6 +305,10 @@
       <name>EnableMSTeams_param</name>
       <expose v="true" />
     </entityParameter>
+    <entityField>
+      <name>recordType</name>
+      <documentation>%aditoprj%/entity/Member_entity/entityfields/recordtype/documentation.adoc</documentation>
+    </entityField>
     <entityParameter>
       <name>SalesprojectCode_param</name>
       <expose v="true" />
@@ -439,12 +444,19 @@
           <name>MST_MEMBERROLE.value</name>
           <expression>%aditoprj%/entity/Member_entity/recordcontainers/db/recordfieldmappings/mst_memberrole.value/expression.js</expression>
         </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>recordType.value</name>
+          <expression>%aditoprj%/entity/Member_entity/recordcontainers/db/recordfieldmappings/recordtype.value/expression.js</expression>
+        </dbRecordFieldMapping>
       </recordFieldMappings>
     </dbRecordContainer>
     <jDitoRecordContainer>
       <name>jdito</name>
       <jDitoRecordAlias>Data_alias</jDitoRecordAlias>
       <contentProcess>%aditoprj%/entity/Member_entity/recordcontainers/jdito/contentProcess.js</contentProcess>
+      <onInsert>%aditoprj%/entity/Member_entity/recordcontainers/jdito/onInsert.js</onInsert>
+      <onUpdate>%aditoprj%/entity/Member_entity/recordcontainers/jdito/onUpdate.js</onUpdate>
+      <onDelete>%aditoprj%/entity/Member_entity/recordcontainers/jdito/onDelete.js</onDelete>
       <recordFieldMappings>
         <jDitoRecordFieldMapping>
           <name>UID.value</name>
@@ -506,6 +518,9 @@
         <jDitoRecordFieldMapping>
           <name>TARGETCONTEXT.value</name>
         </jDitoRecordFieldMapping>
+        <jDitoRecordFieldMapping>
+          <name>recordType.value</name>
+        </jDitoRecordFieldMapping>
       </recordFieldMappings>
     </jDitoRecordContainer>
   </recordContainers>
diff --git a/entity/Member_entity/documentation.adoc b/entity/Member_entity/documentation.adoc
index 575b84d329..3ad972dcc8 100644
--- a/entity/Member_entity/documentation.adoc
+++ b/entity/Member_entity/documentation.adoc
@@ -1,3 +1,14 @@
 = Member_entity
 
-Manages the project team in the sales project and in the campaign
\ No newline at end of file
+Manages the project team in the sales project and in the campaign
+
+This entity has two recordContainers:
+
+* db
+* jdito
+
+While for the must purposes the `db`-recordContainer is perfectly fine to use there is one case necessary for the `jdito`-recordContainer:
+
+Salesproject memebers are loaded via the ``-provider and therefor use the `jdito`-recodContainer. This recordContainer first collects the data from
+the `db`-rc and then loads additional memebers of the salesprojects district. The additional members are not modifyable, while those from the
+`db`-rc are.
\ No newline at end of file
diff --git a/entity/Member_entity/entityfields/recordtype/documentation.adoc b/entity/Member_entity/entityfields/recordtype/documentation.adoc
new file mode 100644
index 0000000000..3120dc65c2
--- /dev/null
+++ b/entity/Member_entity/entityfields/recordtype/documentation.adoc
@@ -0,0 +1 @@
+Field to differ between records from the OBJECTMEMBER-table and additional added district-members (only in the jdito-RecordContainer)
\ No newline at end of file
diff --git a/entity/Member_entity/grantDeleteProcess.js b/entity/Member_entity/grantDeleteProcess.js
new file mode 100644
index 0000000000..a494c8292d
--- /dev/null
+++ b/entity/Member_entity/grantDeleteProcess.js
@@ -0,0 +1,5 @@
+import("system.vars");
+import("system.result");
+
+var res = vars.get("$field.recordType") != "DistrictResponsible";
+result.string(res);
\ No newline at end of file
diff --git a/entity/Member_entity/grantUpdateProcess.js b/entity/Member_entity/grantUpdateProcess.js
new file mode 100644
index 0000000000..a494c8292d
--- /dev/null
+++ b/entity/Member_entity/grantUpdateProcess.js
@@ -0,0 +1,5 @@
+import("system.vars");
+import("system.result");
+
+var res = vars.get("$field.recordType") != "DistrictResponsible";
+result.string(res);
\ No newline at end of file
diff --git a/entity/Member_entity/recordcontainers/db/recordfieldmappings/recordtype.value/expression.js b/entity/Member_entity/recordcontainers/db/recordfieldmappings/recordtype.value/expression.js
new file mode 100644
index 0000000000..ecce98df0c
--- /dev/null
+++ b/entity/Member_entity/recordcontainers/db/recordfieldmappings/recordtype.value/expression.js
@@ -0,0 +1,4 @@
+import("system.result");
+
+//this value only differs in the jdito recordContainer
+result.string("'ObjectMember'");
\ No newline at end of file
diff --git a/entity/Member_entity/recordcontainers/jdito/contentProcess.js b/entity/Member_entity/recordcontainers/jdito/contentProcess.js
index b678a96f2f..7f4c73a178 100644
--- a/entity/Member_entity/recordcontainers/jdito/contentProcess.js
+++ b/entity/Member_entity/recordcontainers/jdito/contentProcess.js
@@ -13,7 +13,8 @@ var memberLoadingConfig = entities.createConfigForLoadingRows()
                                     .addParameter("ObjectType_param", vars.get("$param.ObjectType_param"))
                                     .fields(["#UID", "CONTACT_ID", "CONTACTROLE", "DEPARTMENT", "ICON", "IMAGE", "MEMBERROLE", "OBJECT_ROWID", "OBJECT_TYPE",
                                                 "OBJECTMEMBERID", "ORG_ORGID", "ORGANISATION_NAME", "PERSON_FIRSTNAME", "PERSON_FULL_NAME", 
-                                                "PERSON_LASTNAME", "PERSON_PERSID", "PERSON_SALUTATION", "PERSON_TITLE", "POSITION", "TARGETCONTEXT"]);
+                                                "PERSON_LASTNAME", "PERSON_PERSID", "PERSON_SALUTATION", "PERSON_TITLE", "POSITION", "TARGETCONTEXT",
+                                                "recordType"]);
 
 if (idValues)
 {
@@ -58,7 +59,7 @@ memberRows = memberRows.map(function(row)
     return [row["#UID"], row["OBJECTMEMBERID"], row["CONTACT_ID"], row["CONTACTROLE"], row["DEPARTMENT"], row["ICON"], row["IMAGE"], row["MEMBERROLE"], row["OBJECT_ROWID"], 
                 row["OBJECT_TYPE"], row["ORG_ORGID"], row["ORGANISATION_NAME"], row["PERSON_FIRSTNAME"], 
                 row["PERSON_FULL_NAME"], row["PERSON_LASTNAME"], row["PERSON_PERSID"], row["PERSON_SALUTATION"], row["PERSON_TITLE"], 
-                row["POSITION"], row["TARGETCONTEXT"]];
+                row["POSITION"], row["TARGETCONTEXT"], row["recordType"]];
 });
 
 var responsibleUids = [];
diff --git a/entity/Member_entity/recordcontainers/jdito/onDelete.js b/entity/Member_entity/recordcontainers/jdito/onDelete.js
new file mode 100644
index 0000000000..91caa64d3c
--- /dev/null
+++ b/entity/Member_entity/recordcontainers/jdito/onDelete.js
@@ -0,0 +1,16 @@
+import("system.vars");
+import("system.entities");
+
+//While there is no need to implement the insert or update process the delete process needs to be specified because the 
+//contexts viewmodeProvider for the editView only affect insert and update (delete has no view associated)
+//let's do the db-record container the job to remove the record (additional district records can never be removed):
+var rowData = vars.get("$local.rowdata");
+var memberId = rowData["OBJECTMEMBERID.value"];
+if (memberId)
+{
+    var config = entities.createConfigForDeletingRows().entity("Member_entity").uid(memberId);
+    entities.deleteRow(config);
+}
+else
+    throw new Error("Member_entity: Could not delete member entity because the given OBJECTMEMBERID was empty.");
+    
diff --git a/entity/Member_entity/recordcontainers/jdito/onInsert.js b/entity/Member_entity/recordcontainers/jdito/onInsert.js
new file mode 100644
index 0000000000..d86d21c988
--- /dev/null
+++ b/entity/Member_entity/recordcontainers/jdito/onInsert.js
@@ -0,0 +1 @@
+//No insert code, because it is done in via the contexts viewmodeProvider for the editView
\ No newline at end of file
diff --git a/entity/Member_entity/recordcontainers/jdito/onUpdate.js b/entity/Member_entity/recordcontainers/jdito/onUpdate.js
new file mode 100644
index 0000000000..1e04d32962
--- /dev/null
+++ b/entity/Member_entity/recordcontainers/jdito/onUpdate.js
@@ -0,0 +1 @@
+//No update code, because it is done in via the contexts viewmodeProvider for the editView
\ No newline at end of file
diff --git a/entity/Salesproject_entity/Salesproject_entity.aod b/entity/Salesproject_entity/Salesproject_entity.aod
index 7dd6be4989..412fcc0f20 100644
--- a/entity/Salesproject_entity/Salesproject_entity.aod
+++ b/entity/Salesproject_entity/Salesproject_entity.aod
@@ -259,7 +259,7 @@
       <dependency>
         <name>dependency</name>
         <entityName>Member_entity</entityName>
-        <fieldName>Links</fieldName>
+        <fieldName>WithDistrictResponsibles</fieldName>
       </dependency>
       <children>
         <entityParameter>
diff --git a/neonContext/Member/Member.aod b/neonContext/Member/Member.aod
index 664965d770..a2915648a3 100644
--- a/neonContext/Member/Member.aod
+++ b/neonContext/Member/Member.aod
@@ -24,4 +24,11 @@
       <view>MemberLookup_view</view>
     </neonViewReference>
   </references>
+  <viewmodeProvider>
+    <staticContentEntry>
+      <name>editView</name>
+      <key>editView</key>
+      <value>Links</value>
+    </staticContentEntry>
+  </viewmodeProvider>
 </neonContext>
-- 
GitLab