commit e284c23abb332087a6eea8b15302b6cb1f9bad32
Author: Hrabikv <hrabikv@students.zcu.cz>
Date:   Thu Oct 26 13:17:26 2023 +0200

    https://kivprogrammers.atlassian.net/browse/TSP2-29

diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/ConfigurationController.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/ConfigurationController.java
index be53078..8d5f47b 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/ConfigurationController.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/ConfigurationController.java
@@ -58,11 +58,15 @@ public class ConfigurationController {
         return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.valueOf(returnCode.getStatusCode()));
     }
 
-
+    /**
+     * Endpoint for accessing anti-patterns and configurations
+     * @param userConfiguration Wrapper around client request body
+     * @return ResponseEntity with anti-pattern array and configuration array if everything went well
+     *  ResponseEntity with error status code otherwise
+     */
     @PostMapping(value="/configuration")
     public ResponseEntity<String> getConfiguration(@RequestBody UserConfiguration userConfiguration) {
         Map<String, Object> json = new HashMap<>();
-//        Configuration config = this.configurationService.getConfigurationById(Integer.parseInt(userConfiguration.getId()));
 
         Configuration config = this.configurationService.getConfiguration(userConfiguration.getUser().getName(), Integer.parseInt(userConfiguration.getId()));
 
@@ -83,26 +87,6 @@ public class ConfigurationController {
 
         return new ResponseEntity<>(new Gson().toJson(json),HttpStatus.OK);
 
-//        List<Configuration> configuration = this.configurationService.getUserConfigurations(user);
-//        //this can only happen if db server is offline
-//        //or no default configuration exists in database
-//        if(configuration == null) {
-//            json.put("message", "internal sever error");
-//            return new ResponseEntity<>(JSONBuilder.buildJSON(json), HttpStatus.INTERNAL_SERVER_ERROR);
-//        }
-//        //list of configuration definitions
-//        List<String> configurationDefinition = new ArrayList<>();
-//        //and their names
-//        List<String> configurationNames = new ArrayList<>();
-//        //hipster syntax for Petr
-//        configuration.forEach(cfg -> {
-//            configurationDefinition.add(cfg.getConfig());
-//            configurationNames.add(cfg.getConfigurationName());
-//        });
-//        json.put("configuration",configurationDefinition);
-//        json.put("configuration_names",configurationNames);
-//
-//        return new ResponseEntity<>(new Gson().toJson(json),HttpStatus.OK);
     }
 
     /**
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/dials/ConfigurationControllerStatusCodes.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/dials/ConfigurationControllerStatusCodes.java
index 64cb5c2..4fc7c31 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/dials/ConfigurationControllerStatusCodes.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/dials/ConfigurationControllerStatusCodes.java
@@ -15,7 +15,8 @@ public enum ConfigurationControllerStatusCodes {
 
     INSERT_FAILED("Failed to save configuration",500),
 
-    INSERT_SUCCESSFUL("Configuration saved",200),
+    INSERT_SUCCESSFUL("Configuration saved",201),
+    UPDATE_SUCCESSFUL("Configuration updated",200),
 
     CONFIGURATION_PAIRING_EXISTS("Configuration already exists in your collection",400),
     CONFIGURATION_PAIRING_CREATED("configuration added to collection",200);
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/ConfigRepository.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/ConfigRepository.java
index f296d07..6954251 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/ConfigRepository.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/ConfigRepository.java
@@ -9,6 +9,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.Modifying;
 import org.springframework.data.jpa.repository.Query;
 import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.List;
 @Repository
@@ -21,9 +22,16 @@ public interface ConfigRepository extends JpaRepository<Configuration,Integer> {
     List<Configuration> getPublicConfigurations();
     //retrieve all public configurations and configurations available to the user
     //assumption - default configurations are not assigned to any user explicitly
-    @Query("select config, userconfig from Configuration config left join UserConfigurationJoin userconfig on config.id = userconfig.id.configId where userconfig.id.userId = ?1 or config.isDefault = 'Y'")
+    @Query("select config, userconfig from Configuration config inner join UserConfigurationJoin userconfig on config.id = userconfig.id.configId where userconfig.id.userId = ?1 or config.isDefault = 'Y'")
     List<Object[]> getAllUserConfigurations(int userId);
 
+    @Modifying
+    @Transactional
+    @Query("UPDATE Configuration cfg SET cfg.config = ?2, cfg.configHash= ?3 WHERE cfg.id = ?1")
+    int updateHashAndConfigurationDefinition(int configurationId, String configurationDefinition, String configurationHash);
+
+
+
     @Query("select userconfig.configurationName,userconfig.id.configId from UserConfigurationJoin userconfig where userconfig.id.userId = ?1")
     List<Object[]> getAllUserConfigurationNames(int userId);
 
@@ -31,7 +39,7 @@ public interface ConfigRepository extends JpaRepository<Configuration,Integer> {
     String findConfigurationByCompoundKey(UserConfigKey key);
 
     @Query("SELECT \n" +
-            "cfg.config \n" +
+            "cfg \n" +
             "FROM \n" +
             "UserConfigurationJoin userconfig INNER JOIN Configuration cfg \n" +
             "ON userconfig.id.configId = cfg.id\n" +
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigService.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigService.java
index fa9ce9b..d6ec348 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigService.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigService.java
@@ -10,7 +10,6 @@ import java.util.Map;
 
 public interface ConfigService {
     //upload configuration
-    ConfigurationControllerStatusCodes addConfiguration(Configuration cfg);
     ConfigurationControllerStatusCodes addConfiguration(UserConfiguration cfg);
     ConfigurationControllerStatusCodes updateConfiguration(UserConfiguration cfg);
 
@@ -21,7 +20,7 @@ public interface ConfigService {
 
     Configuration getConfigurationById(int id);
 
-    public Configuration getConfiguration(String userName, int id);
+    Configuration getConfiguration(String userName, int id);
     String getConfigurationName(int userId, int configurationId);
 
     Map<String, AntiPattern> getAntiPatterns();
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigurationServiceImplementation.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigurationServiceImplementation.java
index d972461..3bdb36a 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigurationServiceImplementation.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigurationServiceImplementation.java
@@ -37,16 +37,7 @@ public class ConfigurationServiceImplementation implements ConfigService {
     //user service is also necessary for retrieving information about users (primarily database query for fetching id)
     @Autowired
     private UserService userService;
-    /**
-     * Saves configuration to database
-     * the configuration is default to everyone
-     * @param cfg Configuration - JSON format of configuration with antipatterns and thresholds
-     * @return ResponseEntity with status code and message with more information about operation
-     */
-    @Override
-    public ConfigurationControllerStatusCodes addConfiguration(Configuration cfg) {
-        return null;
-    }
+
     /**
      * Saves configuration to database
      * the configuration is available to the user who sent the request
@@ -67,7 +58,7 @@ public class ConfigurationServiceImplementation implements ConfigService {
         User user = this.getUser(cfg.getUser());
 
         //create the hash of the configuration (w/o salting)
-        String configHash = Crypto.hashString(configuration.getConfig());
+        String configHash = createConfigurationHash(configuration);
         Configuration existingConfiguration = this.configurationRepository.findConfigurationByConfigHash(configHash);
         //configuration definition does not exist => upload the configuration into database
         if (existingConfiguration == null) {
@@ -230,12 +221,24 @@ public class ConfigurationServiceImplementation implements ConfigService {
         }
         return configurationList;
     }
-    //Wrappers around db connection to configuration table
+
+    /**
+     * finds configuration by id
+     * @param id integer id of configuration to be retrieved
+     */
     @Override
     public Configuration getConfigurationById(int id) {
         return this.configurationRepository.findConfigurationById(id);
     }
 
+    /**
+     * Method fetches configuration with id @param id from database
+     * @param userName String username
+     * @param id int configuration id
+     * @return Configuration if configuration with id @param id exists and user @param userName has access to it
+     *          null otherwise
+     */
+    @Override
     public Configuration getConfiguration(String userName, int id) {
         User user = this.userService.getUserByName(userName);
         if (user == null) {
@@ -251,6 +254,10 @@ public class ConfigurationServiceImplementation implements ConfigService {
         return this.configurationRepository.findConfigurationByCompoundKey(new UserConfigKey(userId, configurationId));
     }
 
+    /**
+     * Method loads antipatterns from json files on file system for metadata about anti patterns
+     * @return Map of antipatterns
+     */
     @Override
     public Map<String, AntiPattern> getAntiPatterns() {
         Map<String, AntiPattern> antiPatterns = new HashMap<>();
