Script for migrating deleted Documents
This commit is contained in:
@@ -6,6 +6,7 @@ import com.amazonaws.services.s3.AmazonS3Client;
|
||||
import com.amazonaws.services.s3.model.GetObjectRequest;
|
||||
import com.amazonaws.services.s3.model.ObjectMetadata;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.gepafin.tendermanagement.dao.DelegationDao;
|
||||
import net.gepafin.tendermanagement.dao.S3PathConfig;
|
||||
import net.gepafin.tendermanagement.entities.ApplicationSignedDocumentEntity;
|
||||
import net.gepafin.tendermanagement.entities.UserCompanyDelegationEntity;
|
||||
@@ -56,10 +57,16 @@ public class UserSignedAndDelegationServiceImpl {
|
||||
@Value("${aws.s3.url}")
|
||||
private String s3Url;
|
||||
|
||||
@Autowired
|
||||
private DelegationDao delegationDao;
|
||||
|
||||
private boolean migrationCompleted = false;
|
||||
|
||||
public String migrateUserDelegatedDocuments(String providedKey) {
|
||||
|
||||
Long totalDocuments=0L;
|
||||
Long failedDocuments=0L;
|
||||
Long processDocuments=0L;
|
||||
if (migrationCompleted) {
|
||||
return "Migration already completed.";
|
||||
}
|
||||
@@ -69,25 +76,27 @@ public class UserSignedAndDelegationServiceImpl {
|
||||
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()) {
|
||||
return "No documents found to migrate.";
|
||||
}
|
||||
|
||||
for (UserCompanyDelegationEntity document : documents) {
|
||||
String oldUrl = document.getFilePath();
|
||||
log.info("Processing user designated document: {}", oldUrl);
|
||||
log.info("Processing user designated document and old Url: {}", document.getId(),oldUrl);
|
||||
|
||||
try {
|
||||
File localFile = downloadFileFromS3(oldUrl);
|
||||
String newKey = generateNewS3PathForDelegationDoc();
|
||||
String uploadedPath = uploadFileToNewBucket(localFile, newKey);
|
||||
updateDelegatedDocumentPathAndNameEntry(document, uploadedPath);
|
||||
delegationDao.deleteDelegationFromS3(document);
|
||||
processDocuments++;
|
||||
} catch (Exception e) {
|
||||
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";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user