From 837688e9f9b379d4a5bdf6573ea7aa06271d64df Mon Sep 17 00:00:00 2001
From: "S.Listl" <S.Listl@SLISTL.aditosoftware.local>
Date: Tue, 2 Apr 2019 10:42:56 +0200
Subject: [PATCH] Employee and EmployeeRole

---
 .../recordcontainers/db/conditionProcess.js   |  4 ----
 .../EmployeeRole_entity.aod                   |  8 ++++++--
 .../{uid => role}/possibleItemsProcess.js     |  0
 .../recordcontainers/jdito/contentProcess.js  |  4 ++--
 .../recordcontainers/jdito/onInsert.js        |  2 +-
 .../recordcontainers/jdito/onUpdate.js        |  8 +++++---
 entity/Employee_entity/Employee_entity.aod    | 19 +++++++++---------
 entity/Employee_entity/afterOperatingState.js |  2 +-
 .../entityfields/image/onValueChange.js       |  4 ++++
 .../recordcontainers/jdito/contentProcess.js  | 17 ++++++++++------
 .../recordcontainers/jdito/onDelete.js        |  6 +++++-
 .../recordcontainers/jdito/onInsert.js        |  1 +
 .../recordcontainers/jdito/onUpdate.js        | 20 ++++++++++++++++++-
 .../Timetracking_entity.aod                   |  2 +-
 .../EmployeePreview_view.aod                  |  4 ++++
 .../EmployeeRoleEdit_view.aod                 |  2 +-
 .../EmployeeRoleFilter_view.aod               |  2 +-
 process/Employee_lib/process.js               | 14 ++++++++++++-
 18 files changed, 84 insertions(+), 35 deletions(-)
 rename entity/EmployeeRole_entity/entityfields/{uid => role}/possibleItemsProcess.js (100%)
 create mode 100644 entity/Employee_entity/entityfields/image/onValueChange.js

diff --git a/entity/AttributeRelation_entity/recordcontainers/db/conditionProcess.js b/entity/AttributeRelation_entity/recordcontainers/db/conditionProcess.js
index 623fac4157..d2b6666319 100644
--- a/entity/AttributeRelation_entity/recordcontainers/db/conditionProcess.js
+++ b/entity/AttributeRelation_entity/recordcontainers/db/conditionProcess.js
@@ -1,4 +1,3 @@
-import("system.logging");
 import("system.vars");
 import("system.db");
 import("system.result");
@@ -7,9 +6,6 @@ import("Sql_lib");
 var cond = SqlCondition.begin()
                    .andPrepareVars("AB_ATTRIBUTERELATION.OBJECT_ROWID", "$param.ObjectRowId_param");
 
-if (vars.exists("$param.ObjectRowId_param"))
-    logging.log(vars.get("$param.ObjectRowId_param"))
-
 if (vars.exists("$param.ObjectRowId_param") && vars.get("$param.ObjectRowId_param")
     && vars.exists("$param.FilteredAttributeIds_param") && vars.get("$param.FilteredAttributeIds_param"))
 {
diff --git a/entity/EmployeeRole_entity/EmployeeRole_entity.aod b/entity/EmployeeRole_entity/EmployeeRole_entity.aod
index f184486509..4fd240c55b 100644
--- a/entity/EmployeeRole_entity/EmployeeRole_entity.aod
+++ b/entity/EmployeeRole_entity/EmployeeRole_entity.aod
@@ -9,8 +9,6 @@
     </entityProvider>
     <entityField>
       <name>UID</name>
-      <title>Role</title>
-      <possibleItemsProcess>%aditoprj%/entity/EmployeeRole_entity/entityfields/uid/possibleItemsProcess.js</possibleItemsProcess>
     </entityField>
     <entityParameter>
       <name>UserTitle_param</name>
@@ -35,6 +33,11 @@
         </entityParameter>
       </children>
     </entityProvider>
+    <entityField>
+      <name>ROLE</name>
+      <title>Role</title>
+      <possibleItemsProcess>%aditoprj%/entity/EmployeeRole_entity/entityfields/role/possibleItemsProcess.js</possibleItemsProcess>
+    </entityField>
   </entityFields>
   <recordContainers>
     <jDitoRecordContainer>
@@ -46,6 +49,7 @@
       <onDelete>%aditoprj%/entity/EmployeeRole_entity/recordcontainers/jdito/onDelete.js</onDelete>
       <recordFields>
         <element>UID.value</element>
+        <element>ROLE.value</element>
       </recordFields>
     </jDitoRecordContainer>
   </recordContainers>
diff --git a/entity/EmployeeRole_entity/entityfields/uid/possibleItemsProcess.js b/entity/EmployeeRole_entity/entityfields/role/possibleItemsProcess.js
similarity index 100%
rename from entity/EmployeeRole_entity/entityfields/uid/possibleItemsProcess.js
rename to entity/EmployeeRole_entity/entityfields/role/possibleItemsProcess.js
diff --git a/entity/EmployeeRole_entity/recordcontainers/jdito/contentProcess.js b/entity/EmployeeRole_entity/recordcontainers/jdito/contentProcess.js
index 9f314aac5d..5e4ff916b0 100644
--- a/entity/EmployeeRole_entity/recordcontainers/jdito/contentProcess.js
+++ b/entity/EmployeeRole_entity/recordcontainers/jdito/contentProcess.js
@@ -5,7 +5,7 @@ import("system.tools");
 var roles = [];
 var userTitle = vars.exists("$param.UserTitle_param") && vars.get("$param.UserTitle_param");
 
-if (userTitle)
-    roles = tools.getRoles(userTitle).map(function (role) {return [role]});
+if (userTitle && tools.existUsers(userTitle))
+    roles = tools.getRoles(userTitle).map(function (role) {return [role, role]});
 
 result.object(roles);
\ No newline at end of file
diff --git a/entity/EmployeeRole_entity/recordcontainers/jdito/onInsert.js b/entity/EmployeeRole_entity/recordcontainers/jdito/onInsert.js
index 23ca713599..cd847798b8 100644
--- a/entity/EmployeeRole_entity/recordcontainers/jdito/onInsert.js
+++ b/entity/EmployeeRole_entity/recordcontainers/jdito/onInsert.js
@@ -3,7 +3,7 @@ import("system.vars");
 import("system.tools");
 
 var userTitle = vars.exists("$param.UserTitle_param") && vars.get("$param.UserTitle_param");
-var role = vars.get("$field.UID");
+var role = vars.get("$field.ROLE");
 
 if (userTitle)
 {    
diff --git a/entity/EmployeeRole_entity/recordcontainers/jdito/onUpdate.js b/entity/EmployeeRole_entity/recordcontainers/jdito/onUpdate.js
index 23ca713599..ae08df85d8 100644
--- a/entity/EmployeeRole_entity/recordcontainers/jdito/onUpdate.js
+++ b/entity/EmployeeRole_entity/recordcontainers/jdito/onUpdate.js
@@ -3,14 +3,16 @@ import("system.vars");
 import("system.tools");
 
 var userTitle = vars.exists("$param.UserTitle_param") && vars.get("$param.UserTitle_param");
-var role = vars.get("$field.UID");
+var oldRole = vars.get("$field.UID");
+var newRole = vars.get("$field.ROLE");
 
-if (userTitle)
+if (userTitle && oldRole != newRole)
 {    
     var user = tools.getUser(userTitle);
     var roles = tools.getRoles(userTitle);
     var roleObj = {};
-    roles = [role].concat(roles)
+    roleObj[oldRole] = true;
+    roles = [newRole].concat(roles)
         .filter(function (role) 
         {
             var exists = role in roleObj;
diff --git a/entity/Employee_entity/Employee_entity.aod b/entity/Employee_entity/Employee_entity.aod
index fae8cd5d30..ca823d2c54 100644
--- a/entity/Employee_entity/Employee_entity.aod
+++ b/entity/Employee_entity/Employee_entity.aod
@@ -12,14 +12,6 @@
     <entityProvider>
       <name>#PROVIDER</name>
       <lookupIdfield>CONTACT_ID</lookupIdfield>
-      <dependencies>
-        <entityDependency>
-          <name>823f9c90-c834-4e37-a47b-b3756fd28182</name>
-          <entityName>Timetracking_entity</entityName>
-          <fieldName>Employees</fieldName>
-          <isConsumer v="false" />
-        </entityDependency>
-      </dependencies>
     </entityProvider>
     <entityField>
       <name>UID</name>
@@ -37,7 +29,7 @@
       <title>Person</title>
       <consumer>Contacts</consumer>
       <linkedContext>Person</linkedContext>
-      <mandatory v="false" />
+      <mandatory v="true" />
       <onValidation>%aditoprj%/entity/Employee_entity/entityfields/contact_id/onValidation.js</onValidation>
     </entityField>
     <entityField>
@@ -139,8 +131,8 @@
       <name>IMAGE</name>
       <contentType>IMAGE</contentType>
       <searchable v="false" />
-      <state>READONLY</state>
       <valueProcess>%aditoprj%/entity/Employee_entity/entityfields/image/valueProcess.js</valueProcess>
+      <onValueChange>%aditoprj%/entity/Employee_entity/entityfields/image/onValueChange.js</onValueChange>
       <onValueChangeTypes>
         <element>MASK</element>
       </onValueChangeTypes>
@@ -148,6 +140,7 @@
     <entityField>
       <name>DEPARTMENT</name>
       <title>Department</title>
+      <state>INVISIBLE</state>
     </entityField>
     <entityField>
       <name>DESCRIPTION</name>
@@ -247,6 +240,12 @@
           <fieldName>Employees</fieldName>
           <isConsumer v="false" />
         </entityDependency>
+        <entityDependency>
+          <name>73f93f34-bfe9-48fd-b9ce-7f8ba46014c9</name>
+          <entityName>Timetracking_entity</entityName>
+          <fieldName>Employees</fieldName>
+          <isConsumer v="false" />
+        </entityDependency>
       </dependencies>
       <children>
         <entityParameter>
diff --git a/entity/Employee_entity/afterOperatingState.js b/entity/Employee_entity/afterOperatingState.js
index 96b470c432..bfb14f69c3 100644
--- a/entity/Employee_entity/afterOperatingState.js
+++ b/entity/Employee_entity/afterOperatingState.js
@@ -3,4 +3,4 @@ import("system.neon");
 import("system.vars");
 
 if (vars.get("$sys.operatingstate") == neon.OPERATINGSTATE_NEW)
-    neon.setFieldValue("$field.UID", "");
\ No newline at end of file
+    neon.setFieldValue("$field.UID", ""); //UID = user-title, should be initially empty
\ No newline at end of file
diff --git a/entity/Employee_entity/entityfields/image/onValueChange.js b/entity/Employee_entity/entityfields/image/onValueChange.js
new file mode 100644
index 0000000000..8698d90353
--- /dev/null
+++ b/entity/Employee_entity/entityfields/image/onValueChange.js
@@ -0,0 +1,4 @@
+import("Entity_lib");
+
+// TODO: also there is currently no good way to do updates with fields not connected to the record container. Workaround: imagevariable and update in onDBUpdate Process #1030023
+FieldChanges.setChange("$field.IMAGE");
\ No newline at end of file
diff --git a/entity/Employee_entity/recordcontainers/jdito/contentProcess.js b/entity/Employee_entity/recordcontainers/jdito/contentProcess.js
index f09c6c4a66..917242b94f 100644
--- a/entity/Employee_entity/recordcontainers/jdito/contentProcess.js
+++ b/entity/Employee_entity/recordcontainers/jdito/contentProcess.js
@@ -4,14 +4,21 @@ import("system.tools");
 import("Util_lib");
 import("Contact_lib");
 
+var filter = vars.exists("$local.filter") && vars.get("$local.filter");
+
 var users;
 if (vars.exists("$local.idvalues") && vars.get("$local.idvalues"))
-    users = vars.get("$local.idvalues");
+    users = [tools.getUser(vars.get("$local.idvalues"), tools.PROFILE_FULL)];
 else
-    users = tools.getStoredUsers().map(function (row) {return row[1];});
+{
+    var values = ["true", "false"];
+    if (vars.exists("$param.OnlyActives_param") && vars.get("$param.OnlyActives_param") == "true")
+        values = ["true"];
+    users = tools.getUsersByAttribute(tools.ISACTIVE, values, tools.PROFILE_FULL);
+}
+
 users = users.map(function (user)
 {
-    user = tools.getUser(user, tools.PROFILE_FULL);
     return [
         user[tools.TITLE],
         user[tools.TITLE],
@@ -25,8 +32,6 @@ users = users.map(function (user)
         user[tools.PARAMS][tools.FRAME_STOREDSEARCHES]
     ];
 });
-ArrayUtils.sort2d(users, 0, true, false);
-
-var filter = vars.exists("$local.filter") && vars.get("$local.filter");
+ArrayUtils.sort2d(users, 0, true, false); //sort by username
 
 result.object(users);
\ No newline at end of file
diff --git a/entity/Employee_entity/recordcontainers/jdito/onDelete.js b/entity/Employee_entity/recordcontainers/jdito/onDelete.js
index c6dddbee3d..b3064b9528 100644
--- a/entity/Employee_entity/recordcontainers/jdito/onDelete.js
+++ b/entity/Employee_entity/recordcontainers/jdito/onDelete.js
@@ -1,4 +1,8 @@
+import("system.neon");
 import("system.vars");
 import("system.tools");
+import("Employee_lib");
 
-tools.deleteUser(vars.get("$field.UID"));
\ No newline at end of file
+//TODO: the current user should not delete himself, put this condition in grantDelete when available
+if (EmployeeUtils.getCurrentUserName() != vars.get("$field.UID"))
+    tools.deleteUser(vars.get("$field.UID"));
\ No newline at end of file
diff --git a/entity/Employee_entity/recordcontainers/jdito/onInsert.js b/entity/Employee_entity/recordcontainers/jdito/onInsert.js
index 1ab06f0014..3693a103f6 100644
--- a/entity/Employee_entity/recordcontainers/jdito/onInsert.js
+++ b/entity/Employee_entity/recordcontainers/jdito/onInsert.js
@@ -10,6 +10,7 @@ params[tools.EMAIL] = vars.get("$field.EMAIL_ADDRESS");
 params[tools.CALENDARID] = vars.get("$field.EMAIL_ADDRESS");
 params[tools.CONTACTID] = vars.get("$field.CONTACT_ID");
 params[tools.DESCRIPTION] = vars.get("$field.DESCRIPTION");
+params[tools.ISACTIVE] = vars.get("$field.ISACTIVE");
 
 user[tools.TITLE] = vars.get("$field.UID");
 user[tools.PARAMS] = params;
diff --git a/entity/Employee_entity/recordcontainers/jdito/onUpdate.js b/entity/Employee_entity/recordcontainers/jdito/onUpdate.js
index d3fd1e6b72..5cc71867cd 100644
--- a/entity/Employee_entity/recordcontainers/jdito/onUpdate.js
+++ b/entity/Employee_entity/recordcontainers/jdito/onUpdate.js
@@ -1,10 +1,28 @@
+import("system.db");
 import("system.logging");
 import("system.vars");
 import("system.tools");
+import("Person_lib");
+import("Entity_lib");
 
-var user = tools.getUser(vars.get("$field.TITLE_ORIGINAL"));
+// TODO: this is a workaround for missing possibility to react on changes of fields not connected to record Contqainer #1030023
+FieldChanges.assimilateChangeAndDispose("$field.IMAGE", function (state, value)
+{
+    var personId = db.cell(SqlCondition.begin()
+        .andPrepareVars("CONTACT.CONTACTID", "$field.CONTACT_ID")
+        .buildSql("select PERSON_ID from CONTACT")
+    );
+    if (state == FieldChanges.STATE_CHANGED())
+        PersUtils.setImage(personId, value);
+    else
+        PersUtils.removeImage(personId);
+});
+
+var user = {};
+user[tools.PARAMS] = [];
 
 user[tools.TITLE] = vars.get("$field.UID");
+user[tools.PARAMS][tools.ISACTIVE] = vars.get("$field.ISACTIVE");
 user[tools.PARAMS][tools.FIRSTNAME] = vars.get("$field.FIRSTNAME");
 user[tools.PARAMS][tools.LASTNAME] = vars.get("$field.LASTNAME");
 user[tools.PARAMS][tools.EMAIL] = vars.get("$field.EMAIL_ADDRESS");
diff --git a/entity/Timetracking_entity/Timetracking_entity.aod b/entity/Timetracking_entity/Timetracking_entity.aod
index 443bfc6a97..ec6bf91c59 100644
--- a/entity/Timetracking_entity/Timetracking_entity.aod
+++ b/entity/Timetracking_entity/Timetracking_entity.aod
@@ -94,7 +94,7 @@
       <dependency>
         <name>dependency</name>
         <entityName>Employee_entity</entityName>
-        <fieldName>#PROVIDER</fieldName>
+        <fieldName>Employees</fieldName>
       </dependency>
     </entityConsumer>
   </entityFields>
diff --git a/neonView/EmployeePreview_view/EmployeePreview_view.aod b/neonView/EmployeePreview_view/EmployeePreview_view.aod
index 02a259276e..41c53f7a3b 100644
--- a/neonView/EmployeePreview_view/EmployeePreview_view.aod
+++ b/neonView/EmployeePreview_view/EmployeePreview_view.aod
@@ -20,6 +20,10 @@
       <showDrawer v="true" />
       <entityField>#ENTITY</entityField>
       <fields>
+        <entityFieldLink>
+          <name>68755289-a351-4915-8626-52f023e237f8</name>
+          <entityField>ISACTIVE</entityField>
+        </entityFieldLink>
         <entityFieldLink>
           <name>a5f8b519-26d8-4824-b9cf-9119c03b1bd8</name>
           <entityField>CONTACT_ID</entityField>
diff --git a/neonView/EmployeeRoleEdit_view/EmployeeRoleEdit_view.aod b/neonView/EmployeeRoleEdit_view/EmployeeRoleEdit_view.aod
index 3013b36d8e..adf9bda6f4 100644
--- a/neonView/EmployeeRoleEdit_view/EmployeeRoleEdit_view.aod
+++ b/neonView/EmployeeRoleEdit_view/EmployeeRoleEdit_view.aod
@@ -14,7 +14,7 @@
       <columns>
         <neonTableColumn>
           <name>ecaa1457-eb60-4116-a46c-9c91e2d3fd63</name>
-          <entityField>UID</entityField>
+          <entityField>ROLE</entityField>
         </neonTableColumn>
       </columns>
     </genericMultipleViewTemplate>
diff --git a/neonView/EmployeeRoleFilter_view/EmployeeRoleFilter_view.aod b/neonView/EmployeeRoleFilter_view/EmployeeRoleFilter_view.aod
index 1938871992..92602b2146 100644
--- a/neonView/EmployeeRoleFilter_view/EmployeeRoleFilter_view.aod
+++ b/neonView/EmployeeRoleFilter_view/EmployeeRoleFilter_view.aod
@@ -15,7 +15,7 @@
       <columns>
         <neonTableColumn>
           <name>ab1c8d39-fc29-42e8-8b8e-3557d544b272</name>
-          <entityField>UID</entityField>
+          <entityField>ROLE</entityField>
         </neonTableColumn>
       </columns>
     </tableViewTemplate>
diff --git a/process/Employee_lib/process.js b/process/Employee_lib/process.js
index 9a781c2556..ef3273b495 100644
--- a/process/Employee_lib/process.js
+++ b/process/Employee_lib/process.js
@@ -16,5 +16,17 @@ function EmployeeUtils () {}
  */
 EmployeeUtils.getCurrentContactId = function ()
 {
-    return tools.getCurrentUser()[tools.PARAMS][tools.CONTACTID];
+    var user = tools.getCurrentUser();
+    return user ? user[tools.PARAMS][tools.CONTACTID] : null;
+}
+
+/**
+ * Returns the username id of the current user
+ * 
+ * @return the username
+ */
+EmployeeUtils.getCurrentUserName = function ()
+{
+    var user = tools.getCurrentUser();
+    return user ? user[tools.TITLE] : null;
 }
\ No newline at end of file
-- 
GitLab