commit 1bb59448d2c4c4f3ce5199042c4a3f8b4336eb7a
Author: Jiri Trefil <badjokke@gmail.com>
Date:   Fri May 5 16:35:22 2023 +0200

    #10523 vytvoreni default usera pro konfigurace. mirny refactor konfiguracni service

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 3444451..a5981df 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
@@ -4,11 +4,15 @@ import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.*;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.ConfigRepository;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserConfigurationJoinRepository;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.ConfigurationService;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @Author Jiri Trefil
@@ -26,12 +30,29 @@ public class ConfigurationController {
         return response;
     }
 
-    @PostMapping(value="/get_configuration")
-    public ResponseEntity<String> getUserConfigurations(@RequestBody User user){
+    @PostMapping(value="/configuration")
+    public ResponseEntity<String> getUserConfigurations(@RequestBody User user) {
         ResponseEntity<String> response = this.configurationService.getUserConfigurations(user);
         return response;
     }
 
+    @PostMapping(value="/configuration_name")
+    public ResponseEntity<String> getConfigurationNames(@RequestBody User user) {
+        Map<String, Object> json = new HashMap<>();
+        List<String> configuration = this.configurationService.getConfigurationNames(user);
+        if(configuration == null) {
+            json.put("message", "internal sever error");
+            return new ResponseEntity<>(JSONBuilder.buildJSON(json), HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+        json.put("message", "ok");
+        json.put("configuration_names", configuration);
+        String jsonString = JSONBuilder.buildJSON(json);
+        return new ResponseEntity<>(jsonString, HttpStatus.OK);
+    }
+
+
+
+
 
 
 
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/UserConfigurationJoin.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/UserConfigurationJoin.java
index dc55517..3fef2e8 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/UserConfigurationJoin.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/UserConfigurationJoin.java
@@ -15,14 +15,21 @@ import javax.persistence.*;
 public class UserConfigurationJoin {
     @EmbeddedId
     private UserConfigKey id;
-
+    private String configurationName;
 
     public UserConfigurationJoin(){
     }
 
-    public UserConfigurationJoin(UserConfigKey id){
+
+    public UserConfigurationJoin(UserConfigKey id, String configurationName){
+        this.configurationName = configurationName;
         this.id = id;
     }
+
+    public String getConfigurationName() {
+        return configurationName;
+    }
+
     public UserConfigKey getUserConfigKey(){
         return this.id;
     }
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 8993274..4e427cb 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
@@ -2,6 +2,7 @@ package cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository;
 
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.Configuration;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.UserConfigKey;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.Modifying;
 import org.springframework.data.jpa.repository.Query;
@@ -10,6 +11,8 @@ import org.springframework.stereotype.Repository;
 import java.util.List;
 @Repository
 public interface ConfigRepository extends JpaRepository<Configuration,Integer> {
+    @Value("${default_user_id}")
+    int defaultUserId = 0;
     Configuration findConfigurationById(int id);
     Configuration findConfigurationByConfigHash(String hash);
     //query to get all public configurations
@@ -22,4 +25,9 @@ public interface ConfigRepository extends JpaRepository<Configuration,Integer> {
     List<Configuration> getAllUserConfigurations(int userId);
 
 
+    @Query("select userconfig.configurationName from UserConfigurationJoin userconfig where userconfig.id.userId = ?1 or userconfig.id.userId=2 ")
+    List<String> getAllUserConfigurationNames(int userId);
+
+
+
 }
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/ConfigurationService.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/ConfigurationService.java
index 9553399..ad1ab54 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/ConfigurationService.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/ConfigurationService.java
@@ -3,7 +3,7 @@ import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.Configuration;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.UserConfiguration;
 import org.springframework.http.ResponseEntity;
-
+import java.util.List;
 public interface ConfigurationService {
     //upload configuration
     ResponseEntity<String> addConfiguration(Configuration cfg);
@@ -13,6 +13,7 @@ public interface ConfigurationService {
     //get all configurations available to user
     ResponseEntity<String> getUserConfigurations(User user);
 
+    List<String> getConfigurationNames(User user);
 
 
 
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/ConfigurationServiceImplementation.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/ConfigurationServiceImplementation.java
index 459bc49..7197343 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/ConfigurationServiceImplementation.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/ConfigurationServiceImplementation.java
@@ -100,7 +100,7 @@ public class ConfigurationServiceImplementation implements ConfigurationService{
             return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.BAD_REQUEST);
         }
         //save the relation between user and configuration
-        this.userConfigurationJoinRepository.save(new UserConfigurationJoin(key));
+        this.userConfigurationJoinRepository.save(new UserConfigurationJoin(key,configuration.getConfigurationName()));
         json.put("message","configuration added to collection.");
         return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.OK);
     }
@@ -119,4 +119,18 @@ public class ConfigurationServiceImplementation implements ConfigurationService{
         String jsonString = JSONBuilder.buildJSON(json);
         return new ResponseEntity<>(jsonString,HttpStatus.OK);
     }
+
+    @Override
+    public List<String> getConfigurationNames(User user) {
+        final String userName = user.getName();
+        if(userName == null){
+            return null;
+        }
+        //client can only send his name - he obviously does not know his id in db, we have to query that
+        User userInfo = this.userService.getUserByName(userName);
+        //fetch all configurations this particular user can see
+        //ie all public configs + configurations uploaded by this particular user
+        List<String> configurationNames = this.configurationRepository.getAllUserConfigurationNames(userInfo.getId());
+        return configurationNames;
+    }
 }
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 675176c..633a101 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -31,4 +31,7 @@ logging.level.cz.zcu.fav.kiv.antipatterndetectionapp=TRACE
 # security
 auth.realm.login=http://localhost:8081/login
 auth.realm.logout=http://localhost:8081/logout
-auth.realm.authenticate=http://localhost:8081/authenticate
\ No newline at end of file
+auth.realm.authenticate=http://localhost:8081/authenticate
+
+#default configuration user
+default_user_id = 2
\ No newline at end of file
