commit 627559bc3894b3f6c91ec126e216086eb1c25b61
Author: stepanekp <1stepanekpetr@gmail.com>
Date:   Sun Oct 29 20:32:14 2023 +0100

    https://kivprogrammers.atlassian.net/browse/TSP2-37 načítání projektů a ukládání na SPADe

diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/Project.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/Project.java
index af024ee..857b9e5 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/Project.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/Project.java
@@ -15,7 +15,7 @@ import java.util.Set;
 public class Project {
 
     @Id
-    @GeneratedValue(strategy = GenerationType.AUTO)
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
     @Column(name = "id", nullable = false)
     private Long id;
 
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/CommittedConfiguration.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/CommittedConfiguration.java
index c43f467..8618b7b 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/CommittedConfiguration.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/CommittedConfiguration.java
@@ -9,6 +9,7 @@ import java.sql.Timestamp;
  * Model class for configurations. This is Entity class that is loaded from db.
  */
 @Entity
+@Table(name = "committed_configuration")
 public class CommittedConfiguration {
 
     @OneToOne
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/ProjectInstance.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/ProjectInstance.java
index 96fe9cc..051858f 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/ProjectInstance.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/ProjectInstance.java
@@ -14,6 +14,7 @@ import java.util.Set;
  * Model class for project instances. This is Entity class that is loaded from db.
  */
 @Entity
+@Table(name = "project_instance")
 public class ProjectInstance {
 
     @Id
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/WorkItem.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/WorkItem.java
index 493e28b..8231d54 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/WorkItem.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/WorkItem.java
@@ -9,6 +9,7 @@ import java.sql.Timestamp;
  * Model class for workitems. This is Entity class that is loaded from db.
  */
 @Entity
+@Table(name = "work_item")
 public class WorkItem {
 
     @Id
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/WorkUnit.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/WorkUnit.java
index 6e9a3e2..1fd0a12 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/WorkUnit.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/WorkUnit.java
@@ -13,6 +13,7 @@ import java.util.Set;
  * Model class for work units. This is Entity class that is loaded from db.
  */
 @Entity
+@Table(name = "work_unit")
 public class WorkUnit {
 
     @Id
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/enums/WuType.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/enums/WuType.java
index a27a68f..bdd3878 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/enums/WuType.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/enums/WuType.java
@@ -10,6 +10,7 @@ import javax.persistence.*;
  * Model class for work unit types. This is Entity class that is loaded from db.
  */
 @Entity
+@Table(name = "wu_type")
 public class WuType implements EnumType {
 
     @Id
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/enums/WuTypeClassification.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/enums/WuTypeClassification.java
index f29c5cd..83c611b 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/enums/WuTypeClassification.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/model/management/enums/WuTypeClassification.java
@@ -10,6 +10,7 @@ import java.util.List;
  * Model class for work unit types classes. This is Entity class that is loaded from db.
  */
 @Entity
+@Table(name = "wu_type_classification")
 public class WuTypeClassification implements Classification {
 
     @Id
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 b16620a..94d6170 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
@@ -56,4 +56,12 @@ public interface ProjectService {
      * @return  List of project's children with their hierarchy
      */
     ArrayList<Node> calculate(ProjectDto p);
+
+    /**
+     * Method saving new structure of the projects and superprojects
+     * @param nodes List of the nodes with projects to save
+     * @param superProject Parent of the projects in the list
+     * @return true if successful
+     */
+    boolean saveProjectsStructure(List<Node> nodes, Project superProject);
 }
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 1eb6a85..28b9875 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
@@ -75,4 +75,34 @@ public class ProjectServiceImpl implements ProjectService {
         }
         return nodes;
     }
+
+    @Override
+    public boolean saveProjectsStructure(List<Node> nodes, Project superProject) {
+        if (nodes != null && !nodes.isEmpty()) {
+            for (Node node : nodes) {
+                ProjectDto projectDto = node.project;
+                Project project = new Project();
+
+                if(projectDto.getId() > 0) {
+                    project.setId(projectDto.getId());
+                }
+
+                project.setName(projectDto.getName());
+                project.setDescription(projectDto.getDescription());
+
+                if(superProject != null) {
+                    project.setSuperProject(superProject);
+                }
+
+                project = projectRepository.save(project); // This may update an existing project or insert a new one
+
+                if (node.children != null && !node.children.isEmpty()) {
+                    List<Node> children = node.children;
+                    saveProjectsStructure(children, project);
+                }
+            }
+            return true; // Success
+        }
+        return false; // No data to save
+    }
 }
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
index 9a12a08..61759c5 100644
--- 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
@@ -1,19 +1,23 @@
 package cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller.management;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
 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.JSONBuilder;
 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.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import com.fasterxml.jackson.databind.ObjectMapper;
 
-import java.util.ArrayList;
-import java.util.List;
+import java.util.*;
 
 
 /**
@@ -45,5 +49,26 @@ public class ProjectController {
         return ResponseEntity.status(parents.size() > 0 ? HttpStatus.OK : HttpStatus.NO_CONTENT).body(new Gson().toJson(parents));
     }
 
+    @PostMapping("/saveProjects")
+    public ResponseEntity<String> saveProjects(@RequestBody Map<String, Object> requestData) throws JsonProcessingException {
+        ObjectMapper objectMapper = new ObjectMapper();
+
+        String nodesString = objectMapper.writeValueAsString(requestData.get("projects"));
+        Node[] nodes = objectMapper.readValue(nodesString, Node[].class);
+
+        Map<String, Object> json = new HashMap<>();
+
+        if(projectService.saveProjectsStructure(Arrays.asList(nodes), null)) {
+            json.put("message", "Project data saved successfully.");
+            return new ResponseEntity<>(JSONBuilder.buildJSON(json), HttpStatus.OK);
+        }
+        else {
+            json.put("message", "Project data save failed.");
+            return new ResponseEntity<>(JSONBuilder.buildJSON(json), HttpStatus.INTERNAL_SERVER_ERROR);
+
+        }
+    }
+
+
 }
 
