Script for migrating deleted Documents

This commit is contained in:
nisha
2024-11-29 20:44:18 +05:30
parent d8066aa61d
commit d397b3f7b7
9 changed files with 129 additions and 119 deletions

View File

@@ -4,9 +4,7 @@ import lombok.extern.slf4j.Slf4j;
import java.util.stream.Collectors;
import jakarta.servlet.http.HttpServletRequest;
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
import net.gepafin.tendermanagement.enums.UserActionContextEnum;
import net.gepafin.tendermanagement.enums.VersionActionTypeEnum;
import net.gepafin.tendermanagement.enums.*;
import net.gepafin.tendermanagement.model.request.VersionHistoryRequest;
import net.gepafin.tendermanagement.repositories.ApplicationRepository;
import net.gepafin.tendermanagement.util.LoggingUtil;
@@ -21,7 +19,6 @@ import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.entities.ApplicationEntity;
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.model.response.UploadFileOnAmazonS3Response;
import net.gepafin.tendermanagement.repositories.ApplicationAmendmentRequestRepository;
@@ -188,7 +185,6 @@ public class DocumentDao {
if(Boolean.TRUE.equals(documentEntity.getIsDeleted())){
return;
}
DocumentEntity oldDocumentEntity = Utils.getClonedEntityForData(documentEntity);
Long callId = null;
Long applicationId = null;
Long amendmentId = null;
@@ -207,13 +203,8 @@ public class DocumentDao {
callId = applicationEntity.getCall().getId();
}
amazonS3Service.deleteFileFromS3(documentEntity, callId, applicationId,amendmentId);
documentEntity.setIsDeleted(true);
documentRepository.save(documentEntity);
deleteFileFromS3(documentEntity, callId, applicationId,amendmentId);
/** This code is responsible for adding a version history log for the "Soft delete document" operation. **/
loggingUtil.addVersionHistory(
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldDocumentEntity).newData(documentEntity).build());
}
public DocumentEntity validateDocument(Long id) {
@@ -275,4 +266,26 @@ public class DocumentDao {
DocumentEntity documentEntity = validateDocument(documentId);
return callDao.convertToDocumentResponseBean(documentEntity);
}
public void deleteFileFromS3(DocumentEntity documentEntity, Long callId, Long applicationId,Long amendmentId) {
try {
DocumentEntity oldDocumentEntity = Utils.getClonedEntityForData(documentEntity);
String oldS3Path = documentEntity.getFilePath();
String newS3Path = s3ConfigBean.generateDocumentPathForOther(DocOtherSourceTypeEnum.valueOf("DELETED_" + documentEntity.getSource().toUpperCase()), callId, applicationId,amendmentId);
UploadFileOnAmazonS3Response response = amazonS3Service.moveFile(documentEntity.getFileName(), oldS3Path, newS3Path);
documentEntity.setFileName(response.getFileName());
documentEntity.setFilePath(response.getFilePath());
documentEntity.setIsDeleted(true);
documentRepository.save(documentEntity);
/** This code is responsible for adding a version history log for the "Soft delete document" operation. **/
loggingUtil.addVersionHistory(
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldDocumentEntity).newData(documentEntity).build());
log.info("File for document ID {} successfully moved to deleted folder.", documentEntity.getId());
} catch (Exception e) {
log.error("Error moving file for document ID {} to deleted folder: {}", documentEntity.getId(), e.getMessage());
throw new CustomValidationException(Status.VALIDATION_ERROR, "Error occurred while moving file to deleted folder.");
}
}
}