Created crud operation for evaluation criteria,faq,lookUp data and document

This commit is contained in:
harish
2024-08-26 17:05:03 +05:30
parent 049b53ac46
commit 948e5ec31d
33 changed files with 1075 additions and 77 deletions

View File

@@ -1,8 +1,7 @@
package net.gepafin.tendermanagement.dao;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import org.apache.commons.io.FilenameUtils;
@@ -13,14 +12,20 @@ import org.springframework.web.multipart.MultipartFile;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.entities.CallEntity;
import net.gepafin.tendermanagement.entities.DocumentEntity;
import net.gepafin.tendermanagement.enums.DocumentTypeEnum;
import net.gepafin.tendermanagement.model.response.DocumentResponseBean;
import net.gepafin.tendermanagement.repositories.CallRepository;
import net.gepafin.tendermanagement.repositories.DocumentRepository;
import net.gepafin.tendermanagement.service.AmazonS3Service;
import net.gepafin.tendermanagement.util.Utils;
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
import org.springframework.beans.factory.annotation.Value;
import java.util.ArrayList;
import java.util.List;
@Component
public class DocumentDao {
@@ -34,21 +39,32 @@ public class DocumentDao {
@Autowired
private CallDao callDao;
public List<DocumentResponseBean> uploadFiles(List<MultipartFile> files, DocumentTypeEnum fileType) {
List<DocumentEntity> documentEntities = new ArrayList<>();
@Value("${aws.s3.bucket.name}")
private String bucketName;
@Value("${aws.s3.url.folder}")
private String s3Folder;
@Value("${aws.s3.url}")
private String s3Url;
@Autowired
private CallRepository callRepository;
public List<DocumentResponseBean> uploadFiles(List<MultipartFile> files,Long callId, DocumentTypeEnum fileType) {
List<DocumentEntity> documentEntities = new ArrayList<>();
CallEntity callEntity=callRepository.findById(callId).orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.CALL_NOT_FOUND)));
for (MultipartFile file : files) {
try {
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
String firstNameContain = fileName.substring(0, fileName.lastIndexOf('.'));
fileName = (firstNameContain + "." + extension);
String filepath = amazonS3Service.upload(fileName,file);
DocumentEntity documentEntity = new DocumentEntity();
documentEntity.setFileName(fileName);
documentEntity.setType(fileType.getValue());
documentEntity.setFilePath(filepath);
documentEntities.add(documentEntity);
uploadFileOnAmazonS3 result = uploadFileOnAmazonS3(file);
if(result!=null) {
DocumentEntity documentEntity = new DocumentEntity();
documentEntity.setFileName(result.fileName());
documentEntity.setCall(callEntity);
documentEntity.setType(fileType.getValue());
documentEntity.setFilePath(result.filepath());
documentEntities.add(documentEntity);
}
} catch (IOException e) {}
}
documentRepository.saveAll(documentEntities);
@@ -57,14 +73,62 @@ public class DocumentDao {
.collect(Collectors.toList());
}
public void deleteFile(Long documentId) {
DocumentEntity documentEntity = documentRepository.findById(documentId)
.orElseThrow(() -> new ResourceNotFoundException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND)));
private uploadFileOnAmazonS3 uploadFileOnAmazonS3(MultipartFile file) throws IOException {
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
String firstNameContain = fileName.substring(0, fileName.lastIndexOf('.'));
fileName = (firstNameContain + "." + extension);
String filepath = amazonS3Service.upload(fileName, file);
uploadFileOnAmazonS3 result = new uploadFileOnAmazonS3(fileName, filepath);
return result;
}
String fileName = Utils.extractFileName(documentEntity.getFilePath());
amazonS3Service.delete(fileName);
documentRepository.delete(documentEntity);
private record uploadFileOnAmazonS3(String fileName, String filepath) {
}
}
}
public void deleteFile(Long documentId){
DocumentEntity documentEntity = getDocumentEntity(documentId);
String fileName= Utils.extractFileName(documentEntity.getFilePath());
deleteFileOnAmazonS3(fileName);
documentRepository.delete(documentEntity);
}
private DocumentEntity deleteFileOnAmazonS3(String fileName) {
try {
amazonS3Service.delete(fileName);
}catch (Exception e){}
return null;
}
private DocumentEntity getDocumentEntity(Long documentId) {
Optional<DocumentEntity> documentEntity= documentRepository.findById(documentId);
if(documentEntity.isEmpty()){
throw new ResourceNotFoundException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
}
return documentEntity.orElse(null);
}
public DocumentResponseBean updateDocument(Long documentId, MultipartFile file, DocumentTypeEnum documentTypeEnum){
DocumentEntity documentEntity = getDocumentEntity(documentId);
String fileName= Utils.extractFileName(documentEntity.getFilePath());
deleteFileOnAmazonS3(fileName);
uploadFileOnAmazonS3 result= null;
try {
result = uploadFileOnAmazonS3(file);
} catch (IOException e) {}
if(result!=null){
documentEntity.setFilePath(result.filepath);
documentEntity.setFileName(result.fileName);
documentEntity.setType(documentTypeEnum.getValue());
documentRepository.save(documentEntity);
}
return callDao.convertToDocumentResponseBean(documentEntity);
}
public DocumentResponseBean getDocument(Long documentId) {
Optional<DocumentEntity> documentEntity= documentRepository.findById(documentId);
if(documentEntity.isEmpty()){
new ResourceNotFoundException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
}
return callDao.convertToDocumentResponseBean(documentEntity.orElse(null));
}
}