Resolved Conflicts

This commit is contained in:
piyuskag
2024-10-25 15:29:28 +05:30
parent 43ca16dd6d
commit 9515750540
28 changed files with 1611 additions and 400 deletions

View File

@@ -0,0 +1,19 @@
package net.gepafin.tendermanagement.service;
import net.gepafin.tendermanagement.entities.S3ConfigEntity;
import net.gepafin.tendermanagement.model.request.S3ConfigReq;
import net.gepafin.tendermanagement.model.response.S3ConfigBean;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Component
public interface S3ConfigService {
S3ConfigBean addS3Path(S3ConfigReq s3Path);
Optional<S3ConfigEntity> getS3PathByType(String type);
S3ConfigEntity deleteS3PathById(Long id);
S3ConfigBean updateS3PathConfiguration(S3ConfigReq s3PathConfigurationReq, Long id);
}

View File

@@ -34,7 +34,7 @@ public class DocumentServiceImpl implements DocumentService {
@Override
public DocumentResponseBean updateDocument(HttpServletRequest httpServletRequest, Long documentId, MultipartFile file, DocumentTypeEnum documentTypeEnum) {
return documentDao.updateDocument(documentId,file,documentTypeEnum);
return documentDao.updateDocument(documentId, file,documentTypeEnum);
}
@Override

View File

@@ -0,0 +1,41 @@
package net.gepafin.tendermanagement.service.impl;
import jakarta.transaction.Transactional;
import net.gepafin.tendermanagement.dao.S3ConfigDao;
import net.gepafin.tendermanagement.entities.S3ConfigEntity;
import net.gepafin.tendermanagement.model.request.S3ConfigReq;
import net.gepafin.tendermanagement.model.response.S3ConfigBean;
import net.gepafin.tendermanagement.service.S3ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class S3ConfigServiceImpl implements S3ConfigService {
@Autowired
S3ConfigDao s3ConfigDao;
@Override
public S3ConfigBean addS3Path(S3ConfigReq s3Path) {
return s3ConfigDao.addS3Path(s3Path);
}
@Override
public Optional<S3ConfigEntity> getS3PathByType(String type) {
return s3ConfigDao.getS3PathByType(type);
}
@Override
@Transactional
public S3ConfigEntity deleteS3PathById(Long id) {
return s3ConfigDao.deleteS3PathConfigById(id);
}
@Override
@Transactional
public S3ConfigBean updateS3PathConfiguration(S3ConfigReq s3PathConfigurationReq, Long id) {
return s3ConfigDao.updateS3PathConfiguration(s3PathConfigurationReq, id);
}
}

View File

@@ -0,0 +1,197 @@
package net.gepafin.tendermanagement.service.impl;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.AmazonS3;
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.S3PathConfig;
import net.gepafin.tendermanagement.entities.DocumentEntity;
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
import net.gepafin.tendermanagement.repositories.ApplicationRepository;
import net.gepafin.tendermanagement.repositories.ApplicationSignedDocumentRepository;
import net.gepafin.tendermanagement.repositories.DocumentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Slf4j
@Service
public class S3ReUploadMigrationService {
private static final String OLD_BUCKET = "mementoresources";
private static final String SECURE_KEY = "267163962963";
@Autowired
private DocumentRepository documentRepository;
@Autowired
private AmazonS3Client s3Client;
@Autowired
private S3PathConfig s3ConfigBean;
@Autowired
private ApplicationRepository applicationRepository;
@Autowired
private ApplicationSignedDocumentRepository applicationSignedDocumentRepository;
@Autowired
private AmazonS3 amazonS3;
@Value("${aws.s3.url}")
private String s3Url;
private boolean migrationCompleted = false;
public String reUploadAndMigrateDocuments(String providedKey) {
if (migrationCompleted) {
return "Migration already completed.";
}
// Validate the provided key
if (!isValidKey(providedKey)) {
return "Invalid or missing migration key.";
}
List<DocumentEntity> documents = documentRepository.findAllByIsDeleteFalse();
if (documents.isEmpty()) {
return "No documents found to migrate.";
}
for (DocumentEntity document : documents) {
String oldUrl = document.getFilePath(); // This should contain the full URL
log.info("Processing {}", oldUrl);
try {
File localFile = downloadFileFromS3(oldUrl);
String newKey = generateNewS3Path(document); // Make sure this generates the correct new path
String uploadedPath = uploadFileToNewBucket(localFile, newKey);
updateDocumentPathAndDeleteOldEntry(document, uploadedPath);
} catch (Exception e) {
log.error("Error processing document {}: {}", document.getId(), e.getMessage());
}
}
return "Migrated Successfully.";
}
private boolean isValidKey(String providedKey) {
return providedKey != null && providedKey.equals(SECURE_KEY);
}
private File downloadFileFromS3(String fileUrl) throws Exception {
String key = extractS3KeyFromUrl(fileUrl); // Get the S3 key from the URL
File localFile = new File("/tmp/" + extractFileName(key)); // Save file locally
GetObjectRequest getObjectRequest = new GetObjectRequest(OLD_BUCKET, key); // Use the key
try (InputStream s3Stream = s3Client.getObject(getObjectRequest).getObjectContent(); FileOutputStream outputStream = new FileOutputStream(localFile)) {
s3Stream.transferTo(outputStream);
}
log.info("Downloaded file from old S3 bucket: {}", key);
return localFile;
}
private String extractS3KeyFromUrl(String url) {
// Assuming the URL structure is consistent
return url.replace("https://mementoresources.s3.eu-west-1.amazonaws.com/", "");
}
private String uploadFileToNewBucket(File localFile, String s3Folder) {
InputStream inputStream = null; // Declare the InputStream here for cleanup
try {
// Extract file name from the local file
String fileName = extractFileName(localFile.getAbsolutePath()); // Get the file name
String path = s3Folder + "/" + fileName; // Construct the S3 path
// Create InputStream from the local file
inputStream = new FileInputStream(localFile);
// Set up object metadata
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType("application/octet-stream");
objectMetadata.setContentLength(localFile.length());
// Upload to S3
s3Client.putObject(OLD_BUCKET, path, inputStream, objectMetadata);
// Construct the full S3 URL
String fullUrl = String.format("https://%s.s3.%s.amazonaws.com/%s", OLD_BUCKET, "eu-west-1", path);
log.info("File '{}' uploaded successfully to Amazon S3 with URL: {}", fileName, fullUrl);
return fullUrl;
} catch (IOException e) {
log.error("IOException occurred during file upload for '{}': {}", localFile.getName(), e.getMessage());
throw new RuntimeException("Upload failed for: " + s3Folder + "/" + localFile.getName(), e);
} catch (AmazonServiceException e) {
log.error("Amazon service exception while uploading file '{}': {}", localFile.getName(), e.getMessage());
throw new RuntimeException("Upload failed for: " + s3Folder + "/" + localFile.getName(), e);
} catch (SdkClientException e) {
log.error("SDK client exception while uploading file '{}': {}", localFile.getName(), e.getMessage());
throw new RuntimeException("Upload failed for: " + s3Folder + "/" + localFile.getName(), e);
} finally {
// Close InputStream if it was opened
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log.warn("Failed to close InputStream for file '{}': {}", localFile.getName(), e.getMessage());
}
}
}
}
private String generateNewS3Path(DocumentEntity document) {
DocumentSourceTypeEnum sourceType = DocumentSourceTypeEnum.valueOf(document.getSource());
Long callId;
if (sourceType.equals(DocumentSourceTypeEnum.CALL)) {
return s3ConfigBean.generateDocumentPath(sourceType, document.getSourceId(), 0L);
} else {
callId = applicationRepository.findCallIdById(document.getSourceId());
return s3ConfigBean.generateDocumentPath(sourceType, callId, document.getSourceId());
}
}
private String extractFileName(String filePath) {
String[] parts = filePath.split("/");
return parts[parts.length - 1];
}
private void updateDocumentPathAndDeleteOldEntry(DocumentEntity document, String newPath) {
String fileName = extractFileName(newPath);
DocumentEntity newDocument = new DocumentEntity();
newDocument.setFilePath(newPath);
newDocument.setSource(document.getSource());
newDocument.setType(document.getType());
newDocument.setIsDeleted(false);
newDocument.setSourceId(document.getSourceId());
newDocument.setFileName(fileName);
documentRepository.save(newDocument);
documentRepository.delete(document);
log.info("Migrated document ID: {} to new path: {}", document.getId(), newPath);
}
}

View File

@@ -0,0 +1,261 @@
package net.gepafin.tendermanagement.service.impl;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
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.S3PathConfig;
import net.gepafin.tendermanagement.entities.ApplicationSignedDocumentEntity;
import net.gepafin.tendermanagement.entities.UserCompanyDelegationEntity;
import net.gepafin.tendermanagement.enums.DocOtherSourceTypeEnum;
import net.gepafin.tendermanagement.repositories.ApplicationRepository;
import net.gepafin.tendermanagement.repositories.ApplicationSignedDocumentRepository;
import net.gepafin.tendermanagement.repositories.S3ConfigRepository;
import net.gepafin.tendermanagement.repositories.UserCompanyDelegationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class UserSignedAndDelegationServiceImpl {
private static final String OLD_BUCKET = "mementoresources";
private static final String NEW_BUCKET = "mementoresources";
private static final String SECURE_KEY = "267163962963";
@Autowired
private UserCompanyDelegationRepository userCompanyDelegationRepository;
@Autowired
private AmazonS3Client s3Client;
@Autowired
private S3PathConfig s3ConfigBean;
@Autowired
private ApplicationSignedDocumentRepository applicationSignedDocumentRepository;
@Autowired
ApplicationRepository applicationRepository;
@Autowired
S3ConfigRepository s3ConfigRepository;
@Value("${aws.s3.url}")
private String s3Url;
private boolean migrationCompleted = false;
public String migrateUserDelegatedDocuments(String providedKey) {
if (migrationCompleted) {
return "Migration already completed.";
}
// Validate the provided key
if (isValidKey(providedKey)) {
return "Invalid or missing migration key.";
}
List<UserCompanyDelegationEntity> documents = userCompanyDelegationRepository.findAllByStatus("ACTIVE");
if (documents.isEmpty()) {
return "No documents found to migrate.";
}
for (UserCompanyDelegationEntity document : documents) {
String oldUrl = document.getFilePath();
log.info("Processing user designated document: {}", oldUrl);
try {
File localFile = downloadFileFromS3(oldUrl);
String newKey = generateNewS3PathForDelegationDoc();
String uploadedPath = uploadFileToNewBucket(localFile, newKey);
updateDelegatedDocumentPathAndDeleteOldEntry(document, uploadedPath);
} catch (Exception e) {
log.error("Error processing user designated document {}: {}", document.getId(), e.getMessage());
}
}
return "Migrated";
}
public String migrateUserSignedDocuments(String providedKey) {
if (migrationCompleted) {
return "Migration already completed.";
}
// Validate the provided key
if (isValidKey(providedKey)) {
return "Invalid or missing migration key.";
}
List<ApplicationSignedDocumentEntity> documents = applicationSignedDocumentRepository.findAllByIsStatus("ACTIVE");
if (documents.isEmpty()) {
return "No documents found to migrate.";
}
for (ApplicationSignedDocumentEntity document : documents) {
String oldUrl = document.getFilePath();
log.info("Processing user signed document: {}", oldUrl);
try {
File localFile = downloadFileFromS3(oldUrl);
String newKey = generateNewS3PathForUserSignedDoc(document);
String uploadedPath = uploadFileToNewBucket(localFile, newKey);
updateDocumentPathAndDeleteOldEntry(document, uploadedPath);
} catch (Exception e) {
log.error("Error processing user signed document {}: {}", document.getId(), e.getMessage());
}
}
return "Migrated.";
}
private boolean isValidKey(String providedKey) {
return providedKey == null || !providedKey.equals(SECURE_KEY);
}
private String generateNewS3PathForDelegationDoc() {
return s3ConfigBean.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_DELEGATION, 0L, 0L);
}
private String generateNewS3PathForUserSignedDoc(ApplicationSignedDocumentEntity document) {
// Fetch the list of application IDs associated with the document
List<Long> applicationIds = applicationSignedDocumentRepository.findApplicationIdIdsById(document.getId());
List<String> paths = new ArrayList<>();
// Loop through the application IDs and generate paths
for (Long applicationId : applicationIds) {
Long callId = applicationRepository.findCallIdById(applicationId);
// Construct the path for the current application and call ID
String newPath = String.format("%s/call/call_%d/application/application_%d/user_signed_document", s3ConfigRepository.getPathByTypeOther(
String.valueOf(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT)) , callId, applicationId);
log.info("Generated new S3 path: {}", newPath);
paths.add(newPath);
}
return String.join(",", paths);
}
private File downloadFileFromS3(String fileUrl) throws Exception {
String key = extractS3KeyFromUrl(fileUrl);
File localFile = new File("/tmp/" + extractFileName(key));
GetObjectRequest getObjectRequest = new GetObjectRequest(OLD_BUCKET, key);
try (InputStream s3Stream = s3Client.getObject(getObjectRequest).getObjectContent(); FileOutputStream outputStream = new FileOutputStream(localFile)) {
s3Stream.transferTo(outputStream);
}
log.info("Downloaded file from old S3 bucket: {}", key);
return localFile;
}
private String extractS3KeyFromUrl(String url) {
return url.replace("https://mementoresources.s3.eu-west-1.amazonaws.com/", "");
}
private String uploadFileToNewBucket(File localFile, String s3Path) {
InputStream inputStream = null;
try {
String fileName = extractFileName(localFile.getAbsolutePath()); // Extract file name
String fullPath = String.format("%s/%s", s3Path, fileName); // Construct full path
inputStream = new FileInputStream(localFile); // Create InputStream
// Set metadata for the file
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(localFile.length());
objectMetadata.setContentType("application/octet-stream");
// Upload the file to S3 with the constructed path
s3Client.putObject(NEW_BUCKET, fullPath, inputStream, objectMetadata);
// Construct the full S3 URL for the uploaded file
String fullUrl = String.format("https://%s.s3.%s.amazonaws.com/%s", NEW_BUCKET, "eu-west-1", fullPath);
log.info("File '{}' uploaded successfully to Amazon S3 with URL: {}", fileName, fullUrl);
return fullUrl;
} catch (IOException e) {
log.error("IOException occurred during file upload for '{}': {}", localFile.getName(), e.getMessage());
throw new RuntimeException("Upload failed for: " + localFile, e);
} catch (AmazonServiceException e) {
log.error("Amazon service exception while uploading file '{}': {}", localFile.getName(), e.getMessage());
throw new RuntimeException("Upload failed for: " + localFile, e);
} catch (SdkClientException e) {
log.error("SDK client exception while uploading file '{}': {}", localFile.getName(), e.getMessage());
throw new RuntimeException("Upload failed for: " + localFile, e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log.warn("Failed to close InputStream for file '{}': {}", localFile.getName(), e.getMessage());
}
}
}
}
private String extractFileName(String filePath) {
String[] parts = filePath.split("/");
return parts[parts.length - 1];
}
private String extractFileNameFromPath(String path) {
return path.substring(path.lastIndexOf('/') + 1);
}
private void updateDocumentPathAndDeleteOldEntry(ApplicationSignedDocumentEntity document, String newPath) {
ApplicationSignedDocumentEntity newDocument = new ApplicationSignedDocumentEntity();
String fileName = extractFileNameFromPath(newPath);
newDocument.setFilePath(newPath);
newDocument.setFileName(fileName);
newDocument.setApplication(document.getApplication());
newDocument.setStatus("ACTIVE");
applicationSignedDocumentRepository.save(newDocument);
applicationSignedDocumentRepository.delete(document);
log.info("Migrated document ID: {} to new path: {}", document.getId(), newPath);
}
private void updateDelegatedDocumentPathAndDeleteOldEntry(UserCompanyDelegationEntity document, String newPath) {
String fileName = extractFileNameFromPath(newPath);
UserCompanyDelegationEntity newDocument = new UserCompanyDelegationEntity();
newDocument.setFilePath(newPath);
newDocument.setFileName(fileName);
newDocument.setBeneficiaryId(document.getBeneficiaryId());
newDocument.setUserId(document.getUserId());
newDocument.setCompanyId(document.getCompanyId());
newDocument.setStatus("ACTIVE");
userCompanyDelegationRepository.save(newDocument);
userCompanyDelegationRepository.delete(document);
log.info("Migrated document ID: {} to new path: {}", document.getId(), newPath);
}
}