From 33fb2d70919d9620d7256b36eae03808d2799048 Mon Sep 17 00:00:00 2001 From: piyushkag Date: Mon, 30 Dec 2024 19:44:20 +0530 Subject: [PATCH] Added folder structured documents in download Zip API. --- .../tendermanagement/dao/ApplicationDao.java | 123 ++++++++++++------ .../repositories/DocumentRepository.java | 2 + 2 files changed, 82 insertions(+), 43 deletions(-) diff --git a/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java b/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java index 621fea19..0c56fb16 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java @@ -172,6 +172,12 @@ public class ApplicationDao { @Autowired private RoleRepository roleRepository; + @Autowired + private ApplicationAmendmentRequestRepository applicationAmendmentRequestRepository; + + @Autowired + private ApplicationEvaluationRepository applicationEvaluationRepository; + public ApplicationResponseBean createApplication(HttpServletRequest request, ApplicationRequestBean applicationRequestBean, Long formId, Long applicationId) { FormEntity formEntity = formService.validateForm(formId); // callService.validatePublishedCall(formEntity.getCall().getId()); @@ -1225,90 +1231,121 @@ public class ApplicationDao { } public byte[] downloadApplicationDocumentsAsZip(HttpServletRequest request, Long applicationId) { + ApplicationEntity applicationEntity = validateApplication(applicationId); validateAssignedUser(request, applicationId); - Set documentIds = extractDocumentIdsFromApplicationForms(applicationId); List documents = documentRepository.findAllByIdInAndIsDeletedFalse(documentIds); - - ApplicationSignedDocumentEntity signedDocument = applicationSignedDocumentRepository - .findByApplicationIdAndStatus(applicationId, ApplicationSignedDocumentStatusEnum.ACTIVE.getValue()); - if (documents.isEmpty() && signedDocument == null) { + ApplicationSignedDocumentEntity signedDocument = applicationSignedDocumentRepository.findByApplicationIdAndStatus(applicationId, + ApplicationSignedDocumentStatusEnum.ACTIVE.getValue()); + List amendmentDocuments = fetchAmendmentDocuments(applicationId); + List evaluationDocuments = fetchEvaluationDocuments(applicationId); + if (documents.isEmpty() && signedDocument == null && amendmentDocuments.isEmpty() && evaluationDocuments.isEmpty()) { throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND)); } - return createZipWithDocuments(applicationEntity, documents, signedDocument, applicationId); + return createZipWithDocuments(applicationEntity, documents, signedDocument, amendmentDocuments, evaluationDocuments, applicationId); } + private void validateAssignedUser(HttpServletRequest request, Long applicationId) { - AssignedApplicationsEntity assignedApplications = assignedApplicationsRepository - .findByApplicationIdAndIsDeletedFalse(applicationId).orElse(null); + + AssignedApplicationsEntity assignedApplications = assignedApplicationsRepository.findByApplicationIdAndIsDeletedFalse(applicationId).orElse(null); if (assignedApplications != null) { validator.validatePreInstructor(request, assignedApplications.getUserId()); } } private Set extractDocumentIdsFromApplicationForms(Long applicationId) { + Set documentIds = new HashSet<>(); List applicationForms = applicationFormRepository.findByApplicationId(applicationId); - applicationForms.forEach(applicationForm -> { FormEntity formEntity = applicationForm.getForm(); if (formEntity != null) { List contentResponseBeans = formDao.convertFormEntityToFormResponseBean(formEntity).getContent(); - contentResponseBeans.stream() - .filter(content -> "fileupload".equals(content.getName())) - .forEach(content -> { - Optional formField = applicationFormFieldRepository - .findByFieldIdAndApplicationFormIdAndApplicationFormApplicationId( - content.getId(), applicationForm.getId(), applicationId); - formField.ifPresent(field -> { - if (field.getFieldValue() != null) { - Arrays.stream(field.getFieldValue().split(",")) - .map(String::trim) - .map(Long::valueOf) - .forEach(documentIds::add); - } - }); - }); + contentResponseBeans.stream().filter(content -> "fileupload".equals(content.getName())).forEach(content -> { + Optional formField = applicationFormFieldRepository.findByFieldIdAndApplicationFormIdAndApplicationFormApplicationId( + content.getId(), applicationForm.getId(), applicationId); + formField.ifPresent(field -> { + if (field.getFieldValue() != null) { + Arrays.stream(field.getFieldValue().split(",")).map(String::trim).map(Long::valueOf).forEach(documentIds::add); + } + }); + }); } }); return documentIds; } - private void addDocumentToZip(ZipOutputStream zos, String s3Folder, String filePath, String fileName) { + private List fetchAmendmentDocuments(Long applicationId) { + + List amendmentRequests = applicationAmendmentRequestRepository.findByApplicationIdAndIsDeletedFalse(applicationId); + Set amendmentIds = amendmentRequests.stream().map(ApplicationAmendmentRequestEntity::getId).collect(Collectors.toSet()); + return documentRepository.findBySourceIdInAndSourceAndIsDeletedFalse(amendmentIds, DocumentSourceTypeEnum.AMENDMENT.getValue()); + } + private List fetchEvaluationDocuments(Long applicationId) { + + Optional evaluationEntity = applicationEvaluationRepository.findByApplicationIdAndIsDeletedFalse(applicationId); + if (evaluationEntity.isPresent()) { + Long evaluationId = evaluationEntity.get().getId(); + return documentRepository.findBySourceIdInAndSourceAndIsDeletedFalse(Collections.singleton(evaluationId), DocumentSourceTypeEnum.EVALUATION.getValue()); + } + return Collections.emptyList(); + } + private String fetchProtocolNumberForAmendment(Long amendmentRequestId) { + + ApplicationAmendmentRequestEntity amendmentRequest = applicationAmendmentRequestRepository.findByIdAndIsDeletedFalse(amendmentRequestId).orElse(null); + if (amendmentRequest != null && amendmentRequest.getProtocol() != null) { + return amendmentRequest.getProtocol().getProtocolNumber().toString(); + } + return "unknown"; + } + private void addDocumentToZip(ZipOutputStream zos, String s3Folder, String filePath, String fullPath) { + try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, filePath)) { - zos.putNextEntry(new ZipEntry(fileName)); + zos.putNextEntry(new ZipEntry(fullPath)); IOUtils.copy(fileInputStream, zos); zos.closeEntry(); } catch (IOException e) { - throw new RuntimeException("Error downloading or adding document to ZIP: " + fileName, e); + throw new RuntimeException("Error downloading or adding document to ZIP: " + fullPath, e); } } + private byte[] createZipWithDocuments(ApplicationEntity applicationEntity, List documents, ApplicationSignedDocumentEntity signedDocument, + List amendmentDocuments, List evaluationDocuments, Long applicationId) { - private byte[] createZipWithDocuments(ApplicationEntity applicationEntity, List documents, - ApplicationSignedDocumentEntity signedDocument, Long applicationId) { - try (ByteArrayOutputStream zipOutputStream = new ByteArrayOutputStream(); - ZipOutputStream zos = new ZipOutputStream(zipOutputStream)) { - - String s3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION, applicationEntity.getCall().getId(), applicationId,0L); - + try (ByteArrayOutputStream zipOutputStream = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(zipOutputStream)) { + Long callId = applicationEntity.getCall().getId(); + // Add Application Documents + String appS3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION, callId, applicationId, 0L); for (DocumentEntity document : documents) { String fileName = Utils.extractFileName(document.getFilePath()); - addDocumentToZip(zos, s3Folder, document.getFilePath(), fileName); + addDocumentToZip(zos, appS3Folder, document.getFilePath(), fileName); } - + // Add Signed Document if (signedDocument != null) { - String signedDocS3Folder = s3PathConfig.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, applicationEntity.getCall().getId(), applicationId,0L); - String signedDocFileName = signedDocument.getFileName(); - addDocumentToZip(zos, signedDocS3Folder, signedDocument.getFilePath(), signedDocFileName); + String signedFolder = "SIGNED_DOCUMENT/"; + String signedDocS3Folder = s3PathConfig.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, callId, applicationId, 0L); + String fileName = signedDocument.getFileName(); + addDocumentToZip(zos, signedDocS3Folder, signedDocument.getFilePath(), signedFolder + fileName); + } + // Add Amendment (Soccorso) Documents + for (DocumentEntity amendmentDocument : amendmentDocuments) { + String protocolNumber = fetchProtocolNumberForAmendment(amendmentDocument.getSourceId()); + String amendmentFolder = "SOCCORSO_" + protocolNumber + "/"; + String amendmentS3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.AMENDMENT, callId, applicationId, amendmentDocument.getSourceId()); + String fileName = Utils.extractFileName(amendmentDocument.getFilePath()); + addDocumentToZip(zos, amendmentS3Folder, amendmentDocument.getFilePath(), amendmentFolder + fileName); + } + // Add Evaluation Documents + for (DocumentEntity evaluationDocument : evaluationDocuments) { + String evaluationFolder = "EVALUATION/"; + String evaluationS3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.EVALUATION, callId, applicationId, evaluationDocument.getSourceId()); + String fileName = Utils.extractFileName(evaluationDocument.getFilePath()); + addDocumentToZip(zos, evaluationS3Folder, evaluationDocument.getFilePath(), evaluationFolder + fileName); } - zos.finish(); return zipOutputStream.toByteArray(); - } catch (IOException e) { throw new RuntimeException("Error while creating ZIP file", e); } } - - } diff --git a/src/main/java/net/gepafin/tendermanagement/repositories/DocumentRepository.java b/src/main/java/net/gepafin/tendermanagement/repositories/DocumentRepository.java index 9e808114..0ca46f8e 100644 --- a/src/main/java/net/gepafin/tendermanagement/repositories/DocumentRepository.java +++ b/src/main/java/net/gepafin/tendermanagement/repositories/DocumentRepository.java @@ -33,5 +33,7 @@ public interface DocumentRepository extends JpaRepository List findAllByIsDeleteTrue(); List findAllByIdInAndIsDeletedFalse(Set documentIds); + + List findBySourceIdInAndSourceAndIsDeletedFalse(Set sourceId, String type); }