From e26f6d0f6a09ec427660985346af63876618079f Mon Sep 17 00:00:00 2001
From: Jan Tymel <410388@mail.muni.cz>
Date: Thu, 21 Jan 2021 21:54:36 +0100
Subject: [PATCH] Create correct number of DecisionMatrixRows when creating a
 new phase

---
 .../demo/repository/PhaseLevelRepository.java |  5 +++
 .../demo/service/PhaseLevelService.java       | 31 ++++++++++++-------
 2 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/example/demo/repository/PhaseLevelRepository.java b/src/main/java/com/example/demo/repository/PhaseLevelRepository.java
index f8be71ec..99ac0f35 100644
--- a/src/main/java/com/example/demo/repository/PhaseLevelRepository.java
+++ b/src/main/java/com/example/demo/repository/PhaseLevelRepository.java
@@ -2,6 +2,11 @@ package com.example.demo.repository;
 
 import com.example.demo.domain.PhaseLevel;
 import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
 
 public interface PhaseLevelRepository extends JpaRepository<PhaseLevel, Long> {
+
+    @Query("SELECT COUNT(p.id) FROM PhaseLevel p WHERE p.trainingDefinitionId = :trainingDefinitionId")
+    int getNumberOfExistingPhases(@Param("trainingDefinitionId") Long trainingDefinitionId);
 }
diff --git a/src/main/java/com/example/demo/service/PhaseLevelService.java b/src/main/java/com/example/demo/service/PhaseLevelService.java
index e118b560..5b869dc1 100644
--- a/src/main/java/com/example/demo/service/PhaseLevelService.java
+++ b/src/main/java/com/example/demo/service/PhaseLevelService.java
@@ -12,7 +12,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
-import java.util.Collections;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Optional;
 
 @Service
@@ -33,16 +34,7 @@ public class PhaseLevelService {
         phaseLevel.setTrainingDefinitionId(trainingDefinitionId);
         phaseLevel.setOrder(baseLevelRepository.getCurrentMaxOrder(trainingDefinitionId) + 1);
 
-        DecisionMatrixRow matrixRow = new DecisionMatrixRow();
-        matrixRow.setAA(0.8);
-        matrixRow.setCiT(0.7);
-        matrixRow.setKU(0.6);
-        matrixRow.setSD(0.5);
-        matrixRow.setWF(0.4);
-        matrixRow.setOrder(0);
-        matrixRow.setPhaseLevel(phaseLevel);
-
-        phaseLevel.setDecisionMatrix(Collections.singletonList(matrixRow));
+        phaseLevel.setDecisionMatrix(prepareDefaultDecisionMatrix(trainingDefinitionId, phaseLevel));
 
         PhaseLevel persistedEntity = phaseLevelRepository.save(phaseLevel);
 
@@ -70,4 +62,21 @@ public class PhaseLevelService {
 
         return BeanMapper.INSTANCE.toDto(savedEntity);
     }
+
+    private List<DecisionMatrixRow> prepareDefaultDecisionMatrix(Long trainingDefinitionId, PhaseLevel phaseLevel) {
+        List<DecisionMatrixRow> result = new ArrayList<>();
+
+        int numberOfExistingPhases = phaseLevelRepository.getNumberOfExistingPhases(trainingDefinitionId);
+
+        // number of rows should be equal to number of existing phases + 1
+        for (int i = 0; i <= numberOfExistingPhases; i++) {
+            DecisionMatrixRow decisionMatrixRow = new DecisionMatrixRow();
+            decisionMatrixRow.setPhaseLevel(phaseLevel);
+            decisionMatrixRow.setOrder(i);
+
+            result.add(decisionMatrixRow);
+        }
+
+        return result;
+    }
 }
-- 
GitLab