Done Ticket GEPAFINBE-171

This commit is contained in:
Piyush
2025-05-28 11:56:17 +05:30
parent 9699964b09
commit f9b9c5f2e0
10 changed files with 361 additions and 64 deletions

View File

@@ -82,10 +82,11 @@ public class DocumentDao {
// private String s3Folder;
public List<DocumentResponseBean> uploadFiles(Long userId,List<MultipartFile> files, Long sourceId, DocumentSourceTypeEnum sourceType, DocumentTypeEnum fileType) {
log.info("Uploading files userId={}, sourceType={}, fileType={}", userId,sourceType,fileType);
List<DocumentEntity> documentEntities = new ArrayList<>();
Long source = resolveSourceId(sourceId, sourceType);
for (MultipartFile file : files) {
log.info("Uploading file '{}'", file.getOriginalFilename());
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = uploadFileOnAmazonS3(file, sourceType, sourceId);
if (uploadFileOnAmazonS3Response != null) {
DocumentEntity documentEntity = new DocumentEntity();
@@ -148,6 +149,7 @@ public class DocumentDao {
private UploadFileOnAmazonS3Response uploadFileOnAmazonS3(MultipartFile file, DocumentSourceTypeEnum type, Long sourceId) {
log.info("Starting S3 upload: fileName={}, documentType={}, sourceId={}", file.getOriginalFilename(), type, sourceId);
Long applicationId = 0L;
Long amendmentId = 0L;
Long evaluationId = 0L;
@@ -155,26 +157,32 @@ public class DocumentDao {
if (type == DocumentSourceTypeEnum.APPLICATION) {
applicationId = sourceId;
callId = applicationRepository.findCallIdById(applicationId);
log.info("Processing document of type APPLICATION .Resolved applicationId={}, callId={}", applicationId, callId);
} else if (type == DocumentSourceTypeEnum.AMENDMENT) {
amendmentId = sourceId;
ApplicationEntity applicationEntity = applicationAmendmentRequestRepository.findApplicationByAmendmentId(amendmentId);
applicationId = applicationEntity.getId();
callId = applicationEntity.getCall().getId();
log.info("Processing document of type AMENDMENT .Resolved amendmentId={}, applicationId={}, callId={}", amendmentId, applicationId, callId);
}else if (type == DocumentSourceTypeEnum.EVALUATION) {
evaluationId = sourceId;
ApplicationEntity applicationEntity = applicationEvaluationRepository.findApplicationByEvaluationId(evaluationId);
applicationId = applicationEntity.getId();
callId = applicationEntity.getCall().getId();
log.info("Processing document of type EVALUATION .Resolved evaluationId={}, applicationId={}, callId={}", evaluationId, applicationId, callId);
}
try {
String s3Path = generateS3Path(type, callId, applicationId, amendmentId);
log.info("Generated S3 path {}", s3Path);
return amazonS3Service.uploadFileOnAmazonS3(s3Path, file);
} catch (Exception e) {
log.error("Error uploading file to S3: fileName={}, documentType={}, sourceId={}, error={}",
file.getOriginalFilename(), type, sourceId, e.getMessage(), e);
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.UPLOAD_ERROR_S3));
}
}
public String generateS3Path(DocumentSourceTypeEnum typeOfDocument, Long callId, Long applicationId, Long amendmentId) {
try {
return s3ConfigBean.generateDocumentPath(typeOfDocument, callId, applicationId, amendmentId);
@@ -202,6 +210,7 @@ public class DocumentDao {
DocumentEntity documentEntity = documentRepository.findById(documentId).orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND)));
if(Boolean.TRUE.equals(documentEntity.getIsDeleted())){
log.info("Document with id={} is already marked as deleted. Skipping deletion.", documentId);
return;
}
Long callId = null;
@@ -211,24 +220,28 @@ public class DocumentDao {
if (DocumentSourceTypeEnum.CALL.getValue().equalsIgnoreCase(documentEntity.getSource())) {
callId = documentEntity.getSourceId();
log.info("Processing document of type CALL. Resolved callId={}", callId);
} else if (DocumentSourceTypeEnum.APPLICATION.getValue().equalsIgnoreCase(documentEntity.getSource())) {
applicationId = documentEntity.getSourceId();
ApplicationEntity applicationEntity = applicationService.validateApplication(applicationId);
callId = applicationEntity.getCall().getId();
log.info("Processing document of type APPLICATION. Resolved applicationId={}, callId={}", applicationId, callId);
}
else if(DocumentSourceTypeEnum.AMENDMENT.getValue().equalsIgnoreCase(documentEntity.getSource())){
amendmentId = documentEntity.getSourceId();
ApplicationEntity applicationEntity = applicationAmendmentRequestRepository.findApplicationByAmendmentId(amendmentId);
applicationId = applicationEntity.getId();
callId = applicationEntity.getCall().getId();
log.info("Processing document of type AMENDMENT. Resolved amendmentId={}, applicationId={}, callId={}", amendmentId, applicationId, callId);
} else if(DocumentSourceTypeEnum.EVALUATION.getValue().equalsIgnoreCase(documentEntity.getSource())){
evaluationId = documentEntity.getSourceId();
ApplicationEntity applicationEntity = applicationEvaluationRepository.findApplicationByEvaluationId(evaluationId);
applicationId = applicationEntity.getId();
callId = applicationEntity.getCall().getId();
log.info("Processing document of type EVALUATION. Resolved evaluationId={}, applicationId={}, callId={}", evaluationId, applicationId, callId);
}
deleteFileFromS3(documentEntity, callId, applicationId,amendmentId);
log.info("Successfully deleted file from S3 for documentId={}", documentId);
}
public DocumentEntity validateDocument(Long id) {
@@ -238,6 +251,9 @@ public class DocumentDao {
public DocumentResponseBean updateDocument(Long documentId, MultipartFile file, DocumentTypeEnum documentTypeEnum) {
log.info("Starting document update: documentId={} , newDocumentType={}",
documentId , documentTypeEnum);
DocumentEntity documentEntity = validateDocument(documentId);
//cloned entity for old data
DocumentEntity oldDocumentData = Utils.getClonedEntityForData(documentEntity);
@@ -245,12 +261,15 @@ public class DocumentDao {
String type = documentEntity.getSource();
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = updateFileOnAmazonS3(file, DocumentSourceTypeEnum.valueOf(type), documentEntity.getSourceId());
if (uploadFileOnAmazonS3Response != null) {
log.info("Successfully uploaded new file to S3: fileName={}",
uploadFileOnAmazonS3Response.getFileName());
documentEntity.setFileName(uploadFileOnAmazonS3Response.getFileName());
documentEntity.setFilePath(uploadFileOnAmazonS3Response.getFilePath());
documentEntity.setType(documentTypeEnum.getValue());
documentEntity.setSource(documentEntity.getSource());
documentEntity.setSourceId(documentEntity.getSourceId());
documentRepository.save(documentEntity);
log.info("Document updated in database for documentId={}", documentId);
/** This code is responsible for adding a version history log for the "updating doc or image" operation. **/
loggingUtil.addVersionHistory(
@@ -260,7 +279,6 @@ public class DocumentDao {
}
private UploadFileOnAmazonS3Response updateFileOnAmazonS3(MultipartFile file, DocumentSourceTypeEnum type, Long id) {
try {
Long callId=null;
Long applicationId=null;
@@ -269,27 +287,32 @@ public class DocumentDao {
if (type.equals(DocumentSourceTypeEnum.APPLICATION)) {
callId = applicationRepository.findCallIdById(id);
applicationId = id;
log.info("Processing document of type APPLICATION . Resolved applicationId={}, callId={}", applicationId, callId);
}
else if(type.equals(DocumentSourceTypeEnum.AMENDMENT)){
amendmentId = id;
ApplicationEntity applicationEntity = applicationAmendmentRequestRepository.findApplicationByAmendmentId(amendmentId);
applicationId = applicationEntity.getId();
callId = applicationEntity.getCall().getId();
log.info("Processing document of type AMENDMENT . Resolved amendmentId={}, applicationId={}, callId={}", amendmentId, applicationId, callId);
}else if(type.equals(DocumentSourceTypeEnum.EVALUATION)){
evaluationId = id;
ApplicationEntity applicationEntity = applicationEvaluationRepository.findApplicationByEvaluationId(evaluationId);
applicationId = applicationEntity.getId();
callId = applicationEntity.getCall().getId();
log.info("Processing document of type EVALUATION . Resolved evaluationId={}, applicationId={}, callId={}", evaluationId, applicationId, callId);
}
else {
callId = id;
applicationId = 0L;
log.info("Processing document of type CALL . Resolved callId={}", callId);
}
String s3Path = generateS3Path(type, callId, applicationId,amendmentId);
log.info("Generated S3 path {}", s3Path);
return amazonS3Service.uploadFileOnAmazonS3(s3Path, file);
} catch (Exception e) {
log.error("Error during file update to S3: documentType={}, sourceId={}, error={}",type, id, e.getMessage(), e);
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.UPLOAD_ERROR_S3));
}
}
@@ -304,6 +327,7 @@ public class DocumentDao {
DocumentEntity oldDocumentEntity = Utils.getClonedEntityForData(documentEntity);
String oldS3Path = documentEntity.getFilePath();
String newS3Path = s3ConfigBean.generateDocumentPathForOther(DocOtherSourceTypeEnum.valueOf("DELETED_" + documentEntity.getSource().toUpperCase()), callId, applicationId,amendmentId);
log.info("Moving file to deleted path: oldS3Path={}, newS3Path={}", oldS3Path, newS3Path);
UploadFileOnAmazonS3Response response = amazonS3Service.moveFile(documentEntity.getFileName(), oldS3Path, newS3Path);
documentEntity.setFileName(response.getFileName());
documentEntity.setFilePath(response.getFilePath());