diff --git a/src/main/java/com/olympus/hermione/controllers/ApplicationController.java b/src/main/java/com/olympus/hermione/controllers/ApplicationController.java new file mode 100644 index 0000000..408737c --- /dev/null +++ b/src/main/java/com/olympus/hermione/controllers/ApplicationController.java @@ -0,0 +1,76 @@ +package com.olympus.hermione.controllers; + +import java.util.Optional; +import org.springframework.beans.factory.annotation.Autowired; +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.RestController; + +import com.olympus.hermione.models.Application; +import com.olympus.hermione.models.Project; +import com.olympus.hermione.repository.ApplicationRepository; +import com.olympus.hermione.repository.ProjectRepository; +import com.olympus.hermione.services.ApplicationService; +import com.olympus.hermione.services.ProjectService; +import java.util.List; +import java.util.ArrayList; + + +@RestController +public class ApplicationController { + + @Autowired + ApplicationService appService; + + @Autowired + ApplicationRepository appRepo; + + @Autowired + ProjectRepository projectRepo; + + @GetMapping("/userApplications") + public Iterable getUserApplications() { + return appService.getListApplicationsByProject(); + } + + /*@PostMapping("/getApp") + public Project getUserProjects(@RequestBody String appId) { + Optional a = appRepo.findById(appId); + //Project p = a.get(); + + + return p; + }*/ + + @GetMapping("/create") + public String createApplication() { + Application a = new Application(); + + a.setFE_name(""); + a.setInternal_name(""); + a.setDescription(null); + + a = appRepo.save(a); + + + return ""; + } + + @GetMapping("/createProject") + public Project createProject() { + + Application a = appRepo.findById("6708dfb56fbbc0bafa1df68c").get(); + + Project p = new Project(); + + p.setDescription("DOO Administrator"); + p.setFE_name("DOOA"); + p.setInternal_name("DOOA"); + List lst = new ArrayList(); + lst.add(a); + p.setLstApplications(lst); + projectRepo.save(p); + return p; + } +} diff --git a/src/main/java/com/olympus/hermione/controllers/ProjectController.java b/src/main/java/com/olympus/hermione/controllers/ProjectController.java new file mode 100644 index 0000000..232c015 --- /dev/null +++ b/src/main/java/com/olympus/hermione/controllers/ProjectController.java @@ -0,0 +1,36 @@ +package com.olympus.hermione.controllers; + +import java.util.Optional; +import org.springframework.beans.factory.annotation.Autowired; +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.RestController; +import com.olympus.hermione.models.Project; +import com.olympus.hermione.repository.ProjectRepository; +import com.olympus.hermione.services.ProjectService; + +@RestController +public class ProjectController { + + @Autowired + ProjectService projectService; + + @Autowired + ProjectRepository projectRepo; + + @GetMapping("/userProjects") + public Iterable getUserProjects() { + return projectService.getListProjectsByUser(); + } + + @PostMapping("/updateSelectedProject") + public Boolean setSelectedProjects(@RequestBody Project projectId) { + return projectService.updateUserSelectedProject(projectId); + } + + @PostMapping("/getProject") + public Optional getUserProjects(@RequestBody String projectId) { + return projectRepo.findById(projectId); + } +} diff --git a/src/main/java/com/olympus/hermione/controllers/ScenarioController.java b/src/main/java/com/olympus/hermione/controllers/ScenarioController.java index 3e62c41..dbdf9e0 100644 --- a/src/main/java/com/olympus/hermione/controllers/ScenarioController.java +++ b/src/main/java/com/olympus/hermione/controllers/ScenarioController.java @@ -8,11 +8,14 @@ import org.springframework.web.bind.annotation.RestController; import com.olympus.hermione.dto.ScenarioExecutionInput; import com.olympus.hermione.dto.ScenarioOutput; +import com.olympus.hermione.models.Application; +import com.olympus.hermione.models.Project; import com.olympus.hermione.models.Scenario; import com.olympus.hermione.models.ScenarioExecution; import com.olympus.hermione.repository.ScenarioRepository; import com.olympus.hermione.repository.ScenarioExecutionRepository; import com.olympus.hermione.services.ScenarioExecutionService; +import com.olympus.hermione.services.ScenarioService; import org.springframework.web.bind.annotation.RequestBody; @@ -25,12 +28,24 @@ public class ScenarioController { ScenarioExecutionRepository scenarioExecutionRepository; @Autowired ScenarioExecutionService scenarioExecutionService; + @Autowired + ScenarioService scenarioService; @GetMapping("/scenarios") public Iterable getScenarios() { return scenarioRepository.findAll(); } + @PostMapping("/scenariosProject") + public Iterable getScenariosByProject(@RequestBody Project projectId) { + return scenarioService.getListScenariosByProject(projectId.getId()); + } + + @PostMapping("/scenariosByApp") + public Iterable getScenariosByApp(@RequestBody Application appId) { + return scenarioService.getListScenariosByApplication(appId.getId()); + } + @GetMapping("/scenarios/{id}") public Scenario getScenario(@PathVariable String id) { return scenarioRepository.findById(id).get(); diff --git a/src/main/java/com/olympus/hermione/models/Application.java b/src/main/java/com/olympus/hermione/models/Application.java new file mode 100644 index 0000000..a3c1765 --- /dev/null +++ b/src/main/java/com/olympus/hermione/models/Application.java @@ -0,0 +1,24 @@ +package com.olympus.hermione.models; + +import org.bson.types.ObjectId; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.data.mongodb.core.mapping.DocumentReference; +import org.springframework.data.mongodb.core.mapping.Field; +import org.springframework.data.mongodb.core.mapping.FieldType; + +import lombok.Getter; +import lombok.Setter; + +@Document(collection = "applications") +@Getter @Setter +public class Application { + + @Id + @Field(targetType = FieldType.OBJECT_ID) + private String id; + private String internal_name; + private String FE_name; + private String description; + +} diff --git a/src/main/java/com/olympus/hermione/models/Project.java b/src/main/java/com/olympus/hermione/models/Project.java new file mode 100644 index 0000000..40b759a --- /dev/null +++ b/src/main/java/com/olympus/hermione/models/Project.java @@ -0,0 +1,28 @@ +package com.olympus.hermione.models; + +import java.util.List; + +import org.bson.types.ObjectId; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.data.mongodb.core.mapping.DocumentReference; +import org.springframework.data.mongodb.core.mapping.Field; +import org.springframework.data.mongodb.core.mapping.FieldType; + +import lombok.Getter; +import lombok.Setter; + +@Document(collection = "projects") +@Getter @Setter +public class Project { + + @Id + @Field(targetType = FieldType.OBJECT_ID) + private String id; + private String internal_name; + private String FE_name; + private String description; + @DocumentReference + private List lstApplications; + +} diff --git a/src/main/java/com/olympus/hermione/models/Scenario.java b/src/main/java/com/olympus/hermione/models/Scenario.java index c9af8a7..9515ffd 100644 --- a/src/main/java/com/olympus/hermione/models/Scenario.java +++ b/src/main/java/com/olympus/hermione/models/Scenario.java @@ -4,6 +4,9 @@ import java.util.List; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.data.mongodb.core.mapping.DocumentReference; +import org.springframework.data.mongodb.core.mapping.Field; +import org.springframework.data.mongodb.core.mapping.FieldType; import lombok.Getter; import lombok.Setter; @@ -12,6 +15,7 @@ import lombok.Setter; @Getter @Setter public class Scenario { @Id + @Field(targetType = FieldType.OBJECT_ID) private String id; private String name; private String description; @@ -19,5 +23,10 @@ public class Scenario { private List steps; private List inputs; private String modelId; + @DocumentReference + private List availableForProjects; + @DocumentReference + private List availableForApplications; + private boolean useChatMemory=false; } diff --git a/src/main/java/com/olympus/hermione/repository/ApplicationRepository.java b/src/main/java/com/olympus/hermione/repository/ApplicationRepository.java new file mode 100644 index 0000000..7f99abf --- /dev/null +++ b/src/main/java/com/olympus/hermione/repository/ApplicationRepository.java @@ -0,0 +1,14 @@ +package com.olympus.hermione.repository; + +import org.bson.types.ObjectId; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; +import java.util.List; + +import com.olympus.hermione.models.Application; + +@Repository +public interface ApplicationRepository extends MongoRepository { + + //List findByProjectId(String projectId); +} diff --git a/src/main/java/com/olympus/hermione/repository/ProjectRepository.java b/src/main/java/com/olympus/hermione/repository/ProjectRepository.java new file mode 100644 index 0000000..d47001d --- /dev/null +++ b/src/main/java/com/olympus/hermione/repository/ProjectRepository.java @@ -0,0 +1,14 @@ +package com.olympus.hermione.repository; + +import org.bson.types.ObjectId; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; + +import com.olympus.hermione.dto.ScenarioOutput; +import com.olympus.hermione.models.Project; + +@Repository +public interface ProjectRepository extends MongoRepository { + + +} diff --git a/src/main/java/com/olympus/hermione/repository/ScenarioRepository.java b/src/main/java/com/olympus/hermione/repository/ScenarioRepository.java index 6500a34..652ee5f 100644 --- a/src/main/java/com/olympus/hermione/repository/ScenarioRepository.java +++ b/src/main/java/com/olympus/hermione/repository/ScenarioRepository.java @@ -1,11 +1,27 @@ package com.olympus.hermione.repository; +import org.bson.types.ObjectId; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; +import java.util.List; import com.olympus.hermione.models.Scenario; @Repository -public interface ScenarioRepository extends CrudRepository { +public interface ScenarioRepository extends MongoRepository { + /* @Query("{ 'usable_for._id': ?0 }") + List findByUsableFor(String projectId);*/ + + /* @Query("{ 'usable_for': ?0 }") + List findByUsableFor(String projectId);*/ + + List findByAvailableForProjects_Id(String projectId); + + List findByAvailableForApplications_Id(String projectId); + + + } diff --git a/src/main/java/com/olympus/hermione/security/controllers/AuthController.java b/src/main/java/com/olympus/hermione/security/controllers/AuthController.java index 7542f13..678cd7b 100644 --- a/src/main/java/com/olympus/hermione/security/controllers/AuthController.java +++ b/src/main/java/com/olympus/hermione/security/controllers/AuthController.java @@ -19,6 +19,7 @@ import com.olympus.hermione.security.dto.FetchUserResponse; import com.olympus.hermione.security.entity.User; import com.olympus.hermione.security.utility.JwtTokenProvider; + @RestController @RequestMapping("/api/auth") public class AuthController { @@ -42,10 +43,10 @@ public class AuthController { SecurityContextHolder.getContext().setAuthentication(authentication); String jwt = jwtTokenProvider.createToken(authentication); - HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("authorization", jwt); httpHeaders.add("access-control-expose-headers", "authorization"); + AuthenticationResponse authenticationResponse = new AuthenticationResponse(jwt, (User) authentication.getPrincipal()); return ResponseEntity.ok().headers(httpHeaders).body(authenticationResponse); diff --git a/src/main/java/com/olympus/hermione/security/dto/AuthenticationResponse.java b/src/main/java/com/olympus/hermione/security/dto/AuthenticationResponse.java index 6e65091..4f2bd25 100644 --- a/src/main/java/com/olympus/hermione/security/dto/AuthenticationResponse.java +++ b/src/main/java/com/olympus/hermione/security/dto/AuthenticationResponse.java @@ -1,5 +1,8 @@ package com.olympus.hermione.security.dto; +import java.util.List; + +import com.olympus.hermione.models.Project; import com.olympus.hermione.security.entity.User; import lombok.AllArgsConstructor; diff --git a/src/main/java/com/olympus/hermione/security/entity/User.java b/src/main/java/com/olympus/hermione/security/entity/User.java index 4bbd870..6c9b7eb 100644 --- a/src/main/java/com/olympus/hermione/security/entity/User.java +++ b/src/main/java/com/olympus/hermione/security/entity/User.java @@ -1,12 +1,16 @@ package com.olympus.hermione.security.entity; import java.util.Collection; +import org.bson.types.ObjectId; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.data.mongodb.core.mapping.DocumentReference; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; +import com.olympus.hermione.models.Project; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -29,6 +33,11 @@ public class User implements UserDetails{ private String password; private String name; private String surname; + @DocumentReference + private Project selectedProject; + @DocumentReference + private List lstProjects; + private Role role; diff --git a/src/main/java/com/olympus/hermione/services/ApplicationService.java b/src/main/java/com/olympus/hermione/services/ApplicationService.java new file mode 100644 index 0000000..5560d3c --- /dev/null +++ b/src/main/java/com/olympus/hermione/services/ApplicationService.java @@ -0,0 +1,47 @@ +package com.olympus.hermione.services; + +import org.bson.types.ObjectId; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; + +import com.olympus.hermione.models.Project; +import com.olympus.hermione.repository.ApplicationRepository; +import com.olympus.hermione.repository.ProjectRepository; +import com.olympus.hermione.security.entity.User; +import com.olympus.hermione.models.Application; +import java.util.List; + +@Service +public class ApplicationService { + + private Logger logger = LoggerFactory.getLogger(ApplicationService.class); + + @Autowired + private ApplicationRepository appRepo; + + public List getListApplicationsByProject(){ + List lstApp = null; + User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + + // lstApp = appRepo.findByProjectId(principal.getSelectedProject()); + + //List objectIds = convertToObjectIdList(principal.getSelected_progect()); + + //List lstProject = projectRepo.findAllById(objectIds); + + logger.info("getListProjectByIds function:"); + + /* Application all = new Application(); + all.setFE_name("ALL"); + all.setInternal_name("ALL"); + all.setId(new ObjectId("ALL")); + lstApp.add(all);*/ + + return lstApp; + + + } +} diff --git a/src/main/java/com/olympus/hermione/services/ProjectService.java b/src/main/java/com/olympus/hermione/services/ProjectService.java new file mode 100644 index 0000000..3f8f3a6 --- /dev/null +++ b/src/main/java/com/olympus/hermione/services/ProjectService.java @@ -0,0 +1,95 @@ +package com.olympus.hermione.services; + + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +import org.bson.types.ObjectId; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.olympus.hermione.dto.ScenarioOutput; +import com.olympus.hermione.models.Project; +import com.olympus.hermione.models.ScenarioExecution; +import com.olympus.hermione.repository.ProjectRepository; +import com.olympus.hermione.security.entity.User; +import com.olympus.hermione.security.repository.UserRepository; + +@Service +public class ProjectService { + + private Logger logger = LoggerFactory.getLogger(ProjectService.class); + + @Autowired + private ProjectRepository projectRepo; + + @Autowired + private UserRepository userRepo; + + public List getListProjectsByUser(){ + logger.info("getListProjectByUser function:"); + + User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + + return principal.getLstProjects(); + + } + + public Boolean updateUserSelectedProject(Project idProject){ + User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + + principal.setSelectedProject(idProject); + User u = userRepo.save(principal); + + User principal2 = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + + return true; + + } + + + /* public static List convertToObjectIdList(List listProjectIds) { + List objectIdList = new ArrayList<>(); + ObjectMapper objectMapper = new ObjectMapper(); + + for (String jsonString : listProjectIds) { + try { + // Deserializza la stringa JSON in un oggetto mappa + Map map = objectMapper.readValue(jsonString, Map.class); + // Estrai il valore dell'ObjectId e creane un'istanza + String oid = map.get("$oid"); + if (oid != null) { + objectIdList.add(new ObjectId(oid)); + } + } catch (Exception e) { + e.printStackTrace(); // Gestione degli errori + } + } + + return objectIdList; + } */ + + public static List convertToObjectIdList(List listProjectIds) { + List objectIdList = new ArrayList<>(); + + for (String idString : listProjectIds) { + try { + // Crea un nuovo ObjectId dalla stringa e aggiungilo alla lista + objectIdList.add(new ObjectId(idString)); + } catch (IllegalArgumentException e) { + // Gestione degli errori per stringhe non valide che non possono essere convertite in ObjectId + System.err.println("Invalid ObjectId string: " + idString); + } + } + + return objectIdList; + } + +} diff --git a/src/main/java/com/olympus/hermione/services/ScenarioService.java b/src/main/java/com/olympus/hermione/services/ScenarioService.java new file mode 100644 index 0000000..8d18a9c --- /dev/null +++ b/src/main/java/com/olympus/hermione/services/ScenarioService.java @@ -0,0 +1,52 @@ +package com.olympus.hermione.services; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; + +import com.olympus.hermione.models.Project; +import com.olympus.hermione.models.Scenario; +import com.olympus.hermione.repository.ProjectRepository; +import com.olympus.hermione.repository.ScenarioRepository; +import com.olympus.hermione.security.entity.User; +import java.util.List; +import org.bson.types.ObjectId; + +@Service +public class ScenarioService { + + private Logger logger = LoggerFactory.getLogger(ScenarioService.class); + + @Autowired + private ScenarioRepository scenarioRepo; + + public List getListScenariosByProject(String project){ + logger.info("getListProjectByUser function:"); + List lstScenarios = null; + try{ + lstScenarios = scenarioRepo.findByAvailableForProjects_Id(project); + }catch(Exception e){ + logger.error("Exception ScenarioRepository:", e.getMessage()); + } + + logger.info("getListProjectByUser function:"); + return lstScenarios; + + } + + public List getListScenariosByApplication(String app){ + logger.info("getListProjectByUser function:"); + List lstScenarios = null; + try{ + lstScenarios = scenarioRepo.findByAvailableForApplications_Id(app); + }catch(Exception e){ + logger.error("Exception ScenarioRepository:", e.getMessage()); + } + + logger.info("getListProjectByUser function:"); + return lstScenarios; + + } +} diff --git a/src/main/java/com/olympus/hermione/stepSolvers/BasicQueryRagSolver.java b/src/main/java/com/olympus/hermione/stepSolvers/BasicQueryRagSolver.java index 00bf6ed..dc44f88 100644 --- a/src/main/java/com/olympus/hermione/stepSolvers/BasicQueryRagSolver.java +++ b/src/main/java/com/olympus/hermione/stepSolvers/BasicQueryRagSolver.java @@ -1,7 +1,7 @@ package com.olympus.hermione.stepSolvers; -import java.util.ArrayList; import java.util.List; +import java.util.ArrayList; import org.slf4j.LoggerFactory; import org.springframework.ai.document.Document; @@ -65,7 +65,7 @@ public class BasicQueryRagSolver extends StepSolver { } - @Override + @Override public ScenarioExecution solveStep(){ logger.info("Solving step: " + this.step.getName());