Done Ticket GEPAFINBE-71
This commit is contained in:
@@ -2,7 +2,10 @@ package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
|
||||
import net.gepafin.tendermanagement.repositories.ApplicationRepository;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -24,6 +27,7 @@ import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class DocumentDao {
|
||||
|
||||
@@ -38,6 +42,12 @@ public class DocumentDao {
|
||||
|
||||
@Autowired
|
||||
private CallService callService;
|
||||
|
||||
@Autowired
|
||||
private S3PathConfig s3ConfigBean;
|
||||
|
||||
@Autowired
|
||||
private ApplicationRepository applicationFormRepository;
|
||||
|
||||
@Value("${aws.s3.url.folder}")
|
||||
private String s3Folder;
|
||||
@@ -46,8 +56,7 @@ public class DocumentDao {
|
||||
List<DocumentEntity> documentEntities = new ArrayList<>();
|
||||
Long source = resolveSourceId(sourceId, sourceType);
|
||||
for (MultipartFile file : files) {
|
||||
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = amazonS3Service.uploadFileOnAmazonS3(s3Folder,
|
||||
file);
|
||||
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = uploadFileOnAmazonS3(file, sourceType, sourceId);
|
||||
if (uploadFileOnAmazonS3Response != null) {
|
||||
DocumentEntity documentEntity = new DocumentEntity();
|
||||
documentEntity.setFileName(uploadFileOnAmazonS3Response.getFileName());
|
||||
@@ -62,6 +71,30 @@ public class DocumentDao {
|
||||
documentRepository.saveAll(documentEntities);
|
||||
return documentEntities.stream().map(callDao::convertToDocumentResponseBean).collect(Collectors.toList());
|
||||
}
|
||||
private UploadFileOnAmazonS3Response uploadFileOnAmazonS3(MultipartFile file, DocumentSourceTypeEnum type, Long sourceId) {
|
||||
|
||||
Long applicationId = 0L;
|
||||
Long callId = sourceId;
|
||||
if (type == DocumentSourceTypeEnum.APPLICATION) {
|
||||
applicationId = sourceId;
|
||||
callId = applicationFormRepository.findCallIdById(applicationId);
|
||||
}
|
||||
try {
|
||||
String s3Path = generateS3Path(type, callId, applicationId);
|
||||
log.info("Generated S3 path {}", s3Path);
|
||||
return amazonS3Service.uploadFileOnAmazonS3(s3Path, file);
|
||||
} catch (Exception e) {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.UPLOAD_ERROR_S3));
|
||||
}
|
||||
}
|
||||
public String generateS3Path(DocumentSourceTypeEnum typeOfDocument, Long callId, Long applicationId) {
|
||||
|
||||
try {
|
||||
return s3ConfigBean.generateDocumentPath(typeOfDocument, callId, applicationId);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.S3_PATH_GENERATION_ERROR_MSG));
|
||||
}
|
||||
}
|
||||
private Long resolveSourceId(Long sourceId, DocumentSourceTypeEnum sourceType) {
|
||||
if (sourceType == DocumentSourceTypeEnum.CALL) {
|
||||
CallEntity callEntity = callService.validateCall(sourceId);
|
||||
@@ -91,8 +124,9 @@ public class DocumentDao {
|
||||
|
||||
public DocumentResponseBean updateDocument(Long documentId, MultipartFile file, DocumentTypeEnum documentTypeEnum) {
|
||||
DocumentEntity documentEntity = validateDocument(documentId);
|
||||
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = amazonS3Service.uploadFileOnAmazonS3(s3Folder, file);
|
||||
if (uploadFileOnAmazonS3Response != null) {
|
||||
String type = documentEntity.getSource();
|
||||
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = updateFileOnAmazonS3(file, DocumentSourceTypeEnum.valueOf(type), documentEntity.getSourceId());
|
||||
if (uploadFileOnAmazonS3Response != null) {
|
||||
documentEntity.setFileName(uploadFileOnAmazonS3Response.getFileName());
|
||||
documentEntity.setFilePath(uploadFileOnAmazonS3Response.getFilePath());
|
||||
documentEntity.setType(documentTypeEnum.getValue());
|
||||
@@ -102,7 +136,25 @@ public class DocumentDao {
|
||||
}
|
||||
return callDao.convertToDocumentResponseBean(documentEntity);
|
||||
}
|
||||
private UploadFileOnAmazonS3Response updateFileOnAmazonS3(MultipartFile file, DocumentSourceTypeEnum type, Long id) {
|
||||
|
||||
try {
|
||||
Long callId;
|
||||
Long applicationId;
|
||||
if(type.equals("APPLICATION")){
|
||||
callId = applicationFormRepository.findCallIdById(id);
|
||||
applicationId = id;
|
||||
}else{
|
||||
callId = id;
|
||||
applicationId = 0L;
|
||||
}
|
||||
String s3Path = generateS3Path(type, callId, applicationId);
|
||||
log.info("Generated S3 path {}", s3Path);
|
||||
return amazonS3Service.uploadFileOnAmazonS3(s3Path, file);
|
||||
} catch (Exception e) {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.UPLOAD_ERROR_S3));
|
||||
}
|
||||
}
|
||||
public DocumentResponseBean getDocument(Long documentId) {
|
||||
DocumentEntity documentEntity = validateDocument(documentId);
|
||||
return callDao.convertToDocumentResponseBean(documentEntity);
|
||||
|
||||
Reference in New Issue
Block a user