updated code

This commit is contained in:
rajesh
2025-03-12 18:51:40 +05:30
parent 6bdf208741
commit bf5d3d79b3
7 changed files with 56 additions and 31 deletions

View File

@@ -258,30 +258,29 @@ public class CompanyDocumentDao {
return userActionContext; 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); ApplicationEntity applicationEntity = applicationService.validateApplication(applicationId);
CompanyDocumentEntity companyDocumentEntity = validateCompanyDocument(companyDocumentId); CompanyDocumentEntity companyDocumentEntity = validateCompanyDocument(companyDocumentId);
validator.validateUserWithCompany(request,companyDocumentEntity.getCompanyId()); validator.validateUserWithCompany(request,companyDocumentEntity.getCompanyId());
String oldS3Path = companyDocumentEntity.getFilePath(); String companyDocumentPath = companyDocumentEntity.getFilePath();
String newS3Path = s3ConfigBean.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION,applicationEntity.getCall().getId(),applicationId,0L); 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)); UploadFileOnAmazonS3Response response;
newS3Path = amazonS3ServiceImpl.cleanNewPath(oldS3Path, newS3Path); try {
log.info("Moving file from {} to {} in bucket {}", oldS3Path, newS3Path, bucketName); 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(); DocumentEntity entity = new DocumentEntity();
entity.setFilePath(fileUrl); entity.setFilePath(response.getFilePath());
entity.setFileName(companyDocumentEntity.getFileName()); entity.setFileName(response.getFileName());
entity.setSource(DocumentSourceTypeEnum.APPLICATION.getValue()); entity.setSource(DocumentSourceTypeEnum.APPLICATION.getValue());
entity.setType(documentTypeEnum.getValue()); entity.setType(documentTypeEnum.getValue());
entity.setSourceId(applicationId); entity.setSourceId(applicationId);

View File

@@ -19,4 +19,6 @@ AmazonS3Service {
UploadFileOnAmazonS3Response moveFile(String fileName, String oldPath, String newPath); UploadFileOnAmazonS3Response moveFile(String fileName, String oldPath, String newPath);
UploadFileOnAmazonS3Response copyFile(String fileName, String oldS3Path, String newS3Path);
} }

View File

@@ -20,7 +20,7 @@ public interface CompanyDocumentService {
void deleteCompanyFile(HttpServletRequest request,Long companyDocumentId); 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); List<CompanyDocumentResponseBean> getAllCompanyDocument(HttpServletRequest request ,Long companyId , CompanyDocumentTypeEnum typeEnum);

View File

@@ -160,20 +160,14 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
@Override @Override
public UploadFileOnAmazonS3Response moveFile(String fileName, String oldPath, String newPath) { public UploadFileOnAmazonS3Response moveFile(String fileName, String oldPath, String newPath) {
try { 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); log.info("Moving file from {} to {} in bucket {}", oldPath, newPath, bucketName);
CopyObjectRequest copyRequest = new CopyObjectRequest(bucketName, oldPath, bucketName, newPath); UploadFileOnAmazonS3Response response = copyFile(fileName, oldPath, newPath);
s3Client.copyObject(copyRequest);
log.info("File copied successfully from {} to {}", oldPath, newPath);
s3Client.deleteObject(bucketName, oldPath); s3Client.deleteObject(bucketName, decodeS3Key(cleanOldPath(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(); return response;
} 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;
@@ -193,4 +187,34 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
public String cleanOldPath(String oldPath) { public String cleanOldPath(String oldPath) {
return oldPath.replace(s3Url, ""); 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;
}
}
} }

View File

@@ -53,9 +53,9 @@ public class CompanyDocumentServiceImpl implements CompanyDocumentService {
} }
@Override @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); UserEntity user = validator.validateUser(request);
return companyDocumentDao.validateAndDuplicateCompanyDocument(request, user.getId(), companyDocumentId,applicationId,typeEnum); return companyDocumentDao.createDuplicateCompanyDocument(request, user.getId(), companyDocumentId,applicationId,typeEnum);
} }
@Override @Override

View File

@@ -102,7 +102,7 @@ public interface CompanyDocumentApi {
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) }))}) @ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) }))})
@PutMapping(value = "/{id}/document/upload", produces = MediaType.APPLICATION_JSON_VALUE) @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, @Parameter(description = "Application Id", required = true) @RequestParam Long applicationId,
@RequestParam("documentType") DocumentTypeEnum documentTypeEnum) { @RequestParam("documentType") DocumentTypeEnum documentTypeEnum) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

View File

@@ -91,12 +91,12 @@ public class CompanyDocumentApiControlller implements CompanyDocumentApi {
} }
@Override @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. **/ /** 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()); 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) return ResponseEntity.status(HttpStatus.CREATED)
.body(new Response<DocumentResponseBean>(responseBeans, Status.SUCCESS, Translator.toLocale(GepafinConstant.COMPANY_DOCUMENT_COPIED_SUCCESSFULLY))); .body(new Response<DocumentResponseBean>(responseBeans, Status.SUCCESS, Translator.toLocale(GepafinConstant.COMPANY_DOCUMENT_COPIED_SUCCESSFULLY)));
} }