From c4349e432f0cf0efb1f39c24f67c190ed8fafa0d Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Thu, 23 May 2019 16:55:49 +0200
Subject: [PATCH] Classification admin improvements

---
 .../ClassificationAdmin_entity.aod            |  9 ++++++
 .../contentTitleProcess.js                    |  4 +++
 .../Classification_entity.aod                 | 17 ++++++++--
 .../classificationgroup/dropDownProcess.js    | 13 ++++++--
 .../classificationgroup/onValidation.js       | 17 ++++++++++
 .../classificationgroup/stateProcess.js       | 12 -------
 .../classificationgroup/titleProcess.js       | 13 ++++++++
 .../entityfields/edit/onActionProcess.js      |  2 ++
 .../grantDeleteProcess.js                     | 23 ++++++++++++++
 .../recordcontainers/jdito/contentProcess.js  | 14 +++++++--
 .../recordcontainers/jdito/onUpdate.js        | 21 +++++++++++++
 .../ClassificationAdminTree_view.aod          |  1 +
 .../ClassificationScoreMultipleEdit_view.aod  |  5 +--
 process/Classification_lib/process.js         | 31 +++++++++++++++++++
 14 files changed, 162 insertions(+), 20 deletions(-)
 create mode 100644 entity/ClassificationAdmin_entity/contentTitleProcess.js
 create mode 100644 entity/Classification_entity/entityfields/classificationgroup/onValidation.js
 delete mode 100644 entity/Classification_entity/entityfields/classificationgroup/stateProcess.js
 create mode 100644 entity/Classification_entity/entityfields/classificationgroup/titleProcess.js
 create mode 100644 entity/Classification_entity/grantDeleteProcess.js

diff --git a/entity/ClassificationAdmin_entity/ClassificationAdmin_entity.aod b/entity/ClassificationAdmin_entity/ClassificationAdmin_entity.aod
index b487049e93..cdf27e635e 100644
--- a/entity/ClassificationAdmin_entity/ClassificationAdmin_entity.aod
+++ b/entity/ClassificationAdmin_entity/ClassificationAdmin_entity.aod
@@ -8,12 +8,21 @@
   <grantCreate v="false" />
   <grantUpdate v="false" />
   <grantDelete v="false" />
+  <contentTitleProcess>%aditoprj%/entity/ClassificationAdmin_entity/contentTitleProcess.js</contentTitleProcess>
   <usePermissions v="true" />
   <titlePlural>Classifications</titlePlural>
   <recordContainer>jdito</recordContainer>
   <entityFields>
     <entityProvider>
       <name>#PROVIDER</name>
+      <dependencies>
+        <entityDependency>
+          <name>75825cca-fbff-45a0-9a46-b1b0f639ae6a</name>
+          <entityName>Classification_entity</entityName>
+          <fieldName>ClassificationTypes</fieldName>
+          <isConsumer v="false" />
+        </entityDependency>
+      </dependencies>
     </entityProvider>
     <entityField>
       <name>UID</name>
diff --git a/entity/ClassificationAdmin_entity/contentTitleProcess.js b/entity/ClassificationAdmin_entity/contentTitleProcess.js
new file mode 100644
index 0000000000..552a186ab3
--- /dev/null
+++ b/entity/ClassificationAdmin_entity/contentTitleProcess.js
@@ -0,0 +1,4 @@
+import("system.vars");
+import("system.result");
+
+result.string(vars.get("$field.UID.displayValue"))
\ No newline at end of file
diff --git a/entity/Classification_entity/Classification_entity.aod b/entity/Classification_entity/Classification_entity.aod
index f0bf63ede5..e2d4e933f2 100644
--- a/entity/Classification_entity/Classification_entity.aod
+++ b/entity/Classification_entity/Classification_entity.aod
@@ -3,6 +3,7 @@
   <name>Classification_entity</name>
   <majorModelMode>DISTRIBUTED</majorModelMode>
   <title>Classification</title>
+  <grantDeleteProcess>%aditoprj%/entity/Classification_entity/grantDeleteProcess.js</grantDeleteProcess>
   <afterOperatingState>%aditoprj%/entity/Classification_entity/afterOperatingState.js</afterOperatingState>
   <titlePlural>Classifications</titlePlural>
   <recordContainer>jdito</recordContainer>
@@ -78,14 +79,14 @@
     </entityField>
     <entityField>
       <name>CLASSIFICATIONGROUP</name>
-      <title>Classification group</title>
       <contentType>TEXT</contentType>
       <mandatoryProcess>%aditoprj%/entity/Classification_entity/entityfields/classificationgroup/mandatoryProcess.js</mandatoryProcess>
       <dropDownProcess>%aditoprj%/entity/Classification_entity/entityfields/classificationgroup/dropDownProcess.js</dropDownProcess>
       <textInputAllowed v="true" />
       <groupable v="true" />
-      <stateProcess>%aditoprj%/entity/Classification_entity/entityfields/classificationgroup/stateProcess.js</stateProcess>
+      <titleProcess>%aditoprj%/entity/Classification_entity/entityfields/classificationgroup/titleProcess.js</titleProcess>
       <displayValueProcess>%aditoprj%/entity/Classification_entity/entityfields/classificationgroup/displayValueProcess.js</displayValueProcess>
+      <onValidation>%aditoprj%/entity/Classification_entity/entityfields/classificationgroup/onValidation.js</onValidation>
     </entityField>
     <entityField>
       <name>CLASSIFICATIONTYPE_ID</name>
@@ -137,6 +138,9 @@
     </entityParameter>
     <entityField>
       <name>CLASSIFICATIONTYPE</name>
+      <consumer>ClassificationTypes</consumer>
+      <groupable v="true" />
+      <state>EDITABLE</state>
       <valueProcess>%aditoprj%/entity/Classification_entity/entityfields/classificationtype/valueProcess.js</valueProcess>
     </entityField>
     <entityProvider>
@@ -178,6 +182,15 @@
     <entityField>
       <name>SCORE_RESULTS</name>
     </entityField>
+    <entityConsumer>
+      <name>ClassificationTypes</name>
+      <fieldType>DEPENDENCY_OUT</fieldType>
+      <dependency>
+        <name>dependency</name>
+        <entityName>ClassificationAdmin_entity</entityName>
+        <fieldName>#PROVIDER</fieldName>
+      </dependency>
+    </entityConsumer>
   </entityFields>
   <recordContainers>
     <jDitoRecordContainer>
diff --git a/entity/Classification_entity/entityfields/classificationgroup/dropDownProcess.js b/entity/Classification_entity/entityfields/classificationgroup/dropDownProcess.js
index bd94927bda..9151a529e9 100644
--- a/entity/Classification_entity/entityfields/classificationgroup/dropDownProcess.js
+++ b/entity/Classification_entity/entityfields/classificationgroup/dropDownProcess.js
@@ -1,4 +1,13 @@
+import("system.logging");
+import("Classification_lib");
+import("system.neon");
+import("system.vars");
 import("system.db");
 import("system.result");
-
-result.object(db.table("select distinct CLASSIFICATIONGROUP, CLASSIFICATIONGROUP from CLASSIFICATIONTYPE order by CLASSIFICATIONGROUP"));
\ No newline at end of file
+logging.log(vars.get("$param.IsAdminMode_param"))
+if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW && vars.exists("$param.IsAdminMode_param") && vars.get("$param.IsAdminMode_param") == "1")
+{
+   logging.log(vars.get("$param.ClassificationType_param"))
+    logging.log(ClassificationUtils.getAllGroups(vars.get("$param.ClassificationType_param"), true).toSource())
+    result.object(ClassificationUtils.getAllGroups(vars.get("$param.ClassificationType_param"), true));
+}
\ No newline at end of file
diff --git a/entity/Classification_entity/entityfields/classificationgroup/onValidation.js b/entity/Classification_entity/entityfields/classificationgroup/onValidation.js
new file mode 100644
index 0000000000..4bce0a846e
--- /dev/null
+++ b/entity/Classification_entity/entityfields/classificationgroup/onValidation.js
@@ -0,0 +1,17 @@
+import("system.neon");
+import("system.translate");
+import("system.result");
+import("system.vars");
+import("Classification_lib");
+import("Entity_lib");
+
+if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_EDIT && vars.exists("$param.IsAdminMode_param") && vars.get("$param.IsAdminMode_param") == "1")
+{
+    var newGroup = ProcessHandlingUtils.getOnValidationValue();
+    var groups = ClassificationUtils.getAllGroups(vars.get("$param.ClassificationType_param"), false)
+
+    if (groups.indexOf(newGroup, 0) != -1 && ClassificationUtils.getGroupFromDb(vars.get("$field.CLASSIFICATIONTYPEID")) != newGroup)
+    {
+        result.string(translate.text("The new Classification name already exists."));
+    }
+}
diff --git a/entity/Classification_entity/entityfields/classificationgroup/stateProcess.js b/entity/Classification_entity/entityfields/classificationgroup/stateProcess.js
deleted file mode 100644
index 626da788b0..0000000000
--- a/entity/Classification_entity/entityfields/classificationgroup/stateProcess.js
+++ /dev/null
@@ -1,12 +0,0 @@
-import("system.neon");
-import("system.result");
-import("system.vars");
-
-if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW)
-{
-    result.string(neon.COMPONENTSTATE_AUTO);
-}
-else
-{
-    result.string(neon.COMPONENTSTATE_DISABLED);
-}
\ No newline at end of file
diff --git a/entity/Classification_entity/entityfields/classificationgroup/titleProcess.js b/entity/Classification_entity/entityfields/classificationgroup/titleProcess.js
new file mode 100644
index 0000000000..69092d7e4e
--- /dev/null
+++ b/entity/Classification_entity/entityfields/classificationgroup/titleProcess.js
@@ -0,0 +1,13 @@
+import("system.neon");
+import("system.translate");
+import("system.vars");
+import("system.result");
+
+if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW && vars.exists("$param.IsAdminMode_param") && vars.exists("$param.IsAdminMode_param") == "1") 
+{
+    result.string(translate.text("Group"));
+}
+else
+{
+    result.string(translate.text("Group name"));
+}
\ No newline at end of file
diff --git a/entity/Classification_entity/entityfields/edit/onActionProcess.js b/entity/Classification_entity/entityfields/edit/onActionProcess.js
index 8d018fd74f..84060934fa 100644
--- a/entity/Classification_entity/entityfields/edit/onActionProcess.js
+++ b/entity/Classification_entity/entityfields/edit/onActionProcess.js
@@ -1,3 +1,4 @@
+import("system.logging");
 import("system.vars");
 import("system.neon");
 import("KeywordRegistry_basic");
@@ -5,6 +6,7 @@ import("KeywordRegistry_basic");
 var selection = vars.get("$sys.selectionRows");
 if (selection.length > 0)
 {
+    logging.log(selection.toSource())
     var params = {
         "ClassificationType_param" : vars.get("$param.ClassificationType_param"),
         "ObjectType_param" : vars.get("$param.ObjectType_param"),
diff --git a/entity/Classification_entity/grantDeleteProcess.js b/entity/Classification_entity/grantDeleteProcess.js
new file mode 100644
index 0000000000..9a1336d5cf
--- /dev/null
+++ b/entity/Classification_entity/grantDeleteProcess.js
@@ -0,0 +1,23 @@
+import("system.logging");
+import("system.result");
+import("system.db");
+import("system.vars");
+import("Sql_lib");
+
+/*logging.log(db.cell(SqlCondition.begin()
+                        .andPrepare("CLASSIFICATION.CLASSIFICATIONTYPE_ID", vars.get("$field.CLASSIFICATIONTYPEID"))
+                        .buildSql("select count(*) from CLASSIFICATION", "1=2")))
+/*result.object(parseInt(db.cell(SqlCondition.begin()
+                        .andPrepare("CLASSIFICATION.CLASSIFICATIONTYPE_ID", vars.get("$field.CLASSIFICATIONTYPEID"))
+                        .buildSql("select count(*) from CLASSIFICATION", "1=2"))) <= 0);*/
+
+//{
+    /*db.deleteData("CLASSIFICATION", "CLASSIFICATION.CLASSIFICATIONTYPE_ID in (" + db.translateStatement(
+                       SqlCondition.begin()
+                                   .andPrepare("CLASSIFICATIONTYPE.SCORETYPE", type)
+                                   .buildSql("select CLASSIFICATIONTYPE.CLASSIFICATIONTYPEID from CLASSIFICATIONTYPE", "1=2")) + ")");
+    db.deleteData("CLASSIFICATIONTYPE", SqlCondition.equals("CLASSIFICATIONTYPE.SCORETYPE", type, "1=2"));
+    db.deleteData("CLASSIFICATIONSCORE", SqlCondition.equals("CLASSIFICATIONSCORE.SCORETYPE", type, "1=2"));*/
+    
+//}
+//neon.refreshAll();
\ No newline at end of file
diff --git a/entity/Classification_entity/recordcontainers/jdito/contentProcess.js b/entity/Classification_entity/recordcontainers/jdito/contentProcess.js
index 9e4691657f..d0d8001f50 100644
--- a/entity/Classification_entity/recordcontainers/jdito/contentProcess.js
+++ b/entity/Classification_entity/recordcontainers/jdito/contentProcess.js
@@ -1,3 +1,4 @@
+import("system.logging");
 import("Classification_lib");
 import("system.vars");
 import("Sql_lib");
@@ -46,9 +47,18 @@ if (!classificationId)
             .andSqlCondition(SqlCondition.begin().orPrepareVars("CLASSIFICATION.OBJECT_ROWID", "$param.ObjectRowid_param")
                                                      .or("CLASSIFICATION.OBJECT_ROWID is null"))
 }
-    
+
+logging.log(db.translateStatement(cond.buildSql("select case when CLASSIFICATIONID is not null then 'C,' || CLASSIFICATIONID else 'T,' || CLASSIFICATIONTYPEID end, CLASSIFICATIONID, CLASSIFICATIONGROUP, CLASSIFICATIONSCORE_ID, CLASSIFICATIONTYPEID, CLASSIFICATIONTYPE_ID, OBJECT_TYPE, OBJECT_ROWID, SCORETYPE, CLASSIFICATIONTYPE.CLASSIFICATIONTYPE from CLASSIFICATION \n\
+                                        right join CLASSIFICATIONTYPE on " + db.translateCondition(SqlCondition.begin()
+                                                                                         .and("CLASSIFICATIONTYPE_ID = CLASSIFICATIONTYPEID")
+                                                                                         .andPrepareVars("CLASSIFICATION.OBJECT_TYPE", "$param.ObjectType_param")
+                                                                                         .andPrepareVars("CLASSIFICATION.OBJECT_ROWID", "$param.ObjectRowid_param").build()), (admin ? "1=1" : "1=2"), "order by CLASSIFICATIONGROUP asc")))
+
 result.object(db.table(cond.buildSql("select case when CLASSIFICATIONID is not null then 'C,' || CLASSIFICATIONID else 'T,' || CLASSIFICATIONTYPEID end, CLASSIFICATIONID, CLASSIFICATIONGROUP, CLASSIFICATIONSCORE_ID, CLASSIFICATIONTYPEID, CLASSIFICATIONTYPE_ID, OBJECT_TYPE, OBJECT_ROWID, SCORETYPE, CLASSIFICATIONTYPE.CLASSIFICATIONTYPE from CLASSIFICATION \n\
-                                        right join CLASSIFICATIONTYPE on CLASSIFICATIONTYPE_ID = CLASSIFICATIONTYPEID", "1=2", "order by CLASSIFICATIONGROUP asc"))
+                                        right join CLASSIFICATIONTYPE on " + db.translateCondition(SqlCondition.begin()
+                                                                                         .and("CLASSIFICATIONTYPE_ID = CLASSIFICATIONTYPEID")
+                                                                                         .andPrepareVars("CLASSIFICATION.OBJECT_TYPE", "$param.ObjectType_param")
+                                                                                         .andPrepareVars("CLASSIFICATION.OBJECT_ROWID", "$param.ObjectRowid_param").build()), (admin ? "1=1" : "1=2"), "order by CLASSIFICATIONGROUP asc"))
     .map(function(pRow)
     {
         pRow.push(scores);
diff --git a/entity/Classification_entity/recordcontainers/jdito/onUpdate.js b/entity/Classification_entity/recordcontainers/jdito/onUpdate.js
index f66b5fc329..9a5b1a4a15 100644
--- a/entity/Classification_entity/recordcontainers/jdito/onUpdate.js
+++ b/entity/Classification_entity/recordcontainers/jdito/onUpdate.js
@@ -1,3 +1,5 @@
+import("Classification_lib");
+import("system.logging");
 import("system.neon");
 import("system.util");
 import("Sql_lib");
@@ -6,6 +8,7 @@ import("system.vars");
 
 var changed = vars.get("$local.changed");
 
+logging.log(vars.get("$field.CLASSIFICATIONID"))
 // check if it already exists. Else it only exists as ScoreType but has no Classification-Entry
 if (vars.get("$field.CLASSIFICATIONID"))
 {
@@ -32,4 +35,22 @@ else if (vars.get("$field.CLASSIFICATIONSCORE_ID"))
     ]);
     
     neon.setFieldValue("$field.CLASSIFICATIONID", newId);
+}
+
+// update classificationgroup-name if it was changed (only in admin)
+if (vars.exists("$param.IsAdminMode_param") && vars.exists("$param.IsAdminMode_param") == "1")
+{
+    changed.forEach(function(change) 
+    {
+        logging.log(change)
+        switch (change)
+        {
+            case "CLASSIFICATIONGROUP.value":
+                var oldGroupName = ClassificationUtils.getGroupFromDb(vars.get("$field.CLASSIFICATIONTYPEID"));
+                var newGroupName = vars.get("$field.CLASSIFICATIONGROUP");
+                
+                ClassificationUtils.changeGroupName(oldGroupName, newGroupName, vars.get("$field.CLASSIFICATIONTYPE"))
+        }
+    })
+    
 }
\ No newline at end of file
diff --git a/neonView/ClassificationAdminTree_view/ClassificationAdminTree_view.aod b/neonView/ClassificationAdminTree_view/ClassificationAdminTree_view.aod
index 297fa04cf2..2d44f2e088 100644
--- a/neonView/ClassificationAdminTree_view/ClassificationAdminTree_view.aod
+++ b/neonView/ClassificationAdminTree_view/ClassificationAdminTree_view.aod
@@ -14,6 +14,7 @@
       <defaultGroupFields>
         <element>CLASSIFICATIONGROUP</element>
       </defaultGroupFields>
+      <hideContentSearch v="true" />
       <showChildrenCount v="false" />
       <entryAction>edit</entryAction>
       <entityField>#ENTITY</entityField>
diff --git a/neonView/ClassificationScoreMultipleEdit_view/ClassificationScoreMultipleEdit_view.aod b/neonView/ClassificationScoreMultipleEdit_view/ClassificationScoreMultipleEdit_view.aod
index 6a1e41c260..b8c8b7594b 100644
--- a/neonView/ClassificationScoreMultipleEdit_view/ClassificationScoreMultipleEdit_view.aod
+++ b/neonView/ClassificationScoreMultipleEdit_view/ClassificationScoreMultipleEdit_view.aod
@@ -4,9 +4,10 @@
   <majorModelMode>DISTRIBUTED</majorModelMode>
   <isSmall v="true" />
   <layout>
-    <noneLayout>
+    <drawerLayout>
       <name>layout</name>
-    </noneLayout>
+      <layoutCaption>Possible Values</layoutCaption>
+    </drawerLayout>
   </layout>
   <children>
     <genericMultipleViewTemplate>
diff --git a/process/Classification_lib/process.js b/process/Classification_lib/process.js
index 7e9c2d89fa..a790e3fa72 100644
--- a/process/Classification_lib/process.js
+++ b/process/Classification_lib/process.js
@@ -1,3 +1,5 @@
+import("system.logging");
+import("system.vars");
 import("system.db");
 import("Sql_lib");
 
@@ -62,4 +64,33 @@ ClassificationUtils.mapToClass = function(pScore)
         return "A";
     
     return "_";
+}
+
+ClassificationUtils.getAllGroups = function(pClassificationtype, twoCol)
+{
+    var sql = SqlCondition.begin()
+                .andPrepare("CLASSIFICATIONTYPE.CLASSIFICATIONTYPE", pClassificationtype)
+                .buildSql("select distinct CLASSIFICATIONGROUP" + (twoCol ? ", CLASSIFICATIONGROUP" : "") + " from CLASSIFICATIONTYPE", "", " order by CLASSIFICATIONGROUP")
+    
+    if (twoCol)
+        return db.table(sql);
+    else
+        return db.array(db.COLUMN, sql);
+}
+
+ClassificationUtils.getGroupFromDb = function(pClassificationtypeId)
+{
+    return db.cell(SqlCondition.begin()
+                .andPrepare("CLASSIFICATIONTYPE.CLASSIFICATIONTYPEID", pClassificationtypeId)
+                .buildSql("select CLASSIFICATIONGROUP from CLASSIFICATIONTYPE", "", " order by CLASSIFICATIONGROUP"))
+}
+
+ClassificationUtils.changeGroupName = function(pOldName, pNewName, pClassificationType)
+{
+    var groups = ClassificationUtils.getAllGroups(pClassificationType, false)
+                
+    if (groups.indexOf(pNewName, 0) == -1)
+    {
+        db.updateData("CLASSIFICATIONTYPE", ["CLASSIFICATIONGROUP"], null, [pNewName], SqlCondition.equals("CLASSIFICATIONTYPE.CLASSIFICATIONGROUP", pOldName, "1=2"));
+    }
 }
\ No newline at end of file
-- 
GitLab