commit 66ee753332d69c90fdababa65ddf31deb429afd3
Author: stepanekp <1stepanekpetr@gmail.com>
Date:   Sun Oct 15 14:30:09 2023 +0200

    https://kivprogrammers.atlassian.net/browse/TSP2-28 integrace kontroleru pro hierarchie projektů

diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/management/ProjectController.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/management/ProjectController.java
index 955c8f2..f120934 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/management/ProjectController.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/management/ProjectController.java
@@ -4,6 +4,8 @@ import cz.zcu.fav.kiv.antipatterndetectionapp.Constants;
 import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project;
 import cz.zcu.fav.kiv.antipatterndetectionapp.service.ProjectService;
 import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.types.Node;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.ProjectDto;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.converters.ProjectToDto;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -21,7 +23,7 @@ import java.util.Objects;
 /**
  * This class contains all endpoints of projects.html
  */
-@Controller
+//@Controller
 public class ProjectController {
 
     private final Logger LOGGER = LoggerFactory.getLogger(ProjectController.class);
@@ -36,9 +38,10 @@ public class ProjectController {
      */
     @GetMapping("/management/projects")
     public String projects(Model model) {
+        ProjectToDto projectToDto = new ProjectToDto();
         List<Node> parents = new ArrayList<>();
-        List<Project> parentProjects = projectService.getParentProjects();
-        for(Project parentProject : parentProjects) {
+        List<ProjectDto> parentProjects = projectToDto.convert(projectService.getParentProjects());
+        for(ProjectDto parentProject : parentProjects) {
             Node n = new Node();
             n.project = parentProject;
             n.children = new ArrayList<>();
@@ -46,7 +49,7 @@ public class ProjectController {
         }
 
         for(Node n : parents) {
-            n.children = calculate(n.project);
+            n.children = projectService.calculate(n.project);
         }
 
         model.addAttribute("parents", parents);
@@ -146,10 +149,11 @@ public class ProjectController {
      * @param p Project for which the hierarchy will be generated
      * @return  List of project's children with their hierarchy
      */
-    private ArrayList<Node> calculate(Project p) {
+    private ArrayList<Node> calculate(ProjectDto p) {
+        ProjectToDto projectToDto = new ProjectToDto();
         ArrayList<Node> nodes = new ArrayList<>();
-        List<Project> projects = projectService.getSubordinateProjectsTo(p.getId());
-        for(Project project : projects) {
+        List<ProjectDto> projects = projectToDto.convert(projectService.getSubordinateProjectsTo(p.getId()));
+        for(ProjectDto project : projects) {
             Node n = new Node();
             n.project = project;
             n.children = calculate(project);
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/types/Node.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/types/Node.java
index 1401a96..8d59841 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/types/Node.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/types/Node.java
@@ -1,10 +1,11 @@
 package cz.zcu.fav.kiv.antipatterndetectionapp.model.management.types;
 
 import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.ProjectDto;
 
 import java.util.ArrayList;
 
 public class Node {
-    public Project project;
+    public ProjectDto project;
     public ArrayList<Node> children;
 }
\ No newline at end of file
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/ProjectService.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/ProjectService.java
index 8c94cab..b16620a 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/ProjectService.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/ProjectService.java
@@ -1,7 +1,10 @@
 package cz.zcu.fav.kiv.antipatterndetectionapp.service;
 
 import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project;
+import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.types.Node;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.ProjectDto;
 
+import java.util.ArrayList;
 import java.util.List;
 
 public interface ProjectService {
@@ -46,4 +49,11 @@ public interface ProjectService {
      * @return      List of subordinate projects
      */
     List<Project> getSubordinateProjectsTo(Long id);
+
+    /**
+     * Method creating the hierarchy of projects by recursion
+     * @param p Project for which the hierarchy will be generated
+     * @return  List of project's children with their hierarchy
+     */
+    ArrayList<Node> calculate(ProjectDto p);
 }
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/ProjectServiceImpl.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/ProjectServiceImpl.java
index d691c36..1eb6a85 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/ProjectServiceImpl.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/ProjectServiceImpl.java
@@ -1,7 +1,10 @@
 package cz.zcu.fav.kiv.antipatterndetectionapp.service;
 
 import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project;
+import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.types.Node;
 import cz.zcu.fav.kiv.antipatterndetectionapp.repository.ProjectRepository;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.ProjectDto;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.converters.ProjectToDto;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -58,4 +61,18 @@ public class ProjectServiceImpl implements ProjectService {
     public List<Project> getSubordinateProjectsTo(Long id) {
         return projectRepository.getSubordinateProjectsTo(id);
     }
+
+    @Override
+    public ArrayList<Node> calculate(ProjectDto p) {
+        ProjectToDto projectToDto = new ProjectToDto();
+        ArrayList<Node> nodes = new ArrayList<>();
+        List<ProjectDto> projects = projectToDto.convert(getSubordinateProjectsTo(p.getId()));
+        for(ProjectDto project : projects) {
+            Node n = new Node();
+            n.project = project;
+            n.children = calculate(project);
+            nodes.add(n);
+        }
+        return nodes;
+    }
 }
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/management/ProjectController.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/management/ProjectController.java
new file mode 100644
index 0000000..9a12a08
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/management/ProjectController.java
@@ -0,0 +1,49 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller.management;
+
+import com.google.gson.Gson;
+import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.types.Node;
+import cz.zcu.fav.kiv.antipatterndetectionapp.service.ProjectService;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.ProjectDto;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.converters.ProjectToDto;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+/**
+ * This class contains all endpoints of projects.html
+ */
+@Controller
+@RequestMapping("v2/management")
+public class ProjectController {
+
+    @Autowired
+    private ProjectService projectService;
+
+    @GetMapping("/projects")
+    public ResponseEntity<String> getProjectsData() {
+        ProjectToDto projectToDto = new ProjectToDto();
+        List<Node> parents = new ArrayList<>();
+        List<ProjectDto> parentProjects = projectToDto.convert(projectService.getParentProjects());
+        for(ProjectDto parentProject : parentProjects) {
+            Node n = new Node();
+            n.project = parentProject;
+            n.children = new ArrayList<>();
+            parents.add(n);
+        }
+
+        for(Node n : parents) {
+            n.children = projectService.calculate(n.project);
+        }
+
+        return ResponseEntity.status(parents.size() > 0 ? HttpStatus.OK : HttpStatus.NO_CONTENT).body(new Gson().toJson(parents));
+    }
+
+}
+
