Project + KsDocument

This commit is contained in:
Florinda
2024-11-27 09:27:52 +01:00
parent 3c93b9e686
commit 840ae0ba51
8 changed files with 291 additions and 7 deletions

View File

@@ -3,10 +3,21 @@ package com.olympus.apollo.controllers.FeApi;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.UrlResource;
import org.springframework.web.bind.annotation.*;
import com.olympus.model.apollo.KSDocument;
import com.olympus.apollo.repository.KSDocumentRepository;
import com.olympus.apollo.services.KSDocumentService;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import java.nio.file.Files;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import java.nio.file.Path;
import java.nio.file.Paths;
@RestController
@RequestMapping("/fe-api/ksdocuments")
@@ -14,12 +25,14 @@ public class KsDocumentController {
@Autowired
private KSDocumentRepository ksDocumentREpository;
@Autowired
private KSDocumentService ksDocumentService;
@GetMapping("")
public List<KSDocument> getDocuments() {
List<KSDocument> result = (List<KSDocument>) ksDocumentREpository.findAll();
List<KSDocument> result = ksDocumentService.findByProjectNameAndApplicationName();
return result;
}
@@ -30,4 +43,33 @@ public class KsDocumentController {
return result;
}
/* @PostMapping("/downloadKSDocument")
public ResponseEntity<Resource> downloadFile(@RequestBody KSDocument doc) {
try {
// Percorso al file
Path filePath = Paths.get(doc.getFilePath()).normalize();
Resource resource = new UrlResource(filePath.toUri());
if (!resource.exists()) {
return ResponseEntity.notFound().build();
}
// Configurazione della risposta HTTP
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}*/
@PostMapping("/downloadKSDocument")
public ResponseEntity<Resource> downloadFile(@RequestBody KSDocument doc) {
return ksDocumentService.downloadKSDocument(doc);
}
}

View File

@@ -0,0 +1,45 @@
package com.olympus.apollo.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.apollo.repository.ProjectRepository;
import com.olympus.apollo.security.entity.User;
import com.olympus.apollo.services.ProjectService;
import com.olympus.model.Application;
import com.olympus.model.Project;
@RestController
public class ProjectController {
@Autowired
ProjectService projectService;
@Autowired
ProjectRepository projectRepo;
@GetMapping("/userProjects")
public Iterable<Project> getUserProjects() {
return projectService.getListProjectsByUser();
}
@PostMapping("/updateSelectedProject")
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

@@ -1,7 +1,11 @@
package com.olympus.apollo.repository;
import java.util.List;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.web.bind.annotation.CrossOrigin;
@@ -13,4 +17,11 @@ import com.olympus.model.apollo.KSDocument;
public interface KSDocumentRepository extends MongoRepository<KSDocument, String> {
public Iterable<KSDocument> findAllByIngestionStatus(String status);
@Query("{ 'ingestionInfo.metadata.KsProjectName': ?0, 'ingestionInfo.metadata.KsApplicationName': ?1 }")
public List<KSDocument> findByProjectNameAndApplicationName(String projectName, String applicationName, Sort sort);
@Query("{ 'ingestionInfo.metadata.KsProjectName': ?0 }")
public List<KSDocument> findByProjectName(String projectName, Sort sort);
}

View File

@@ -0,0 +1,12 @@
package com.olympus.apollo.repository;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import com.olympus.model.Project;
@Repository
public interface ProjectRepository extends MongoRepository<Project, String> {
}

View File

@@ -4,10 +4,14 @@ import java.util.Collection;
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.model.Application;
import com.olympus.model.Project;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@@ -31,6 +35,14 @@ public class User implements UserDetails{
private String name;
private String surname;
@DocumentReference
private Project selectedProject;
@DocumentReference
private List<Project> lstProjects;
@DocumentReference
private Application selectedApplication;
private Role role;
@Override

View File

@@ -0,0 +1,78 @@
package com.olympus.apollo.services;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.apache.tomcat.util.openssl.openssl_h;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.olympus.apollo.repository.KSDocumentRepository;
import com.olympus.apollo.repository.ProjectRepository;
import com.olympus.apollo.security.entity.User;
import com.olympus.model.apollo.KSDocument;
@Service
public class KSDocumentService {
private Logger logger = LoggerFactory.getLogger(KSDocumentService.class);
@Autowired
private KSDocumentRepository ksdocRepo;
public List<KSDocument> findByProjectNameAndApplicationName() {
logger.info("findByProjectNameAndApplicationName function:");
User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
try {
if( principal.getSelectedApplication()==null){
return ksdocRepo.findByProjectName(principal.getSelectedProject().getInternal_name(), Sort.by(Sort.Direction.DESC, "ingestionDate"));
}else{
return ksdocRepo.findByProjectNameAndApplicationName(principal.getSelectedProject().getInternal_name(), principal.getSelectedApplication().getInternal_name(), Sort.by(Sort.Direction.DESC, "ingestionDate"));
}
} catch (Exception e) {
logger.error("Error in findByProjectNameAndApplicationName function: " + e.getMessage());
}
return null;
}
public ResponseEntity<Resource> downloadKSDocument(KSDocument doc) {
logger.info("downloadKSDocument function:");
try {
// Percorso al file
Path filePath = Paths.get(doc.getFilePath()).normalize();
Resource resource = new UrlResource(filePath.toUri());
if (!resource.exists()) {
return ResponseEntity.notFound().build();
}
// Determina il tipo MIME dinamicamente
String contentType = Files.probeContentType(filePath);
if (contentType == null) {
// Tipo MIME predefinito se non determinabile
contentType = "application/octet-stream";
}
// Configurazione della risposta HTTP
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, contentType) // Tipo MIME dinamico
.body(resource);
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
}

View File

@@ -0,0 +1,80 @@
package com.olympus.apollo.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.apollo.repository.ProjectRepository;
import com.olympus.apollo.security.entity.User;
import com.olympus.apollo.security.repository.UserRepository;
import com.olympus.model.Application;
import com.olympus.model.Project;
@Service
public class ProjectService {
private Logger logger = LoggerFactory.getLogger(ProjectService.class);
@Autowired
private ProjectRepository projectRepo;
@Autowired
private UserRepository userRepo;
public List<Project> getListProjectsByUser(){
logger.info("getListProjectByUser function:");
User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return principal.getLstProjects();
}
public User updateUserSelectedProject(Project idProject){
User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
principal.setSelectedProject(idProject);
principal.setSelectedApplication(null);
User u = userRepo.save(principal);
return u;
}
public User updateUserSelectedApplication(Application application){
User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
principal.setSelectedApplication(application);
User u = userRepo.save(principal);
return u;
}
public static List<ObjectId> convertToObjectIdList(List<String> listProjectIds) {
List<ObjectId> 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;
}
}

View File

@@ -12,8 +12,8 @@ spring:
ai:
azure:
openai:
endpoint: "https://ai-olympus.openai.azure.com/"
api-key: "9fb33cc69d914d4c8225b974876510b5"
endpoint: "https://ai-olympus-new.openai.azure.com/"
api-key: "4eHwvw6h7vHxTmI2870cR3EpEBs5L9sXZabr9nz37y39TXtk0xY5JQQJ99AKAC5RqLJXJ3w3AAABACOGLdow"
openai:
api-key: "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
vectorstore:
@@ -23,10 +23,14 @@ spring:
initialize-schema: true
data:
mongodb:
uri: mongodb+srv://olympus_adm:26111979@olympus.l6qor4p.mongodb.net/?retryWrites=true&w=majority&appName=Olympus
uri: mongodb+srv://olympusadmin:Camilla123!@db-olympus.global.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000
database: olympus
username: olympus_adm
password: 26111979
username: olympusadmin
password: Camilla123!
# uri: mongodb+srv://olympus_adm:26111979@olympus.l6qor4p.mongodb.net/?retryWrites=true&w=majority&appName=Olympus
# database: olympus
# username: olympus_adm
# password: 26111979
servlet:
multipart:
max-file-size: 5000000MB