2018 lines
112 KiB
Java
2018 lines
112 KiB
Java
package net.gepafin.tendermanagement.dao;
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import jakarta.persistence.criteria.CriteriaBuilder;
|
|
import jakarta.persistence.criteria.Root;
|
|
import jakarta.persistence.criteria.*;
|
|
import net.gepafin.tendermanagement.config.Translator;
|
|
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
|
import net.gepafin.tendermanagement.entities.*;
|
|
import net.gepafin.tendermanagement.entities.SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum;
|
|
import net.gepafin.tendermanagement.enums.*;
|
|
import net.gepafin.tendermanagement.model.request.*;
|
|
import net.gepafin.tendermanagement.model.response.*;
|
|
import net.gepafin.tendermanagement.model.util.SortBy;
|
|
import net.gepafin.tendermanagement.repositories.*;
|
|
import net.gepafin.tendermanagement.service.AmazonS3Service;
|
|
import net.gepafin.tendermanagement.service.ApplicationEvaluationService;
|
|
import net.gepafin.tendermanagement.service.CallService;
|
|
import net.gepafin.tendermanagement.service.CompanyService;
|
|
import net.gepafin.tendermanagement.service.DocumentService;
|
|
import net.gepafin.tendermanagement.service.FormService;
|
|
import net.gepafin.tendermanagement.service.HubService;
|
|
import net.gepafin.tendermanagement.service.SystemEmailTemplatesService;
|
|
import net.gepafin.tendermanagement.service.UserService;
|
|
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
|
import net.gepafin.tendermanagement.util.FieldValidator;
|
|
import net.gepafin.tendermanagement.util.LoggingUtil;
|
|
import net.gepafin.tendermanagement.util.Utils;
|
|
import net.gepafin.tendermanagement.util.Validator;
|
|
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
|
|
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
|
|
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
|
|
|
import org.h2.util.IOUtils;
|
|
import org.json.JSONObject;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.PageRequest;
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.math.BigDecimal;
|
|
import java.sql.Timestamp;
|
|
import java.text.MessageFormat;
|
|
import java.text.NumberFormat;
|
|
import java.text.ParseException;
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalTime;
|
|
import java.time.OffsetDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeParseException;
|
|
import java.util.*;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Stream;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
import static org.apache.commons.lang3.StringUtils.isEmpty;
|
|
import static org.hibernate.validator.internal.engine.messageinterpolation.el.RootResolver.FORMATTER;
|
|
|
|
@Component
|
|
public class ApplicationDao {
|
|
|
|
private final Logger log = LoggerFactory.getLogger(ApplicationDao.class);
|
|
|
|
@Autowired
|
|
private CallService callService;
|
|
|
|
@Autowired
|
|
private ApplicationRepository applicationRepository;
|
|
@Autowired
|
|
private DocumentRepository documentRepository;
|
|
@Autowired
|
|
private ApplicationFormRepository applicationFormRepository;
|
|
|
|
@Autowired
|
|
private ApplicationFormFieldRepository applicationFormFieldRepository;
|
|
|
|
@Autowired
|
|
private FormService formService;
|
|
|
|
@Autowired
|
|
private DocumentService documentService;
|
|
|
|
@Autowired
|
|
private CallDao callDao;
|
|
|
|
@Autowired
|
|
private FlowFormDao flowFormDao;
|
|
|
|
@Autowired
|
|
private FlowEdgesRepository flowEdgesRepository;
|
|
|
|
@Autowired
|
|
private FlowDataRepository flowDataRepository;
|
|
|
|
@Autowired
|
|
private UserCompanyDelegationRepository userCompanyDelegationRepository;
|
|
|
|
@Autowired
|
|
private Validator validator;
|
|
|
|
@Autowired
|
|
private CompanyService companyService;
|
|
@Autowired
|
|
private S3PathConfig s3PathConfig;
|
|
|
|
@Autowired
|
|
private SystemEmailTemplatesService systemEmailTemplatesService;
|
|
@Autowired
|
|
private AssignedApplicationsRepository assignedApplicationsRepository;
|
|
|
|
// @Value("${default_System_Receiver_Email}")
|
|
// private String defaultSystemReceiverEmail;
|
|
|
|
@Value("${rinaldo_email}")
|
|
private String rinaldoEmail;
|
|
|
|
|
|
@Value("${call.id}")
|
|
private String callId;
|
|
|
|
@Autowired
|
|
private AmazonS3Service amazonS3Service;
|
|
|
|
@Autowired
|
|
private ApplicationSignedDocumentRepository applicationSignedDocumentRepository;
|
|
|
|
// @Value("${aws.s3.url.folder.signed.document}")
|
|
// private String signedDocumentS3Folder;
|
|
|
|
@Value("${default.hub.uuid}")
|
|
private String defaultHubUuid;
|
|
|
|
@Autowired
|
|
private UserService userService;
|
|
|
|
@Autowired
|
|
private S3PathConfig s3ConfigBean;
|
|
|
|
@Autowired
|
|
private ProtocolDao protocolDao;
|
|
|
|
@Autowired
|
|
private HubService hubService;
|
|
|
|
@Autowired
|
|
private EmailNotificationDao emailNotificationDao;
|
|
|
|
@Autowired
|
|
private FormDao formDao;
|
|
|
|
@Autowired
|
|
private EmailLogDao emailLogDao;
|
|
|
|
@Autowired
|
|
private UserWithCompanyRepository userWithCompanyRepository;
|
|
|
|
@Autowired
|
|
private LoggingUtil loggingUtil;
|
|
|
|
@Autowired
|
|
private HttpServletRequest request;
|
|
|
|
@Autowired
|
|
private ApplicationEvaluationService applicationEvaluationService;
|
|
|
|
@Autowired
|
|
private NotificationDao notificationDao;
|
|
|
|
@Autowired
|
|
private UserRepository userRepository;
|
|
|
|
@Autowired
|
|
private RoleRepository roleRepository;
|
|
|
|
@Autowired
|
|
private ApplicationAmendmentRequestRepository applicationAmendmentRequestRepository;
|
|
|
|
@Autowired
|
|
private ApplicationEvaluationRepository applicationEvaluationRepository;
|
|
|
|
@Autowired
|
|
private ApplicationViewRepository applicationViewRepository;
|
|
|
|
|
|
public ApplicationResponseBean createApplication(HttpServletRequest request, ApplicationRequestBean applicationRequestBean, Long formId, Long applicationId) {
|
|
FormEntity formEntity = formService.validateForm(formId);
|
|
// callService.validatePublishedCall(formEntity.getCall().getId());
|
|
validateFormFields(applicationRequestBean,formEntity);
|
|
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
|
checkCallEndDate(applicationEntity.getCall());
|
|
validator.validateUserWithCompany(request, applicationEntity.getCompanyId());
|
|
if(Boolean.FALSE.equals(applicationEntity.getStatus().equals(ApplicationStatusTypeEnum.DRAFT.getValue()))) {
|
|
throw new CustomValidationException(Status.BAD_REQUEST,Translator.toLocale(GepafinConstant.APPLICATION_NOT_IN_DRAFT_STATUS));
|
|
}
|
|
formService.validateFormField(applicationRequestBean.getFormFields(),applicationEntity,formEntity);
|
|
ApplicationFormEntity applicationFormEntity = getApplicationFormOrCreate(formEntity, applicationEntity);
|
|
createOrUpdateMultipleFormFields(applicationRequestBean.getFormFields(), applicationFormEntity, formEntity);
|
|
return getApplicationById(applicationEntity.getId(),formEntity.getId());
|
|
}
|
|
public void validateDelegation(UserEntity user, UserWithCompanyEntity userWithCompany) {
|
|
|
|
UserCompanyDelegationEntity userCompanyDelegationEntity = userCompanyDelegationRepository
|
|
.findByUserIdAndUserWithCompanyIdAndStatus(user.getId(), userWithCompany.getId(),
|
|
UserCompanyDelegationStatusEnum.ACTIVE.getValue());
|
|
|
|
if (!userWithCompany.getIsLegalRepresentant() && userCompanyDelegationEntity == null) {
|
|
throw new CustomValidationException(Status.BAD_REQUEST,
|
|
Translator.toLocale(GepafinConstant.USER_NOT_AUTHORIZED_TO_CREATE_APPLICATION));
|
|
}
|
|
}
|
|
|
|
|
|
public ApplicationFormEntity saveApplicationFormEntity(ApplicationFormEntity applicationFormEntity) {
|
|
ApplicationFormEntity applicationFormEntity1 = applicationFormRepository.save(applicationFormEntity);
|
|
return applicationFormEntity1;
|
|
}
|
|
|
|
public ApplicationFormEntity createApplicationFormEntity(ApplicationEntity application, FormEntity formEntity) {
|
|
ApplicationFormEntity applicationFormEntity = new ApplicationFormEntity();
|
|
applicationFormEntity.setApplication(application);
|
|
applicationFormEntity.setForm(formEntity);
|
|
applicationFormEntity = saveApplicationFormEntity(applicationFormEntity);
|
|
return applicationFormEntity;
|
|
}
|
|
|
|
public ApplicationEntity createApplicationEntity(UserEntity user, CallEntity call, UserWithCompanyEntity userWithCompany) {
|
|
// validateDelegation(user,userWithCompany);
|
|
ApplicationEntity entity = new ApplicationEntity();
|
|
entity.setUserId(user.getId());
|
|
entity.setCompanyId(userWithCompany.getCompanyId());
|
|
entity.setCall(call);
|
|
entity.setHubId(call.getHub().getId());
|
|
entity.setUserWithCompany(userWithCompany);
|
|
entity.setIsDeleted(false);
|
|
entity.setStatus(ApplicationStatusTypeEnum.DRAFT.getValue());
|
|
entity.setEvaluationVersion(call.getEvaluationVersion());
|
|
return entity;
|
|
}
|
|
|
|
public ApplicationResponseBean getApplicationById(Long id,Long formId) {
|
|
log.info("Fetching application with ID: {}", id);
|
|
|
|
ApplicationEntity applicationEntity = validateApplication(id);
|
|
ApplicationFormEntity applicationFormEntity = applicationFormRepository.findByApplicationIdAndFormId(applicationEntity.getId(),formId);
|
|
List<ApplicationFormFieldResponseBean> applicationFormFieldResponseBeans=new ArrayList<>();
|
|
List<ApplicationFormFieldEntity> applicationFormFieldEntities = applicationFormFieldRepository.findByApplicationFormId(applicationFormEntity.getId());
|
|
applicationFormFieldResponseBeans=createApplicationFormFieldResponse(applicationFormFieldEntities, applicationFormEntity, applicationFormFieldResponseBeans);
|
|
ApplicationResponseBean applicationResponseBean= convertApplicationEntityToApplicationResponseBean(applicationEntity);
|
|
applicationResponseBean.setFormFields(applicationFormFieldResponseBeans);
|
|
return applicationResponseBean;
|
|
}
|
|
|
|
private List<ApplicationFormFieldResponseBean> createApplicationFormFieldResponse(
|
|
List<ApplicationFormFieldEntity> applicationFormFieldEntities,
|
|
ApplicationFormEntity applicationFormEntity,
|
|
List<ApplicationFormFieldResponseBean> applicationFormFieldResponseBeans) {
|
|
|
|
// List<ContentResponseBean> contentResponseBeans = Utils.convertJsonStringToList(
|
|
// applicationFormEntity.getForm().getContent(), ContentResponseBean.class);
|
|
|
|
List<ContentResponseBean> contentResponseBeans = formDao.convertFormEntityToFormResponseBean(applicationFormEntity.getForm()).getContent();
|
|
|
|
for (ApplicationFormFieldEntity applicationFormFieldEntity : applicationFormFieldEntities) {
|
|
|
|
Optional<ContentResponseBean> fileUploadContent = contentResponseBeans.stream()
|
|
.filter(contentResponseBean -> ("fileupload".equals(contentResponseBean.getName()) ||
|
|
"fileselect".equals(contentResponseBean.getName())) &&
|
|
contentResponseBean.getId().equals(applicationFormFieldEntity.getFieldId()))
|
|
.findFirst();
|
|
|
|
List<DocumentResponseBean> documentResponseBeans = new ArrayList<>();
|
|
if (fileUploadContent.isPresent()) {
|
|
String documentId = applicationFormFieldEntity.getFieldValue();
|
|
if (documentId != null && !documentId.isEmpty()) {
|
|
documentResponseBeans = Arrays.stream(documentId.split(","))
|
|
.map(String::trim)
|
|
.map(Long::parseLong)
|
|
.map(docId -> {
|
|
DocumentEntity documentEntity = documentService.validateDocument(docId);
|
|
if (Boolean.FALSE.equals(DocumentSourceTypeEnum.APPLICATION.getValue().equals(documentEntity.getSource()))) {
|
|
throw new CustomValidationException(Status.NOT_FOUND,Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
|
|
}
|
|
return documentEntity;
|
|
})
|
|
.map(callDao::convertToDocumentResponseBean)
|
|
.collect(Collectors.toList());
|
|
}
|
|
}
|
|
ApplicationFormFieldResponseBean responseBean = convertApplicationFormFieldEntityToApplicationFormFieldResponseBean(
|
|
applicationFormFieldEntity, applicationFormEntity.getId());
|
|
if (!documentResponseBeans.isEmpty()) {
|
|
responseBean.setFieldValue(documentResponseBeans);
|
|
}
|
|
applicationFormFieldResponseBeans.add(responseBean);
|
|
}
|
|
|
|
return applicationFormFieldResponseBeans;
|
|
}
|
|
|
|
public void deleteById(HttpServletRequest request, Long id) {
|
|
|
|
log.info("Deleting application with ID: {}", id);
|
|
|
|
ApplicationEntity applicationEntity= validateApplication(id);
|
|
if (Boolean.FALSE.equals(ApplicationStatusTypeEnum.DRAFT.getValue().equals(applicationEntity.getStatus()))) {
|
|
throw new CustomValidationException(
|
|
Status.VALIDATION_ERROR,
|
|
Translator.toLocale(GepafinConstant.APPLICATION_NOT_IN_DRAFT_STATUS)
|
|
);
|
|
}
|
|
ApplicationEntity oldApplicationDataEntity = Utils.getClonedEntityForData(applicationEntity);
|
|
|
|
validator.validateUserWithCompany(request, applicationEntity.getCompanyId());
|
|
applicationEntity.setIsDeleted(true);
|
|
applicationEntity = applicationRepository.save(applicationEntity);
|
|
|
|
/** This code is responsible for adding a version history log for the "Delete application" operation. **/
|
|
loggingUtil.addVersionHistory(
|
|
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldApplicationDataEntity).newData(applicationEntity).build());
|
|
|
|
log.info("Application deleted with ID: {}", id);
|
|
}
|
|
|
|
// public List<ApplicationResponse> getAllApplications(UserEntity userEntity, Long callId, CompanyEntity companyEntity) {
|
|
// boolean isBeneficiary = validator.checkIsBeneficiary();
|
|
//
|
|
// log.info("Fetching applications for RoleType: {}", userEntity.getRoleEntity().getRoleType());
|
|
// List<ApplicationResponse> applicationResponses = new ArrayList<>();
|
|
//
|
|
// if (callId != null) {
|
|
// // Fetch based on callId and user if role is BENEFICIARY, otherwise fetch all for the call
|
|
// log.info("Fetching applications for callId: {}", callId);
|
|
// CallEntity call = callService.validateCall(callId);
|
|
//
|
|
// // Use a single method to handle both conditions for consistency
|
|
// List<ApplicationEntity> applicationEntities = isBeneficiary
|
|
// ? applicationRepository.findByUserIdAndCallIdAndIsDeletedFalse(userEntity.getId(), call.getId())
|
|
// .map(List::of) // Convert Optional<ApplicationEntity> to a List of one element
|
|
// .orElse(List.of()) // If not present, return an empty list
|
|
// : applicationRepository.findByCallIdAndIsDeletedFalse(call.getId());
|
|
//
|
|
// applicationResponses = applicationEntities.stream()
|
|
// .map(this::getApplicationResponse)
|
|
// .collect(Collectors.toList());
|
|
//
|
|
// } else {
|
|
// // Fetch all applications for the user if BENEFICIARY, or fetch all applications in general
|
|
// List<ApplicationEntity> applicationEntities = isBeneficiary
|
|
// ? applicationRepository.findByUserIdAndIsDeletedFalse(companyEntity.getId())
|
|
// : applicationRepository.findByIsDeletedFalse();
|
|
//
|
|
// applicationResponses = applicationEntities.stream()
|
|
// .map(this::getApplicationResponse)
|
|
// .collect(Collectors.toList());
|
|
// }
|
|
//
|
|
// return applicationResponses;
|
|
// }
|
|
|
|
public List<ApplicationResponse> getAllApplications(UserEntity userEntity, Long callId, Long companyId,List<ApplicationStatusTypeEnum> statusList) {
|
|
|
|
log.info("Fetching applications for RoleType: {}", userEntity.getRoleEntity().getRoleType());
|
|
|
|
Specification<ApplicationEntity> spec = search(userEntity, callId, companyId,statusList);
|
|
|
|
List<ApplicationEntity> applicationEntities = applicationRepository.findAll(spec);
|
|
|
|
return applicationEntities.stream()
|
|
.map(this::getApplicationResponse)
|
|
.collect(Collectors.toList());
|
|
}
|
|
|
|
|
|
private Specification<ApplicationEntity> search(UserEntity userEntity, Long callId, Long companyId,List<ApplicationStatusTypeEnum> statusList) {
|
|
return (root, query, builder) -> {
|
|
Boolean isBeneficiary = validator.checkIsBeneficiary();
|
|
Predicate predicate = builder.isFalse(root.get("isDeleted"));
|
|
if (Boolean.TRUE.equals(validator.checkIsBeneficiary()) || Boolean.TRUE.equals(validator.checkIsConfidi())) {
|
|
predicate = builder.and(predicate, builder.equal(root.get("userId"), userEntity.getId()));
|
|
}
|
|
if (callId != null) {
|
|
predicate = builder.and(predicate, builder.equal(root.get("call").get("id"), callId));
|
|
}
|
|
if (companyId != null) {
|
|
predicate = builder.and(predicate, builder.equal(root.get("companyId"), companyId));
|
|
}
|
|
if (statusList != null && !statusList.isEmpty()) {
|
|
List<String> statusNames = statusList.stream()
|
|
.map(Enum::name)
|
|
.collect(Collectors.toList());
|
|
predicate = builder.and(predicate, root.get("status").in(statusNames));
|
|
}
|
|
query.orderBy(
|
|
builder.desc(builder.isNotNull(root.get(GepafinConstant.SUBMISSION_DATE))),
|
|
builder.desc(root.get(GepafinConstant.SUBMISSION_DATE))
|
|
);
|
|
predicate = builder.and(predicate, builder.equal(root.get("hubId"), userEntity.getHub().getId()));
|
|
return predicate;
|
|
};
|
|
}
|
|
|
|
private ApplicationResponse getApplicationResponse(ApplicationEntity applicationEntity) {
|
|
ApplicationResponse responseBean = new ApplicationResponse();
|
|
List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findByCallId(applicationEntity.getCall().getId());
|
|
Long totalFormSteps = flowFormDao.calculateTotalSteps(flowEdgesList);
|
|
Long completedSteps= Long.valueOf(flowFormDao.getCompletedSteps(applicationEntity, false));
|
|
Integer progress = calculateProgress(totalFormSteps, completedSteps);
|
|
responseBean.setId(applicationEntity.getId());
|
|
responseBean.setProgress(progress);
|
|
responseBean.setCallTitle(applicationEntity.getCall().getName());
|
|
responseBean.setCallEndDate(applicationEntity.getCall().getEndDate());
|
|
responseBean.setCallEndTime(applicationEntity.getCall().getEndTime());
|
|
responseBean.setModifiedDate(applicationEntity.getUpdatedDate());
|
|
responseBean.setCallId(applicationEntity.getCall().getId());
|
|
responseBean.setSubmissionDate(applicationEntity.getSubmissionDate());
|
|
responseBean.setStatus(applicationEntity.getStatus());
|
|
responseBean.setEvaluationVersion(EvaluationVersionEnum.valueOf(applicationEntity.getCall().getEvaluationVersion()));
|
|
responseBean.setComments(applicationEntity.getComments());
|
|
responseBean.setCompanyId(applicationEntity.getCompanyId());
|
|
Optional<AssignedApplicationsEntity> assignedApplicationsOptional =
|
|
assignedApplicationsRepository.findByApplicationIdAndIsDeletedFalse(applicationEntity.getId());
|
|
if(assignedApplicationsOptional.isPresent()){
|
|
responseBean.setAssignedUserId(assignedApplicationsOptional.get().getUserId());
|
|
UserEntity user = userService.validateUser(assignedApplicationsOptional.get().getUserId());
|
|
String firstName = user.getFirstName() != null ? user.getFirstName() : "";
|
|
String lastName = user.getLastName() != null ? user.getLastName() : "";
|
|
String userName = String.join(" ", firstName, lastName).trim();
|
|
responseBean.setAssignedUserName(userName);
|
|
}
|
|
CompanyEntity company=companyService.validateCompany(applicationEntity.getCompanyId());
|
|
responseBean.setCompanyName(company.getCompanyName());
|
|
if(applicationEntity.getProtocol() != null) {
|
|
responseBean.setProtocolNumber(applicationEntity.getProtocol().getProtocolNumber());
|
|
}
|
|
responseBean.setAmountAccepted(applicationEntity.getAmountAccepted());
|
|
responseBean.setAmountRequested(applicationEntity.getAmountRequested());
|
|
responseBean.setDateAccepted(applicationEntity.getDateAccepted());
|
|
responseBean.setDateRejected(applicationEntity.getDateRejected());
|
|
return responseBean;
|
|
}
|
|
|
|
public ApplicationEntity validateApplication(Long id) {
|
|
ApplicationEntity applicationEntity = applicationRepository.findById(id)
|
|
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
|
|
Translator.toLocale(GepafinConstant.APPLICATION_NOT_FOUND_MSG)));
|
|
return applicationEntity;
|
|
}
|
|
|
|
private ApplicationResponseBean convertApplicationEntityToApplicationResponseBean(ApplicationEntity entity) {
|
|
ApplicationResponseBean response = new ApplicationResponseBean();
|
|
response.setId(entity.getId());
|
|
response.setSubmissionDate(entity.getSubmissionDate());
|
|
response.setStatus(entity.getStatus());
|
|
response.setComments(entity.getComments());
|
|
response.setCallId(entity.getCall().getId());
|
|
response.setCreatedDate(entity.getCreatedDate());
|
|
response.setUpdatedDate(entity.getUpdatedDate());
|
|
response.setAmountAccepted(entity.getAmountAccepted());
|
|
response.setAmountRequested(entity.getAmountRequested());
|
|
response.setDateAccepted(entity.getDateAccepted());
|
|
response.setDateRejected(entity.getDateRejected());
|
|
if(entity.getProtocol() != null) {
|
|
response.setProtocolNumber(entity.getProtocol().getProtocolNumber());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
private ApplicationFormEntity getApplicationFormOrCreate(FormEntity formEntity, ApplicationEntity applicationEntity) {
|
|
|
|
ApplicationFormEntity applicationFormEntity = applicationFormRepository.findByApplicationIdAndFormId(applicationEntity.getId(), formEntity.getId());
|
|
ApplicationFormEntity oldApplicationFormEntity = Utils.getClonedEntityForData(applicationFormEntity);
|
|
if (applicationFormEntity == null) {
|
|
applicationFormEntity = createApplicationFormEntity(applicationEntity, formEntity);
|
|
|
|
/** This code is responsible for adding a version history log for the "Create application form" operation. **/
|
|
loggingUtil.addVersionHistory(
|
|
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.INSERT).oldData(oldApplicationFormEntity).newData(applicationFormEntity)
|
|
.build());
|
|
}
|
|
return applicationFormEntity;
|
|
}
|
|
|
|
public List<ApplicationFormFieldEntity> createOrUpdateMultipleFormFields(List<ApplicationFormFieldRequestBean> formFieldResponseBeans,
|
|
ApplicationFormEntity applicationFormEntity, FormEntity formEntity) {
|
|
FieldValidator fieldValidator = FieldValidator.create();
|
|
|
|
List<ApplicationFormFieldEntity> existingFields = applicationFormFieldRepository.findByApplicationFormId(applicationFormEntity.getId());
|
|
|
|
List<ApplicationFormFieldEntity> applicationFormFieldEntities=formFieldResponseBeans.stream().map(requestBean -> createOrUpdateApplicationFormField(requestBean, applicationFormEntity, existingFields, formEntity,fieldValidator))
|
|
.collect(Collectors.toList());
|
|
fieldValidator.validate();
|
|
return applicationFormFieldEntities;
|
|
}
|
|
|
|
public ApplicationFormFieldEntity createOrUpdateApplicationFormField(ApplicationFormFieldRequestBean applicationFormFieldRequestBean,
|
|
ApplicationFormEntity applicationFormEntity, List<ApplicationFormFieldEntity> applicationFormFieldEntities, FormEntity formEntity,FieldValidator fieldValidator) {
|
|
|
|
ApplicationFormFieldEntity applicationFormFieldEntity = new ApplicationFormFieldEntity();
|
|
|
|
List<Long> newDocumentIds = validateFileUploadDocuments(applicationFormFieldRequestBean, formEntity);
|
|
validateFileUploadDocuments(applicationFormFieldRequestBean, formEntity);
|
|
VersionActionTypeEnum actionType = VersionActionTypeEnum.INSERT;
|
|
List<ContentResponseBean> contentResponseBeans = formDao.convertFormEntityToFormResponseBean(formEntity).getContent();
|
|
|
|
contentResponseBeans.stream()
|
|
.filter(content -> "numberinput".equals(content.getName()) && content.getId().toString().equals(applicationFormFieldRequestBean.getFieldId()))
|
|
.map(ContentResponseBean::getSettings)
|
|
.flatMap(List::stream)
|
|
.filter(setting -> "isRequestedAmount".equals(setting.getName()) && Boolean.TRUE.equals(setting.getValue()))
|
|
.findFirst()
|
|
.ifPresent(setting -> {
|
|
Object fieldValue = applicationFormFieldRequestBean.getFieldValue();
|
|
if(fieldValue!=null) {
|
|
try {
|
|
BigDecimal amountRequested = new BigDecimal(fieldValue.toString());
|
|
applicationFormEntity.getApplication().setAmountRequested(amountRequested);
|
|
} catch (NumberFormatException e) {
|
|
throw new IllegalArgumentException("Field value is not a valid number: " + fieldValue, e);
|
|
}
|
|
}
|
|
});
|
|
|
|
|
|
ApplicationFormFieldEntity oldApplicationFormFieldData = null;
|
|
|
|
if (applicationFormFieldEntities == null || applicationFormFieldEntities.isEmpty()) {
|
|
applicationFormFieldEntity.setApplicationForm(applicationFormEntity);
|
|
} else {
|
|
for (ApplicationFormFieldEntity applicationFormFieldEntity1 : applicationFormFieldEntities) {
|
|
if (applicationFormFieldEntity1.getFieldId().equals(applicationFormFieldRequestBean.getFieldId())) {
|
|
applicationFormFieldEntity = applicationFormFieldEntity1;
|
|
oldApplicationFormFieldData = Utils.getClonedEntityForData(applicationFormFieldEntity);
|
|
if (applicationFormEntity.getForm().getId().equals(applicationFormEntity.getApplication().getCall().getInitialForm()) && checkIfRequestFieldIsDifferent(applicationFormFieldEntity1, applicationFormFieldRequestBean)) {
|
|
validateRequiredFields(applicationFormEntity.getForm(), applicationFormEntity.getApplication(), applicationFormFieldRequestBean.getFieldId());
|
|
}
|
|
actionType = VersionActionTypeEnum.UPDATE;
|
|
break;
|
|
} else {
|
|
applicationFormFieldEntity.setApplicationForm(applicationFormEntity);
|
|
}
|
|
}
|
|
}
|
|
calculationProcessForFormula(applicationFormEntity,contentResponseBeans,applicationFormFieldRequestBean,fieldValidator);
|
|
Utils.setIfUpdated(applicationFormFieldEntity::getFieldId, applicationFormFieldEntity::setFieldId, applicationFormFieldRequestBean.getFieldId());
|
|
|
|
if (applicationFormFieldRequestBean.getFieldValue() != null) {
|
|
updateDocumentDeletionStatus(applicationFormFieldEntity, applicationFormFieldRequestBean, formEntity, newDocumentIds, null, false);
|
|
applicationFormFieldEntity.setFieldValue(Utils.convertObjectToJsonString(applicationFormFieldRequestBean.getFieldValue()));
|
|
}
|
|
if (applicationFormFieldRequestBean.getFieldValue() == null) {
|
|
updateDocumentDeletionStatus(applicationFormFieldEntity, applicationFormFieldRequestBean, formEntity, newDocumentIds, null, false);
|
|
}
|
|
if (applicationFormFieldRequestBean.getFieldValue() != null) {
|
|
applicationFormFieldEntity.setFieldValue(Utils.convertObjectToJsonString(applicationFormFieldRequestBean.getFieldValue()));
|
|
} else {
|
|
applicationFormFieldEntity.setFieldValue(null);
|
|
}
|
|
|
|
ApplicationFormFieldEntity applicationFormField = applicationFormFieldRepository.save(applicationFormFieldEntity);
|
|
|
|
/** This code is responsible for adding a version history log for the "Create update application form" operation. **/
|
|
loggingUtil.addVersionHistory(
|
|
VersionHistoryRequest.builder().request(request).actionType(actionType).oldData(oldApplicationFormFieldData).newData(applicationFormField).build());
|
|
|
|
log.info("Version history logged for action: {}, Field ID: {}", actionType, applicationFormFieldEntity.getFieldId());
|
|
return applicationFormField;
|
|
}
|
|
|
|
private Boolean checkIfRequestFieldIsDifferent(ApplicationFormFieldEntity applicationFormFieldEntity,
|
|
ApplicationFormFieldRequestBean applicationFormFieldRequestBean) {
|
|
|
|
// Retrieve the field values from both objects
|
|
String entityFieldValue = applicationFormFieldEntity.getFieldValue();
|
|
Object requestFieldValue = applicationFormFieldRequestBean.getFieldValue();
|
|
|
|
// Check if both are null
|
|
if (entityFieldValue == null && requestFieldValue == null) {
|
|
return false; // No difference if both are null
|
|
}
|
|
|
|
// Compare values
|
|
Boolean check = !Objects.equals(entityFieldValue, requestFieldValue);
|
|
|
|
// Additional comparison if both are non-null
|
|
if (Boolean.TRUE.equals(check) && entityFieldValue != null && requestFieldValue != null) {
|
|
check = !entityFieldValue.equals(requestFieldValue.toString());
|
|
}
|
|
|
|
return check;
|
|
}
|
|
|
|
void updateDocumentDeletionStatus(ApplicationFormFieldEntity applicationFormFieldEntity, ApplicationFormFieldRequestBean applicationFormFieldRequestBean, FormEntity formEntity, List<Long> newDocumentIds,
|
|
List<String> preInstructorDocumentId,boolean isPreInstructor) {
|
|
if (newDocumentIds == null) {
|
|
newDocumentIds = Collections.emptyList();
|
|
}
|
|
if (preInstructorDocumentId == null) {
|
|
preInstructorDocumentId = Collections.emptyList();
|
|
}
|
|
|
|
List<ContentResponseBean> contentResponseBeans = formDao.convertFormEntityToFormResponseBean(formEntity).getContent();
|
|
for (ContentResponseBean contentResponseBean : contentResponseBeans) {
|
|
if (Boolean.FALSE.equals(contentResponseBean.getName().equals("fileupload") || contentResponseBean.getName().equals("fileselect"))) {
|
|
return;
|
|
}
|
|
}
|
|
if(!isPreInstructor){
|
|
List<Long> currentDocumentIds = parseDocumentIds(applicationFormFieldEntity.getFieldValue());
|
|
if (Boolean.TRUE.equals(newDocumentIds.isEmpty())) {
|
|
currentDocumentIds.forEach(docId -> documentService.deleteFile(docId));
|
|
} else {
|
|
List<Long> finalNewDocumentIds = newDocumentIds;
|
|
List<Long> documentsToDelete = currentDocumentIds.stream()
|
|
.filter(docId -> !finalNewDocumentIds.contains(docId))
|
|
.toList();
|
|
documentsToDelete.forEach(docId -> documentService.deleteFile(docId));
|
|
}}
|
|
else{
|
|
List<Long> currentDocumentIds = parseDocumentIds(applicationFormFieldEntity.getFieldValue());
|
|
if (Boolean.TRUE.equals(preInstructorDocumentId.isEmpty())) {
|
|
currentDocumentIds.forEach(docId -> documentService.deleteFile(docId));
|
|
} else {
|
|
List<Long> preInstructorDocIds = preInstructorDocumentId.stream()
|
|
.map(Long::valueOf)
|
|
.collect(Collectors.toList());
|
|
|
|
List<Long> documentsToDelete = currentDocumentIds.stream()
|
|
.filter(docId -> !preInstructorDocIds.contains(docId))
|
|
.toList();
|
|
documentsToDelete.forEach(docId -> documentService.deleteFile(docId));
|
|
}}
|
|
}
|
|
|
|
|
|
private List<Long> parseDocumentIds(String fieldValue) {
|
|
if (fieldValue == null || fieldValue.isEmpty()) {
|
|
return Collections.emptyList();
|
|
}
|
|
if (fieldValue.contains(",")) {
|
|
return Arrays.stream(fieldValue.split(","))
|
|
.map(String::trim)
|
|
.map(Long::parseLong)
|
|
.collect(Collectors.toList());
|
|
} else {
|
|
try {
|
|
return Collections.singletonList(Long.parseLong(fieldValue.trim()));
|
|
} catch (NumberFormatException e) {
|
|
e.printStackTrace();
|
|
return Collections.emptyList();
|
|
}
|
|
}
|
|
}
|
|
|
|
private List<Long> validateFileUploadDocuments(ApplicationFormFieldRequestBean applicationFormFieldRequestBean, FormEntity formEntity) {
|
|
List<Long> documentIds=null;
|
|
// List<ContentResponseBean> contentResponseBeans=Utils.convertJsonStringToList(formEntity.getContent(),ContentResponseBean.class);
|
|
List<ContentResponseBean> contentResponseBeans=formDao.convertFormEntityToFormResponseBean(formEntity).getContent();
|
|
for (ContentResponseBean contentResponseBean:contentResponseBeans){
|
|
if(Boolean.TRUE.equals(contentResponseBean.getName().equals("fileupload")) || Boolean.TRUE.equals(contentResponseBean.getName().equals("fileselect"))) {
|
|
if (contentResponseBean.getId().equals(applicationFormFieldRequestBean.getFieldId())) {
|
|
Object fieldValueObject = applicationFormFieldRequestBean.getFieldValue();
|
|
if (fieldValueObject instanceof String) {
|
|
// Safely cast the object to a string
|
|
String documentId = (String) fieldValueObject;
|
|
// Now you can use documentId as needed
|
|
documentIds = validateDocumentIds(documentId);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return documentIds;
|
|
}
|
|
|
|
public List<Long> validateDocumentIds(String documentId) {
|
|
if (documentId != null && !documentId.isEmpty()) {
|
|
return Arrays.stream(documentId.split(","))
|
|
.map(Long::parseLong)
|
|
.peek(docId -> documentService.validateDocument(docId))
|
|
.collect(Collectors.toList());
|
|
}
|
|
return Collections.emptyList();
|
|
}
|
|
|
|
|
|
public ApplicationFormFieldEntity validateApplicationFormField(Long applicationFormFieldId) {
|
|
Optional<ApplicationFormFieldEntity> applicationFormFieldEntity = applicationFormFieldRepository.findById(applicationFormFieldId);
|
|
if (applicationFormFieldEntity.isEmpty()) {
|
|
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.APPLICATION_FORM_FIELD_NOT_FOUND));
|
|
}
|
|
return applicationFormFieldEntity.get();
|
|
}
|
|
|
|
public List<ApplicationFormFieldEntity> saveApplicationFormFieldEntities(List<ApplicationFormFieldEntity> applicationFormFieldEntities) {
|
|
List<ApplicationFormFieldEntity> applicationFormFieldEntities1 = applicationFormFieldRepository.saveAll(applicationFormFieldEntities);
|
|
return applicationFormFieldEntities1;
|
|
}
|
|
|
|
public List<ApplicationFormFieldResponseBean> convertApplicationFormFieldEntitiesToApplicationFormFieldResponseBeans(List<ApplicationFormFieldEntity> applicationFormFieldEntities, Long applicationFormId) {
|
|
return applicationFormFieldEntities.stream()
|
|
.map(requestBean -> convertApplicationFormFieldEntityToApplicationFormFieldResponseBean(requestBean, applicationFormId))
|
|
.collect(Collectors.toList());
|
|
}
|
|
|
|
public ApplicationFormFieldResponseBean convertApplicationFormFieldEntityToApplicationFormFieldResponseBean(ApplicationFormFieldEntity applicationFormFieldEntity, Long applicationFormId) {
|
|
ApplicationFormFieldResponseBean applicationFormFieldResponseBean = new ApplicationFormFieldResponseBean();
|
|
applicationFormFieldResponseBean.setApplicationFormId(applicationFormId);
|
|
applicationFormFieldResponseBean.setFieldId(applicationFormFieldEntity.getFieldId());
|
|
if(applicationFormFieldEntity.getFieldValue() != null) {
|
|
applicationFormFieldResponseBean.setFieldValue(Utils.getFieldValueAsObject(applicationFormFieldEntity.getFieldValue()));
|
|
}
|
|
applicationFormFieldResponseBean.setId(applicationFormFieldEntity.getId());
|
|
applicationFormFieldResponseBean.setCreatedDate(applicationFormFieldEntity.getCreatedDate());
|
|
applicationFormFieldResponseBean.setUpdatedDate(applicationFormFieldEntity.getUpdatedDate());
|
|
return applicationFormFieldResponseBean;
|
|
}
|
|
|
|
public ApplicationEntity saveApplicationEntity(ApplicationEntity application) {
|
|
|
|
ApplicationEntity applicationEntity = applicationRepository.save(application);
|
|
|
|
/** This code is responsible for adding a version history log for "Create application" operation. **/
|
|
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.INSERT).oldData(null).newData(applicationEntity).build());
|
|
|
|
return applicationEntity;
|
|
}
|
|
|
|
public ApplicationGetResponseBean getApplicationByFormId(HttpServletRequest request, Long applicationId, Long formId) {
|
|
List<FormApplicationResponse> formApplicationResponses = new ArrayList<>();
|
|
List<FormEntity> formEntities = new ArrayList<>();
|
|
UserEntity userEntity = validator.validateUser(request);
|
|
boolean isBeneficiary = Boolean.TRUE.equals(validator.checkIsBeneficiary()) || Boolean.TRUE.equals(validator.checkIsConfidi());
|
|
ApplicationEntity applicationEntity = isBeneficiary
|
|
? applicationRepository.findByIdAndUserIdAndIsDeletedFalse(applicationId, userEntity.getId())
|
|
.orElseThrow(() -> new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.APPLICATION_NOT_FOUND_MSG)))
|
|
: applicationRepository.findById(applicationId)
|
|
.stream().findFirst()
|
|
.orElseThrow(() -> new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.APPLICATION_NOT_FOUND_MSG)));
|
|
if (formId != null) {
|
|
FormEntity formEntity = formService.validateForm(formId);
|
|
Optional<ApplicationEntity> application = applicationRepository.findByIdAndUserIdAndCallIdAndIsDeletedFalse(applicationId, userEntity.getId(),
|
|
formEntity.getCall().getId());
|
|
applicationEntity=application.get();
|
|
formEntities.add(formEntity);
|
|
addFormApplication(formEntity, applicationEntity, formApplicationResponses);
|
|
}
|
|
else {
|
|
List<ApplicationFormEntity> applicationFormEntities = applicationFormRepository.findByApplicationId(applicationEntity.getId());
|
|
List<ApplicationFormEntity> sequencedApplicationFormEntity = new ArrayList<>();
|
|
Long formIdMiddle = null;
|
|
List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findBySourceIdAndCallId(
|
|
applicationEntity.getCall().getInitialForm(), applicationEntity.getCall().getId());
|
|
|
|
if (!flowEdgesList.isEmpty()) {
|
|
if (flowEdgesList.size() == 1) {
|
|
formIdMiddle = flowEdgesList.get(0).getTargetId();
|
|
} else {
|
|
List<Long> nextFormIds = flowEdgesList.stream()
|
|
.map(FlowEdgesEntity::getTargetId)
|
|
.toList();
|
|
|
|
FlowDataEntity flowDataEntity = flowDataRepository.findByFormIdAndCallId(
|
|
applicationEntity.getCall().getInitialForm(), applicationEntity.getCall().getId());
|
|
|
|
ApplicationFormFieldEntity applicationFormFieldEntity = applicationFormFieldRepository
|
|
.findByFieldIdAndApplicationFormFormIdAndApplicationFormApplicationId(
|
|
flowDataEntity.getChoosenField(), applicationEntity.getCall().getInitialForm(), applicationEntity.getId())
|
|
.orElse(null);
|
|
|
|
if (applicationFormFieldEntity != null) {
|
|
formIdMiddle = flowDataRepository.findByChoosenValueAndFormIdIn(
|
|
applicationFormFieldEntity.getFieldValue(), nextFormIds)
|
|
.map(FlowDataEntity::getFormId)
|
|
.orElse(null);
|
|
}
|
|
}
|
|
}
|
|
List<Long> applicationFormIds = new ArrayList<>();
|
|
applicationFormIds.add(applicationEntity.getCall().getInitialForm());
|
|
if (formIdMiddle != null && formIdMiddle > 0) {
|
|
applicationFormIds.add(formIdMiddle);
|
|
}
|
|
applicationFormIds.add(applicationEntity.getCall().getFinalForm());
|
|
if (applicationFormEntities.size() == 3) {
|
|
for (Long applicationFormId : applicationFormIds) {
|
|
for (ApplicationFormEntity applicationFormEntity : applicationFormEntities) {
|
|
if (applicationFormEntity.getForm().getId().equals(applicationFormId)) {
|
|
sequencedApplicationFormEntity.add(applicationFormEntity);
|
|
FormEntity form = formService.validateForm(applicationFormId);
|
|
formEntities.add(form);
|
|
addFormApplication(form, applicationEntity, formApplicationResponses);
|
|
}
|
|
}
|
|
}
|
|
}else {
|
|
ApplicationFormEntity applicationFormEntity1=applicationFormRepository.findByApplicationIdAndFormId(applicationEntity.getId(),applicationEntity.getCall().getInitialForm());
|
|
sequencedApplicationFormEntity.add(applicationFormEntity1);
|
|
FormEntity form1 = formService.validateForm(applicationFormEntity1.getForm().getId());
|
|
formEntities.add(form1);
|
|
addFormApplication(form1, applicationEntity, formApplicationResponses);
|
|
ApplicationFormEntity applicationFormEntity2=applicationFormRepository.findByApplicationIdAndFormId(applicationEntity.getId(),applicationEntity.getCall().getFinalForm());
|
|
sequencedApplicationFormEntity.add(applicationFormEntity2);
|
|
FormEntity form2= formService.validateForm(applicationFormEntity2.getForm().getId());
|
|
formEntities.add(form2);
|
|
addFormApplication(form2, applicationEntity, formApplicationResponses);
|
|
}
|
|
}
|
|
|
|
return createApplicationGetResponseBean(applicationEntity, formEntities, formApplicationResponses);
|
|
}
|
|
|
|
private boolean isBeneficiary(UserEntity userEntity) {
|
|
RoleStatusEnum roleStatus = RoleStatusEnum.valueOf(userEntity.getRoleEntity().getRoleType());
|
|
boolean isBeneficiary = RoleStatusEnum.ROLE_BENEFICIARY.equals(roleStatus);
|
|
return isBeneficiary;
|
|
}
|
|
|
|
private void addFormApplication(FormEntity formEntity, ApplicationEntity applicationEntity,
|
|
List<FormApplicationResponse> formApplicationResponses) {
|
|
FormApplicationResponse formApplicationResponse = processForm(formEntity, applicationEntity);
|
|
if(formApplicationResponse.getContent() != null && formApplicationResponse.getFormFields() != null) {
|
|
formApplicationResponses.add(formApplicationResponse);
|
|
}
|
|
|
|
}
|
|
|
|
public FormApplicationResponse processForm(FormEntity formEntity, ApplicationEntity applicationEntity) {
|
|
FormApplicationResponse formApplicationResponse = createFormApplicationResponse(formEntity);
|
|
List<ApplicationFormFieldResponseBean> applicationFormFieldResponseBeans =new ArrayList<>();
|
|
ApplicationFormEntity applicationFormEntity = applicationFormRepository.findByApplicationIdAndFormId(applicationEntity.getId(), formEntity.getId());
|
|
if(applicationFormEntity!=null) {
|
|
List<ApplicationFormFieldEntity> applicationFormFieldEntities = applicationFormFieldRepository.findByApplicationFormId(applicationFormEntity.getId());
|
|
// formApplicationResponse = createFormApplicationResponse(formEntity);
|
|
applicationFormFieldResponseBeans = createApplicationFormFieldResponse(applicationFormFieldEntities, applicationFormEntity,applicationFormFieldResponseBeans);
|
|
formApplicationResponse.setFormFields(applicationFormFieldResponseBeans);
|
|
}
|
|
return formApplicationResponse;
|
|
}
|
|
|
|
private ApplicationGetResponseBean createApplicationGetResponseBean(ApplicationEntity applicationEntity, List<FormEntity> formEntities, List<FormApplicationResponse> formApplicationResponses) {
|
|
ApplicationGetResponseBean applicationGetResponseBean = createApplicationGetResponseBean(applicationEntity);
|
|
applicationGetResponseBean.setForm(formApplicationResponses);
|
|
return applicationGetResponseBean;
|
|
}
|
|
|
|
private ApplicationGetResponseBean createApplicationGetResponseBean(ApplicationEntity applicationEntity) {
|
|
ApplicationGetResponseBean applicationGetResponseBean = new ApplicationGetResponseBean();
|
|
applicationGetResponseBean.setId(applicationEntity.getId());
|
|
applicationGetResponseBean.setStatus(applicationEntity.getStatus());
|
|
applicationGetResponseBean.setComments(applicationEntity.getComments());
|
|
applicationGetResponseBean.setSubmissionDate(applicationEntity.getSubmissionDate());
|
|
applicationGetResponseBean.setCallId(applicationEntity.getCall().getId());
|
|
applicationGetResponseBean.setCallTitle(applicationEntity.getCall().getName());
|
|
applicationGetResponseBean.setCompanyId(applicationEntity.getCompanyId());
|
|
applicationGetResponseBean.setAmountAccepted(applicationEntity.getAmountAccepted());
|
|
applicationGetResponseBean.setAmountRequested(applicationEntity.getAmountRequested());
|
|
applicationGetResponseBean.setDateAccepted(applicationEntity.getDateAccepted());
|
|
applicationGetResponseBean.setDateRejected(applicationEntity.getDateRejected());
|
|
if(applicationEntity.getProtocol() != null) {
|
|
applicationGetResponseBean.setProtocolNumber(applicationEntity.getProtocol().getProtocolNumber());
|
|
}
|
|
CompanyEntity company=companyService.validateCompany(applicationEntity.getCompanyId());
|
|
applicationGetResponseBean.setCompanyName(company.getCompanyName());
|
|
return applicationGetResponseBean;
|
|
}
|
|
|
|
private FormApplicationResponse createFormApplicationResponse(FormEntity formEntity) {
|
|
FormApplicationResponse formApplicationResponse=new FormApplicationResponse();
|
|
formApplicationResponse.setId(formEntity.getId());
|
|
formApplicationResponse.setLabel(formEntity.getLabel());
|
|
formApplicationResponse.setCallId(formEntity.getCall().getId());
|
|
// formApplicationResponse.setContent(Utils.convertJsonStringToList(formEntity.getContent(), ContentResponseBean.class)
|
|
formApplicationResponse.setContent(formDao.convertFormEntityToFormResponseBean(formEntity).getContent());
|
|
return formApplicationResponse;
|
|
}
|
|
|
|
public ApplicationResponse createApplicationByCallId(CompanyEntity companyEntity,
|
|
ApplicationRequest applicationRequest, Long callId, UserEntity userEntity) {
|
|
CallEntity call = callService.validateCall(callId);
|
|
UserWithCompanyEntity userWithCompanyEntity=companyService.getUserWithCompany(userEntity.getId(),companyEntity.getId());
|
|
checkCallEndDate(call);
|
|
// call = callService.validatePublishedCall(call.getId());
|
|
// checkIfApplicationExists(call, userWithCompanyEntity, userEntity);
|
|
HubEntity hubEntity = hubService.valdateHub(call.getHub().getId());
|
|
if(hubEntity.getUniqueUuid().equals(defaultHubUuid)){
|
|
checkIfApplicationExists(call, userWithCompanyEntity, userEntity);
|
|
}
|
|
ApplicationEntity applicationEntity = createApplicationEntity(userEntity, call, userWithCompanyEntity);
|
|
applicationEntity.setComments(applicationRequest.getComments());
|
|
applicationEntity = saveApplicationEntity(applicationEntity);
|
|
return getApplicationResponse(applicationEntity);
|
|
}
|
|
// public void checkIfApplicationExists(CallEntity call, UserWithCompanyEntity userWithCompanyEntity, UserEntity userEntity){
|
|
// Optional<ApplicationEntity> applicationEntity=applicationRepository.findByUserIdAndUserWithCompanyIdAndCallIdAndIsDeletedFalse(userEntity.getId(), userWithCompanyEntity.getId(),call.getId());
|
|
// if(applicationEntity.isPresent()){
|
|
// throw new CustomValidationException(Status.BAD_REQUEST,Translator.toLocale(GepafinConstant.APPLICATION_ALREADY_EXISTS));
|
|
// }
|
|
// }
|
|
|
|
public void checkIfApplicationExists(CallEntity call, UserWithCompanyEntity userWithCompanyEntity, UserEntity userEntity){
|
|
|
|
List<ApplicationEntity> applications = applicationRepository.findByUserIdAndUserWithCompany_IdAndCall_IdAndIsDeletedFalseAndStatusNot(
|
|
userEntity.getId(), userWithCompanyEntity.getId(), call.getId(), ApplicationStatusTypeEnum.REJECTED.name()
|
|
);
|
|
|
|
if (!applications.isEmpty()) {
|
|
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.APPLICATION_ALREADY_EXISTS));
|
|
}
|
|
}
|
|
|
|
|
|
public ApplicationResponse updateApplicationStatus(HttpServletRequest request, Long applicationId, ApplicationStatusTypeEnum status) {
|
|
|
|
log.info("Updating status for Application id : " + applicationId);
|
|
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
|
checkCallEndDate(applicationEntity.getCall());
|
|
log.info("Call end date verified successfully | callId: {}", applicationEntity.getCall().getId());
|
|
//cloned entity for old application data
|
|
ApplicationEntity oldApplicationEntity = Utils.getClonedEntityForData(applicationEntity);
|
|
|
|
UserEntity userEntity = userService.validateUser(applicationEntity.getUserId());
|
|
validator.validateUserWithCompany(request, applicationEntity.getCompanyId());
|
|
if (ApplicationStatusTypeEnum.SUBMIT.getValue().equals(applicationEntity.getStatus())) {
|
|
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.APPLICATION_SUBMITTED_CANNOT_CHANGE));
|
|
}
|
|
|
|
if (Boolean.TRUE.equals(applicationEntity.getStatus().equals(status.getValue()))) {
|
|
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.APPLICATION_ALREADY_IN_PREVIOUS_STATUS));
|
|
}
|
|
if (status.equals(ApplicationStatusTypeEnum.SUBMIT) && Boolean.TRUE.equals(applicationEntity.getStatus().equals(ApplicationStatusTypeEnum.READY.getValue()))) {
|
|
// callService.validatePublishedCall(applicationEntity.getCall().getId(), userEntity.getHub().getId());
|
|
Long protocolNumber = protocolDao.getProtocolNumber(userEntity.getHub());
|
|
ProtocolEntity protocolEntity = protocolDao.createProtocolEntity(applicationEntity, protocolNumber, userEntity.getHub().getId(),true);
|
|
applicationEntity.setProtocol(protocolEntity);
|
|
applicationEntity.setStatus(ApplicationStatusTypeEnum.SUBMIT.getValue());
|
|
applicationEntity.setSubmissionDate(protocolEntity.getCreatedDate());
|
|
applicationEntity = applicationRepository.save(applicationEntity);
|
|
Map<String ,String> placeHolders=notificationDao.sendNotificationToBeneficiary(applicationEntity,NotificationTypeEnum.APPLICATION_SUBMISSION);
|
|
notificationDao.sendNotificationToSuperUser(applicationEntity,placeHolders,NotificationTypeEnum.APPLICATION_SUBMISSION);
|
|
|
|
/** This code is responsible for adding a version history log for "Update application status" operation. **/
|
|
loggingUtil.addVersionHistory(
|
|
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplicationEntity).newData(applicationEntity).build());
|
|
|
|
sendMailToUserAndCompany(userEntity, applicationEntity);
|
|
sendMailTodefaultSystemAndGepafin(userEntity, applicationEntity);
|
|
applicationEntity.setStatus(status.getValue());
|
|
log.info("Status updated to SUBMIT for applicationId: " + applicationId);
|
|
}
|
|
if (status.equals(ApplicationStatusTypeEnum.DRAFT) && Boolean.TRUE.equals(applicationEntity.getStatus().equals(ApplicationStatusTypeEnum.AWAITING.getValue()))) {
|
|
applicationEntity.setStatus(status.getValue());
|
|
log.info("Status updated to DRAFT for applicationId: " + applicationId);
|
|
}
|
|
if (status.equals(ApplicationStatusTypeEnum.AWAITING) && Boolean.TRUE.equals(applicationEntity.getStatus().equals(ApplicationStatusTypeEnum.READY.getValue()))) {
|
|
ApplicationSignedDocumentEntity applicationSignedDocument = applicationSignedDocumentRepository.findByApplicationIdAndStatus(applicationId,
|
|
ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
|
deleteSignedDocumentFromS3(applicationSignedDocument);
|
|
applicationEntity.setStatus(status.getValue());
|
|
log.info("Status updated to AWAITING for applicationId: " + applicationId);
|
|
}
|
|
applicationEntity = applicationRepository.save(applicationEntity);
|
|
log.info("Application status updated successfully | applicationId: {}, newStatus: {}", applicationId, applicationEntity.getStatus());
|
|
|
|
if (!status.equals(ApplicationStatusTypeEnum.SUBMIT)) {
|
|
/** This code is responsible for adding a version history log for "Update application status" operation. **/
|
|
loggingUtil.addVersionHistory(
|
|
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplicationEntity).newData(applicationEntity).build());
|
|
}
|
|
return getApplicationResponse(applicationEntity);
|
|
}
|
|
|
|
public Integer calculateProgress(Long totalSteps, Long completedSteps) {
|
|
if (FieldValidator.isNullOrZero(totalSteps)) {
|
|
throw new CustomValidationException(Status.BAD_REQUEST,Translator.toLocale(GepafinConstant.TOTAL_STEPS_NOT_BE_ZERO));
|
|
}
|
|
|
|
if (completedSteps < 0 || completedSteps > totalSteps) {
|
|
throw new CustomValidationException(Status.BAD_REQUEST,Translator.toLocale(GepafinConstant.COMPLETED_STEPS_NOT_VALID));
|
|
}
|
|
|
|
double progress = ((double) completedSteps / totalSteps) * 100;
|
|
return (int) Math.round(progress);
|
|
}
|
|
public void validateFormFields(ApplicationRequestBean request, FormEntity formEntity) {
|
|
|
|
// List<ContentResponseBean> contentResponseBeans=Utils.convertJsonStringToList(formEntity.getContent(),ContentResponseBean.class);
|
|
List<ContentResponseBean> contentResponseBeans=formDao.convertFormEntityToFormResponseBean(formEntity).getContent();
|
|
|
|
List<ApplicationFormFieldRequestBean> requestFields = request.getFormFields();
|
|
|
|
Map<String, String> contentMap = contentResponseBeans.stream()
|
|
.collect(Collectors.toMap(ContentResponseBean::getId, ContentResponseBean::getLabel)); // Change getLabel() if needed
|
|
FieldValidator validator = FieldValidator.create();
|
|
for (ApplicationFormFieldRequestBean requestField : requestFields) {
|
|
String fieldId = requestField.getFieldId();
|
|
|
|
if (!contentMap.containsKey(fieldId)) {
|
|
validator.addError(MessageFormat.format(Translator.toLocale(GepafinConstant.FIELD_ID_NOT_FOUND), fieldId));
|
|
}
|
|
|
|
}
|
|
validator.validate();
|
|
}
|
|
|
|
public void validateRequiredFields(FormEntity formEntity, ApplicationEntity applicationEntity, String fieldId) {
|
|
FlowDataEntity flowDataEntity = flowDataRepository.findByFormIdAndCallId(
|
|
formEntity.getId(), applicationEntity.getCall().getId());
|
|
|
|
if (flowDataEntity == null) {
|
|
return;
|
|
}
|
|
|
|
ApplicationFormFieldEntity applicationFormFieldEntity = applicationFormFieldRepository
|
|
.findByFieldIdAndApplicationFormFormIdAndApplicationFormApplicationId(
|
|
flowDataEntity.getChoosenField(), formEntity.getId(), applicationEntity.getId())
|
|
.orElse(null);
|
|
|
|
if (applicationFormFieldEntity == null || !fieldId.equals(applicationFormFieldEntity.getFieldId())) {
|
|
return;
|
|
}
|
|
List<Long> nextFormIds = flowEdgesRepository.findBySourceIdAndCallId(
|
|
formEntity.getId(), applicationEntity.getCall().getId())
|
|
.stream()
|
|
.map(FlowEdgesEntity::getTargetId)
|
|
.collect(Collectors.toList());
|
|
|
|
Optional<Long> nextFormIdOptional = flowDataRepository.findByChoosenValueAndFormIdIn(
|
|
applicationFormFieldEntity.getFieldValue(), nextFormIds)
|
|
.map(FlowDataEntity::getFormId);
|
|
|
|
if (nextFormIdOptional.isPresent()) {
|
|
Long nextFormId = nextFormIdOptional.get();
|
|
|
|
FormEntity nextForm = formService.validateForm(nextFormId);
|
|
ApplicationFormEntity nextApplicationFormEntity = applicationFormRepository.findByApplicationIdAndFormId(
|
|
applicationEntity.getId(), nextForm.getId());
|
|
|
|
if (nextApplicationFormEntity != null) {
|
|
List<ApplicationFormFieldEntity> nextApplicationFormFieldEntities = applicationFormFieldRepository.findByApplicationFormId(nextApplicationFormEntity.getId());
|
|
|
|
nextApplicationFormFieldEntities.forEach(applicationFormFieldEntityToDelete ->
|
|
/** This code is responsible for adding a version history log for "Deleting application form field" operation. **/
|
|
loggingUtil.addVersionHistory(
|
|
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.DELETE).oldData(applicationFormFieldEntityToDelete).build()));
|
|
|
|
applicationFormFieldRepository.deleteAll(nextApplicationFormFieldEntities);
|
|
|
|
/** This code is responsible for adding a version history log for the "Deleting next application form" operation. **/
|
|
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.DELETE).oldData(nextApplicationFormEntity).build());
|
|
|
|
applicationFormRepository.delete(nextApplicationFormEntity);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void sendMailToUserAndCompany(UserEntity userEntity, ApplicationEntity applicationEntity) {
|
|
CallEntity call =applicationEntity.getCall();
|
|
CompanyEntity company=companyService.validateCompany(applicationEntity.getCompanyId());
|
|
UserWithCompanyEntity userWithCompany=companyService.getUserWithCompany(userEntity.getId(),company.getId());
|
|
ProtocolEntity protocol= applicationEntity.getProtocol();
|
|
HubEntity hub = hubService.valdateHub(applicationEntity.getHubId());
|
|
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService
|
|
.retrieveTemplateByTypeAndCall(SystemEmailTemplatesEntityTypeEnum.APPLICATION_SUBMISSION_TO_USER_AND_COMPANY,
|
|
hub, null);
|
|
|
|
// Create the map for subject placeholders
|
|
Map<String, String> subjectPlaceholders = new HashMap<>();
|
|
subjectPlaceholders.put("{{call_name}}", call.getName());
|
|
subjectPlaceholders.put("{{company_name}}", company.getCompanyName());
|
|
|
|
// Create the map for body placeholders
|
|
Map<String, String> bodyPlaceholders = new HashMap<>();
|
|
bodyPlaceholders.put("{{call_name}}", call.getName());
|
|
bodyPlaceholders.put("{{protocol_number}}", protocol.getProtocolNumber().toString());
|
|
bodyPlaceholders.put("{{date}}", DateTimeUtil.formatLocalDateTime(protocol.getCreatedDate(), GepafinConstant.DD_MM_YYYY));
|
|
bodyPlaceholders.put("{{time}}", DateTimeUtil.parseLocalTimeToString(protocol.getTime(), GepafinConstant.HH_MM_SS));
|
|
|
|
// Replace placeholders in the subject and body
|
|
String subject = Utils.replacePlaceholders(systemEmailTemplateResponse.getSubject(), subjectPlaceholders);
|
|
String body = Utils.replacePlaceholders(systemEmailTemplateResponse.getHtmlContent(), bodyPlaceholders);
|
|
EmailLogRequest emailLogRequest=emailLogDao.createEmailLogRequest(systemEmailTemplateResponse.getEmailScenario(),RecipientTypeEnum.USER,userEntity.getId(),userEntity.getEmail(),userEntity.getId(),applicationEntity.getId(),null,applicationEntity.getCall().getId());
|
|
String email = userEntity.getEmail();
|
|
if (userEntity.getBeneficiary() != null) {
|
|
emailLogRequest.setRecipientType(RecipientTypeEnum.BENEFICIARY);
|
|
email = userEntity.getBeneficiary().getEmail();
|
|
emailLogRequest.setUserId(userEntity.getBeneficiary().getId());
|
|
}
|
|
emailNotificationDao.sendMail(hub.getId(), subject, body, List.of(email),emailLogRequest);
|
|
List<String> recipientEmails = new ArrayList<>();
|
|
// recipientEmails.add(email);
|
|
String companyEmail = userWithCompany.getEmail();
|
|
String contactEmail = userWithCompany.getContactEmail();
|
|
|
|
if (companyEmail != null && !companyEmail.isEmpty()) {
|
|
recipientEmails.add(companyEmail);
|
|
}
|
|
|
|
if (contactEmail != null && !contactEmail.isEmpty() && !contactEmail.equals(companyEmail)) {
|
|
recipientEmails.add(contactEmail);
|
|
}
|
|
if(Boolean.FALSE.equals(recipientEmails.isEmpty())){
|
|
emailLogRequest.setRecipientId(applicationEntity.getCompanyId());
|
|
emailLogRequest.setRecipientType(RecipientTypeEnum.COMPANY);
|
|
emailLogRequest.setRecipientEmails(companyEmail);
|
|
}
|
|
emailNotificationDao.sendMail(hub.getId(), subject, body, recipientEmails,emailLogRequest);
|
|
}
|
|
private void sendMailTodefaultSystemAndGepafin(UserEntity userEntity, ApplicationEntity applicationEntity) {
|
|
CallEntity call = applicationEntity.getCall();
|
|
CompanyEntity company=companyService.validateCompany(applicationEntity.getCompanyId());
|
|
ProtocolEntity protocol = applicationEntity.getProtocol();
|
|
HubEntity hub = hubService.valdateHub(applicationEntity.getHubId());
|
|
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService
|
|
.retrieveTemplateByTypeAndCall(SystemEmailTemplatesEntityTypeEnum.APPLICATION_SUBMISSION_TO_GEPAFIN,
|
|
hub, null);
|
|
|
|
// Create the map for subject placeholders
|
|
Map<String, String> subjectPlaceholders = new HashMap<>();
|
|
subjectPlaceholders.put("{{call_name}}", call.getName());
|
|
subjectPlaceholders.put("{{company_name}}", company.getCompanyName());
|
|
|
|
// Create the map for body placeholders
|
|
Map<String, String> bodyPlaceholders = new HashMap<>();
|
|
bodyPlaceholders.put("{{call_name}}", call.getName());
|
|
bodyPlaceholders.put("{{protocol_number}}", protocol.getProtocolNumber().toString());
|
|
bodyPlaceholders.put("{{date}}", DateTimeUtil.formatLocalDateTime(protocol.getCreatedDate(), GepafinConstant.DD_MM_YYYY));
|
|
bodyPlaceholders.put("{{time}}", DateTimeUtil.parseLocalTimeToString(protocol.getTime(), GepafinConstant.HH_MM_SS));
|
|
|
|
// Replace placeholders in the subject and body
|
|
String subject = Utils.replacePlaceholders(systemEmailTemplateResponse.getSubject(), subjectPlaceholders);
|
|
String body = Utils.replacePlaceholders(systemEmailTemplateResponse.getHtmlContent(), bodyPlaceholders);
|
|
|
|
EmailLogRequest emailLogRequest=emailLogDao.createEmailLogRequest(systemEmailTemplateResponse.getEmailScenario(),RecipientTypeEnum.PROPERTIES,null,userEntity.getEmail(),userEntity.getId(),applicationEntity.getId(),null,applicationEntity.getCall().getId());
|
|
|
|
// mailUtil.sendByMailGun(subject, body, List.of(defaultSystemReceiverEmail), null);
|
|
// mailUtil.sendByMailGun(subject, body, List.of(gepafinEmail), null);
|
|
// mailUtil.sendByMailGun(subject, body, List.of(rinaldoEmail), null);
|
|
// if(Boolean.TRUE.equals(hub.getUniqueUuid().equals(defaultHubUuid))) {
|
|
// if (validator.isProductionProfileActivated()) {
|
|
// emailLogRequest.setRecipientEmails(carloEmail);
|
|
//// mailUtil.sendByMailGun(subject, body, List.of(carloEmail), null);
|
|
// emailNotificationDao.sendMail(hub.getId(), subject, body, List.of(carloEmail),emailLogRequest);
|
|
// }
|
|
// List<String> listDefaultSystemReceiverEmail = Arrays.stream(defaultSystemReceiverEmail.split(","))
|
|
// .map(String::trim)
|
|
// .filter(email -> !email.isEmpty())
|
|
// .toList();
|
|
//
|
|
// emailLogRequest.setRecipientEmails(defaultSystemReceiverEmail);
|
|
// emailNotificationDao.sendMail(hub.getId(), subject, body, listDefaultSystemReceiverEmail, emailLogRequest);
|
|
// }
|
|
|
|
List<String> hubEmails = Arrays.stream(hub.getEmail().split(","))
|
|
.map(String::trim)
|
|
.filter(email -> !email.isEmpty())
|
|
.toList();
|
|
|
|
emailLogRequest.setRecipientEmails(hub.getEmail());
|
|
emailNotificationDao.sendMail(hub.getId(), subject, body,hubEmails,emailLogRequest);
|
|
emailLogRequest.setRecipientEmails(rinaldoEmail);
|
|
emailNotificationDao.sendMail(hub.getId(), subject, body, List.of(rinaldoEmail),emailLogRequest);
|
|
}
|
|
public ApplicationSignedDocumentResponse uploadSignedDocument(HttpServletRequest request, Long applicationId,
|
|
MultipartFile file) {
|
|
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
|
checkCallEndDate(applicationEntity.getCall());
|
|
//cloned entity for old data
|
|
ApplicationEntity oldApplicationData = Utils.getClonedEntityForData(applicationEntity);
|
|
|
|
validateFileTypeForCall(file, applicationEntity);
|
|
ApplicationSignedDocumentEntity applicationSignedDocument = applicationSignedDocumentRepository.findByApplicationIdAndStatus(applicationId,
|
|
ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
|
//cloned entity for old data
|
|
ApplicationSignedDocumentEntity oldApplicationSingedDocumentData = Utils.getClonedEntityForData(applicationSignedDocument);
|
|
|
|
if (applicationSignedDocument != null) {
|
|
deleteSignedDocumentFromS3(applicationSignedDocument);
|
|
}
|
|
UploadFileOnAmazonS3Response uploadFileOnAmazonS3 = uploadFileOnAmazonS3ForUserSignedDocument(file, applicationEntity.getCall().getId(), applicationId);
|
|
applicationSignedDocument = new ApplicationSignedDocumentEntity();
|
|
applicationSignedDocument.setApplication(applicationEntity);
|
|
applicationSignedDocument.setFileName(uploadFileOnAmazonS3.getFileName());
|
|
applicationSignedDocument.setFilePath(uploadFileOnAmazonS3.getFilePath());
|
|
applicationSignedDocument.setStatus(ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
|
applicationSignedDocument = applicationSignedDocumentRepository.save(applicationSignedDocument);
|
|
|
|
/** This code is responsible for adding a version history log for the "assign application document" operation. **/
|
|
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.INSERT).oldData(oldApplicationSingedDocumentData)
|
|
.newData(applicationSignedDocument).build());
|
|
|
|
applicationEntity.setStatus(ApplicationStatusTypeEnum.READY.getValue());
|
|
applicationEntity = applicationRepository.save(applicationEntity);
|
|
|
|
/** This code is responsible for adding a version history log for the "Create Call" operation. **/
|
|
loggingUtil.addVersionHistory(
|
|
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplicationData).newData(applicationEntity).build());
|
|
|
|
return convertApplicationSignedDocumentToApplicationSignedDocumentResponse(applicationSignedDocument);
|
|
}
|
|
public void deleteSignedDocumentFromS3(ApplicationSignedDocumentEntity applicationSignedDocumentEntity){
|
|
ApplicationSignedDocumentEntity oldApplicationSignedDocument = Utils.getClonedEntityForData(applicationSignedDocumentEntity);
|
|
String oldS3Path = applicationSignedDocumentEntity.getFilePath();
|
|
String newS3Path = s3ConfigBean.generateDocumentPathForOther(DocOtherSourceTypeEnum.DELETED_USER_SIGNED_DOCUMENT,applicationSignedDocumentEntity.getApplication().getCall().getId(),applicationSignedDocumentEntity.getApplication().getId(),0L);
|
|
|
|
UploadFileOnAmazonS3Response response = amazonS3Service.moveFile(applicationSignedDocumentEntity.getFileName(), oldS3Path, newS3Path);
|
|
applicationSignedDocumentEntity.setStatus(ApplicationSignedDocumentStatusEnum.INACTIVE.getValue());
|
|
applicationSignedDocumentEntity.setFileName(response.getFileName());
|
|
applicationSignedDocumentEntity.setFilePath(response.getFilePath());
|
|
|
|
applicationSignedDocumentRepository.save(applicationSignedDocumentEntity);
|
|
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.SOFT_DELETE).oldData(oldApplicationSignedDocument).newData(applicationSignedDocumentEntity).build());
|
|
}
|
|
|
|
private void validateFileTypeForCall(MultipartFile file, ApplicationEntity applicationEntity) {
|
|
List<String> validCallIds = Arrays.asList(callId.split(","));
|
|
if (applicationEntity != null && validCallIds.contains(applicationEntity.getCall().getId().toString())) {
|
|
return;
|
|
}
|
|
validateFileType(file);
|
|
}
|
|
|
|
|
|
private UploadFileOnAmazonS3Response uploadFileOnAmazonS3ForUserSignedDocument(MultipartFile file, Long callId, Long applicationId) {
|
|
try {
|
|
String s3Path = generateS3PathForDelegation(callId, applicationId);
|
|
log.info("S3 Path {}", s3Path);
|
|
return amazonS3Service.uploadFileOnAmazonS3(s3Path, file);
|
|
} catch (Exception e) {
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.UPLOAD_ERROR_S3));
|
|
}
|
|
}
|
|
private String generateS3PathForDelegation(Long callId, Long applicationId) {
|
|
try {
|
|
return s3ConfigBean.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, callId, applicationId,0L);
|
|
} catch (IllegalArgumentException e) {
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.S3_PATH_GENERATION_ERROR_MSG));
|
|
}
|
|
}
|
|
private ApplicationSignedDocumentResponse convertApplicationSignedDocumentToApplicationSignedDocumentResponse(
|
|
ApplicationSignedDocumentEntity applicationSignedDocument) {
|
|
ApplicationSignedDocumentResponse applicationSignedDocumentResponse = new ApplicationSignedDocumentResponse();
|
|
applicationSignedDocumentResponse.setId(applicationSignedDocument.getId());
|
|
applicationSignedDocumentResponse.setApplicationId(applicationSignedDocument.getApplication().getId());
|
|
applicationSignedDocumentResponse.setFileName(applicationSignedDocument.getFileName());
|
|
applicationSignedDocumentResponse.setFilePath(applicationSignedDocument.getFilePath());
|
|
applicationSignedDocumentResponse
|
|
.setStatus(ApplicationSignedDocumentStatusEnum.valueOf(applicationSignedDocument.getStatus()));
|
|
applicationSignedDocumentResponse.setCreatedDate(applicationSignedDocument.getCreatedDate());
|
|
applicationSignedDocumentResponse.setUpdatedDate(applicationSignedDocument.getUpdatedDate());
|
|
return applicationSignedDocumentResponse;
|
|
}
|
|
|
|
private void validateFileType(MultipartFile file) {
|
|
if (file.isEmpty()) {
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
|
Translator.toLocale(GepafinConstant.VALIDATION_ERROR_FILE_EMPTY));
|
|
}
|
|
String filename = file.getOriginalFilename();
|
|
if (filename == null || !filename.endsWith(".p7m")) {
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
|
Translator.toLocale(GepafinConstant.VALIDATION_ERROR_FILE_INVALIDTYPE));
|
|
}
|
|
}
|
|
|
|
public ApplicationSignedDocumentResponse getSignedDocument(HttpServletRequest request, Long applicationId) {
|
|
|
|
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
|
// validator.validateUserWithCompany(request, applicationEntity.getCompanyId());
|
|
|
|
if (validator.checkIsPreInstructor()) {
|
|
ApplicationEvaluationEntity applicationEvaluationEntity = applicationEvaluationService.validateApplicationEvaluationByApplicationId(applicationId);
|
|
validator.validatePreInstructor(request, applicationEvaluationEntity.getUserId());
|
|
} else {
|
|
validator.validateUserId(request, applicationEntity.getUserId());
|
|
}
|
|
|
|
ApplicationSignedDocumentEntity applicationSignedDocument = applicationSignedDocumentRepository
|
|
.findByApplicationIdAndStatus(applicationId, ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
|
if(applicationSignedDocument == null) {
|
|
throw new ResourceNotFoundException(Status.NOT_FOUND,
|
|
Translator.toLocale(GepafinConstant.APPLICATION_SIGNED_DOCUMENT_NOT_FOUND));
|
|
}
|
|
return convertApplicationSignedDocumentToApplicationSignedDocumentResponse(applicationSignedDocument);
|
|
}
|
|
|
|
public void deleteSignedDocument(HttpServletRequest request, Long applicationId) {
|
|
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
|
validator.validateUserWithCompany(request, applicationEntity.getCompanyId());
|
|
|
|
ApplicationSignedDocumentEntity applicationSignedDocument = applicationSignedDocumentRepository
|
|
.findByApplicationIdAndStatus(applicationId, ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
|
//cloned entity for old data
|
|
ApplicationSignedDocumentEntity oldApplicationSignedDocument = Utils.getClonedEntityForData(applicationSignedDocument);
|
|
if(applicationSignedDocument == null) {
|
|
throw new ResourceNotFoundException(Status.NOT_FOUND,
|
|
Translator.toLocale(GepafinConstant.APPLICATION_SIGNED_DOCUMENT_NOT_FOUND));
|
|
}
|
|
applicationSignedDocument.setStatus(ApplicationSignedDocumentStatusEnum.INACTIVE.getValue());
|
|
applicationSignedDocument = applicationSignedDocumentRepository.save(applicationSignedDocument);
|
|
|
|
loggingUtil.addVersionHistory(
|
|
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplicationSignedDocument).newData(applicationSignedDocument)
|
|
.build());
|
|
}
|
|
|
|
public ApplicationResponse validateApplication(HttpServletRequest request, Long applicationId) {
|
|
|
|
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
|
ApplicationEntity oldApplicationEntity = Utils.getClonedEntityForData(applicationEntity);
|
|
checkCallEndDate(applicationEntity.getCall());
|
|
|
|
UserEntity userEntity = userService.validateUser(applicationEntity.getUserId());
|
|
validator.validateUserWithCompany(request, applicationEntity.getCompanyId());
|
|
if (Boolean.FALSE.equals(ApplicationStatusTypeEnum.DRAFT.getValue().equals(applicationEntity.getStatus()))) {
|
|
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.APPLICATION_NOT_IN_DRAFT_STATUS));
|
|
}
|
|
if (applicationEntity.getAmountRequested() == null || applicationEntity.getAmountRequested().compareTo(BigDecimal.ZERO) <= 0 ) {
|
|
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.AMOUNT_REQUEST_SHOULD_GREATED_THEN_ZERO));
|
|
}
|
|
List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findByCallId(applicationEntity.getCall().getId());
|
|
Long totalSteps = flowFormDao.calculateTotalSteps(flowEdgesList);
|
|
Integer completedSteps = flowFormDao.getCompletedSteps(applicationEntity, true);
|
|
if (totalSteps.intValue() != completedSteps) {
|
|
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.APPLICATION_IS_INCOMPLETE_MSG));
|
|
}
|
|
|
|
applicationEntity.setStatus(ApplicationStatusTypeEnum.AWAITING.getValue());
|
|
applicationEntity = applicationRepository.save(applicationEntity);
|
|
|
|
/** This code is responsible for adding a version history log for "Update application status or other details" operation. **/
|
|
loggingUtil.addVersionHistory(
|
|
VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplicationEntity).newData(applicationEntity).build());
|
|
|
|
return getApplicationResponse(applicationEntity);
|
|
}
|
|
|
|
public byte[] downloadApplicationDocumentsAsZip(HttpServletRequest request, Long applicationId) {
|
|
|
|
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
|
validateAssignedUser(request, applicationId);
|
|
Set<Long> documentIds = extractDocumentIdsFromApplicationForms(applicationId);
|
|
List<DocumentEntity> documents = documentRepository.findAllByIdInAndIsDeletedFalse(documentIds);
|
|
ApplicationSignedDocumentEntity signedDocument = applicationSignedDocumentRepository.findByApplicationIdAndStatus(applicationId,
|
|
ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
|
List<DocumentEntity> amendmentDocuments = fetchAmendmentDocuments(applicationId);
|
|
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));
|
|
}
|
|
return createZipWithDocuments(applicationEntity, documents, signedDocument, amendmentDocuments, evaluationDocuments, applicationId);
|
|
}
|
|
|
|
private void validateAssignedUser(HttpServletRequest request, Long applicationId) {
|
|
|
|
AssignedApplicationsEntity assignedApplications = assignedApplicationsRepository.findByApplicationIdAndIsDeletedFalse(applicationId).orElse(null);
|
|
if (assignedApplications != null) {
|
|
validator.validatePreInstructor(request, assignedApplications.getUserId());
|
|
}
|
|
}
|
|
|
|
private Set<Long> extractDocumentIdsFromApplicationForms(Long applicationId) {
|
|
|
|
Set<Long> documentIds = new HashSet<>();
|
|
List<ApplicationFormEntity> applicationForms = applicationFormRepository.findByApplicationId(applicationId);
|
|
applicationForms.forEach(applicationForm -> {
|
|
FormEntity formEntity = applicationForm.getForm();
|
|
if (formEntity != null) {
|
|
List<ContentResponseBean> contentResponseBeans = formDao.convertFormEntityToFormResponseBean(formEntity).getContent();
|
|
contentResponseBeans.stream().filter(content -> "fileupload".equals(content.getName()) || "fileselect".equals(content.getName())).forEach(content -> {
|
|
Optional<ApplicationFormFieldEntity> 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 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)) {
|
|
zos.putNextEntry(new ZipEntry(fullPath));
|
|
IOUtils.copy(fileInputStream, zos);
|
|
zos.closeEntry();
|
|
} catch (IOException 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) {
|
|
|
|
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, appS3Folder, document.getFilePath(), fileName);
|
|
}
|
|
// Add Signed Document
|
|
if (signedDocument != null) {
|
|
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);
|
|
}
|
|
}
|
|
|
|
public PageableResponseBean<List<ApplicationResponse>> getAllApplicationByPagination(UserEntity userEntity, Long callId, Long companyId, ApplicationPageableRequestBean applicationPageableRequestBean) {
|
|
Integer pageNo = null;
|
|
Integer pageLimit = null;
|
|
|
|
UserWithCompanyEntity userWithCompany= userWithCompanyRepository.findByUserIdAndCompanyIdAndIsDeletedFalse(userEntity.getId(), companyId).orElse(null);
|
|
Long userWithCompanyId = userWithCompany!=null?userWithCompany.getId():null;
|
|
if (applicationPageableRequestBean.getGlobalFilters() != null) {
|
|
pageNo = applicationPageableRequestBean.getGlobalFilters().getPage();
|
|
pageLimit = applicationPageableRequestBean.getGlobalFilters().getLimit();
|
|
}
|
|
if (pageLimit == null || pageLimit <= 0) {
|
|
pageLimit = GepafinConstant.DEFAULT_PAGE_LIMIT;
|
|
}
|
|
if (pageNo == null || pageNo <= 0) {
|
|
pageNo = GepafinConstant.DEFAULT_PAGE;
|
|
}
|
|
Specification<ApplicationEntity> spec = search(callId,companyId, userWithCompanyId, applicationPageableRequestBean, userEntity);
|
|
Page<ApplicationEntity> entityPage = applicationRepository.findAll(spec, PageRequest.of(pageNo - 1, pageLimit));
|
|
// Prepare the response
|
|
|
|
|
|
List<ApplicationResponse> applicationResponses = entityPage.getContent().stream()
|
|
.map(application -> {
|
|
ApplicationResponse response = getApplicationResponse(application);
|
|
return response;
|
|
})
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
PageableResponseBean<List<ApplicationResponse>> pageableResponseBean = new PageableResponseBean<>();
|
|
pageableResponseBean.setBody(applicationResponses);
|
|
pageableResponseBean.setCurrentPage(entityPage.getNumber() + 1); // Page numbers typically start from 0, so add 1 for user-friendly indexing
|
|
pageableResponseBean.setTotalPages(entityPage.getTotalPages());
|
|
pageableResponseBean.setTotalRecords(entityPage.getTotalElements());
|
|
pageableResponseBean.setPageSize(entityPage.getSize());
|
|
|
|
return pageableResponseBean;
|
|
}
|
|
|
|
public Specification<ApplicationEntity> search(Long callId, Long companyId, Long userWithCompanyId, ApplicationPageableRequestBean applicationPageableRequestBean, UserEntity userEntity) {
|
|
return (root, query, criteriaBuilder) -> {
|
|
|
|
List<Predicate> predicates = getPredicates(applicationPageableRequestBean, criteriaBuilder, root, callId,companyId, userWithCompanyId, userEntity);
|
|
SortBy sortBy = new SortBy(GepafinConstant.CREATED_DATE, true);
|
|
|
|
if (applicationPageableRequestBean.getGlobalFilters() != null
|
|
&& applicationPageableRequestBean.getGlobalFilters().getSortBy() != null &&
|
|
applicationPageableRequestBean.getGlobalFilters().getSortBy().getColumnName() != null && Boolean.FALSE.equals(
|
|
isEmpty(applicationPageableRequestBean.getGlobalFilters().getSortBy().getColumnName()))) {
|
|
sortBy.setColumnName(applicationPageableRequestBean.getGlobalFilters().getSortBy().getColumnName());
|
|
sortBy.setSortDesc(true);
|
|
if (applicationPageableRequestBean.getGlobalFilters().getSortBy().getSortDesc() != null) {
|
|
sortBy.setSortDesc(applicationPageableRequestBean.getGlobalFilters().getSortBy().getSortDesc());
|
|
}
|
|
}
|
|
|
|
query.orderBy(criteriaBuilder.desc(root.get(sortBy.getColumnName())));
|
|
if (Boolean.FALSE.equals(sortBy.getSortDesc())) {
|
|
query.orderBy(criteriaBuilder.asc(root.get(sortBy.getColumnName())));
|
|
}
|
|
return query.where(criteriaBuilder.and(predicates.toArray(new Predicate[0]))).getRestriction();
|
|
};
|
|
}
|
|
|
|
|
|
private List<Predicate> getPredicates(ApplicationPageableRequestBean applicationPageableRequestBean,
|
|
CriteriaBuilder criteriaBuilder, Root<ApplicationEntity> root, Long callId,Long companyId, Long userWithCompanyId, UserEntity userEntity) {
|
|
|
|
Integer year = null;
|
|
String search = null;
|
|
Integer daysRange = null;
|
|
Map<String, FilterCriteria> filters = new HashMap<>();
|
|
if (applicationPageableRequestBean.getGlobalFilters() != null) {
|
|
year = applicationPageableRequestBean.getGlobalFilters().getYear();
|
|
search = applicationPageableRequestBean.getGlobalFilters().getSearch();
|
|
daysRange = applicationPageableRequestBean.getDaysRange();
|
|
}
|
|
if (applicationPageableRequestBean.getFilters() != null) {
|
|
filters = applicationPageableRequestBean.getFilters();
|
|
}
|
|
List<Predicate> predicates = new ArrayList<>();
|
|
|
|
// Boolean isBeneficiary = validator.checkIsBeneficiary();
|
|
if (Boolean.TRUE.equals(validator.checkIsBeneficiary()) || Boolean.TRUE.equals(validator.checkIsConfidi())) {
|
|
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.USER_ID), userEntity.getId()));
|
|
}
|
|
if (year != null && year > 0) {
|
|
int filterYear = applicationPageableRequestBean.getGlobalFilters().getYear();
|
|
|
|
// Create LocalDateTime boundaries for the start and end of the year
|
|
LocalDateTime startOfYear = LocalDateTime.of(filterYear, 1, 1, 0, 0);
|
|
LocalDateTime endOfYear = LocalDateTime.of(filterYear, 12, 31, 23, 59, 59);
|
|
|
|
// Add the range comparison to filter records within the year
|
|
predicates.add(criteriaBuilder.between(root.get(GepafinConstant.CREATED_DATE), startOfYear, endOfYear));
|
|
|
|
}
|
|
// Search in `title` and `message` (if search term is provided)
|
|
if (search != null && !search.isEmpty()) {
|
|
Predicate titlePredicate = criteriaBuilder.like(
|
|
criteriaBuilder.upper(root.get(GepafinConstant.COMMENTS)),
|
|
"%" + search.toUpperCase() + "%"
|
|
);
|
|
// Predicate protocolPredicate = criteriaBuilder.like(
|
|
// criteriaBuilder.function(
|
|
// "TO_CHAR",
|
|
// String.class,
|
|
// criteriaBuilder.function("CAST", String.class, root.get(GepafinConstant.PROTOCOL).get(GepafinConstant.PROTOCOL_NUMBER))
|
|
// ),
|
|
// "%" + search + "%"
|
|
// );
|
|
|
|
Predicate callNamePredicate =criteriaBuilder.like(
|
|
criteriaBuilder.upper(root.get(GepafinConstant.CALL).get(GepafinConstant.NAME)), // Adjust field name
|
|
"%" + search.toUpperCase() + "%"
|
|
);
|
|
|
|
// predicates.add(criteriaBuilder.or(protocolPredicate));
|
|
predicates.add(criteriaBuilder.or(callNamePredicate));
|
|
predicates.add(criteriaBuilder.or(titlePredicate));
|
|
}
|
|
|
|
// Filter by `status` (if status list is provided)
|
|
if (applicationPageableRequestBean.getStatus() != null && !applicationPageableRequestBean.getStatus().isEmpty()) {
|
|
List<String> statusValues = applicationPageableRequestBean.getStatus().stream()
|
|
.map(ApplicationStatusTypeEnum::name) // Convert enum to string
|
|
.toList();
|
|
predicates.add(root.get(GepafinConstant.STATUS).in(statusValues));
|
|
}
|
|
|
|
if (callId != null) {
|
|
CallEntity call = callService.validateCall(callId);
|
|
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.CALL).get(GepafinConstant.ID), callId));
|
|
}
|
|
|
|
// Optional companyId filter
|
|
if (userWithCompanyId != null) {
|
|
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.USER_WITH_COMPANY).get(GepafinConstant.ID), userWithCompanyId));
|
|
}
|
|
if (companyId != null) {
|
|
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.COMPANY_ID), companyId));
|
|
}
|
|
|
|
if (daysRange != null && daysRange >= 0) {
|
|
LocalDateTime today = LocalDateTime.now();
|
|
LocalDateTime pastDate = today.minusDays(daysRange);
|
|
predicates.add(criteriaBuilder.between(root.get(GepafinConstant.CREATED_DATE), pastDate, today));
|
|
}
|
|
applyFilters(root, criteriaBuilder, predicates, filters);
|
|
|
|
predicates.add(criteriaBuilder.isFalse(root.get(GepafinConstant.IS_DELETED)));
|
|
|
|
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.HUB_ID), userEntity.getHub().getId()));
|
|
|
|
|
|
return predicates;
|
|
}
|
|
private void applyFilters(Root<?> root, CriteriaBuilder criteriaBuilder, List<Predicate> predicates, Map<String, FilterCriteria> filters) {
|
|
if (Boolean.FALSE.equals(filters.isEmpty())) {
|
|
for (Map.Entry<String, FilterCriteria> entry : filters.entrySet()) {
|
|
String fieldName = entry.getKey();
|
|
FilterCriteria filterCriteria = entry.getValue();
|
|
Object value = filterCriteria.getValue();
|
|
MatchModeEnum matchMode = filterCriteria.getMatchMode();
|
|
|
|
if (value != null && matchMode != null) {
|
|
Path<?> fieldPath = root.get(fieldName);
|
|
if (fieldPath != null) {
|
|
Utils.applyStringFilter(fieldPath, criteriaBuilder, predicates, value, matchMode);
|
|
Utils.applyNumberFilter(fieldPath, criteriaBuilder, predicates, value, matchMode);
|
|
Utils.applyDateFilter(fieldPath, criteriaBuilder, predicates, value, matchMode,root);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
// private Path<?> getFieldPath(Root<?> root, String fieldName) {
|
|
// try {
|
|
// return switch (fieldName) {
|
|
//// case GepafinConstant.CALL_ID -> root.get(GepafinConstant.CALL).get(GepafinConstant.ID);
|
|
//// case GepafinConstant.CALL_TITLE -> root.get(GepafinConstant.CALL).get(GepafinConstant.NAME);
|
|
//// case GepafinConstant.CALL_END_DATE -> root.get(GepafinConstant.CALL).get(GepafinConstant.END_DATE);
|
|
//// case GepafinConstant.CALL_END_TIME -> root.get(GepafinConstant.CALL).get(GepafinConstant.END_TIME);
|
|
//// case GepafinConstant.MODIFIED_DATE -> root.get(GepafinConstant.CALL).get(GepafinConstant.UPDATED_DATE);
|
|
//// case GepafinConstant.PROTOCOL_NUMBER-> root.get(GepafinConstant.PROTOCOL).get(GepafinConstant.PROTOCOL_NUMBER);
|
|
// default -> root.get(fieldName);
|
|
// };
|
|
// } catch (IllegalArgumentException e) {
|
|
// return null;
|
|
// }
|
|
// }
|
|
|
|
|
|
public void checkCallEndDate(CallEntity call) {
|
|
|
|
callService.validatePublishedCall(call.getId(), call.getHub().getId());
|
|
// LocalDateTime now = DateTimeUtil.DateServerToUTC(LocalDateTime.now());
|
|
//
|
|
// LocalDateTime callEndDateTime = LocalDateTime.of(
|
|
// call.getEndDate().toLocalDate(),
|
|
// call.getEndTime()
|
|
// );
|
|
//
|
|
// if (now.isAfter(callEndDateTime)) {
|
|
// throw new CustomValidationException(
|
|
// Status.BAD_REQUEST,
|
|
// Translator.toLocale(GepafinConstant.CALL_EXPIRED)
|
|
// );
|
|
// }
|
|
}
|
|
|
|
public void calculationProcessForFormula(ApplicationFormEntity applicationFormEntity, List<ContentResponseBean> contentResponseBeans, ApplicationFormFieldRequestBean applicationFormFieldRequestBean,FieldValidator fieldValidator) {
|
|
List<String> formulaValue = new ArrayList<>();
|
|
String formulaValueOpt=null;
|
|
String label=null;
|
|
for (ContentResponseBean contentResponseBean:contentResponseBeans){
|
|
if(contentResponseBean.getId().equals(applicationFormFieldRequestBean.getFieldId())){
|
|
for (SettingResponseBean settingResponseBean:contentResponseBean.getSettings()){
|
|
if (settingResponseBean.getName().equals("label")){
|
|
label= String.valueOf(settingResponseBean.getValue());
|
|
}
|
|
if(settingResponseBean.getName().equals("formula")){
|
|
String value= (String) settingResponseBean.getValue();
|
|
formulaValueOpt=value;
|
|
formulaValue=Utils.extractValues(value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Map<String, String> mappedFormulaValue = new HashMap<>();
|
|
Object fieldValue = applicationFormFieldRequestBean.getFieldValue();
|
|
if (formulaValueOpt != null && fieldValue==null) {
|
|
fieldValue=0;
|
|
}
|
|
|
|
for (ContentResponseBean contentResponseBean : contentResponseBeans) {
|
|
String contentId = contentResponseBean.getId();
|
|
|
|
// Extract variable values once per contentResponseBean to avoid repeated stream operations
|
|
Set<String> variableValues = contentResponseBean.getSettings().stream()
|
|
.filter(setting -> "variable".equals(setting.getName()))
|
|
.flatMap(setting -> {
|
|
Object value = setting.getValue(); // Get the raw value
|
|
if (value instanceof String) {
|
|
return Stream.of((String) value); // Handle single String case
|
|
} else if (value instanceof List) {
|
|
return ((List<?>) value).stream()
|
|
.filter(item -> item instanceof String) // Ensure it's a String
|
|
.map(item -> (String) item); // Convert to String
|
|
} else {
|
|
return Stream.empty(); // Ignore unexpected types
|
|
}
|
|
})
|
|
.collect(Collectors.toSet()); // Collect into a Set for uniqueness
|
|
|
|
for (String formula : formulaValue) {
|
|
if (variableValues.contains(formula)) { // O(1) lookup instead of O(n)
|
|
mappedFormulaValue.put(formula, contentId);
|
|
}
|
|
}
|
|
}
|
|
Map<String, String> updatedMappedFormulaValue = new HashMap<>();
|
|
|
|
for (Map.Entry<String, String> entry : mappedFormulaValue.entrySet()) {
|
|
String variable = entry.getKey();
|
|
String contentId = entry.getValue();
|
|
|
|
// Repository call using contentId
|
|
Optional<ApplicationFormFieldEntity> optionalEntity = applicationFormFieldRepository.findByApplicationFormIdAndFieldId(applicationFormEntity.getId(),contentId);
|
|
// If entity is found, extract fieldValue and fieldId
|
|
optionalEntity.ifPresent(entity -> {
|
|
String entityFieldValue = entity.getFieldValue(); // Assuming getter method exists
|
|
String fieldId = entity.getFieldId(); // Assuming getter method exists
|
|
String tableType = contentResponseBeans.stream()
|
|
.filter(content -> content.getId().equals(fieldId)) // Match Content ID with fieldId
|
|
.flatMap(content -> content.getSettings().stream()) // Extract settings
|
|
.filter(setting -> "criteria_table_columns".equals(setting.getName())) // Match name
|
|
.map(setting -> setting.getName()) // Return the name of the setting
|
|
.findFirst() // Get the first match
|
|
.orElse(null); // Default to null if no match
|
|
|
|
if(tableType!=null){
|
|
JSONObject jsonObject = new JSONObject(entityFieldValue);
|
|
|
|
// Extract the value of total
|
|
entityFieldValue = jsonObject.getString("total");
|
|
|
|
}
|
|
|
|
updatedMappedFormulaValue.put(fieldId, entityFieldValue);
|
|
});
|
|
}
|
|
if(formulaValueOpt==null || formulaValueOpt.isEmpty()){
|
|
return;
|
|
}
|
|
double finalValue = evaluateFormula(formulaValueOpt, mappedFormulaValue, updatedMappedFormulaValue);
|
|
|
|
fieldValidator.formulaValidation(fieldValue, finalValue, label);
|
|
}
|
|
|
|
|
|
public static double evaluateFormula(String formula, Map<String, String> mappedFormulaValue, Map<String, String> updatedMappedFormulaValue) {
|
|
// Step 1: Extract all placeholders (variables) like {rest}, {another_var}, etc.
|
|
Pattern pattern = Pattern.compile("\\{(.*?)\\}");
|
|
Matcher matcher = pattern.matcher(formula);
|
|
List<String> variables = new ArrayList<>();
|
|
|
|
while (matcher.find()) {
|
|
variables.add(matcher.group(1)); // Extract variable names inside the curly braces
|
|
}
|
|
|
|
// Step 2: Replace placeholders with corresponding fieldValue
|
|
Map<String, Double> variableValues = new HashMap<>();
|
|
|
|
for (String variable : variables) {
|
|
String fieldId = mappedFormulaValue.get(variable);
|
|
if (fieldId != null && updatedMappedFormulaValue.containsKey(fieldId)) {
|
|
String fieldValueStr = updatedMappedFormulaValue.get(fieldId);
|
|
try {
|
|
double fieldValue = Double.parseDouble(fieldValueStr); // Assuming fieldValue is numeric
|
|
variableValues.put(variable, fieldValue);
|
|
} catch (NumberFormatException e) {
|
|
// Handle invalid number format gracefully (e.g., log an error or default to 0)
|
|
variableValues.put(variable, 0.0);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Step 3: Replace variables in the formula with their corresponding values
|
|
String expression = formula;
|
|
for (String variable : variables) {
|
|
Double value = variableValues.get(variable);
|
|
if (value != null) {
|
|
// Replace {variable} with its corresponding value in the formula
|
|
expression = expression.replace("{" + variable + "}", String.valueOf(value));
|
|
}
|
|
}
|
|
|
|
// Step 4: Evaluate the mathematical expression
|
|
return Utils.evaluateExpression(expression);
|
|
}
|
|
public ApplicationEntity validateApplicationWithCompany(Long id,Long companyId) {
|
|
ApplicationEntity application=applicationRepository.findByIdAndCompanyIdAndIsDeletedFalse(id,companyId);
|
|
if (application==null){
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.PERMISSION_DENIED));
|
|
}
|
|
return application;
|
|
}
|
|
public PageableResponseBean<List<ApplicationResponse>> getAllApplicationByPaginationByView(UserEntity userEntity, Long callId, Long companyId, ApplicationPageableRequestBean applicationPageableRequestBean) {
|
|
Integer pageNo = null;
|
|
Integer pageLimit = null;
|
|
|
|
UserWithCompanyEntity userWithCompany= userWithCompanyRepository.findByUserIdAndCompanyIdAndIsDeletedFalse(userEntity.getId(), companyId).orElse(null);
|
|
Long userWithCompanyId = userWithCompany!=null?userWithCompany.getId():null;
|
|
if (applicationPageableRequestBean.getGlobalFilters() != null) {
|
|
pageNo = applicationPageableRequestBean.getGlobalFilters().getPage();
|
|
pageLimit = applicationPageableRequestBean.getGlobalFilters().getLimit();
|
|
}
|
|
if (pageLimit == null || pageLimit <= 0) {
|
|
pageLimit = GepafinConstant.DEFAULT_PAGE_LIMIT;
|
|
}
|
|
if (pageNo == null || pageNo <= 0) {
|
|
pageNo = GepafinConstant.DEFAULT_PAGE;
|
|
}
|
|
Specification<ApplicationView> spec = searchByView(callId,companyId, userWithCompanyId, applicationPageableRequestBean, userEntity);
|
|
Page<ApplicationView> entityPage = applicationViewRepository.findAll(spec, PageRequest.of(pageNo - 1, pageLimit));
|
|
// Prepare the response
|
|
|
|
|
|
List<ApplicationResponse> applicationResponses = entityPage.getContent().stream()
|
|
.map(application -> {
|
|
ApplicationResponse response = getApplicationResponseByView(application);
|
|
return response;
|
|
})
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
PageableResponseBean<List<ApplicationResponse>> pageableResponseBean = new PageableResponseBean<>();
|
|
pageableResponseBean.setBody(applicationResponses);
|
|
pageableResponseBean.setCurrentPage(entityPage.getNumber() + 1); // Page numbers typically start from 0, so add 1 for user-friendly indexing
|
|
pageableResponseBean.setTotalPages(entityPage.getTotalPages());
|
|
pageableResponseBean.setTotalRecords(entityPage.getTotalElements());
|
|
pageableResponseBean.setPageSize(entityPage.getSize());
|
|
|
|
return pageableResponseBean;
|
|
}
|
|
|
|
public Specification<ApplicationView> searchByView(Long callId, Long companyId, Long userWithCompanyId, ApplicationPageableRequestBean applicationPageableRequestBean, UserEntity userEntity) {
|
|
return (root, query, criteriaBuilder) -> {
|
|
|
|
List<Predicate> predicates = getPredicatesByView(applicationPageableRequestBean, criteriaBuilder, root, callId,companyId, userWithCompanyId, userEntity);
|
|
SortBy sortBy = new SortBy(GepafinConstant.CREATED_DATE, true);
|
|
|
|
if (applicationPageableRequestBean.getGlobalFilters() != null
|
|
&& applicationPageableRequestBean.getGlobalFilters().getSortBy() != null &&
|
|
applicationPageableRequestBean.getGlobalFilters().getSortBy().getColumnName() != null && Boolean.FALSE.equals(
|
|
isEmpty(applicationPageableRequestBean.getGlobalFilters().getSortBy().getColumnName()))) {
|
|
sortBy.setColumnName(applicationPageableRequestBean.getGlobalFilters().getSortBy().getColumnName());
|
|
sortBy.setSortDesc(true);
|
|
if (applicationPageableRequestBean.getGlobalFilters().getSortBy().getSortDesc() != null) {
|
|
sortBy.setSortDesc(applicationPageableRequestBean.getGlobalFilters().getSortBy().getSortDesc());
|
|
}
|
|
}
|
|
|
|
query.orderBy(criteriaBuilder.desc(root.get(sortBy.getColumnName())));
|
|
if (Boolean.FALSE.equals(sortBy.getSortDesc())) {
|
|
query.orderBy(criteriaBuilder.asc(root.get(sortBy.getColumnName())));
|
|
}
|
|
return query.where(criteriaBuilder.and(predicates.toArray(new Predicate[0]))).getRestriction();
|
|
};
|
|
}
|
|
|
|
|
|
private List<Predicate> getPredicatesByView(ApplicationPageableRequestBean applicationPageableRequestBean,
|
|
CriteriaBuilder criteriaBuilder, Root<ApplicationView> root, Long callId,Long companyId, Long userWithCompanyId, UserEntity userEntity) {
|
|
|
|
Integer year = null;
|
|
String search = null;
|
|
Integer daysRange = null;
|
|
Map<String, FilterCriteria> filters = new HashMap<>();
|
|
if (applicationPageableRequestBean.getGlobalFilters() != null) {
|
|
year = applicationPageableRequestBean.getGlobalFilters().getYear();
|
|
search = applicationPageableRequestBean.getGlobalFilters().getSearch();
|
|
daysRange = applicationPageableRequestBean.getDaysRange();
|
|
}
|
|
if (applicationPageableRequestBean.getFilters() != null) {
|
|
filters = applicationPageableRequestBean.getFilters();
|
|
}
|
|
List<Predicate> predicates = new ArrayList<>();
|
|
|
|
// Boolean isBeneficiary = validator.checkIsBeneficiary();
|
|
if (Boolean.TRUE.equals(validator.checkIsBeneficiary()) || Boolean.TRUE.equals(validator.checkIsConfidi())) {
|
|
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.USER_ID), userEntity.getId()));
|
|
}
|
|
if (year != null && year > 0) {
|
|
int filterYear = applicationPageableRequestBean.getGlobalFilters().getYear();
|
|
|
|
// Create LocalDateTime boundaries for the start and end of the year
|
|
LocalDateTime startOfYear = LocalDateTime.of(filterYear, 1, 1, 0, 0);
|
|
LocalDateTime endOfYear = LocalDateTime.of(filterYear, 12, 31, 23, 59, 59);
|
|
|
|
// Add the range comparison to filter records within the year
|
|
predicates.add(criteriaBuilder.between(root.get(GepafinConstant.CREATED_DATE), startOfYear, endOfYear));
|
|
|
|
}
|
|
// Search in `title` and `message` (if search term is provided)
|
|
if (search != null && !search.isEmpty()) {
|
|
Predicate titlePredicate = criteriaBuilder.like(
|
|
criteriaBuilder.upper(root.get(GepafinConstant.COMMENTS)),
|
|
"%" + search.toUpperCase() + "%"
|
|
);
|
|
// Predicate protocolPredicate = criteriaBuilder.like(
|
|
// criteriaBuilder.function(
|
|
// "TO_CHAR",
|
|
// String.class,
|
|
// criteriaBuilder.function("CAST", String.class, root.get(GepafinConstant.PROTOCOL).get(GepafinConstant.PROTOCOL_NUMBER))
|
|
// ),
|
|
// "%" + search + "%"
|
|
// );
|
|
|
|
Predicate callNamePredicate =criteriaBuilder.like(
|
|
criteriaBuilder.upper(root.get(GepafinConstant.CALL_TITLE)), // Adjust field name
|
|
"%" + search.toUpperCase() + "%"
|
|
);
|
|
|
|
// predicates.add(criteriaBuilder.or(protocolPredicate));
|
|
predicates.add(criteriaBuilder.or(callNamePredicate));
|
|
predicates.add(criteriaBuilder.or(titlePredicate));
|
|
}
|
|
|
|
// Filter by `status` (if status list is provided)
|
|
if (applicationPageableRequestBean.getStatus() != null && !applicationPageableRequestBean.getStatus().isEmpty()) {
|
|
List<String> statusValues = applicationPageableRequestBean.getStatus().stream()
|
|
.map(ApplicationStatusTypeEnum::name) // Convert enum to string
|
|
.toList();
|
|
predicates.add(root.get(GepafinConstant.STATUS).in(statusValues));
|
|
}
|
|
|
|
if (callId != null) {
|
|
CallEntity call = callService.validateCall(callId);
|
|
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.CALL_ID), callId));
|
|
}
|
|
|
|
// Optional companyId filter
|
|
if (userWithCompanyId != null) {
|
|
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.USER_WITH_COMPANY_ID), userWithCompanyId));
|
|
}
|
|
if (companyId != null) {
|
|
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.COMPANY_ID), companyId));
|
|
}
|
|
|
|
if (daysRange != null && daysRange >= 0) {
|
|
LocalDateTime today = LocalDateTime.now();
|
|
LocalDateTime pastDate = today.minusDays(daysRange);
|
|
predicates.add(criteriaBuilder.between(root.get(GepafinConstant.CREATED_DATE), pastDate, today));
|
|
}
|
|
applyFilters(root, criteriaBuilder, predicates, filters);
|
|
|
|
predicates.add(criteriaBuilder.isFalse(root.get(GepafinConstant.IS_DELETED)));
|
|
|
|
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.HUB_ID), userEntity.getHub().getId()));
|
|
|
|
|
|
return predicates;
|
|
}
|
|
|
|
private ApplicationResponse getApplicationResponseByView(ApplicationView applicationView) {
|
|
ApplicationResponse responseBean = new ApplicationResponse();
|
|
List<FlowEdgesEntity> flowEdgesList = flowEdgesRepository.findByCallId(applicationView.getCallId());
|
|
Long totalFormSteps = flowFormDao.calculateTotalSteps(flowEdgesList);
|
|
Long completedSteps= Long.valueOf(flowFormDao.getCompletedStepsByView(applicationView.getId(), false));
|
|
Integer progress = calculateProgress(totalFormSteps, completedSteps);
|
|
responseBean.setId(applicationView.getId());
|
|
responseBean.setProgress(progress);
|
|
responseBean.setCallTitle(applicationView.getCallTitle());
|
|
responseBean.setCallEndDate(applicationView.getCallEndDate());
|
|
responseBean.setCallEndTime(applicationView.getCallEndTime());
|
|
responseBean.setModifiedDate(applicationView.getModifiedDate());
|
|
responseBean.setCallId(applicationView.getCallId());
|
|
responseBean.setSubmissionDate(applicationView.getSubmissionDate());
|
|
responseBean.setStatus(applicationView.getStatus());
|
|
responseBean.setEvaluationVersion(EvaluationVersionEnum.valueOf(applicationView.getEvaluationVersion()));
|
|
responseBean.setComments(applicationView.getComments());
|
|
responseBean.setCompanyId(applicationView.getCompanyId());
|
|
Optional<AssignedApplicationsEntity> assignedApplicationsOptional =
|
|
assignedApplicationsRepository.findByApplicationIdAndIsDeletedFalse(applicationView.getId());
|
|
if(assignedApplicationsOptional.isPresent()){
|
|
responseBean.setAssignedUserId(assignedApplicationsOptional.get().getUserId());
|
|
UserEntity user = userService.validateUser(assignedApplicationsOptional.get().getUserId());
|
|
String firstName = user.getFirstName() != null ? user.getFirstName() : "";
|
|
String lastName = user.getLastName() != null ? user.getLastName() : "";
|
|
String userName = String.join(" ", firstName, lastName).trim();
|
|
responseBean.setAssignedUserName(userName);
|
|
}
|
|
CompanyEntity company=companyService.validateCompany(applicationView.getCompanyId());
|
|
responseBean.setCompanyName(company.getCompanyName());
|
|
responseBean.setProtocolNumber(applicationView.getProtocolNumber());
|
|
responseBean.setAmountAccepted(applicationView.getAmountAccepted());
|
|
responseBean.setAmountRequested(applicationView.getAmountRequested());
|
|
responseBean.setDateAccepted(applicationView.getDateAccepted());
|
|
responseBean.setDateRejected(applicationView.getDateRejected());
|
|
return responseBean;
|
|
}
|
|
}
|