updated code
This commit is contained in:
@@ -258,30 +258,29 @@ public class CompanyDocumentDao {
|
||||
return userActionContext;
|
||||
}
|
||||
|
||||
public DocumentResponseBean validateAndDuplicateCompanyDocument(HttpServletRequest request , Long userId ,Long companyDocumentId , Long applicationId , DocumentTypeEnum documentTypeEnum){
|
||||
public DocumentResponseBean createDuplicateCompanyDocument(HttpServletRequest request , Long userId ,Long companyDocumentId , Long applicationId , DocumentTypeEnum documentTypeEnum){
|
||||
ApplicationEntity applicationEntity = applicationService.validateApplication(applicationId);
|
||||
CompanyDocumentEntity companyDocumentEntity = validateCompanyDocument(companyDocumentId);
|
||||
validator.validateUserWithCompany(request,companyDocumentEntity.getCompanyId());
|
||||
|
||||
String oldS3Path = companyDocumentEntity.getFilePath();
|
||||
String newS3Path = s3ConfigBean.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION,applicationEntity.getCall().getId(),applicationId,0L);
|
||||
String companyDocumentPath = companyDocumentEntity.getFilePath();
|
||||
String documentPath = s3ConfigBean.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION,applicationEntity.getCall().getId(),applicationId,0L);
|
||||
|
||||
log.info("Original Paths - oldPath: {}, newPath: {}", oldS3Path, newS3Path);
|
||||
log.info("Original Paths - oldPath: {}, newPath: {}", companyDocumentPath, documentPath);
|
||||
|
||||
oldS3Path = amazonS3ServiceImpl.decodeS3Key(amazonS3ServiceImpl.cleanOldPath(oldS3Path));
|
||||
newS3Path = amazonS3ServiceImpl.cleanNewPath(oldS3Path, newS3Path);
|
||||
log.info("Moving file from {} to {} in bucket {}", oldS3Path, newS3Path, bucketName);
|
||||
UploadFileOnAmazonS3Response response;
|
||||
try {
|
||||
response = amazonS3ServiceImpl.copyFile(companyDocumentEntity.getName(), companyDocumentPath, documentPath);
|
||||
} catch (Exception e) {
|
||||
log.error("Error occurred while uploading file from Amazon S3: {}", e);
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
||||
Translator.toLocale(GepafinConstant.UPLOAD_ERROR_S3));
|
||||
}
|
||||
|
||||
CopyObjectRequest copyRequest = new CopyObjectRequest(bucketName, oldS3Path, bucketName, newS3Path);
|
||||
s3Client.copyObject(copyRequest);
|
||||
log.info("File copied successfully from {} to {}", oldS3Path, newS3Path);
|
||||
|
||||
URL amazonS3Url = amazonS3.getUrl(bucketName, newS3Path);
|
||||
String fileUrl = amazonS3Url.toString();
|
||||
|
||||
DocumentEntity entity = new DocumentEntity();
|
||||
entity.setFilePath(fileUrl);
|
||||
entity.setFileName(companyDocumentEntity.getFileName());
|
||||
entity.setFilePath(response.getFilePath());
|
||||
entity.setFileName(response.getFileName());
|
||||
entity.setSource(DocumentSourceTypeEnum.APPLICATION.getValue());
|
||||
entity.setType(documentTypeEnum.getValue());
|
||||
entity.setSourceId(applicationId);
|
||||
|
||||
@@ -19,4 +19,6 @@ AmazonS3Service {
|
||||
|
||||
UploadFileOnAmazonS3Response moveFile(String fileName, String oldPath, String newPath);
|
||||
|
||||
UploadFileOnAmazonS3Response copyFile(String fileName, String oldS3Path, String newS3Path);
|
||||
|
||||
}
|
||||
@@ -20,7 +20,7 @@ public interface CompanyDocumentService {
|
||||
|
||||
void deleteCompanyFile(HttpServletRequest request,Long companyDocumentId);
|
||||
|
||||
DocumentResponseBean validateAndDuplicateCompanyDocument(HttpServletRequest request, Long companyDocumentId, Long applicationId, DocumentTypeEnum typeEnum);
|
||||
DocumentResponseBean createDuplicateCompanyDocument(HttpServletRequest request, Long companyDocumentId, Long applicationId, DocumentTypeEnum typeEnum);
|
||||
|
||||
List<CompanyDocumentResponseBean> getAllCompanyDocument(HttpServletRequest request ,Long companyId , CompanyDocumentTypeEnum typeEnum);
|
||||
|
||||
|
||||
@@ -160,20 +160,14 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
|
||||
@Override
|
||||
public UploadFileOnAmazonS3Response moveFile(String fileName, String oldPath, String newPath) {
|
||||
try {
|
||||
log.info("Original Paths - oldPath: {}, newPath: {}", oldPath, newPath);
|
||||
|
||||
oldPath = decodeS3Key(cleanOldPath(oldPath));
|
||||
newPath = cleanNewPath(oldPath, newPath);
|
||||
log.info("Moving file from {} to {} in bucket {}", oldPath, newPath, bucketName);
|
||||
|
||||
CopyObjectRequest copyRequest = new CopyObjectRequest(bucketName, oldPath, bucketName, newPath);
|
||||
s3Client.copyObject(copyRequest);
|
||||
log.info("File copied successfully from {} to {}", oldPath, newPath);
|
||||
UploadFileOnAmazonS3Response response = copyFile(fileName, oldPath, newPath);
|
||||
|
||||
s3Client.deleteObject(bucketName, oldPath);
|
||||
s3Client.deleteObject(bucketName, decodeS3Key(cleanOldPath(oldPath)));
|
||||
log.info("Original file deleted successfully: {}", oldPath);
|
||||
String filePath = s3Url + newPath;
|
||||
return UploadFileOnAmazonS3Response.builder().fileName(fileName).filePath(filePath).build();
|
||||
|
||||
return response;
|
||||
} catch (AmazonServiceException e) {
|
||||
log.error("AWS service error while moving file: {}", e.getErrorMessage(), e);
|
||||
throw e;
|
||||
@@ -193,4 +187,34 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
|
||||
public String cleanOldPath(String oldPath) {
|
||||
return oldPath.replace(s3Url, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadFileOnAmazonS3Response copyFile(String fileName, String oldS3Path, String newS3Path){
|
||||
try {
|
||||
log.info("Copying file from {} to {} in bucket {}", oldS3Path, newS3Path, bucketName);
|
||||
|
||||
oldS3Path = decodeS3Key(cleanOldPath(oldS3Path));
|
||||
newS3Path = cleanNewPath(oldS3Path, newS3Path);
|
||||
log.info("Copying file from {} to {} in bucket {}", oldS3Path, newS3Path, bucketName);
|
||||
|
||||
CopyObjectRequest copyRequest = new CopyObjectRequest(bucketName, oldS3Path, bucketName, newS3Path);
|
||||
s3Client.copyObject(copyRequest);
|
||||
log.info("File copied successfully from {} to {}", oldS3Path, newS3Path);
|
||||
|
||||
URL amazonS3Url = amazonS3.getUrl(bucketName, newS3Path);
|
||||
String fileUrl = amazonS3Url.toString();
|
||||
return UploadFileOnAmazonS3Response.builder().fileName(fileName).filePath(fileUrl).build();
|
||||
}
|
||||
catch (AmazonServiceException e) {
|
||||
log.error("AWS service error while copying file: {}", e.getErrorMessage(), e);
|
||||
throw e;
|
||||
} catch (SdkClientException e) {
|
||||
log.error("SDK client error while copying file: {}", e.getMessage(), e);
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
log.error("Unexpected error while copying file: {}", e.getMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -53,9 +53,9 @@ public class CompanyDocumentServiceImpl implements CompanyDocumentService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DocumentResponseBean validateAndDuplicateCompanyDocument(HttpServletRequest request, Long companyDocumentId, Long applicationId, DocumentTypeEnum typeEnum) {
|
||||
public DocumentResponseBean createDuplicateCompanyDocument(HttpServletRequest request, Long companyDocumentId, Long applicationId, DocumentTypeEnum typeEnum) {
|
||||
UserEntity user = validator.validateUser(request);
|
||||
return companyDocumentDao.validateAndDuplicateCompanyDocument(request, user.getId(), companyDocumentId,applicationId,typeEnum);
|
||||
return companyDocumentDao.createDuplicateCompanyDocument(request, user.getId(), companyDocumentId,applicationId,typeEnum);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -102,7 +102,7 @@ public interface CompanyDocumentApi {
|
||||
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
||||
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) }))})
|
||||
@PutMapping(value = "/{id}/document/upload", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
default ResponseEntity<Response<DocumentResponseBean>> validateAndDuplicateCompanyDocument (HttpServletRequest httpServletRequest, @Parameter(description = "Company Document Id", required = true) @PathVariable("id") Long companyDocumentId,
|
||||
default ResponseEntity<Response<DocumentResponseBean>> createDuplicateCompanyDocument (HttpServletRequest httpServletRequest, @Parameter(description = "Company Document Id", required = true) @PathVariable("id") Long companyDocumentId,
|
||||
@Parameter(description = "Application Id", required = true) @RequestParam Long applicationId,
|
||||
@RequestParam("documentType") DocumentTypeEnum documentTypeEnum) {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
|
||||
|
||||
@@ -91,12 +91,12 @@ public class CompanyDocumentApiControlller implements CompanyDocumentApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Response<DocumentResponseBean>> validateAndDuplicateCompanyDocument(HttpServletRequest httpServletRequest, Long companyDocumentId , Long applicationId,DocumentTypeEnum typeEnum) {
|
||||
public ResponseEntity<Response<DocumentResponseBean>> createDuplicateCompanyDocument(HttpServletRequest httpServletRequest, Long companyDocumentId , Long applicationId,DocumentTypeEnum typeEnum) {
|
||||
|
||||
/** This code is responsible for creating user action logs for the "duplicate company document" operation. **/
|
||||
loggingUtil.logUserAction(UserActionRequest.builder().request(httpServletRequest).actionType(UserActionLogsEnum.UPDATE).actionContext(UserActionContextEnum.DUPLICATE_COMPANY_DOCUMENT).build());
|
||||
|
||||
DocumentResponseBean responseBeans = companyDocumentService.validateAndDuplicateCompanyDocument(httpServletRequest, companyDocumentId,applicationId,typeEnum);
|
||||
DocumentResponseBean responseBeans = companyDocumentService.createDuplicateCompanyDocument(httpServletRequest, companyDocumentId,applicationId,typeEnum);
|
||||
return ResponseEntity.status(HttpStatus.CREATED)
|
||||
.body(new Response<DocumentResponseBean>(responseBeans, Status.SUCCESS, Translator.toLocale(GepafinConstant.COMPANY_DOCUMENT_COPIED_SUCCESSFULLY)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user