commit 9e0adfdffb205a186da00b41cb8052a05e8c0732
Author: Hrabikv <75799502+Hrabikv@users.noreply.github.com>
Date:   Fri Nov 17 21:10:32 2023 +0100

    https://kivprogrammers.atlassian.net/browse/TSP2-50 zpracování dat z frontendu, posílání odpovědí

diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/repository/managment/PhaseRepository.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/repository/managment/PhaseRepository.java
index 3c38409..a3c264b 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/repository/managment/PhaseRepository.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/repository/managment/PhaseRepository.java
@@ -1,10 +1,18 @@
 package cz.zcu.fav.kiv.antipatterndetectionapp.repository.managment;
 
+import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.Iteration;
 import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.Phase;
 import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
+
+import java.util.List;
 
 /**
  * Simple class that is implements JPA for easy loading project from DB.
  */
 public interface PhaseRepository extends JpaRepository<Phase, Long> {
+
+    @Query("select ph from Phase ph where ph.id IN :ids")
+    List<Phase> fetchPhasesByIds(List<Long> ids);
+
 }
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/management/IterationAndPhasesController.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/management/IterationAndPhasesController.java
index 234ecad..6342919 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/management/IterationAndPhasesController.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/management/IterationAndPhasesController.java
@@ -32,25 +32,29 @@ public class IterationAndPhasesController {
         Project project = projectService.getProjectById(ProjectId);
 
         if (project == null) {
-            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Project not found");
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Project not found.");
         }
 
         return iterationsAndPhasesService.getIterationAndPhases(project);
     }
 
     @PostMapping("/changeIteration")
-    public ResponseEntity<String> changeIteration(@RequestBody Map<String, String[]> requestData) {
+    public ResponseEntity<Map<String, Object>> changeIteration(@RequestBody Map<String, String[]> requestData) {
         if (requestData.get("Ids").length == 0) {
-            return ResponseEntity.status(HttpStatus.NO_CONTENT).body("No Iterations send");
+            Map<String,Object> json = new HashMap<>();
+            json.put("errormessage", "No Iterations send.");
+            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(json);
         }
 
         return iterationsAndPhasesService.changeToPhase(requestData.get("Ids"));
     }
 
     @PostMapping("/changePhase")
-    public ResponseEntity<String> changePhase(@RequestBody Map<String, String[]> requestData) {
+    public ResponseEntity<Map<String, Object>> changePhase(@RequestBody Map<String, String[]> requestData) {
         if (requestData.get("Ids").length == 0) {
-            return ResponseEntity.status(HttpStatus.NO_CONTENT).body("No Phases send");
+            Map<String,Object> json = new HashMap<>();
+            json.put("errormessage", "No Phases send.");
+            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(json);
         }
 
         return iterationsAndPhasesService.changeToIteration(requestData.get("Ids"));
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/managment/IterationsAndPhasesService.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/managment/IterationsAndPhasesService.java
index eea7b9c..832bf2e 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/managment/IterationsAndPhasesService.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/managment/IterationsAndPhasesService.java
@@ -3,11 +3,13 @@ package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.managment;
 import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project;
 import org.springframework.http.ResponseEntity;
 
+import java.util.Map;
+
 public interface IterationsAndPhasesService {
 
     public ResponseEntity<String> getIterationAndPhases(Project project);
 
-    public ResponseEntity<String> changeToPhase(String[] iterationsIds);
+    public ResponseEntity<Map<String, Object>> changeToPhase(String[] iterationsIds);
 
-    public ResponseEntity<String> changeToIteration(String[] phasesIds);
+    public ResponseEntity<Map<String, Object>> changeToIteration(String[] phasesIds);
 }
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/managment/IterationsAndPhasesServiceImpl.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/managment/IterationsAndPhasesServiceImpl.java
index 5451d80..10fee60 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/managment/IterationsAndPhasesServiceImpl.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/managment/IterationsAndPhasesServiceImpl.java
@@ -1,26 +1,47 @@
 package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.managment;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
+import cz.zcu.fav.kiv.antipatterndetectionapp.Constants;
 import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project;
 import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.Iteration;
 import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.Phase;
+import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.WorkUnit;
+import cz.zcu.fav.kiv.antipatterndetectionapp.repository.managment.IterationRepository;
+import cz.zcu.fav.kiv.antipatterndetectionapp.repository.managment.PhaseRepository;
+import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.IterationService;
+import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.PhaseService;
+import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.WorkUnitService;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.IterationAndPhasesDto;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.IterationDto;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.PhaseDto;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.converters.IterationAndPhasesToDto;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.converters.IterationToDto;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.converters.PhaseToDto;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
+import java.util.*;
 
 @Service
 public class IterationsAndPhasesServiceImpl implements IterationsAndPhasesService {
 
+    @Autowired
+    private IterationRepository iterationRepository;
+
+    @Autowired
+    private PhaseRepository phaseRepository;
+
+    @Autowired
+    private PhaseService phaseService;
+
+    @Autowired
+    private WorkUnitService workUnitService;
+
+    @Autowired
+    private IterationService iterationService;
+
     @Override
     public ResponseEntity<String> getIterationAndPhases(Project project) {
         ObjectMapper objectMapper = new ObjectMapper();
@@ -47,13 +68,158 @@ public class IterationsAndPhasesServiceImpl implements IterationsAndPhasesServic
     }
 
     @Override
-    public ResponseEntity<String> changeToPhase(String[] iterationsIds) {
-        return null;
+    public ResponseEntity<Map<String, Object>> changeToPhase(String[] iterationsIds) {
+        List<Long> iterationIds = new ArrayList<>();
+        for (String id : iterationsIds)
+                iterationIds.add(Long.parseLong(id));
+        List<Iteration> selectedIterations = iterationRepository.fetchIterationsByIds(iterationIds);
+
+
+        StringBuilder message = new StringBuilder();
+        int errors = 0;
+
+        for (Iteration iteration : selectedIterations) {
+            message.append("Iteration: ").append(iteration.getName()).append(System.getProperty("line.separator"));
+
+            if (iteration.getWorkUnits().size() == 0) {
+                errors += 1;
+                message.append(Constants.INDENT)
+                        .append("cannot be changed to Phase due to non-existent work unit")
+                        .append(System.getProperty("line.separator"))
+                        .append(System.getProperty("line.separator"));
+                continue;
+            }
+
+            Phase phase = null;
+            for (WorkUnit workUnit : iteration.getWorkUnits()) { // If exists WorkUnit with empty Phase - create Phase from Iteration
+                if (workUnit.getPhase() == null) {
+                    phase = new Phase(iteration.getExternalId(),
+                            iteration.getName(),
+                            iteration.getDescription(),
+                            iteration.getEndDate(),
+                            iteration.getStartDate(),
+                            iteration.getCreated(),
+                            iteration.getSuperProjectId());
+                    phaseService.savePhase(phase);
+                    break;
+                }
+            }
+
+            for (WorkUnit workUnit : iteration.getWorkUnits()) { // For every WorkUnit with empty Phase - assign created Phase
+                Phase workUnitPhrase = workUnit.getPhase();
+                if (workUnitPhrase == null) {
+                    workUnit.setPhase(phase);
+                    message.append(Constants.INDENT)
+                            .append("successfully assigned to WU with id ").append(workUnit.getId())
+                            .append(System.getProperty("line.separator"));
+                    workUnitService.saveWorkUnit(workUnit);
+
+                } else {
+                    errors += 1;
+                    message.append(Constants.INDENT)
+                            .append("cannot be assigned to WU with id ").append(workUnit.getId())
+                            .append(" as it already exists with Phase ").append(workUnit.getPhase().getName())
+                            .append(System.getProperty("line.separator"));
+                }
+                if (phase != null) {
+                    workUnit.setIteration(null);
+                }
+                if(phase != null || workUnitPhrase == null){
+                    workUnitService.saveWorkUnit(workUnit);
+                }
+            }
+            message.append(System.getProperty("line.separator"));
+            if (phase != null) {
+                iteration.setWorkUnits(null);
+                iterationService.deleteIteration(iteration);
+            }
+        }
+
+        Map<String,Object> json = new HashMap<>();
+        json.put("message", message);
+
+        if (errors == 0) {
+            json.put("successMessage", "All selected iterations (" + selectedIterations.size() + ") were transformed to phases");
+        } else {
+            json.put("informMessage", "Some iterations cannot be assign, look at the log for more information");
+        }
+
+        return ResponseEntity.status(HttpStatus.OK).body(json);
     }
 
     @Override
-    public ResponseEntity<String> changeToIteration(String[] phasesIds) {
-        return null;
+    public ResponseEntity<Map<String, Object>> changeToIteration(String[] phasesIds) {
+        List<Long> phaseIds = new ArrayList<>();
+        for (String id : phasesIds)
+            phaseIds.add(Long.parseLong(id));
+        List<Phase> selectedPhases = phaseRepository.fetchPhasesByIds(phaseIds);
+
+        StringBuilder message = new StringBuilder();
+        int errors = 0;
+
+        for (Phase phase : selectedPhases) {
+            message.append("Phase: ").append(phase.getName()).append(System.getProperty("line.separator"));
+
+            if (phase.getWorkUnits().size() == 0) {
+                errors += 1;
+                message.append(Constants.INDENT)
+                        .append("cannot be changed to Phase due to non-existent work unit")
+                        .append(System.getProperty("line.separator"))
+                        .append(System.getProperty("line.separator"));
+                continue;
+            }
+
+            Iteration iteration = null;
+            for (WorkUnit workUnit : phase.getWorkUnits()) { // If exists WorkUnit with empty Phase - create Phase from Iteration
+                if (workUnit.getIteration() == null) {
+                    iteration = new Iteration(phase.getExternalId(),
+                            phase.getName(),
+                            phase.getDescription(),
+                            phase.getEndDate(),
+                            phase.getStartDate(),
+                            phase.getCreated(),
+                            phase.getSuperProjectId());
+                    iterationService.saveIteration(iteration);
+                    break;
+                }
+            }
+
+            for (WorkUnit workUnit : phase.getWorkUnits()) { // For every WorkUnit with empty Phase - assign created Phase
+                if (workUnit.getIteration() == null) {
+                    workUnit.setIteration(iteration);
+                    workUnitService.saveWorkUnit(workUnit);
+                    message.append(Constants.INDENT)
+                            .append("successfully assigned to WU with id ").append(workUnit.getId())
+                            .append(System.getProperty("line.separator"));
+                } else {
+                    errors += 1;
+                    message.append(Constants.INDENT)
+                            .append("cannot be assigned to WU with id ").append(workUnit.getId())
+                            .append(" as it already exists with Iteration ").append(workUnit.getIteration().getName())
+                            .append(System.getProperty("line.separator"));
+                }
+                if (iteration != null) {
+                    workUnit.setPhase(null);
+                }
+                workUnitService.saveWorkUnit(workUnit);
+            }
+            message.append(System.getProperty("line.separator"));
+            if (iteration != null) {
+                phase.setWorkUnits(null);
+                phaseService.deletePhase(phase);
+            }
+        }
+
+        Map<String,Object> json = new HashMap<>();
+        json.put("message", message);
+
+        if (errors == 0) {
+            json.put("successMessage", "All selected iterations (" + selectedPhases.size() + ") were transformed to phases");
+        } else {
+            json.put("informMessage", "Some iterations cannot be assign, look at the log for more information");
+        }
+
+        return ResponseEntity.status(HttpStatus.OK).body(json);
     }
 
 }
