From 3fe1e4051aacdda5f1043251f0441fea2f139e15 Mon Sep 17 00:00:00 2001
From: Simon Leipold <s.leipold@adito.de>
Date: Wed, 19 Feb 2020 10:48:59 +0100
Subject: [PATCH] [Projekt: Entwicklung - Neon][TicketNr.: 1049945][Rollen -
 Fehler bei Zuweisung von Kindrollen]

---
 entity/Role_entity/Role_entity.aod            |  1 +
 .../rolechildrens/onValidation.js             | 70 +++++++++++++++++++
 2 files changed, 71 insertions(+)
 create mode 100644 entity/Role_entity/entityfields/rolechildrens/onValidation.js

diff --git a/entity/Role_entity/Role_entity.aod b/entity/Role_entity/Role_entity.aod
index 667568f4c04..526b317caff 100644
--- a/entity/Role_entity/Role_entity.aod
+++ b/entity/Role_entity/Role_entity.aod
@@ -126,6 +126,7 @@
     </entityField>
     <entityConsumer>
       <name>RoleChildrens</name>
+      <onValidation>%aditoprj%/entity/Role_entity/entityfields/rolechildrens/onValidation.js</onValidation>
       <dependency>
         <name>dependency</name>
         <entityName>RoleChildren_entity</entityName>
diff --git a/entity/Role_entity/entityfields/rolechildrens/onValidation.js b/entity/Role_entity/entityfields/rolechildrens/onValidation.js
new file mode 100644
index 00000000000..353d075913c
--- /dev/null
+++ b/entity/Role_entity/entityfields/rolechildrens/onValidation.js
@@ -0,0 +1,70 @@
+import("system.result");
+import("Sql_lib");
+import("system.logging");
+import("system.vars");
+
+var changedRows = vars.get("$field.RoleChildrens.changedRows");
+var insertedRows = vars.get("$field.RoleChildrens.insertedRows");
+
+// check for duplicates
+for each (let row in insertedRows) {
+    if (isRoleAlreadyChildren(row)) {
+        result.string("No duplicates allowed!");
+    }
+}
+
+for each (let row in changedRows) {
+    if (isRoleAlreadyChildren(row)) {
+        result.string("No duplicates allowed!");
+    }
+}
+
+function isRoleAlreadyChildren(pRole) {
+    var alias = SqlUtils.getSystemAlias();
+    var parentRoleName = vars.get("$field.ROLENAME")
+    var childRoles = newSelect("CHILD_ROLE", alias)
+    .from("ASYS_ROLES_CHILDREN")
+    .where("ASYS_ROLES_CHILDREN.PARENT_ROLE", parentRoleName)
+    .table();
+
+    // check for duplicates in database
+    for each (let child in childRoles) {
+        if (child == pRole.CHILD_ROLE) {
+            return true;
+        }
+    }
+    
+    // check for duplicate in changed or inserted rows
+    if (checkDuplicateInObject("CHILD_ROLE", changedRows) || checkDuplicateInObject("CHILD_ROLE", insertedRows))
+        return true;
+    
+    // crosscheck for duplicates in inserted and changed rows
+    for each (let insRow in insertedRows) {
+        for each (let chaRow in changedRows) {
+            if (insRow.CHILD_ROLE == chaRow.CHILD_ROLE) {
+                return true;
+            }
+        }
+    }
+    return false;
+}
+
+function checkDuplicateInObject(propertyName, inputArray) {
+    var seenDuplicate = false,
+    testObject = {};
+
+    inputArray.map(function(item) {
+        var itemPropertyName = item[propertyName];    
+        if (itemPropertyName in testObject) {
+            testObject[itemPropertyName].duplicate = true;
+            item.duplicate = true;
+            seenDuplicate = true;
+        }
+        else {
+            testObject[itemPropertyName] = item;
+            delete item.duplicate;
+        }
+    });
+
+    return seenDuplicate;
+}
\ No newline at end of file
-- 
GitLab