From 1b9359971acac1e21d4df8a5489a804948727eeb Mon Sep 17 00:00:00 2001 From: rajesh Date: Wed, 28 Aug 2024 12:46:41 +0530 Subject: [PATCH] updated code --- .../tendermanagement/dao/DocumentDao.java | 175 +++++++++--------- .../web/rest/api/LookUpDataApi.java | 10 +- 2 files changed, 96 insertions(+), 89 deletions(-) diff --git a/src/main/java/net/gepafin/tendermanagement/dao/DocumentDao.java b/src/main/java/net/gepafin/tendermanagement/dao/DocumentDao.java index 9c7804ba..3a0b1b9b 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/DocumentDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/DocumentDao.java @@ -29,97 +29,104 @@ import java.util.List; @Component public class DocumentDao { - @Autowired - private AmazonS3Service amazonS3Service; + @Autowired + private AmazonS3Service amazonS3Service; - @Autowired - private DocumentRepository documentRepository; + @Autowired + private DocumentRepository documentRepository; - @Autowired - private CallDao callDao; + @Autowired + private CallDao callDao; - @Autowired - private CallRepository callRepository; + @Autowired + private CallRepository callRepository; - public List uploadFiles(List files,Long callId, DocumentTypeEnum fileType) { - List documentEntities = new ArrayList<>(); - CallEntity callEntity=callRepository.findById(callId).orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.CALL_NOT_FOUND))); - for (MultipartFile file : files) { - try { - uploadFileOnAmazonS3 result = uploadFileOnAmazonS3(file); - if(result!=null) { - DocumentEntity documentEntity = new DocumentEntity(); - documentEntity.setFileName(result.fileName()); - documentEntity.setCall(callEntity); - documentEntity.setType(fileType.getValue()); - documentEntity.setFilePath(result.filepath()); - documentEntity.setIsDeleted(false); - documentEntities.add(documentEntity); - } - } catch (IOException e) {} - } - documentRepository.saveAll(documentEntities); - return documentEntities.stream() - .map(callDao::convertToDocumentResponseBean) - .collect(Collectors.toList()); - } + public List uploadFiles(List files, Long callId, DocumentTypeEnum fileType) { + List documentEntities = new ArrayList<>(); + CallEntity callEntity = callRepository.findById(callId) + .orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, + Translator.toLocale(GepafinConstant.CALL_NOT_FOUND))); + for (MultipartFile file : files) { + try { + uploadFileOnAmazonS3 result = uploadFileOnAmazonS3(file); + if (result != null) { + DocumentEntity documentEntity = new DocumentEntity(); + documentEntity.setFileName(result.fileName()); + documentEntity.setCall(callEntity); + documentEntity.setType(fileType.getValue()); + documentEntity.setFilePath(result.filepath()); + documentEntity.setIsDeleted(false); + documentEntities.add(documentEntity); + } + } catch (IOException e) { + } + } + documentRepository.saveAll(documentEntities); + return documentEntities.stream().map(callDao::convertToDocumentResponseBean).collect(Collectors.toList()); + } - private uploadFileOnAmazonS3 uploadFileOnAmazonS3(MultipartFile file) throws IOException { - String extension = FilenameUtils.getExtension(file.getOriginalFilename()); - String fileName = StringUtils.cleanPath(file.getOriginalFilename()); - String firstNameContain = fileName.substring(0, fileName.lastIndexOf('.')); - fileName = (firstNameContain + "." + extension); - String filepath = amazonS3Service.upload(fileName, file); - uploadFileOnAmazonS3 result = new uploadFileOnAmazonS3(fileName, filepath); - return result; - } + private uploadFileOnAmazonS3 uploadFileOnAmazonS3(MultipartFile file) throws IOException { + String extension = FilenameUtils.getExtension(file.getOriginalFilename()); + String fileName = StringUtils.cleanPath(file.getOriginalFilename()); + String firstNameContain = fileName.substring(0, fileName.lastIndexOf('.')); + fileName = (firstNameContain + "." + extension); + String filepath = amazonS3Service.upload(fileName, file); + uploadFileOnAmazonS3 result = new uploadFileOnAmazonS3(fileName, filepath); + return result; + } - private record uploadFileOnAmazonS3(String fileName, String filepath) { - } + private record uploadFileOnAmazonS3(String fileName, String filepath) { + } - public void deleteFile(Long documentId){ - DocumentEntity documentEntity = getDocumentEntity(documentId); - String fileName= Utils.extractFileName(documentEntity.getFilePath()); - deleteFileOnAmazonS3(fileName); - documentRepository.delete(documentEntity); - } + public void deleteFile(Long documentId) { + DocumentEntity documentEntity = getDocumentEntity(documentId); +// String fileName= Utils.extractFileName(documentEntity.getFilePath()); +// deleteFileOnAmazonS3(fileName); + documentEntity.setIsDeleted(true); + documentRepository.save(documentEntity); + } - private DocumentEntity deleteFileOnAmazonS3(String fileName) { - try { - amazonS3Service.delete(fileName); - }catch (Exception e){} - return null; - } + private DocumentEntity deleteFileOnAmazonS3(String fileName) { + try { + amazonS3Service.delete(fileName); + } catch (Exception e) { + } + return null; + } - private DocumentEntity getDocumentEntity(Long documentId) { - Optional documentEntity= documentRepository.findById(documentId); - if(documentEntity.isEmpty()){ - throw new ResourceNotFoundException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND)); - } - return documentEntity.orElse(null); - } + private DocumentEntity getDocumentEntity(Long documentId) { + Optional documentEntity = documentRepository.findById(documentId); + if (documentEntity.isEmpty()) { + throw new ResourceNotFoundException(Status.VALIDATION_ERROR, + Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND)); + } + return documentEntity.orElse(null); + } - public DocumentResponseBean updateDocument(Long documentId, MultipartFile file, DocumentTypeEnum documentTypeEnum){ - DocumentEntity documentEntity = getDocumentEntity(documentId); - String fileName= Utils.extractFileName(documentEntity.getFilePath()); - deleteFileOnAmazonS3(fileName); - uploadFileOnAmazonS3 result= null; - try { - result = uploadFileOnAmazonS3(file); - } catch (IOException e) {} - if(result!=null){ - documentEntity.setFilePath(result.filepath); - documentEntity.setFileName(result.fileName); - documentEntity.setType(documentTypeEnum.getValue()); - documentRepository.save(documentEntity); - } - return callDao.convertToDocumentResponseBean(documentEntity); - } - public DocumentResponseBean getDocument(Long documentId) { - Optional documentEntity= documentRepository.findById(documentId); - if(documentEntity.isEmpty()){ - new ResourceNotFoundException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND)); - } - return callDao.convertToDocumentResponseBean(documentEntity.orElse(null)); - } - } + public DocumentResponseBean updateDocument(Long documentId, MultipartFile file, DocumentTypeEnum documentTypeEnum) { + DocumentEntity documentEntity = getDocumentEntity(documentId); + String fileName = Utils.extractFileName(documentEntity.getFilePath()); + deleteFileOnAmazonS3(fileName); + uploadFileOnAmazonS3 result = null; + try { + result = uploadFileOnAmazonS3(file); + } catch (IOException e) { + } + if (result != null) { + documentEntity.setFilePath(result.filepath); + documentEntity.setFileName(result.fileName); + documentEntity.setType(documentTypeEnum.getValue()); + documentRepository.save(documentEntity); + } + return callDao.convertToDocumentResponseBean(documentEntity); + } + + public DocumentResponseBean getDocument(Long documentId) { + Optional documentEntity = documentRepository.findById(documentId); + if (documentEntity.isEmpty()) { + new ResourceNotFoundException(Status.VALIDATION_ERROR, + Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND)); + } + return callDao.convertToDocumentResponseBean(documentEntity.orElse(null)); + } +} diff --git a/src/main/java/net/gepafin/tendermanagement/web/rest/api/LookUpDataApi.java b/src/main/java/net/gepafin/tendermanagement/web/rest/api/LookUpDataApi.java index b0670e4b..5497d9a4 100644 --- a/src/main/java/net/gepafin/tendermanagement/web/rest/api/LookUpDataApi.java +++ b/src/main/java/net/gepafin/tendermanagement/web/rest/api/LookUpDataApi.java @@ -19,7 +19,7 @@ import java.util.List; public interface LookUpDataApi { - @Operation(summary = "API to create LookUp Data", + @Operation(summary = "Api to create LookUp Data", responses = { @ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @@ -31,7 +31,7 @@ public interface LookUpDataApi { @PostMapping(value = "", consumes = "application/json", produces = "application/json") ResponseEntity> createLookUpData(HttpServletRequest request, @Valid @RequestBody LookUpDataRequest lookUpDataReq); - @Operation(summary = "API to get LookUp Data by id", + @Operation(summary = "Api to get LookUp Data by id", responses = { @ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @@ -43,7 +43,7 @@ public interface LookUpDataApi { @GetMapping(value = "/{id}", produces = "application/json") ResponseEntity> getLookUpDataById(HttpServletRequest request, @PathVariable Long id); - @Operation(summary = "API to update LookUp Data", + @Operation(summary = "Api to update LookUp Data", responses = { @ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @@ -55,7 +55,7 @@ public interface LookUpDataApi { @PutMapping(value = "/{id}", consumes = "application/json", produces = "application/json") ResponseEntity> updateLookUpData(HttpServletRequest request, @PathVariable Long id, @Valid @RequestBody LookUpDataRequest lookUpDataReq); - @Operation(summary = "API to delete LookUp Data", + @Operation(summary = "Api to delete LookUp Data", responses = { @ApiResponse(responseCode = "204", description = "No Content"), @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @@ -67,7 +67,7 @@ public interface LookUpDataApi { @DeleteMapping(value = "/{id}") ResponseEntity> deleteLookUpData(HttpServletRequest request, @PathVariable Long id); - @Operation(summary = "API to get LookUp Data by type", + @Operation(summary = "Api to get LookUp Data by type", responses = { @ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {