Added folder structured documents in download Zip API.
This commit is contained in:
@@ -172,6 +172,12 @@ public class ApplicationDao {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private RoleRepository roleRepository;
|
private RoleRepository roleRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ApplicationAmendmentRequestRepository applicationAmendmentRequestRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ApplicationEvaluationRepository applicationEvaluationRepository;
|
||||||
|
|
||||||
public ApplicationResponseBean createApplication(HttpServletRequest request, ApplicationRequestBean applicationRequestBean, Long formId, Long applicationId) {
|
public ApplicationResponseBean createApplication(HttpServletRequest request, ApplicationRequestBean applicationRequestBean, Long formId, Long applicationId) {
|
||||||
FormEntity formEntity = formService.validateForm(formId);
|
FormEntity formEntity = formService.validateForm(formId);
|
||||||
// callService.validatePublishedCall(formEntity.getCall().getId());
|
// callService.validatePublishedCall(formEntity.getCall().getId());
|
||||||
@@ -1225,90 +1231,121 @@ public class ApplicationDao {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public byte[] downloadApplicationDocumentsAsZip(HttpServletRequest request, Long applicationId) {
|
public byte[] downloadApplicationDocumentsAsZip(HttpServletRequest request, Long applicationId) {
|
||||||
|
|
||||||
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
||||||
validateAssignedUser(request, applicationId);
|
validateAssignedUser(request, applicationId);
|
||||||
|
|
||||||
Set<Long> documentIds = extractDocumentIdsFromApplicationForms(applicationId);
|
Set<Long> documentIds = extractDocumentIdsFromApplicationForms(applicationId);
|
||||||
List<DocumentEntity> documents = documentRepository.findAllByIdInAndIsDeletedFalse(documentIds);
|
List<DocumentEntity> documents = documentRepository.findAllByIdInAndIsDeletedFalse(documentIds);
|
||||||
|
ApplicationSignedDocumentEntity signedDocument = applicationSignedDocumentRepository.findByApplicationIdAndStatus(applicationId,
|
||||||
ApplicationSignedDocumentEntity signedDocument = applicationSignedDocumentRepository
|
ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
||||||
.findByApplicationIdAndStatus(applicationId, ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
List<DocumentEntity> amendmentDocuments = fetchAmendmentDocuments(applicationId);
|
||||||
if (documents.isEmpty() && signedDocument == null) {
|
List<DocumentEntity> evaluationDocuments = fetchEvaluationDocuments(applicationId);
|
||||||
|
if (documents.isEmpty() && signedDocument == null && amendmentDocuments.isEmpty() && evaluationDocuments.isEmpty()) {
|
||||||
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
|
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) {
|
private void validateAssignedUser(HttpServletRequest request, Long applicationId) {
|
||||||
AssignedApplicationsEntity assignedApplications = assignedApplicationsRepository
|
|
||||||
.findByApplicationIdAndIsDeletedFalse(applicationId).orElse(null);
|
AssignedApplicationsEntity assignedApplications = assignedApplicationsRepository.findByApplicationIdAndIsDeletedFalse(applicationId).orElse(null);
|
||||||
if (assignedApplications != null) {
|
if (assignedApplications != null) {
|
||||||
validator.validatePreInstructor(request, assignedApplications.getUserId());
|
validator.validatePreInstructor(request, assignedApplications.getUserId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<Long> extractDocumentIdsFromApplicationForms(Long applicationId) {
|
private Set<Long> extractDocumentIdsFromApplicationForms(Long applicationId) {
|
||||||
|
|
||||||
Set<Long> documentIds = new HashSet<>();
|
Set<Long> documentIds = new HashSet<>();
|
||||||
List<ApplicationFormEntity> applicationForms = applicationFormRepository.findByApplicationId(applicationId);
|
List<ApplicationFormEntity> applicationForms = applicationFormRepository.findByApplicationId(applicationId);
|
||||||
|
|
||||||
applicationForms.forEach(applicationForm -> {
|
applicationForms.forEach(applicationForm -> {
|
||||||
FormEntity formEntity = applicationForm.getForm();
|
FormEntity formEntity = applicationForm.getForm();
|
||||||
if (formEntity != null) {
|
if (formEntity != null) {
|
||||||
List<ContentResponseBean> contentResponseBeans = formDao.convertFormEntityToFormResponseBean(formEntity).getContent();
|
List<ContentResponseBean> contentResponseBeans = formDao.convertFormEntityToFormResponseBean(formEntity).getContent();
|
||||||
contentResponseBeans.stream()
|
contentResponseBeans.stream().filter(content -> "fileupload".equals(content.getName())).forEach(content -> {
|
||||||
.filter(content -> "fileupload".equals(content.getName()))
|
Optional<ApplicationFormFieldEntity> formField = applicationFormFieldRepository.findByFieldIdAndApplicationFormIdAndApplicationFormApplicationId(
|
||||||
.forEach(content -> {
|
content.getId(), applicationForm.getId(), applicationId);
|
||||||
Optional<ApplicationFormFieldEntity> formField = applicationFormFieldRepository
|
formField.ifPresent(field -> {
|
||||||
.findByFieldIdAndApplicationFormIdAndApplicationFormApplicationId(
|
if (field.getFieldValue() != null) {
|
||||||
content.getId(), applicationForm.getId(), applicationId);
|
Arrays.stream(field.getFieldValue().split(",")).map(String::trim).map(Long::valueOf).forEach(documentIds::add);
|
||||||
formField.ifPresent(field -> {
|
}
|
||||||
if (field.getFieldValue() != null) {
|
});
|
||||||
Arrays.stream(field.getFieldValue().split(","))
|
});
|
||||||
.map(String::trim)
|
|
||||||
.map(Long::valueOf)
|
|
||||||
.forEach(documentIds::add);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return documentIds;
|
return documentIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addDocumentToZip(ZipOutputStream zos, String s3Folder, String filePath, String fileName) {
|
private List<DocumentEntity> fetchAmendmentDocuments(Long applicationId) {
|
||||||
|
|
||||||
|
List<ApplicationAmendmentRequestEntity> amendmentRequests = applicationAmendmentRequestRepository.findByApplicationIdAndIsDeletedFalse(applicationId);
|
||||||
|
Set<Long> amendmentIds = amendmentRequests.stream().map(ApplicationAmendmentRequestEntity::getId).collect(Collectors.toSet());
|
||||||
|
return documentRepository.findBySourceIdInAndSourceAndIsDeletedFalse(amendmentIds, DocumentSourceTypeEnum.AMENDMENT.getValue());
|
||||||
|
}
|
||||||
|
private List<DocumentEntity> fetchEvaluationDocuments(Long applicationId) {
|
||||||
|
|
||||||
|
Optional<ApplicationEvaluationEntity> 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)) {
|
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, filePath)) {
|
||||||
zos.putNextEntry(new ZipEntry(fileName));
|
zos.putNextEntry(new ZipEntry(fullPath));
|
||||||
IOUtils.copy(fileInputStream, zos);
|
IOUtils.copy(fileInputStream, zos);
|
||||||
zos.closeEntry();
|
zos.closeEntry();
|
||||||
} catch (IOException e) {
|
} 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<DocumentEntity> documents, ApplicationSignedDocumentEntity signedDocument,
|
||||||
|
List<DocumentEntity> amendmentDocuments, List<DocumentEntity> evaluationDocuments, Long applicationId) {
|
||||||
|
|
||||||
private byte[] createZipWithDocuments(ApplicationEntity applicationEntity, List<DocumentEntity> documents,
|
try (ByteArrayOutputStream zipOutputStream = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(zipOutputStream)) {
|
||||||
ApplicationSignedDocumentEntity signedDocument, Long applicationId) {
|
Long callId = applicationEntity.getCall().getId();
|
||||||
try (ByteArrayOutputStream zipOutputStream = new ByteArrayOutputStream();
|
// Add Application Documents
|
||||||
ZipOutputStream zos = new ZipOutputStream(zipOutputStream)) {
|
String appS3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION, callId, applicationId, 0L);
|
||||||
|
|
||||||
String s3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION, applicationEntity.getCall().getId(), applicationId,0L);
|
|
||||||
|
|
||||||
for (DocumentEntity document : documents) {
|
for (DocumentEntity document : documents) {
|
||||||
String fileName = Utils.extractFileName(document.getFilePath());
|
String fileName = Utils.extractFileName(document.getFilePath());
|
||||||
addDocumentToZip(zos, s3Folder, document.getFilePath(), fileName);
|
addDocumentToZip(zos, appS3Folder, document.getFilePath(), fileName);
|
||||||
}
|
}
|
||||||
|
// Add Signed Document
|
||||||
if (signedDocument != null) {
|
if (signedDocument != null) {
|
||||||
String signedDocS3Folder = s3PathConfig.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, applicationEntity.getCall().getId(), applicationId,0L);
|
String signedFolder = "SIGNED_DOCUMENT/";
|
||||||
String signedDocFileName = signedDocument.getFileName();
|
String signedDocS3Folder = s3PathConfig.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, callId, applicationId, 0L);
|
||||||
addDocumentToZip(zos, signedDocS3Folder, signedDocument.getFilePath(), signedDocFileName);
|
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();
|
zos.finish();
|
||||||
return zipOutputStream.toByteArray();
|
return zipOutputStream.toByteArray();
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException("Error while creating ZIP file", e);
|
throw new RuntimeException("Error while creating ZIP file", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,5 +33,7 @@ public interface DocumentRepository extends JpaRepository<DocumentEntity, Long>
|
|||||||
List<DocumentEntity> findAllByIsDeleteTrue();
|
List<DocumentEntity> findAllByIsDeleteTrue();
|
||||||
|
|
||||||
List<DocumentEntity> findAllByIdInAndIsDeletedFalse(Set<Long> documentIds);
|
List<DocumentEntity> findAllByIdInAndIsDeletedFalse(Set<Long> documentIds);
|
||||||
|
|
||||||
|
List<DocumentEntity> findBySourceIdInAndSourceAndIsDeletedFalse(Set<Long> sourceId, String type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user