From ccf8ab8eb324e80df34d29c0057a80f596fbdb28 Mon Sep 17 00:00:00 2001
From: Martin Groppe <m.groppe@adito.de>
Date: Tue, 1 Jun 2021 15:28:43 +0000
Subject: [PATCH] M 1074934 campaignstep sorting changeable

---
 .../CampaignStep_entity.aod                   | 19 +++++++
 .../children/movedown/onActionProcess.js      |  9 ++++
 .../group/children/movedown/stateProcess.js   | 27 ++++++++++
 .../group/children/moveup/onActionProcess.js  |  8 +++
 .../group/children/moveup/stateProcess.js     | 25 ++++++++++
 .../recordcontainers/db/onDBDelete.js         | 18 +++++++
 .../CampaignStepFilter_view.aod               |  2 +
 process/Campaign_lib/process.js               | 50 +++++++++++++++++++
 8 files changed, 158 insertions(+)
 create mode 100644 entity/CampaignStep_entity/entityfields/group/children/movedown/onActionProcess.js
 create mode 100644 entity/CampaignStep_entity/entityfields/group/children/movedown/stateProcess.js
 create mode 100644 entity/CampaignStep_entity/entityfields/group/children/moveup/onActionProcess.js
 create mode 100644 entity/CampaignStep_entity/entityfields/group/children/moveup/stateProcess.js

diff --git a/entity/CampaignStep_entity/CampaignStep_entity.aod b/entity/CampaignStep_entity/CampaignStep_entity.aod
index c090c20d4c..98f8c9ab6b 100644
--- a/entity/CampaignStep_entity/CampaignStep_entity.aod
+++ b/entity/CampaignStep_entity/CampaignStep_entity.aod
@@ -379,10 +379,29 @@
         </entityParameter>
       </children>
     </entityConsumer>
+    <entityActionGroup>
+      <name>group</name>
+      <children>
+        <entityActionField>
+          <name>moveUp</name>
+          <onActionProcess>%aditoprj%/entity/CampaignStep_entity/entityfields/group/children/moveup/onActionProcess.js</onActionProcess>
+          <iconId>VAADIN:ARROW_UP</iconId>
+          <state>DISABLED</state>
+          <stateProcess>%aditoprj%/entity/CampaignStep_entity/entityfields/group/children/moveup/stateProcess.js</stateProcess>
+        </entityActionField>
+        <entityActionField>
+          <name>moveDown</name>
+          <onActionProcess>%aditoprj%/entity/CampaignStep_entity/entityfields/group/children/movedown/onActionProcess.js</onActionProcess>
+          <iconId>VAADIN:ARROW_DOWN</iconId>
+          <stateProcess>%aditoprj%/entity/CampaignStep_entity/entityfields/group/children/movedown/stateProcess.js</stateProcess>
+        </entityActionField>
+      </children>
+    </entityActionGroup>
   </entityFields>
   <recordContainers>
     <dbRecordContainer>
       <name>db</name>
+      <hasDependentRecords v="true" />
       <conditionProcess>%aditoprj%/entity/CampaignStep_entity/recordcontainers/db/conditionProcess.js</conditionProcess>
       <orderClauseProcess>%aditoprj%/entity/CampaignStep_entity/recordcontainers/db/orderClauseProcess.js</orderClauseProcess>
       <onDBInsert>%aditoprj%/entity/CampaignStep_entity/recordcontainers/db/onDBInsert.js</onDBInsert>
diff --git a/entity/CampaignStep_entity/entityfields/group/children/movedown/onActionProcess.js b/entity/CampaignStep_entity/entityfields/group/children/movedown/onActionProcess.js
new file mode 100644
index 0000000000..d55de329e8
--- /dev/null
+++ b/entity/CampaignStep_entity/entityfields/group/children/movedown/onActionProcess.js
@@ -0,0 +1,9 @@
+import("system.vars");
+import("system.neon");
+import("Campaign_lib");
+
+CampaignUtils.moveStepDown(vars.get("$field.CAMPAIGNSTEPID"))
+
+neon.refreshAll();
+
+
diff --git a/entity/CampaignStep_entity/entityfields/group/children/movedown/stateProcess.js b/entity/CampaignStep_entity/entityfields/group/children/movedown/stateProcess.js
new file mode 100644
index 0000000000..984483b8fc
--- /dev/null
+++ b/entity/CampaignStep_entity/entityfields/group/children/movedown/stateProcess.js
@@ -0,0 +1,27 @@
+import("system.result");
+import("Sql_lib");
+import("system.vars");
+import("system.neon");
+
+var sorting = parseInt(vars.get("$field.SORTING"));
+var campaignId = vars.get("$field.CAMPAIGN_ID");
+var campaignStepId = vars.get("$field.CAMPAIGNSTEPID");
+
+var validNextStep = newSelect("CAMPAIGNSTEP.CAMPAIGNSTEPID")
+                        .from("CAMPAIGNSTEP")
+                        .where("CAMPAIGNSTEP.CAMPAIGN_ID",campaignId)
+                        .and("CAMPAIGNSTEP.SORTING",sorting+1)
+                        .and(newWhere("CAMPAIGNSTEP.PREDECESSORSTEP_ID",campaignStepId,SqlBuilder.NOT_EQUAL())
+                            .or("CAMPAIGNSTEP.PREDECESSORSTEP_ID is null"))
+                        .cell();
+
+
+if (validNextStep)
+{
+    result.string(neon.COMPONENTSTATE_EDITABLE);
+}
+else
+{
+    result.string(neon.COMPONENTSTATE_DISABLED);
+}
+
diff --git a/entity/CampaignStep_entity/entityfields/group/children/moveup/onActionProcess.js b/entity/CampaignStep_entity/entityfields/group/children/moveup/onActionProcess.js
new file mode 100644
index 0000000000..f6c210312a
--- /dev/null
+++ b/entity/CampaignStep_entity/entityfields/group/children/moveup/onActionProcess.js
@@ -0,0 +1,8 @@
+import("system.vars");
+import("system.neon");
+import("Campaign_lib");
+
+CampaignUtils.moveStepUp(vars.get("$field.CAMPAIGNSTEPID"))
+
+
+neon.refreshAll();
diff --git a/entity/CampaignStep_entity/entityfields/group/children/moveup/stateProcess.js b/entity/CampaignStep_entity/entityfields/group/children/moveup/stateProcess.js
new file mode 100644
index 0000000000..f24f17d1e3
--- /dev/null
+++ b/entity/CampaignStep_entity/entityfields/group/children/moveup/stateProcess.js
@@ -0,0 +1,25 @@
+import("system.result");
+import("Sql_lib");
+import("system.vars");
+import("system.neon");
+
+var sorting = parseInt(vars.get("$field.SORTING"));
+var campaignId = vars.get("$field.CAMPAIGN_ID");
+var predecessorStepId = vars.get("$field.PREDECESSORSTEP_ID");
+
+var validPreviousStep = newSelect("CAMPAIGNSTEP.CAMPAIGNSTEPID")
+                        .from("CAMPAIGNSTEP")
+                        .where("CAMPAIGNSTEP.CAMPAIGN_ID",campaignId)
+                        .and("CAMPAIGNSTEP.SORTING",sorting-1)
+                        .and("CAMPAIGNSTEP.CAMPAIGNSTEPID",predecessorStepId,SqlBuilder.NOT_EQUAL())
+                        .cell();
+
+if (validPreviousStep)
+{
+    result.string(neon.COMPONENTSTATE_EDITABLE);
+}
+else
+{
+    result.string(neon.COMPONENTSTATE_DISABLED);
+}
+
diff --git a/entity/CampaignStep_entity/recordcontainers/db/onDBDelete.js b/entity/CampaignStep_entity/recordcontainers/db/onDBDelete.js
index 6ff91a5eb3..c851ee9b66 100644
--- a/entity/CampaignStep_entity/recordcontainers/db/onDBDelete.js
+++ b/entity/CampaignStep_entity/recordcontainers/db/onDBDelete.js
@@ -1,3 +1,21 @@
+import("system.neon");
+import("system.vars");
 import("Workflow_lib");
+import("Sql_lib");
+import("Campaign_lib");
+
+newSelect("CAMPAIGNSTEP.CAMPAIGNSTEPID")
+.from("CAMPAIGNSTEP")
+.where("CAMPAIGNSTEP.CAMPAIGN_ID",vars.get("$field.CAMPAIGN_ID"))
+.and("CAMPAIGNSTEP.SORTING",vars.get("$field.SORTING"),SqlBuilder.GREATER_OR_EQUAL())
+.orderBy("CAMPAIGNSTEP.SORTING asc")
+.arrayColumn()
+.forEach(function(pCampaignStepId)
+        {
+            CampaignUtils.moveStepUp(pCampaignStepId)
+        });
+        
+newWhere("CAMPAIGNSTEP.PREDECESSORSTEP_ID",vars.get("$field.CAMPAIGNSTEPID"))
+.updateFields({"CAMPAIGNSTEP.PREDECESSORSTEP_ID":null});
 
 WorkflowSignalSender.deleted();
\ No newline at end of file
diff --git a/neonView/CampaignStepFilter_view/CampaignStepFilter_view.aod b/neonView/CampaignStepFilter_view/CampaignStepFilter_view.aod
index 84f150264f..6f7b3669c8 100644
--- a/neonView/CampaignStepFilter_view/CampaignStepFilter_view.aod
+++ b/neonView/CampaignStepFilter_view/CampaignStepFilter_view.aod
@@ -15,6 +15,7 @@
       <name>StepsTable</name>
       <autoNewRow v="false" />
       <entityField>#ENTITY</entityField>
+      <favoriteActionGroup1>group</favoriteActionGroup1>
       <isEditable v="true" />
       <columns>
         <neonTableColumn>
@@ -62,6 +63,7 @@
     <treeTableViewTemplate>
       <name>Treetable</name>
       <entityField>#ENTITY</entityField>
+      <favoriteActionGroup1>group</favoriteActionGroup1>
       <columns>
         <neonTreeTableColumn>
           <name>2246fe5a-d4ed-454a-9007-6c1716ad2a1e</name>
diff --git a/process/Campaign_lib/process.js b/process/Campaign_lib/process.js
index f488f0ee65..808c85d412 100644
--- a/process/Campaign_lib/process.js
+++ b/process/Campaign_lib/process.js
@@ -814,4 +814,54 @@ CampaignUtils.getResolvedCampaignStepSqlpPart = function(pCampaignId){
     var res =  SqlUtils.getResolvingCaseWhen(campaignSteps, "CAMPAIGNSTEP.CAMPAIGNSTEPID"); 
     
     return  SqlUtils.translateStatementWithQuotes(res);
+}
+
+/*
+ * Swaps the Sorting of a campaignstep with the one above it
+ *
+ *  @param {String} pCampaignStepId CampaignStepId of the step that should be moved up
+ */
+CampaignUtils.moveStepUp = function(pCampaignStepId){
+    var sorting;
+    var campaignId;
+    
+    [campaignId,sorting] = newSelect(["CAMPAIGNSTEP.CAMPAIGN_ID","CAMPAIGNSTEP.SORTING"])
+                            .from("CAMPAIGNSTEP")
+                            .where("CAMPAIGNSTEP.CAMPAIGNSTEPID",pCampaignStepId)
+                            .arrayRow();
+                            
+    sorting = parseInt(sorting);
+
+
+    newWhere("CAMPAIGNSTEP.CAMPAIGN_ID",campaignId)
+    .and("CAMPAIGNSTEP.SORTING",sorting-1)
+    .updateFields({"SORTING":sorting});
+
+    newWhere("CAMPAIGNSTEP.CAMPAIGNSTEPID",pCampaignStepId)
+    .updateFields({"SORTING":sorting-1});
+}
+
+/*
+ * Swaps the Sorting of a campaignstep with the one below it
+ *
+ *  @param {String} pCampaignStepId CampaignStepId of the step that should be moved down
+ */
+CampaignUtils.moveStepDown = function(pCampaignStepId){
+    var sorting;
+    var campaignId;
+    
+    [campaignId,sorting] = newSelect(["CAMPAIGNSTEP.CAMPAIGN_ID","CAMPAIGNSTEP.SORTING"])
+                            .from("CAMPAIGNSTEP")
+                            .where("CAMPAIGNSTEP.CAMPAIGNSTEPID",pCampaignStepId)
+                            .arrayRow();
+                            
+    sorting = parseInt(sorting);
+
+
+    newWhere("CAMPAIGNSTEP.CAMPAIGN_ID",campaignId)
+    .and("CAMPAIGNSTEP.SORTING",sorting+1)
+    .updateFields({"SORTING":sorting});
+
+    newWhere("CAMPAIGNSTEP.CAMPAIGNSTEPID",pCampaignStepId)
+    .updateFields({"SORTING":sorting+1});
 }
\ No newline at end of file
-- 
GitLab