resolved conflicts
This commit is contained in:
@@ -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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class DocumentDao {
|
||||
|
||||
@@ -33,22 +38,24 @@ public class DocumentDao {
|
||||
@Autowired
|
||||
private CallDao callDao;
|
||||
|
||||
public List<DocumentResponseBean> uploadFiles(List<MultipartFile> files, DocumentTypeEnum fileType) {
|
||||
List<DocumentEntity> documentEntities = new ArrayList<>();
|
||||
@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);
|
||||
documentEntity.setIsDeleted(false);
|
||||
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());
|
||||
documentEntity.setIsDeleted(false);
|
||||
documentEntities.add(documentEntity);
|
||||
}
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
documentRepository.saveAll(documentEntities);
|
||||
@@ -57,13 +64,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;
|
||||
}
|
||||
|
||||
documentEntity.setIsDeleted(true);
|
||||
documentRepository.save(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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user