Done ticket GEPAFINBE-6143
This commit is contained in:
@@ -662,7 +662,7 @@ public class ApplicationAmendmentRequestDao {
|
||||
response.setApplicationFormFields(fileDetails);
|
||||
}
|
||||
|
||||
private List<DocumentResponseBean> getDocumentResponseBean(String documentId) {
|
||||
public List<DocumentResponseBean> getDocumentResponseBean(String documentId) {
|
||||
List<Long> documentIds = extractIds(documentId);
|
||||
List<DocumentResponseBean> documentResponseBeans = documentIds.stream()
|
||||
.map(id -> {
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import net.gepafin.tendermanagement.config.Translator;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.entities.ApplicationContractEntity;
|
||||
import net.gepafin.tendermanagement.entities.ApplicationEntity;
|
||||
import net.gepafin.tendermanagement.entities.ApplicationEvaluationEntity;
|
||||
import net.gepafin.tendermanagement.entities.UserEntity;
|
||||
import net.gepafin.tendermanagement.enums.*;
|
||||
import net.gepafin.tendermanagement.model.request.ApplicationContractRequest;
|
||||
import net.gepafin.tendermanagement.model.request.VersionHistoryRequest;
|
||||
import net.gepafin.tendermanagement.model.response.ApplicationContractResponse;
|
||||
import net.gepafin.tendermanagement.model.response.DocumentResponseBean;
|
||||
import net.gepafin.tendermanagement.repositories.ApplicationContractRepository;
|
||||
import net.gepafin.tendermanagement.repositories.ApplicationRepository;
|
||||
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
||||
import net.gepafin.tendermanagement.util.LoggingUtil;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class ApplicationContractDao {
|
||||
|
||||
@Autowired
|
||||
private ApplicationDao applicationDao;
|
||||
|
||||
@Autowired
|
||||
private DocumentDao documentDao;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContractRepository applicationContractRepository;
|
||||
|
||||
@Autowired
|
||||
private ApplicationRepository applicationRepository;
|
||||
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
@Autowired
|
||||
private LoggingUtil loggingUtil;
|
||||
|
||||
@Autowired
|
||||
private ApplicationAmendmentRequestDao applicationAmendmentRequestDao;
|
||||
|
||||
@Autowired
|
||||
private EmailNotificationDao emailNotificationDao;
|
||||
|
||||
@Autowired
|
||||
private ApplicationEvaluationDao applicationEvaluationDao;
|
||||
|
||||
@Autowired
|
||||
private NotificationDao notificationDao;
|
||||
|
||||
public ApplicationContractResponse createApplicationContract(Long applicationId, List<MultipartFile> contractDocuments, ApplicationContractRequest applicationContractRequest, UserEntity user) {
|
||||
ApplicationEntity applicationEntity = applicationDao.validateApplication(applicationId);
|
||||
|
||||
if (Boolean.FALSE.equals(applicationEntity.getStatus().equals(ApplicationStatusTypeEnum.APPROVED.getValue()))) {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.APPLICATION_NOT_APPROVED));
|
||||
}
|
||||
if (applicationContractRequest.getSubject() == null || applicationContractRequest.getText() == null) {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.SUBJECT_AND_BODY_REQUIRED));
|
||||
}
|
||||
ApplicationEntity oldApplicationData = Utils.getClonedEntityForData(applicationEntity);
|
||||
ApplicationContractEntity existingApplicationContractEntity = applicationContractRepository.findByApplicationIdAndIsDeletedFalse(applicationId);
|
||||
if (existingApplicationContractEntity != null) {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.APPLICATION_CONTRACT_ALREADY_EXIST));
|
||||
}
|
||||
ApplicationContractEntity applicationContractEntity = createApplicationContractEntity(applicationContractRequest, user, applicationEntity);
|
||||
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.INSERT).oldData(null).newData(applicationContractEntity).build());
|
||||
List<DocumentResponseBean> documentResponseBeans = setContractDocuments(contractDocuments, user, applicationContractEntity);
|
||||
applicationEntity.setStatus(ApplicationStatusTypeEnum.AWAITING_CONTRACT.getValue());
|
||||
applicationRepository.save(applicationEntity);
|
||||
loggingUtil.addVersionHistory(
|
||||
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplicationData).newData(applicationEntity).build());
|
||||
emailNotificationDao.sendEmailForApplicationContracted(applicationEntity, applicationContractEntity, user);
|
||||
return createApplicationContractResponse(applicationContractEntity, documentResponseBeans, null);
|
||||
}
|
||||
|
||||
private ApplicationContractResponse createApplicationContractResponse(ApplicationContractEntity applicationContractEntity, List<DocumentResponseBean> instructorDocuments, List<DocumentResponseBean> beneficiaryDocuments) {
|
||||
ApplicationContractResponse applicationContractResponse = new ApplicationContractResponse();
|
||||
applicationContractResponse.setId(applicationContractEntity.getId());
|
||||
applicationContractResponse.setText(applicationContractEntity.getText());
|
||||
applicationContractResponse.setSubject(applicationContractEntity.getSubject());
|
||||
applicationContractResponse.setInstructorId(applicationContractEntity.getInstructorId());
|
||||
applicationContractResponse.setStatus(ApplicationContractStatusEnum.valueOf(applicationContractEntity.getStatus()));
|
||||
applicationContractResponse.setInstructorDocuments(instructorDocuments);
|
||||
applicationContractResponse.setBeneficiaryDocuments(beneficiaryDocuments);
|
||||
applicationContractResponse.setCompletionDate(applicationContractEntity.getCompletionDate());
|
||||
applicationContractResponse.setBeneficiaryUserId(applicationContractEntity.getBeneficiaryUserId());
|
||||
return applicationContractResponse;
|
||||
}
|
||||
|
||||
private List<DocumentResponseBean> setContractDocuments(List<MultipartFile> contractDocuments, UserEntity user, ApplicationContractEntity applicationContractEntity) {
|
||||
List<DocumentResponseBean> documentResponseBeans = uploadContractDocument(user.getId(), contractDocuments, applicationContractEntity.getId());
|
||||
List<Long> contractDocumentIds = documentResponseBeans.stream()
|
||||
.map(DocumentResponseBean::getId)
|
||||
.collect(Collectors.toList());
|
||||
String contractDocumentId = contractDocumentIds.stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining(","));
|
||||
applicationContractEntity.setInstructorDocument(contractDocumentId);
|
||||
applicationContractRepository.save(applicationContractEntity);
|
||||
return documentResponseBeans;
|
||||
}
|
||||
|
||||
private ApplicationContractEntity createApplicationContractEntity(ApplicationContractRequest applicationContractRequest, UserEntity user, ApplicationEntity applicationEntity) {
|
||||
ApplicationContractEntity applicationContractEntity = new ApplicationContractEntity();
|
||||
applicationContractEntity.setSubject(applicationContractRequest.getSubject());
|
||||
applicationContractEntity.setText(applicationContractRequest.getText());
|
||||
applicationContractEntity.setApplicationId(applicationEntity.getId());
|
||||
applicationContractEntity.setInstructorId(user.getId());
|
||||
applicationContractEntity.setIsDeleted(Boolean.FALSE);
|
||||
applicationContractEntity.setApplicationId(applicationEntity.getId());
|
||||
applicationContractEntity.setStatus(ApplicationContractStatusEnum.DRAFT.getValue());
|
||||
applicationContractEntity.setBeneficiaryUserId(applicationEntity.getUserId());
|
||||
applicationContractRepository.save(applicationContractEntity);
|
||||
return applicationContractEntity;
|
||||
}
|
||||
|
||||
public List<DocumentResponseBean> uploadContractDocument(Long userId, List<MultipartFile> files, Long applicationContractId) {
|
||||
if (files != null) {
|
||||
return documentDao.uploadFiles(userId, files, applicationContractId, DocumentSourceTypeEnum.CONTRACT, DocumentTypeEnum.DOCUMENT);
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
public ApplicationContractResponse updateApplicationContract(Long applicationContractId, List<MultipartFile> beneficiaryContractDocuments, UserEntity user) {
|
||||
ApplicationContractEntity applicationContractEntity = validateApplicationContract(applicationContractId);
|
||||
ApplicationContractEntity oldApplicationContract = Utils.getClonedEntityForData(applicationContractEntity);
|
||||
applicationContractEntity.setCompletionDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now()));
|
||||
applicationContractEntity.setStatus(ApplicationContractStatusEnum.SIGNED.getValue());
|
||||
List<DocumentResponseBean> beneficiaryContractDocuments1 = setBeneficiaryContractDocuments(beneficiaryContractDocuments, user, applicationContractEntity);
|
||||
List<DocumentResponseBean> documentResponseBeans = applicationAmendmentRequestDao.getDocumentResponseBean(applicationContractEntity.getInstructorDocument());
|
||||
loggingUtil.addVersionHistory(
|
||||
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplicationContract).newData(applicationContractEntity).build());
|
||||
ApplicationEntity applicationEntity = applicationDao.validateApplication(applicationContractEntity.getApplicationId());
|
||||
ApplicationEntity oldApplicationData = Utils.getClonedEntityForData(applicationEntity);
|
||||
ApplicationEvaluationEntity applicationEvaluationEntity = applicationEvaluationDao.validateApplicationEvaluation(applicationEntity.getApplicationEvaluationId());
|
||||
applicationEntity.setStatus(ApplicationStatusTypeEnum.CONTRACT_SIGNED.getValue());
|
||||
applicationRepository.save(applicationEntity);
|
||||
loggingUtil.addVersionHistory(
|
||||
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplicationData).newData(applicationEntity).build());
|
||||
Map<String, String> placeHolders = new HashMap<>();
|
||||
placeHolders.put("{{call_name}}", applicationEntity.getCall().getName());
|
||||
String protocolNumber = applicationEntity.getProtocol().getExternalProtocolNumber();
|
||||
if (protocolNumber == null) {
|
||||
protocolNumber = String.valueOf(applicationEntity.getProtocol().getProtocolNumber());
|
||||
}
|
||||
placeHolders.put("{{protocol_number}}", protocolNumber);
|
||||
notificationDao.sendNotificationToInstructor(placeHolders, applicationEvaluationEntity, NotificationTypeEnum.CONTRACT_UPLOAD);
|
||||
|
||||
return createApplicationContractResponse(applicationContractEntity, documentResponseBeans, beneficiaryContractDocuments1);
|
||||
}
|
||||
|
||||
public ApplicationContractEntity validateApplicationContract(Long applicationContractId) {
|
||||
ApplicationContractEntity applicationContractEntity = applicationContractRepository.findByIdAndIsDeletedFalse(applicationContractId);
|
||||
if (applicationContractEntity == null) {
|
||||
throw new CustomValidationException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.APPLICATION_CONTRACT_NOT_FOUND));
|
||||
}
|
||||
return applicationContractEntity;
|
||||
}
|
||||
|
||||
private List<DocumentResponseBean> setBeneficiaryContractDocuments(List<MultipartFile> contractDocuments, UserEntity user, ApplicationContractEntity applicationContractEntity) {
|
||||
List<DocumentResponseBean> documentResponseBeans = uploadContractDocument(user.getId(), contractDocuments, applicationContractEntity.getId());
|
||||
List<Long> contractDocumentIds = documentResponseBeans.stream()
|
||||
.map(DocumentResponseBean::getId)
|
||||
.collect(Collectors.toList());
|
||||
String contractDocumentId = contractDocumentIds.stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining(","));
|
||||
applicationContractEntity.setBeneficiaryDocument(contractDocumentId);
|
||||
applicationContractRepository.save(applicationContractEntity);
|
||||
return documentResponseBeans;
|
||||
}
|
||||
|
||||
public ApplicationContractResponse getContractById(Long contractId) {
|
||||
ApplicationContractEntity applicationContractEntity = validateApplicationContract(contractId);
|
||||
return createApplicationContractResponseFromEntity(applicationContractEntity);
|
||||
}
|
||||
|
||||
private ApplicationContractResponse createApplicationContractResponseFromEntity(ApplicationContractEntity applicationContractEntity) {
|
||||
List<DocumentResponseBean> instructorDocuments = applicationAmendmentRequestDao.getDocumentResponseBean(applicationContractEntity.getInstructorDocument());
|
||||
List<DocumentResponseBean> beneficiaryDocuments = applicationAmendmentRequestDao.getDocumentResponseBean(applicationContractEntity.getBeneficiaryDocument());
|
||||
return createApplicationContractResponse(applicationContractEntity, instructorDocuments, beneficiaryDocuments);
|
||||
}
|
||||
|
||||
public ApplicationContractResponse getContractByApplicationId(Long applicationId) {
|
||||
ApplicationContractEntity applicationContractEntity = applicationContractRepository.findByApplicationIdAndIsDeletedFalse(applicationId);
|
||||
if (applicationContractEntity == null) {
|
||||
return null;
|
||||
}
|
||||
return createApplicationContractResponseFromEntity(applicationContractEntity);
|
||||
}
|
||||
|
||||
public List<ApplicationContractResponse> getContractByBeneficiaryUserId(UserEntity user) {
|
||||
|
||||
List<ApplicationContractEntity> applicationContractEntities = applicationContractRepository.findByBeneficiaryUserIdAndStatusAndIsDeletedFalse(user.getId(), ApplicationContractStatusEnum.DRAFT.getValue());
|
||||
if (applicationContractEntities.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
List<ApplicationContractResponse> applicationContractResponses = new ArrayList<>();
|
||||
for (ApplicationContractEntity applicationContractEntity : applicationContractEntities) {
|
||||
ApplicationContractResponse applicationContractResponse = createApplicationContractResponseFromEntity(applicationContractEntity);
|
||||
applicationContractResponses.add(applicationContractResponse);
|
||||
}
|
||||
return applicationContractResponses;
|
||||
}
|
||||
}
|
||||
@@ -229,6 +229,9 @@ public class ApplicationDao {
|
||||
@Autowired
|
||||
private SystemEmailTemplatesDao systemEmailTemplatesDao;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContractRepository applicationContractRepository;
|
||||
|
||||
public final Random random = new Random();
|
||||
|
||||
public ApplicationResponseBean createApplication(HttpServletRequest request, ApplicationRequestBean applicationRequestBean, Long formId, Long applicationId) {
|
||||
@@ -1383,7 +1386,7 @@ public class ApplicationDao {
|
||||
ApplicationSignedDocumentEntity oldApplicationSignedDocument = Utils.getClonedEntityForData(applicationSignedDocumentEntity);
|
||||
String oldS3Path = applicationSignedDocumentEntity.getFilePath();
|
||||
log.debug("Old S3 path: {} ", oldS3Path);
|
||||
String newS3Path = s3ConfigBean.generateDocumentPathForOther(DocOtherSourceTypeEnum.DELETED_USER_SIGNED_DOCUMENT,applicationSignedDocumentEntity.getApplication().getCall().getId(),applicationSignedDocumentEntity.getApplication().getId(),0L,0L);
|
||||
String newS3Path = s3ConfigBean.generateDocumentPathForOther(DocOtherSourceTypeEnum.DELETED_USER_SIGNED_DOCUMENT,applicationSignedDocumentEntity.getApplication().getCall().getId(),applicationSignedDocumentEntity.getApplication().getId(),0L,0L,0l);
|
||||
log.debug("Generated new S3 path for deleted document: {}", newS3Path);
|
||||
|
||||
UploadFileOnAmazonS3Response response = amazonS3Service.moveFile(applicationSignedDocumentEntity.getFileName(), oldS3Path, newS3Path);
|
||||
@@ -1418,7 +1421,7 @@ public class ApplicationDao {
|
||||
}
|
||||
private String generateS3PathForDelegation(Long callId, Long applicationId) {
|
||||
try {
|
||||
return s3ConfigBean.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, callId, applicationId,0L,0L);
|
||||
return s3ConfigBean.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, callId, applicationId,0L,0L,0L);
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.error("Failed to generate S3 path for delegation | callId: {}, applicationId: {}, error: {}", callId, applicationId, e.getMessage(), e);
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.S3_PATH_GENERATION_ERROR_MSG));
|
||||
@@ -1541,11 +1544,12 @@ public class ApplicationDao {
|
||||
List<DocumentEntity> amendmentDocuments = fetchAmendmentDocuments(applicationId);
|
||||
List<DocumentEntity> evaluationDocuments = fetchEvaluationDocuments(applicationId);
|
||||
List<DocumentEntity> communicationnDocuments = fetchCommunicationDocuments(applicationId);
|
||||
List<DocumentEntity> contractDocuments=fetchContractDocuments(applicationId);
|
||||
if (documents.isEmpty() && signedDocument == null && amendmentDocuments.isEmpty() && evaluationDocuments.isEmpty()) {
|
||||
log.warn("No documents found for applicationId: {}", applicationId);
|
||||
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
|
||||
}
|
||||
return createZipWithDocuments(applicationEntity, documents, signedDocument, amendmentDocuments, evaluationDocuments, applicationId,communicationnDocuments);
|
||||
return createZipWithDocuments(applicationEntity, documents, signedDocument, amendmentDocuments, evaluationDocuments, applicationId,communicationnDocuments,contractDocuments);
|
||||
}
|
||||
|
||||
private void validateAssignedUser(HttpServletRequest request, Long applicationId) {
|
||||
@@ -1637,12 +1641,12 @@ public class ApplicationDao {
|
||||
}
|
||||
}
|
||||
private byte[] createZipWithDocuments(ApplicationEntity applicationEntity, List<DocumentEntity> documents, ApplicationSignedDocumentEntity signedDocument,
|
||||
List<DocumentEntity> amendmentDocuments, List<DocumentEntity> evaluationDocuments, Long applicationId,List<DocumentEntity> communicationDocuments) {
|
||||
List<DocumentEntity> amendmentDocuments, List<DocumentEntity> evaluationDocuments, Long applicationId,List<DocumentEntity> communicationDocuments,List<DocumentEntity> contractDocuments) {
|
||||
|
||||
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,0L);
|
||||
String appS3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION, callId, applicationId, 0L,0L,0L);
|
||||
for (DocumentEntity document : documents) {
|
||||
String fileName = Utils.extractFileName(document.getFilePath());
|
||||
addDocumentToZip(zos, appS3Folder, document.getFilePath(), fileName);
|
||||
@@ -1650,7 +1654,7 @@ public class ApplicationDao {
|
||||
// Add Signed Document
|
||||
if (signedDocument != null) {
|
||||
String signedFolder = "SIGNED_DOCUMENT/";
|
||||
String signedDocS3Folder = s3PathConfig.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, callId, applicationId, 0L,0L);
|
||||
String signedDocS3Folder = s3PathConfig.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, callId, applicationId, 0L,0L,0L);
|
||||
String fileName = signedDocument.getFileName();
|
||||
addDocumentToZip(zos, signedDocS3Folder, signedDocument.getFilePath(), signedFolder + fileName);
|
||||
}
|
||||
@@ -1658,23 +1662,30 @@ public class ApplicationDao {
|
||||
for (DocumentEntity amendmentDocument : amendmentDocuments) {
|
||||
String protocolNumber = fetchProtocolNumberForAmendment(amendmentDocument.getSourceId());
|
||||
String amendmentFolder = "SOCCORSO_" + protocolNumber + "/";
|
||||
String amendmentS3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.AMENDMENT, callId, applicationId, amendmentDocument.getSourceId(),0L);
|
||||
String amendmentS3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.AMENDMENT, callId, applicationId, amendmentDocument.getSourceId(),0L,0L);
|
||||
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(),0L);
|
||||
String evaluationS3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.EVALUATION, callId, applicationId, evaluationDocument.getSourceId(),0L,0L);
|
||||
String fileName = Utils.extractFileName(evaluationDocument.getFilePath());
|
||||
addDocumentToZip(zos, evaluationS3Folder, evaluationDocument.getFilePath(), evaluationFolder + fileName);
|
||||
}
|
||||
for (DocumentEntity communicationDocument : communicationDocuments) {
|
||||
Optional<CommunicationEntity> communicationEntity=communicationRepository.findByIdAndIsDeletedFalse(communicationDocument.getSourceId());
|
||||
String evaluationFolder = "COMMUNICATION/";
|
||||
String evaluationS3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.COMMUNICATION, callId, applicationId, communicationEntity.get().getApplicationAmendmentRequest().getId(),communicationDocument.getSourceId());
|
||||
String communicationFolder = "COMMUNICATION/";
|
||||
String communicationS3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.COMMUNICATION, callId, applicationId, communicationEntity.get().getApplicationAmendmentRequest().getId(),communicationDocument.getSourceId(),0L);
|
||||
String fileName = Utils.extractFileName(communicationDocument.getFilePath());
|
||||
addDocumentToZip(zos, evaluationS3Folder, communicationDocument.getFilePath(), evaluationFolder + fileName);
|
||||
addDocumentToZip(zos, communicationS3Folder, communicationDocument.getFilePath(), communicationFolder + fileName);
|
||||
}
|
||||
for (DocumentEntity contractDocument : contractDocuments) {
|
||||
ApplicationContractEntity applicationContractEntity=applicationContractRepository.findByIdAndIsDeletedFalse(contractDocument.getSourceId());
|
||||
String contractFolder = "CONTRACT/";
|
||||
String contractS3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.CONTRACT, callId, applicationId, 0L,contractDocument.getSourceId(),applicationContractEntity.getApplicationId());
|
||||
String fileName = Utils.extractFileName(contractDocument.getFilePath());
|
||||
addDocumentToZip(zos, contractS3Folder, contractDocument.getFilePath(), contractFolder + fileName);
|
||||
}
|
||||
zos.finish();
|
||||
return zipOutputStream.toByteArray();
|
||||
@@ -2599,5 +2610,18 @@ public class ApplicationDao {
|
||||
|
||||
return out.toByteArray();
|
||||
}
|
||||
private List<DocumentEntity> fetchContractDocuments(Long applicationId) {
|
||||
log.info("Fetching contract documents for applicationId: {}", applicationId);
|
||||
ApplicationContractEntity applicationContractEntity=applicationContractRepository.findByApplicationIdAndIsDeletedFalse(applicationId);
|
||||
List<DocumentEntity> documentEntities=new ArrayList<>();
|
||||
if(applicationContractEntity!=null){
|
||||
Long contractId = applicationContractEntity.getId();
|
||||
log.debug("Found contract entity with id: {}", contractId);
|
||||
|
||||
List<DocumentEntity> communicationDocuments= documentRepository.findBySourceIdInAndSourceAndIsDeletedFalse(Collections.singleton(contractId), DocumentSourceTypeEnum.CONTRACT.getValue());
|
||||
documentEntities.addAll(communicationDocuments);
|
||||
return documentEntities;
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ public class CallDao {
|
||||
|
||||
for (DocumentEntity document : documents) {
|
||||
log.info("Adding document to ZIP: documentId={}, fileName={}", document.getId(), document.getFileName());
|
||||
String s3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.CALL, callId, 0L,0L,0L);
|
||||
String s3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.CALL, callId, 0L,0L,0L,0L);
|
||||
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, document.getFilePath())) {
|
||||
String fileName = Utils.extractFileName(document.getFilePath());
|
||||
ZipEntry zipEntry = new ZipEntry(fileName);
|
||||
|
||||
@@ -281,7 +281,7 @@ public class CompanyDocumentDao {
|
||||
validator.validateUserWithCompany(request,companyDocumentEntity.getCompanyId());
|
||||
|
||||
String companyDocumentPath = companyDocumentEntity.getFilePath();
|
||||
String documentPath = s3ConfigBean.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION,applicationEntity.getCall().getId(),applicationId,0L,0L);
|
||||
String documentPath = s3ConfigBean.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION,applicationEntity.getCall().getId(),applicationId,0L,0L,0L);
|
||||
|
||||
log.info("Original Paths - oldPath: {}, newPath: {}", companyDocumentPath, documentPath);
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ public class DelegationDao {
|
||||
|
||||
public ByteArrayOutputStream generateDocument(Map<String, String> placeholders, String templateName) {
|
||||
try {
|
||||
String s3Folder = s3ConfigBean.generateDocumentPathForOther(DocOtherSourceTypeEnum.TEMPLATE, 0L, 0L,0L,0L);
|
||||
String s3Folder = s3ConfigBean.generateDocumentPathForOther(DocOtherSourceTypeEnum.TEMPLATE, 0L, 0L,0L,0L,0L);
|
||||
InputStream templateStream = amazonS3Service.getFile(s3Folder ,templateName);
|
||||
XWPFDocument doc = loadTemplate(templateStream);
|
||||
replacePlaceholders(doc, placeholders);
|
||||
|
||||
@@ -93,6 +93,9 @@ public class DocumentDao {
|
||||
@Autowired
|
||||
private CommunicationRepository communicationRepository;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContractRepository applicationContractRepository;
|
||||
|
||||
// @Value("${aws.s3.url.folder}")
|
||||
// private String s3Folder;
|
||||
|
||||
@@ -172,6 +175,7 @@ public class DocumentDao {
|
||||
Long amendmentId = 0L;
|
||||
Long evaluationId = 0L;
|
||||
Long communicationId = 0L;
|
||||
Long contractId=0L;
|
||||
Long callId = sourceId;
|
||||
if (type == DocumentSourceTypeEnum.APPLICATION) {
|
||||
applicationId = sourceId;
|
||||
@@ -197,9 +201,16 @@ public class DocumentDao {
|
||||
applicationId=applicationAmendmentRequestEntity.get().getApplicationId();
|
||||
callId = applicationAmendmentRequestEntity.get().getApplicationEvaluationEntity().getAssignedApplicationsEntity().getApplication().getCall().getId();
|
||||
log.info("Processing document of type COMMUNICATION .Resolved evaluationId={}, applicationId={}, callId={}", evaluationId, applicationId, callId);
|
||||
}else if (type == DocumentSourceTypeEnum.CONTRACT) {
|
||||
contractId = sourceId;
|
||||
ApplicationContractEntity applicationContractEntity=applicationContractRepository.findByIdAndIsDeletedFalse(contractId);
|
||||
ApplicationEntity applicationEntity=applicationService.validateApplication(applicationContractEntity.getApplicationId());
|
||||
applicationId=applicationEntity.getId();
|
||||
callId = applicationEntity.getCall().getId();
|
||||
log.info("Processing document of type CONTRACT .Resolved evaluationId={}, applicationId={}, callId={}, contractId={}", evaluationId, applicationId, callId,contractId);
|
||||
}
|
||||
try {
|
||||
String s3Path = generateS3Path(type, callId, applicationId, amendmentId,communicationId);
|
||||
String s3Path = generateS3Path(type, callId, applicationId, amendmentId,communicationId,contractId);
|
||||
log.info("Generated S3 path {}", s3Path);
|
||||
return amazonS3Service.uploadFileOnAmazonS3(s3Path, file);
|
||||
} catch (Exception e) {
|
||||
@@ -210,9 +221,9 @@ public class DocumentDao {
|
||||
}
|
||||
|
||||
|
||||
public String generateS3Path(DocumentSourceTypeEnum typeOfDocument, Long callId, Long applicationId, Long amendmentId,Long communicationId) {
|
||||
public String generateS3Path(DocumentSourceTypeEnum typeOfDocument, Long callId, Long applicationId, Long amendmentId,Long communicationId,Long contractId) {
|
||||
try {
|
||||
return s3ConfigBean.generateDocumentPath(typeOfDocument, callId, applicationId, amendmentId,communicationId);
|
||||
return s3ConfigBean.generateDocumentPath(typeOfDocument, callId, applicationId, amendmentId,communicationId,contractId);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.S3_PATH_GENERATION_ERROR_MSG));
|
||||
}
|
||||
@@ -245,6 +256,7 @@ public class DocumentDao {
|
||||
Long amendmentId = null;
|
||||
Long evaluationId = null;
|
||||
Long communicationId=null;
|
||||
Long contractId=null;
|
||||
|
||||
if (DocumentSourceTypeEnum.CALL.getValue().equalsIgnoreCase(documentEntity.getSource())) {
|
||||
callId = documentEntity.getSourceId();
|
||||
@@ -340,9 +352,20 @@ public class DocumentDao {
|
||||
communicationEntity1.setDocuments(updatedValue);
|
||||
communicationRepository.save(communicationEntity1);
|
||||
}
|
||||
}
|
||||
else if(DocumentSourceTypeEnum.CONTRACT.getValue().equalsIgnoreCase(documentEntity.getSource())) {
|
||||
contractId = documentEntity.getSourceId();
|
||||
ApplicationContractEntity applicationContractEntity=applicationContractRepository.findByIdAndIsDeletedFalse(contractId);
|
||||
ApplicationEntity applicationEntity=applicationService.validateApplication(applicationContractEntity.getApplicationId());
|
||||
String beneficiaryDocument=applicationContractEntity.getBeneficiaryDocument();
|
||||
if(beneficiaryDocument!=null) {
|
||||
String updatedValue = removeDocumentIdFromFieldValue(beneficiaryDocument, documentId);
|
||||
applicationContractEntity.setBeneficiaryDocument(updatedValue);
|
||||
applicationContractRepository.save(applicationContractEntity);
|
||||
}
|
||||
|
||||
}
|
||||
deleteFileFromS3(documentEntity, callId, applicationId,amendmentId,communicationId);
|
||||
deleteFileFromS3(documentEntity, callId, applicationId,amendmentId,communicationId,contractId);
|
||||
log.info("Successfully deleted file from S3 for documentId={}", documentId);
|
||||
}
|
||||
|
||||
@@ -387,6 +410,7 @@ public class DocumentDao {
|
||||
Long amendmentId=null;
|
||||
Long evaluationId=null;
|
||||
Long communicationId=null;
|
||||
Long contractId=null;
|
||||
if (type.equals(DocumentSourceTypeEnum.APPLICATION)) {
|
||||
callId = applicationRepository.findCallIdById(id);
|
||||
applicationId = id;
|
||||
@@ -412,6 +436,13 @@ public class DocumentDao {
|
||||
applicationId=applicationAmendmentRequestEntity.get().getApplicationId();
|
||||
callId = applicationAmendmentRequestEntity.get().getApplicationEvaluationEntity().getAssignedApplicationsEntity().getApplication().getCall().getId();
|
||||
log.info("Processing document of type EVALUATION .Resolved evaluationId={}, applicationId={}, callId={}", evaluationId, applicationId, callId);
|
||||
}else if (type == DocumentSourceTypeEnum.CONTRACT) {
|
||||
contractId = id;
|
||||
ApplicationContractEntity applicationContractEntity=applicationContractRepository.findByIdAndIsDeletedFalse(contractId);
|
||||
ApplicationEntity applicationEntity=applicationService.validateApplication(applicationContractEntity.getApplicationId());
|
||||
applicationId=applicationContractEntity.getApplicationId();
|
||||
callId = applicationEntity.getCall().getId();
|
||||
log.info("Processing document of type CONTRACT .Resolved evaluationId={}, applicationId={}, callId={}, contractId={}", evaluationId, applicationId, callId,contractId);
|
||||
}
|
||||
|
||||
else {
|
||||
@@ -419,7 +450,7 @@ public class DocumentDao {
|
||||
applicationId = 0L;
|
||||
log.info("Processing document of type CALL . Resolved callId={}", callId);
|
||||
}
|
||||
String s3Path = generateS3Path(type, callId, applicationId,amendmentId,communicationId);
|
||||
String s3Path = generateS3Path(type, callId, applicationId,amendmentId,communicationId,contractId);
|
||||
log.info("Generated S3 path {}", s3Path);
|
||||
return amazonS3Service.uploadFileOnAmazonS3(s3Path, file);
|
||||
} catch (Exception e) {
|
||||
@@ -432,12 +463,12 @@ public class DocumentDao {
|
||||
return callDao.convertToDocumentResponseBean(documentEntity);
|
||||
}
|
||||
|
||||
public void deleteFileFromS3(DocumentEntity documentEntity, Long callId, Long applicationId,Long amendmentId,Long communicationId) {
|
||||
public void deleteFileFromS3(DocumentEntity documentEntity, Long callId, Long applicationId,Long amendmentId,Long communicationId,Long contractId) {
|
||||
try {
|
||||
|
||||
DocumentEntity oldDocumentEntity = Utils.getClonedEntityForData(documentEntity);
|
||||
String oldS3Path = documentEntity.getFilePath();
|
||||
String newS3Path = s3ConfigBean.generateDocumentPathForOther(DocOtherSourceTypeEnum.valueOf("DELETED_" + documentEntity.getSource().toUpperCase()), callId, applicationId,amendmentId,communicationId);
|
||||
String newS3Path = s3ConfigBean.generateDocumentPathForOther(DocOtherSourceTypeEnum.valueOf("DELETED_" + documentEntity.getSource().toUpperCase()), callId, applicationId,amendmentId,communicationId,contractId);
|
||||
log.info("Moving file to deleted path: oldS3Path={}, newS3Path={}", oldS3Path, newS3Path);
|
||||
UploadFileOnAmazonS3Response response = amazonS3Service.moveFile(documentEntity.getFileName(), oldS3Path, newS3Path);
|
||||
documentEntity.setFileName(response.getFileName());
|
||||
|
||||
@@ -97,6 +97,9 @@ public class EmailNotificationDao {
|
||||
@Autowired
|
||||
private ApplicationDao applicationDao;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContractDao applicationContractDao;
|
||||
|
||||
public void sendEmail(ApplicationEntity applicationEntity, SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum templateType, Map<String, String> bodyPlaceholders,
|
||||
List<String> additionalRecipients, Long amendmentId,String emailType) {
|
||||
|
||||
@@ -106,7 +109,7 @@ public class EmailNotificationDao {
|
||||
|
||||
EmailContentResponse emailContent = prepareEmailContent(applicationEntity, templateType, hubEntity, bodyPlaceholders,emailType);
|
||||
UserEntity userEntity = userService.validateUser(applicationEntity.getUserId());
|
||||
sendEmails(applicationEntity, userEntity, additionalRecipients,amendmentId,emailContent.getSystemEmailTemplateResponse(),emailContent.getSubject(),emailContent.getBody());
|
||||
sendEmails(applicationEntity, userEntity, additionalRecipients,amendmentId,emailContent.getSystemEmailTemplateResponse(),emailContent.getSubject(),emailContent.getBody(),null);
|
||||
}
|
||||
|
||||
public EmailContentResponse prepareEmailContent(
|
||||
@@ -131,7 +134,7 @@ public class EmailNotificationDao {
|
||||
return new EmailContentResponse(subject, body, systemEmailTemplateResponse);
|
||||
}
|
||||
|
||||
private void sendEmails(ApplicationEntity applicationEntity, UserEntity userEntity, List<String> additionalRecipients,Long amendmentId,SystemEmailTemplateResponse systemEmailTemplateResponse,String subject,String body) {
|
||||
private void sendEmails(ApplicationEntity applicationEntity, UserEntity userEntity, List<String> additionalRecipients,Long amendmentId,SystemEmailTemplateResponse systemEmailTemplateResponse,String subject,String body,Long contractId) {
|
||||
|
||||
Optional<ApplicationEvaluationEntity> applicationEvaluationEntity = applicationEvaluationRepository.findByApplicationIdAndIsDeletedFalse(applicationEntity.getId());
|
||||
CompanyEntity company = companyService.validateCompany(applicationEntity.getCompanyId());
|
||||
@@ -177,6 +180,14 @@ public class EmailNotificationDao {
|
||||
: new HashSet<>(documentIds);
|
||||
documentEntities=documentRepository.findAllByIdInAndIsDeletedFalse(setOfDocumentIds);
|
||||
}
|
||||
if(Boolean.TRUE.equals(userEntity.getHub().getUniqueUuid().equals(defaultHubUuid)) && Boolean.TRUE.equals(systemEmailTemplateResponse.getEmailScenario().equals(EmailScenarioTypeEnum.APPLICATION_CONTRACT_CREATED))) {
|
||||
ApplicationContractEntity applicationContractEntity=applicationContractDao.validateApplicationContract(contractId);
|
||||
List<Long> documentIds=applicationDao.validateDocumentIds(applicationContractEntity.getInstructorDocument());
|
||||
Set<Long> setOfDocumentIds = (documentIds == null)
|
||||
? Collections.emptySet()
|
||||
: new HashSet<>(documentIds);
|
||||
documentEntities=documentRepository.findAllByIdInAndIsDeletedFalse(setOfDocumentIds);
|
||||
}
|
||||
|
||||
urls = documentEntities.stream()
|
||||
.map(DocumentEntity::getFilePath) // or getUrl()
|
||||
@@ -225,7 +236,7 @@ public class EmailNotificationDao {
|
||||
}
|
||||
}
|
||||
|
||||
if (userEntity.getBeneficiary().getEmail() != null) {
|
||||
if (userEntity.getBeneficiary()!= null) {
|
||||
String beneficiaryEmail = null;
|
||||
RecipientTypeEnum recipientTypeEnum=RecipientTypeEnum.BENEFICIARY;
|
||||
if (Boolean.TRUE.equals(userEntity.getHub().getUniqueUuid().equals(defaultHubUuid))){
|
||||
@@ -514,4 +525,33 @@ public class EmailNotificationDao {
|
||||
sendEmail(applicationEntity, SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.SPECIAL_APPLICATION_AMENDMENT_REQUESTED, bodyPlaceholders, null,
|
||||
applicationAmendmentRequestEntity.getId(),null);
|
||||
}
|
||||
public void sendEmailForApplicationContracted(ApplicationEntity applicationEntity,ApplicationContractEntity applicationContractEntity,UserEntity user) {
|
||||
Map<String, String> bodyPlaceholders = new HashMap<>();
|
||||
bodyPlaceholders.put("{{call_name}}", applicationEntity.getCall().getName());
|
||||
String protocolNumber=applicationEntity.getProtocol().getExternalProtocolNumber();
|
||||
if(protocolNumber==null){
|
||||
protocolNumber= String.valueOf(applicationEntity.getProtocol().getProtocolNumber());
|
||||
}
|
||||
bodyPlaceholders.put("{{protocol_number}}", protocolNumber);
|
||||
String protocolDate= DateTimeUtil.formatLocalDateTime(applicationEntity.getProtocol().getCreatedDate(), GepafinConstant.DD_MM_YYYY);
|
||||
if(applicationEntity.getProtocol().getExternalProtocolDate()!=null){
|
||||
protocolDate= DateTimeUtil.formatLocalDateTime(applicationEntity.getProtocol().getExternalProtocolDate(), GepafinConstant.DD_MM_YYYY);
|
||||
}
|
||||
bodyPlaceholders.put("{{protocol_date}}", protocolDate);
|
||||
bodyPlaceholders.put("{{protocol_time}}", DateTimeUtil.parseLocalTimeToString(applicationEntity.getProtocol().getTime(), GepafinConstant.HH_MM_SS));
|
||||
HubEntity hubEntity = hubService.valdateHub(applicationEntity.getHubId());
|
||||
Map<String, String> subjectPlaceholders = new HashMap<>();
|
||||
CompanyEntity company = companyService.validateCompany(applicationEntity.getCompanyId());
|
||||
subjectPlaceholders.put("{{call_name}}", applicationEntity.getCall().getName());
|
||||
subjectPlaceholders.put("{{company_name}}", company.getCompanyName());
|
||||
// bodyPlaceholders.put("{{legal_mail}}", legalMail);
|
||||
String subject = Utils.replacePlaceholders(applicationContractEntity.getSubject(), subjectPlaceholders);
|
||||
String body = Utils.replacePlaceholders(applicationContractEntity.getText(), bodyPlaceholders);
|
||||
SystemEmailTemplateResponse systemEmailTemplateResponse=new SystemEmailTemplateResponse();
|
||||
systemEmailTemplateResponse.setSubject(subject);
|
||||
systemEmailTemplateResponse.setHtmlContent(body);
|
||||
systemEmailTemplateResponse.setEmailScenario(EmailScenarioTypeEnum.APPLICATION_CONTRACT_CREATED);
|
||||
EmailContentResponse emailContentResponse= new EmailContentResponse(subject, body, systemEmailTemplateResponse);
|
||||
sendEmails(applicationEntity, user, null,null,emailContentResponse.getSystemEmailTemplateResponse(),emailContentResponse.getSubject(),emailContentResponse.getBody(),applicationContractEntity.getId());
|
||||
}
|
||||
}
|
||||
@@ -15,27 +15,28 @@ public class S3PathConfig {
|
||||
@Autowired
|
||||
S3ConfigRepository s3ConfigRepository;
|
||||
|
||||
public String generateDocumentPath(DocumentSourceTypeEnum type, Long callId, Long applicationId,Long amendmentId,Long communicationId) {
|
||||
public String generateDocumentPath(DocumentSourceTypeEnum type, Long callId, Long applicationId,Long amendmentId,Long communicationId,Long contractId) {
|
||||
|
||||
S3ConfigEntity config = getDocumentPath(type);
|
||||
return config.getParentFolder() + "/" + buildS3Path(config.getPath(), callId, applicationId,amendmentId,communicationId);
|
||||
return config.getParentFolder() + "/" + buildS3Path(config.getPath(), callId, applicationId,amendmentId,communicationId,contractId);
|
||||
}
|
||||
public String generateDocumentPathForOther(DocOtherSourceTypeEnum type, Long callId, Long applicationId,Long amendmentId,Long communicationId) {
|
||||
public String generateDocumentPathForOther(DocOtherSourceTypeEnum type, Long callId, Long applicationId,Long amendmentId,Long communicationId,Long contractId) {
|
||||
|
||||
S3ConfigEntity config = getDocumentPathForOther(type);
|
||||
return config.getParentFolder() + "/" + buildS3Path(config.getPath(), callId, applicationId,amendmentId,communicationId);
|
||||
return config.getParentFolder() + "/" + buildS3Path(config.getPath(), callId, applicationId,amendmentId,communicationId,contractId);
|
||||
}
|
||||
public String generateDocumentPathForDelegationAndSignedDocument(DocOtherSourceTypeEnum type) {
|
||||
|
||||
S3ConfigEntity config = getDocumentPathForOther(type);
|
||||
return config.getParentFolder() + "/" + config.getPath();
|
||||
}
|
||||
private String buildS3Path(String pathTemplate, Long callId, Long applicationId, Long amendmentId,Long communicationId) {
|
||||
private String buildS3Path(String pathTemplate, Long callId, Long applicationId, Long amendmentId,Long communicationId,Long contractId) {
|
||||
return pathTemplate
|
||||
.replace("{call_id}", callId != null && callId != 0L ? "call_" + callId : "")
|
||||
.replace("{application_id}", applicationId != null && applicationId != 0L ? "application_" + applicationId : "")
|
||||
.replace("{amendment_id}", amendmentId != null && amendmentId != 0L ? "amendment_" + amendmentId : "")
|
||||
.replace("{communication_id}", communicationId != null && communicationId != 0L ? "communication_" + communicationId : "");
|
||||
.replace("{communication_id}", communicationId != null && communicationId != 0L ? "communication_" + communicationId : "")
|
||||
.replace("{contract_id}", contractId != null && contractId != 0L ? "contract_" + contractId : "");
|
||||
}
|
||||
|
||||
private S3ConfigEntity getDocumentPath(DocumentSourceTypeEnum type) {
|
||||
|
||||
Reference in New Issue
Block a user