Refactor project controller and user entity

- Refactor the ProjectController class to add support for updating the selected application.
- Update the User entity to include a selected application field.

Related work items: #1234
This commit is contained in:
andrea.terzani
2024-10-25 19:22:02 +02:00
parent 00a06300b9
commit 035510537a
5 changed files with 45 additions and 25 deletions

View File

@@ -8,7 +8,9 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.olympus.hermione.repository.ProjectRepository;
import com.olympus.hermione.security.entity.User;
import com.olympus.hermione.services.ProjectService;
import com.olympus.model.Application;
import com.olympus.model.Project;
@RestController
@@ -26,10 +28,14 @@ public class ProjectController {
}
@PostMapping("/updateSelectedProject")
public Boolean setSelectedProjects(@RequestBody Project projectId) {
public User setSelectedProjects(@RequestBody Project projectId) {
return projectService.updateUserSelectedProject(projectId);
}
@PostMapping("/updateSelectedApplication")
public User setSelectedApplication(@RequestBody Application application) {
return projectService.updateUserSelectedApplication(application);
}
@PostMapping("/getProject")
public Optional<Project> getUserProjects(@RequestBody String projectId) {
return projectRepo.findById(projectId);

View File

@@ -28,6 +28,8 @@ public class ScenarioExecution {
private String nextStepId;
private String currentStepDescription;
private String executedByUserId;
private ScenarioExecutionInput scenarioExecutionInput;
}

View File

@@ -9,6 +9,7 @@ import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import com.olympus.model.Application;
import com.olympus.model.Project;
import lombok.AllArgsConstructor;
@@ -38,6 +39,9 @@ public class User implements UserDetails{
@DocumentReference
private List<Project> lstProjects;
@DocumentReference
private Application selectedApplication;
private Role role;

View File

@@ -20,6 +20,7 @@ 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;
import com.olympus.model.Application;
import com.olympus.model.Project;
@Service
@@ -42,39 +43,24 @@ public class ProjectService {
}
public Boolean updateUserSelectedProject(Project idProject){
public User 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;
return u;
}
public User updateUserSelectedApplication(Application application){
User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
/* public static List<ObjectId> convertToObjectIdList(List<String> listProjectIds) {
List<ObjectId> objectIdList = new ArrayList<>();
ObjectMapper objectMapper = new ObjectMapper();
principal.setSelectedApplication(application);
User u = userRepo.save(principal);
return u;
for (String jsonString : listProjectIds) {
try {
// Deserializza la stringa JSON in un oggetto mappa
Map<String, String> 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<ObjectId> convertToObjectIdList(List<String> listProjectIds) {
List<ObjectId> objectIdList = new ArrayList<>();

View File

@@ -20,6 +20,7 @@ import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.olympus.hermione.dto.ScenarioExecutionInput;
@@ -31,6 +32,7 @@ import com.olympus.hermione.models.ScenarioStep;
import com.olympus.hermione.repository.AiModelRepository;
import com.olympus.hermione.repository.ScenarioExecutionRepository;
import com.olympus.hermione.repository.ScenarioRepository;
import com.olympus.hermione.security.entity.User;
import com.olympus.hermione.stepSolvers.AdvancedAIPromptSolver;
import com.olympus.hermione.stepSolvers.BasicAIPromptSolver;
import com.olympus.hermione.stepSolvers.BasicQueryRagSolver;
@@ -235,10 +237,30 @@ public class ScenarioExecutionService {
Scenario scenario = o_scenario.get();
logger.info("Executing scenario: " + scenario.getName());
ScenarioExecution scenarioExecution = new ScenarioExecution();
scenarioExecution.setScenario(scenario);
scenarioExecution.setScenarioExecutionInput(scenarioExecutionInput);
try{
if( SecurityContextHolder.getContext() != null
&& SecurityContextHolder.getContext().getAuthentication() != null
&& SecurityContextHolder.getContext().getAuthentication().getPrincipal()!= null
){
User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
scenarioExecution.setExecutedByUserId(principal.getId());
scenarioExecutionInput.getInputs().put("selected_application", principal.getSelectedApplication().getInternal_name());
scenarioExecutionInput.getInputs().put("selected_project", principal.getSelectedProject().getInternal_name());
}
}catch(Exception e){
logger.error("Error while saving user information in scenario execution: " + e.getMessage());
}
scenarioExecutionRepository.save(scenarioExecution);
scenarioOutput.setScenarioExecution_id(scenarioExecution.getId());