Script for migrating deleted Documents
This commit is contained in:
@@ -202,14 +202,8 @@ public class DelegationDao {
|
|||||||
.findByUserIdAndUserWithCompanyIdAndStatus(userEntity.getId(), userWithCompanyEntity.getId(),
|
.findByUserIdAndUserWithCompanyIdAndStatus(userEntity.getId(), userWithCompanyEntity.getId(),
|
||||||
UserCompanyDelegationStatusEnum.ACTIVE.getValue());
|
UserCompanyDelegationStatusEnum.ACTIVE.getValue());
|
||||||
|
|
||||||
UserCompanyDelegationEntity oldUserCompanyDelegationEntity = Utils.getClonedEntityForData(userCompanyDelegationEntity);
|
|
||||||
if (userCompanyDelegationEntity != null) {
|
if (userCompanyDelegationEntity != null) {
|
||||||
userCompanyDelegationEntity.setStatus(UserCompanyDelegationStatusEnum.INACTIVE.getValue());
|
deleteDelegationFromS3(userCompanyDelegationEntity);
|
||||||
userCompanyDelegationRepository.save(userCompanyDelegationEntity);
|
|
||||||
|
|
||||||
/** This code is responsible for adding a version history log for the "update user company delegation status" operation. **/
|
|
||||||
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldUserCompanyDelegationEntity)
|
|
||||||
.newData(userCompanyDelegationEntity).build());
|
|
||||||
}
|
}
|
||||||
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = uploadFileOnAmazonS3ForCompanyDelegation(file);
|
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = uploadFileOnAmazonS3ForCompanyDelegation(file);
|
||||||
userCompanyDelegationEntity = new UserCompanyDelegationEntity();
|
userCompanyDelegationEntity = new UserCompanyDelegationEntity();
|
||||||
@@ -282,16 +276,22 @@ public class DelegationDao {
|
|||||||
.findByUserIdAndUserWithCompanyIdAndStatus(userEntity.getId(), userWithCompanyEntity.getId(),
|
.findByUserIdAndUserWithCompanyIdAndStatus(userEntity.getId(), userWithCompanyEntity.getId(),
|
||||||
UserCompanyDelegationStatusEnum.ACTIVE.getValue());
|
UserCompanyDelegationStatusEnum.ACTIVE.getValue());
|
||||||
companyDao.getUserWithCompany(userEntity.getId(), companyId);
|
companyDao.getUserWithCompany(userEntity.getId(), companyId);
|
||||||
//cloned entity for old data
|
|
||||||
UserCompanyDelegationEntity oldUserCompanyDelegation = Utils.getClonedEntityForData(userCompanyDelegationEntity);
|
|
||||||
|
|
||||||
if (userCompanyDelegationEntity == null) {
|
if (userCompanyDelegationEntity == null) {
|
||||||
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.DELEGATION_NOT_FOUND));
|
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.DELEGATION_NOT_FOUND));
|
||||||
}
|
}
|
||||||
amazonS3Service.deleteDelegationfromS3(userCompanyDelegationEntity);
|
deleteDelegationFromS3(userCompanyDelegationEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteDelegationFromS3(UserCompanyDelegationEntity userCompanyDelegationEntity) {
|
||||||
|
UserCompanyDelegationEntity oldUserCompanyDelegation = Utils.getClonedEntityForData(userCompanyDelegationEntity);
|
||||||
|
String oldS3Path = userCompanyDelegationEntity.getFilePath();
|
||||||
|
String newS3Path = s3ConfigBean.generateDocumentPathForDelegationAndSignedDocument(DocOtherSourceTypeEnum.DELETED_USER_DELEGATION);
|
||||||
|
UploadFileOnAmazonS3Response response = amazonS3Service.moveFile(userCompanyDelegationEntity.getFileName(), oldS3Path, newS3Path);
|
||||||
|
userCompanyDelegationEntity.setFileName(response.getFileName());
|
||||||
|
userCompanyDelegationEntity.setFilePath(response.getFilePath());
|
||||||
userCompanyDelegationEntity.setStatus(UserCompanyDelegationStatusEnum.INACTIVE.getValue());
|
userCompanyDelegationEntity.setStatus(UserCompanyDelegationStatusEnum.INACTIVE.getValue());
|
||||||
userCompanyDelegationRepository.save(userCompanyDelegationEntity);
|
userCompanyDelegationRepository.save(userCompanyDelegationEntity);
|
||||||
|
|
||||||
/** This code is responsible for adding a version history log for the "Soft Deleting company delegation " operation. **/
|
/** This code is responsible for adding a version history log for the "Soft Deleting company delegation " operation. **/
|
||||||
loggingUtil.addVersionHistory(
|
loggingUtil.addVersionHistory(
|
||||||
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldUserCompanyDelegation).newData(userCompanyDelegationEntity)
|
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldUserCompanyDelegation).newData(userCompanyDelegationEntity)
|
||||||
|
|||||||
@@ -4,9 +4,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
|
import net.gepafin.tendermanagement.enums.*;
|
||||||
import net.gepafin.tendermanagement.enums.UserActionContextEnum;
|
|
||||||
import net.gepafin.tendermanagement.enums.VersionActionTypeEnum;
|
|
||||||
import net.gepafin.tendermanagement.model.request.VersionHistoryRequest;
|
import net.gepafin.tendermanagement.model.request.VersionHistoryRequest;
|
||||||
import net.gepafin.tendermanagement.repositories.ApplicationRepository;
|
import net.gepafin.tendermanagement.repositories.ApplicationRepository;
|
||||||
import net.gepafin.tendermanagement.util.LoggingUtil;
|
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.ApplicationEntity;
|
||||||
import net.gepafin.tendermanagement.entities.CallEntity;
|
import net.gepafin.tendermanagement.entities.CallEntity;
|
||||||
import net.gepafin.tendermanagement.entities.DocumentEntity;
|
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.DocumentResponseBean;
|
||||||
import net.gepafin.tendermanagement.model.response.UploadFileOnAmazonS3Response;
|
import net.gepafin.tendermanagement.model.response.UploadFileOnAmazonS3Response;
|
||||||
import net.gepafin.tendermanagement.repositories.ApplicationAmendmentRequestRepository;
|
import net.gepafin.tendermanagement.repositories.ApplicationAmendmentRequestRepository;
|
||||||
@@ -188,7 +185,6 @@ public class DocumentDao {
|
|||||||
if(Boolean.TRUE.equals(documentEntity.getIsDeleted())){
|
if(Boolean.TRUE.equals(documentEntity.getIsDeleted())){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
DocumentEntity oldDocumentEntity = Utils.getClonedEntityForData(documentEntity);
|
|
||||||
Long callId = null;
|
Long callId = null;
|
||||||
Long applicationId = null;
|
Long applicationId = null;
|
||||||
Long amendmentId = null;
|
Long amendmentId = null;
|
||||||
@@ -207,13 +203,8 @@ public class DocumentDao {
|
|||||||
callId = applicationEntity.getCall().getId();
|
callId = applicationEntity.getCall().getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
amazonS3Service.deleteFileFromS3(documentEntity, callId, applicationId,amendmentId);
|
deleteFileFromS3(documentEntity, callId, applicationId,amendmentId);
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DocumentEntity validateDocument(Long id) {
|
public DocumentEntity validateDocument(Long id) {
|
||||||
@@ -275,4 +266,26 @@ public class DocumentDao {
|
|||||||
DocumentEntity documentEntity = validateDocument(documentId);
|
DocumentEntity documentEntity = validateDocument(documentId);
|
||||||
return callDao.convertToDocumentResponseBean(documentEntity);
|
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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ public interface DocumentRepository extends JpaRepository<DocumentEntity, Long>
|
|||||||
|
|
||||||
@Query("SELECT d FROM DocumentEntity d WHERE d.isDeleted = false")
|
@Query("SELECT d FROM DocumentEntity d WHERE d.isDeleted = false")
|
||||||
List<DocumentEntity> findAllByIsDeleteFalse();
|
List<DocumentEntity> findAllByIsDeleteFalse();
|
||||||
|
|
||||||
|
@Query("SELECT d FROM DocumentEntity d WHERE d.isDeleted = true")
|
||||||
|
List<DocumentEntity> findAllByIsDeleteTrue();
|
||||||
|
|
||||||
List<DocumentEntity> findAllByIdInAndIsDeletedFalse(Set<Long> documentIds);
|
List<DocumentEntity> findAllByIdInAndIsDeletedFalse(Set<Long> documentIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
package net.gepafin.tendermanagement.service;
|
package net.gepafin.tendermanagement.service;
|
||||||
import net.gepafin.tendermanagement.entities.DocumentEntity;
|
|
||||||
import net.gepafin.tendermanagement.entities.UserCompanyDelegationEntity;
|
|
||||||
import net.gepafin.tendermanagement.enums.DocOtherSourceTypeEnum;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@@ -20,14 +17,6 @@ AmazonS3Service {
|
|||||||
|
|
||||||
InputStream getFile(String s3Folder, String filePath) throws IOException;
|
InputStream getFile(String s3Folder, String filePath) throws IOException;
|
||||||
|
|
||||||
String generateS3PathForDeletedDocument(DocOtherSourceTypeEnum typeOfDocument, Long callId, Long applicationId,Long amendmentId);
|
UploadFileOnAmazonS3Response moveFile(String fileName, String oldPath, String newPath);
|
||||||
|
|
||||||
String generateS3PathForDeletedDocumentForOther();
|
|
||||||
|
|
||||||
void moveFile(String bucketName, String oldPath, String newPath);
|
|
||||||
|
|
||||||
void deleteDelegationfromS3(UserCompanyDelegationEntity userCompanyDelegationEntity);
|
|
||||||
|
|
||||||
void deleteFileFromS3(DocumentEntity documentEntity, Long callId, Long applicationId,Long amendmentId);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -61,9 +61,9 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
|
|||||||
@Value("${aws.s3.region}")
|
@Value("${aws.s3.region}")
|
||||||
private String region;
|
private String region;
|
||||||
|
|
||||||
private String getBucketUrlPrefix() {
|
@Autowired
|
||||||
return "https://" + bucketName + ".s3." + region + ".amazonaws.com/";
|
S3ReUploadMigrationService s3ReUploadMigrationService;
|
||||||
}
|
|
||||||
|
|
||||||
private String upload(String fileName, String s3Folder,
|
private String upload(String fileName, String s3Folder,
|
||||||
MultipartFile file) throws IOException {
|
MultipartFile file) throws IOException {
|
||||||
@@ -146,32 +146,10 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String generateS3PathForDeletedDocument(DocOtherSourceTypeEnum typeOfDocument, Long callId, Long applicationId,Long amendmentId) {
|
public UploadFileOnAmazonS3Response moveFile(String fileName, String oldPath, String newPath) {
|
||||||
try {
|
|
||||||
return s3ConfigBean.generateDocumentPathForOther(typeOfDocument, callId, applicationId,amendmentId);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
throw new CustomValidationException(
|
|
||||||
Status.VALIDATION_ERROR,
|
|
||||||
Translator.toLocale(GepafinConstant.S3_PATH_GENERATION_ERROR_MSG)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String generateS3PathForDeletedDocumentForOther() {
|
|
||||||
try {
|
|
||||||
return s3ConfigBean.generateDocumentPathForDelegationAndSignedDocument(DocOtherSourceTypeEnum.DELETED_USER_DELEGATION);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
throw new CustomValidationException(
|
|
||||||
Status.VALIDATION_ERROR,
|
|
||||||
Translator.toLocale(GepafinConstant.S3_PATH_GENERATION_ERROR_MSG)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void moveFile(String bucketName, String oldPath, String newPath) {
|
|
||||||
try {
|
try {
|
||||||
|
newPath = cleanNewPath(oldPath, newPath);
|
||||||
|
oldPath = cleanOldPath(oldPath);
|
||||||
log.info("Moving file from {} to {} in bucket {}", oldPath, newPath, bucketName);
|
log.info("Moving file from {} to {} in bucket {}", oldPath, newPath, bucketName);
|
||||||
|
|
||||||
CopyObjectRequest copyRequest = new CopyObjectRequest(bucketName, oldPath, bucketName, newPath);
|
CopyObjectRequest copyRequest = new CopyObjectRequest(bucketName, oldPath, bucketName, newPath);
|
||||||
@@ -180,6 +158,8 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
|
|||||||
|
|
||||||
s3Client.deleteObject(bucketName, oldPath);
|
s3Client.deleteObject(bucketName, oldPath);
|
||||||
log.info("Original file deleted successfully: {}", oldPath);
|
log.info("Original file deleted successfully: {}", oldPath);
|
||||||
|
String filePath = s3Url + newPath;
|
||||||
|
return UploadFileOnAmazonS3Response.builder().fileName(fileName).filePath(filePath).build();
|
||||||
} catch (AmazonServiceException e) {
|
} catch (AmazonServiceException e) {
|
||||||
log.error("AWS service error while moving file: {}", e.getErrorMessage(), e);
|
log.error("AWS service error while moving file: {}", e.getErrorMessage(), e);
|
||||||
throw e;
|
throw e;
|
||||||
@@ -192,36 +172,11 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private String cleanNewPath(String oldPath, String newPath) {
|
||||||
public void deleteDelegationfromS3(UserCompanyDelegationEntity userCompanyDelegationEntity) {
|
return newPath + "/" + oldPath.substring(oldPath.lastIndexOf("/") + 1);
|
||||||
String oldS3Path = userCompanyDelegationEntity.getFilePath();
|
|
||||||
String newS3Path = generateS3PathForDeletedDocumentForOther()
|
|
||||||
+ "/" + oldS3Path.substring(oldS3Path.lastIndexOf("/") + 1);
|
|
||||||
|
|
||||||
String bucketUrlPrefix = getBucketUrlPrefix();
|
|
||||||
if (oldS3Path.startsWith(bucketUrlPrefix)) {
|
|
||||||
oldS3Path = oldS3Path.replace(bucketUrlPrefix, "");
|
|
||||||
}
|
|
||||||
moveFile(bucketName, oldS3Path, newS3Path);
|
|
||||||
log.info("File for company ID {} successfully moved to deleted folder.", userCompanyDelegationEntity.getId());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private String cleanOldPath(String oldPath) {
|
||||||
public void deleteFileFromS3(DocumentEntity documentEntity, Long callId, Long applicationId,Long amendmentId) {
|
return oldPath.replace(s3Url, "");
|
||||||
try {
|
|
||||||
String oldS3Path = documentEntity.getFilePath();
|
|
||||||
String newS3Path = generateS3PathForDeletedDocument(DocOtherSourceTypeEnum.valueOf("DELETED_" + documentEntity.getSource().toUpperCase()), callId, applicationId,amendmentId)
|
|
||||||
+ "/" + oldS3Path.substring(oldS3Path.lastIndexOf("/") + 1);
|
|
||||||
String bucketUrlPrefix = getBucketUrlPrefix();
|
|
||||||
if (oldS3Path.startsWith(bucketUrlPrefix)) {
|
|
||||||
oldS3Path = oldS3Path.replace(bucketUrlPrefix, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
moveFile(bucketName, oldS3Path, newS3Path);
|
|
||||||
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.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,12 +7,17 @@ import com.amazonaws.services.s3.AmazonS3Client;
|
|||||||
import com.amazonaws.services.s3.model.GetObjectRequest;
|
import com.amazonaws.services.s3.model.GetObjectRequest;
|
||||||
import com.amazonaws.services.s3.model.ObjectMetadata;
|
import com.amazonaws.services.s3.model.ObjectMetadata;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.gepafin.tendermanagement.dao.DocumentDao;
|
||||||
import net.gepafin.tendermanagement.dao.S3PathConfig;
|
import net.gepafin.tendermanagement.dao.S3PathConfig;
|
||||||
|
import net.gepafin.tendermanagement.entities.ApplicationEntity;
|
||||||
import net.gepafin.tendermanagement.entities.DocumentEntity;
|
import net.gepafin.tendermanagement.entities.DocumentEntity;
|
||||||
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
|
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
|
||||||
|
import net.gepafin.tendermanagement.repositories.ApplicationAmendmentRequestRepository;
|
||||||
import net.gepafin.tendermanagement.repositories.ApplicationRepository;
|
import net.gepafin.tendermanagement.repositories.ApplicationRepository;
|
||||||
import net.gepafin.tendermanagement.repositories.ApplicationSignedDocumentRepository;
|
import net.gepafin.tendermanagement.repositories.ApplicationSignedDocumentRepository;
|
||||||
import net.gepafin.tendermanagement.repositories.DocumentRepository;
|
import net.gepafin.tendermanagement.repositories.DocumentRepository;
|
||||||
|
import net.gepafin.tendermanagement.service.AmazonS3Service;
|
||||||
|
import net.gepafin.tendermanagement.service.ApplicationService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -50,13 +55,35 @@ public class S3ReUploadMigrationService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private AmazonS3 amazonS3;
|
private AmazonS3 amazonS3;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ApplicationService applicationService;
|
||||||
|
|
||||||
@Value("${aws.s3.url}")
|
@Value("${aws.s3.url}")
|
||||||
private String s3Url;
|
private String s3Url;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
AmazonS3Service amazonS3Service;
|
||||||
|
|
||||||
|
@Value("${aws.s3.bucket.name}")
|
||||||
|
private String bucketName;
|
||||||
|
|
||||||
|
@Value("${aws.s3.region}")
|
||||||
|
private String region;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ApplicationAmendmentRequestRepository applicationAmendmentRequestRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DocumentDao documentDao;
|
||||||
|
|
||||||
|
|
||||||
private boolean migrationCompleted = false;
|
private boolean migrationCompleted = false;
|
||||||
|
|
||||||
public String reUploadAndMigrateDocuments(String providedKey) {
|
public String reUploadAndMigrateDocuments(String providedKey) {
|
||||||
|
|
||||||
|
Long totalDocuments=0L;
|
||||||
|
Long failedDocuments=0L;
|
||||||
|
Long processDocuments=0L;
|
||||||
if (migrationCompleted) {
|
if (migrationCompleted) {
|
||||||
return "Migration already completed.";
|
return "Migration already completed.";
|
||||||
}
|
}
|
||||||
@@ -66,26 +93,48 @@ public class S3ReUploadMigrationService {
|
|||||||
return "Invalid or missing migration key.";
|
return "Invalid or missing migration key.";
|
||||||
}
|
}
|
||||||
|
|
||||||
List<DocumentEntity> documents = documentRepository.findAllByIsDeleteFalse();
|
List<DocumentEntity> documents = documentRepository.findAllByIsDeleteTrue();
|
||||||
|
totalDocuments = Long.valueOf(documents.size());
|
||||||
|
|
||||||
if (documents.isEmpty()) {
|
if (documents.isEmpty()) {
|
||||||
return "No documents found to migrate.";
|
return "No documents found to migrate.";
|
||||||
}
|
}
|
||||||
|
|
||||||
for (DocumentEntity document : documents) {
|
for (DocumentEntity document : documents) {
|
||||||
String oldUrl = document.getFilePath(); // This should contain the full URL
|
log.info("Processing for the Document id and url:{} ",document.getId(),document.getFilePath());
|
||||||
log.info("Processing {}", oldUrl);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
File localFile = downloadFileFromS3(oldUrl);
|
|
||||||
String newKey = generateNewS3Path(document); // Make sure this generates the correct new path
|
Long callId = null;
|
||||||
String uploadedPath = uploadFileToNewBucket(localFile, newKey);
|
Long applicationId = null;
|
||||||
updateDocumentPathAndNameEntry(document, uploadedPath);
|
Long amendmentId = null;
|
||||||
|
|
||||||
|
if (DocumentSourceTypeEnum.CALL.getValue().equalsIgnoreCase(document.getSource())) {
|
||||||
|
callId = document.getSourceId();
|
||||||
|
} else if (DocumentSourceTypeEnum.APPLICATION.getValue().equalsIgnoreCase(document.getSource())) {
|
||||||
|
applicationId = document.getSourceId();
|
||||||
|
ApplicationEntity applicationEntity = applicationService.validateApplication(applicationId);
|
||||||
|
callId = applicationEntity.getCall().getId();
|
||||||
|
}
|
||||||
|
else if(DocumentSourceTypeEnum.AMENDMENT.getValue().equalsIgnoreCase(document.getSource())){
|
||||||
|
amendmentId = document.getSourceId();
|
||||||
|
ApplicationEntity applicationEntity = applicationAmendmentRequestRepository.findApplicationByAmendmentId(amendmentId);
|
||||||
|
applicationId = applicationEntity.getId();
|
||||||
|
callId = applicationEntity.getCall().getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
documentDao.deleteFileFromS3(document,callId,applicationId,amendmentId);
|
||||||
|
processDocuments++;
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error processing document {}: {}", document.getId(), e.getMessage());
|
log.error("Error processing document {}: {}", document.getId(), e.getMessage());
|
||||||
|
failedDocuments++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.info("Total Documents Fetched ",totalDocuments);
|
||||||
|
log.info("Total Process Documents :{}",processDocuments);
|
||||||
|
log.info("Total Failed Documents :{}",failedDocuments);
|
||||||
return "Migrated Successfully.";
|
return "Migrated Successfully.";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValidKey(String providedKey) {
|
private boolean isValidKey(String providedKey) {
|
||||||
@@ -177,13 +226,4 @@ public class S3ReUploadMigrationService {
|
|||||||
return parts[parts.length - 1];
|
return parts[parts.length - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void updateDocumentPathAndNameEntry(DocumentEntity document, String newPath) {
|
|
||||||
|
|
||||||
String fileName = extractFileName(newPath);
|
|
||||||
document.setFilePath(newPath);
|
|
||||||
document.setFileName(fileName);
|
|
||||||
documentRepository.save(document);
|
|
||||||
log.info("Migrated document ID: {} to new path: {}", document.getId(), newPath);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.amazonaws.services.s3.AmazonS3Client;
|
|||||||
import com.amazonaws.services.s3.model.GetObjectRequest;
|
import com.amazonaws.services.s3.model.GetObjectRequest;
|
||||||
import com.amazonaws.services.s3.model.ObjectMetadata;
|
import com.amazonaws.services.s3.model.ObjectMetadata;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.gepafin.tendermanagement.dao.DelegationDao;
|
||||||
import net.gepafin.tendermanagement.dao.S3PathConfig;
|
import net.gepafin.tendermanagement.dao.S3PathConfig;
|
||||||
import net.gepafin.tendermanagement.entities.ApplicationSignedDocumentEntity;
|
import net.gepafin.tendermanagement.entities.ApplicationSignedDocumentEntity;
|
||||||
import net.gepafin.tendermanagement.entities.UserCompanyDelegationEntity;
|
import net.gepafin.tendermanagement.entities.UserCompanyDelegationEntity;
|
||||||
@@ -56,10 +57,16 @@ public class UserSignedAndDelegationServiceImpl {
|
|||||||
@Value("${aws.s3.url}")
|
@Value("${aws.s3.url}")
|
||||||
private String s3Url;
|
private String s3Url;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DelegationDao delegationDao;
|
||||||
|
|
||||||
private boolean migrationCompleted = false;
|
private boolean migrationCompleted = false;
|
||||||
|
|
||||||
public String migrateUserDelegatedDocuments(String providedKey) {
|
public String migrateUserDelegatedDocuments(String providedKey) {
|
||||||
|
|
||||||
|
Long totalDocuments=0L;
|
||||||
|
Long failedDocuments=0L;
|
||||||
|
Long processDocuments=0L;
|
||||||
if (migrationCompleted) {
|
if (migrationCompleted) {
|
||||||
return "Migration already completed.";
|
return "Migration already completed.";
|
||||||
}
|
}
|
||||||
@@ -69,25 +76,27 @@ public class UserSignedAndDelegationServiceImpl {
|
|||||||
return "Invalid or missing migration key.";
|
return "Invalid or missing migration key.";
|
||||||
}
|
}
|
||||||
|
|
||||||
List<UserCompanyDelegationEntity> documents = userCompanyDelegationRepository.findAllByStatus("ACTIVE");
|
List<UserCompanyDelegationEntity> documents = userCompanyDelegationRepository.findAllByStatus("INACTIVE");
|
||||||
|
totalDocuments = Long.valueOf(documents.size());
|
||||||
if (documents.isEmpty()) {
|
if (documents.isEmpty()) {
|
||||||
return "No documents found to migrate.";
|
return "No documents found to migrate.";
|
||||||
}
|
}
|
||||||
|
|
||||||
for (UserCompanyDelegationEntity document : documents) {
|
for (UserCompanyDelegationEntity document : documents) {
|
||||||
String oldUrl = document.getFilePath();
|
String oldUrl = document.getFilePath();
|
||||||
log.info("Processing user designated document: {}", oldUrl);
|
log.info("Processing user designated document and old Url: {}", document.getId(),oldUrl);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
File localFile = downloadFileFromS3(oldUrl);
|
delegationDao.deleteDelegationFromS3(document);
|
||||||
String newKey = generateNewS3PathForDelegationDoc();
|
processDocuments++;
|
||||||
String uploadedPath = uploadFileToNewBucket(localFile, newKey);
|
|
||||||
updateDelegatedDocumentPathAndNameEntry(document, uploadedPath);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error processing user designated document {}: {}", document.getId(), e.getMessage());
|
log.error("Error processing user designated document {}: {}", document.getId(), e.getMessage());
|
||||||
|
failedDocuments++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.info("Total Documents Fetched:{} ",totalDocuments);
|
||||||
|
log.info("Total Process Documents :{}",processDocuments);
|
||||||
|
log.info("Total Failed Documents :{}",failedDocuments);
|
||||||
return "Migrated";
|
return "Migrated";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import io.swagger.v3.oas.annotations.Parameter;
|
|||||||
import io.swagger.v3.oas.annotations.media.Content;
|
import io.swagger.v3.oas.annotations.media.Content;
|
||||||
import io.swagger.v3.oas.annotations.media.ExampleObject;
|
import io.swagger.v3.oas.annotations.media.ExampleObject;
|
||||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||||
import jakarta.validation.Valid;
|
|
||||||
import net.gepafin.tendermanagement.web.rest.api.errors.ErrorConstants;
|
import net.gepafin.tendermanagement.web.rest.api.errors.ErrorConstants;
|
||||||
import org.springframework.data.repository.query.Param;
|
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
@@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.PutMapping;
|
|||||||
@Validated
|
@Validated
|
||||||
public interface S3MigrationApi {
|
public interface S3MigrationApi {
|
||||||
|
|
||||||
@Operation(summary = "Api to migrate S3 doc to db and update s3 files as per specified folder.", responses = { @ApiResponse(responseCode = "200", description = "OK"),
|
@Operation(summary = "Api to migrate deleted documents to the deleted Folder ", responses = { @ApiResponse(responseCode = "200", description = "OK"),
|
||||||
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
||||||
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
|
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
|
||||||
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||||||
|
|
||||||
@Validated
|
@Validated
|
||||||
public interface UserSignedAndDelegationApi {
|
public interface UserSignedAndDelegationApi {
|
||||||
@Operation(summary = "Api to migrate S3 doc to db and user-delegated folder", responses = { @ApiResponse(responseCode = "200", description = "OK"),
|
@Operation(summary = "Api to migrate S3 user-delegated deleted doc to deleted folder", responses = { @ApiResponse(responseCode = "200", description = "OK"),
|
||||||
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
||||||
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
|
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
|
||||||
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
||||||
|
|||||||
Reference in New Issue
Block a user