commit e72cefc2886d8917fb7fde295404e9bc030b4d03
Author: stepanekp <1stepanekpetr@gmail.com>
Date:   Sun Apr 3 13:40:32 2022 +0200

    #14 Saving configurations to files added

diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/AppController.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/AppController.java
index 5b8be39..2b06ec3 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/AppController.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/AppController.java
@@ -231,7 +231,7 @@ public class AppController {
      * @param thresholdNames changed configuration names
      * @return html file name for thymeleaf template
      */
-    @PostMapping("/configuration")
+    @PostMapping(value = "/configuration", params = "configuration-save-button")
     public String configurationPost(Model model,
                                     @RequestParam(value = "thresholdValues", required = false) String[] thresholdValues,
                                     @RequestParam(value = "thresholdNames", required = false) String[] thresholdNames,
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/repository/ConfigurationRepository.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/repository/ConfigurationRepository.java
index 3ad5279..28cd3d3 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/repository/ConfigurationRepository.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/repository/ConfigurationRepository.java
@@ -1,6 +1,8 @@
 package cz.zcu.fav.kiv.antipatterndetectionapp.repository;
 
 import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import cz.zcu.fav.kiv.antipatterndetectionapp.utils.JsonParser;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -10,7 +12,10 @@ import org.springframework.web.context.ServletContextAware;
 import javax.servlet.ServletContext;
 import java.io.*;
 import java.net.URL;
+import java.nio.file.Paths;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 @Component
@@ -109,4 +114,37 @@ public class ConfigurationRepository implements ServletContextAware {
 
         LOGGER.info("-------FINISHED READING CONFIGURATIONS FROM FILES-------");
     }
+
+    public void saveConfigurationToFile(String configurationName, Map<String, Map <String, String>> newConfiguration){
+        ObjectNode root = JsonParser.getObjectMapper().createObjectNode();
+
+        ArrayNode array = JsonParser.getObjectMapper().createArrayNode();
+
+        for(String antiPatternName : newConfiguration.keySet()){
+            ObjectNode antiPattern = JsonParser.getObjectMapper().createObjectNode();
+            antiPattern.put("antiPattern", antiPatternName);
+
+            ArrayNode thresholdsArray = JsonParser.getObjectMapper().createArrayNode();
+
+            for(String thresholdName : newConfiguration.get(antiPatternName).keySet()){
+                ObjectNode threshold = JsonParser.getObjectMapper().createObjectNode();
+                threshold.put("thresholdName", thresholdName);
+                threshold.put("value", newConfiguration.get(antiPatternName).get(thresholdName));
+                thresholdsArray.add(threshold);
+            }
+
+            antiPattern.set("thresholds", thresholdsArray);
+            array.add(antiPattern);
+        }
+
+        root.set("configuration", array);
+
+        try {
+            URL url = servletContext.getResource(CONFIGURATION_DIR + configurationName + ".json");
+            JsonParser.getObjectWriter().writeValue(Paths.get(url.toURI()).toFile(), root);
+        } catch (Exception e) {
+            LOGGER.error("Cannot write configuration to the file");
+        }
+
+    }
 }
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/ConfigurationServiceImpl.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/ConfigurationServiceImpl.java
index 7bd89dc..8a2b3e4 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/ConfigurationServiceImpl.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/ConfigurationServiceImpl.java
@@ -130,6 +130,8 @@ public class ConfigurationServiceImpl implements ConfigurationService {
         else
             configurationRepository.allConfigurations.replace(configurationName, newConfiguration);
 
+        configurationRepository.saveConfigurationToFile(configurationName, newConfiguration);
+
         return incorrectParameters;
     }
 }
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/utils/JsonParser.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/utils/JsonParser.java
index 3a24069..89c680d 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/utils/JsonParser.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/utils/JsonParser.java
@@ -1,7 +1,9 @@
 package cz.zcu.fav.kiv.antipatterndetectionapp.utils;
 
+import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectWriter;
 
 import java.io.IOException;
 
@@ -20,4 +22,13 @@ public class JsonParser {
     public static JsonNode parse(String src) throws IOException {
         return objectMapper.readTree(src);
     }
+
+    public static ObjectWriter getObjectWriter() {
+        ObjectWriter writer = objectMapper.writer(new DefaultPrettyPrinter());
+        return writer;
+    }
+
+    public static ObjectMapper getObjectMapper(){
+        return objectMapper;
+    }
 }
diff --git a/src/main/webapp/WEB-INF/templates/configuration.html b/src/main/webapp/WEB-INF/templates/configuration.html
index 1e6a541..d981b9c 100644
--- a/src/main/webapp/WEB-INF/templates/configuration.html
+++ b/src/main/webapp/WEB-INF/templates/configuration.html
@@ -88,8 +88,8 @@
 
             <!-- Submit button to save configuration -->
             <div class="save-configuration-button-container">
-                <button type="submit" class="btn btn-primary btn-lg custom-button" id="configuration-save-button" hidden>Save</button>
-                <button type="submit" class="btn btn-outline-primary btn-lg custom-button" id="configuration-save-as-button" hidden>Save as</button>
+                <button type="submit" class="btn btn-primary btn-lg custom-button" id="configuration-save-button" name="configuration-save-button" hidden>Save</button>
+                <button type="submit" class="btn btn-outline-primary btn-lg custom-button" id="configuration-save-as-button" name="configuration-save-as-button" hidden>Save as</button>
             </div>
             <!-- ./Submit button to save configuration -->
         </form>
