227 lines
12 KiB
Java
227 lines
12 KiB
Java
package net.gepafin.tendermanagement.dao;
|
|
|
|
import net.gepafin.tendermanagement.config.Translator;
|
|
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
|
import net.gepafin.tendermanagement.entities.*;
|
|
import net.gepafin.tendermanagement.model.request.*;
|
|
import net.gepafin.tendermanagement.model.response.ContentResponseBean;
|
|
import net.gepafin.tendermanagement.model.response.FormResponseBean;
|
|
import net.gepafin.tendermanagement.repositories.*;
|
|
import net.gepafin.tendermanagement.service.CallService;
|
|
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
|
import net.gepafin.tendermanagement.util.FieldValidator;
|
|
import net.gepafin.tendermanagement.util.Utils;
|
|
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.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.text.MessageFormat;
|
|
import java.time.LocalDateTime;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
public class FormDao {
|
|
|
|
@Autowired
|
|
private FormRepository formRepository;
|
|
|
|
@Autowired
|
|
private CallService callService;
|
|
|
|
@Autowired
|
|
private ApplicationFormRepository applicationFormRepository;
|
|
|
|
@Autowired
|
|
private CallDao callDao;
|
|
|
|
@Autowired
|
|
private FlowDataRepository flowDataRepository;
|
|
|
|
@Autowired
|
|
private FlowEdgesRepository flowEdgesRepository;
|
|
|
|
@Autowired
|
|
private CallRepository callRepository;
|
|
|
|
public FormEntity saveFormEntity(FormEntity formEntity){
|
|
formEntity=formRepository.save(formEntity);
|
|
return formEntity;
|
|
}
|
|
|
|
public FormEntity convertFormRequestToFormEntity(Long callId,FormRequest formRequest){
|
|
FormEntity formEntity=new FormEntity();
|
|
CallEntity callEntity=callService.getCallEntityById(callId);
|
|
formEntity.setCall(callEntity);
|
|
formEntity.setLabel(formRequest.getLabel());
|
|
formEntity.setContent(setContentResponseBean(formRequest.getContent()));
|
|
formEntity=saveFormEntity(formEntity);
|
|
return formEntity;
|
|
}
|
|
public FormResponseBean convertFormEntityToFormResponseBean(FormEntity formEntity){
|
|
FormResponseBean formResponseBean=new FormResponseBean();
|
|
formResponseBean.setId(formEntity.getId());
|
|
formResponseBean.setContent(Utils.convertJsonStringToList(formEntity.getContent(), ContentResponseBean.class));
|
|
formResponseBean.setLabel(formEntity.getLabel());
|
|
formResponseBean.setCallId(formEntity.getCall().getId());
|
|
return formResponseBean;
|
|
}
|
|
public FormResponseBean createForm(Long callId,FormRequest formRequest){
|
|
validateForm(formRequest);
|
|
CallEntity callEntity=callService.validateCall(callId);
|
|
List<FlowDataEntity> flowDataEntities=flowDataRepository.findByCallId(callId);
|
|
List<FlowEdgesEntity> flowEdgesEntities=flowEdgesRepository.findByCallId(callId);
|
|
if(Boolean.FALSE.equals(flowDataEntities.isEmpty() || flowDataEntities==null ) || Boolean.FALSE.equals(flowEdgesEntities.isEmpty() || flowEdgesEntities==null) ){
|
|
flowDataRepository.deleteAll(flowDataEntities);
|
|
flowEdgesRepository.deleteAll(flowEdgesEntities);
|
|
callEntity.setInitialForm(null);
|
|
callEntity.setFinalForm(null);
|
|
callRepository.save(callEntity);
|
|
}
|
|
FormEntity formEntity=convertFormRequestToFormEntity(callId,formRequest);
|
|
return convertFormEntityToFormResponseBean(formEntity);
|
|
}
|
|
public void validateForm(FormRequest formRequest){
|
|
if(formRequest.getContent()==null || formRequest.getLabel()==null ){
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.REQUIRED_PARAMETER_NOT_FOUND_FOR_FORM));
|
|
}
|
|
}
|
|
public FormResponseBean updateForm(Long formId, FormRequest formRequest,Boolean forceDeleteFlow){
|
|
ContentRequestBean contentRequestBean2=null;
|
|
String choosenField=null;
|
|
FormEntity formEntity = validateForm(formId);
|
|
callDao.validateUpdate(formEntity.getCall());
|
|
List<ContentRequestBean> contentRequestBean = Utils.convertJsonStringToList(formEntity.getContent(), ContentRequestBean.class);
|
|
for (ContentRequestBean contentRequestBean1 : contentRequestBean) {
|
|
FlowDataEntity flowDataEntity = flowDataRepository.findByFormIdAndChoosenField(formEntity.getId(), contentRequestBean1.getId());
|
|
if (flowDataEntity != null) {
|
|
choosenField = flowDataEntity.getChoosenField();
|
|
if (Boolean.TRUE.equals(contentRequestBean1.getId().equals(choosenField))) {
|
|
contentRequestBean2 = contentRequestBean1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (contentRequestBean2 != null) {
|
|
List<SettingRequestBean> settingRequestBeansDB = contentRequestBean2.getSettings();
|
|
for (ContentRequestBean contentRequestBeanRequest : formRequest.getContent()) {
|
|
if (contentRequestBeanRequest.getId().equals(contentRequestBean2.getId())) {
|
|
for (SettingRequestBean settingRequestBeanRequest : contentRequestBeanRequest.getSettings()) {
|
|
for (SettingRequestBean settingRequestBeanDB : settingRequestBeansDB) {
|
|
if (settingRequestBeanRequest.getName().equals(settingRequestBeanDB.getName())) {
|
|
if (!settingRequestBeanRequest.getValue().equals(settingRequestBeanDB.getValue())) {
|
|
if (Boolean.TRUE.equals(forceDeleteFlow)) {
|
|
Utils.setIfUpdated(formEntity::getLabel, formEntity::setLabel, formRequest.getLabel());
|
|
Utils.setIfUpdated(formEntity::getContent, formEntity::setContent, setContentResponseBean(formRequest.getContent()));
|
|
formEntity.setUpdatedDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now()));
|
|
formEntity = saveFormEntity(formEntity);
|
|
List<FlowDataEntity> flowDataEntities = flowDataRepository.findByCallId(formEntity.getCall().getId());
|
|
List<FlowEdgesEntity> flowEdgesEntities = flowEdgesRepository.findByCallId(formEntity.getCall().getId());
|
|
flowDataRepository.deleteAll(flowDataEntities);
|
|
flowEdgesRepository.deleteAll(flowEdgesEntities);
|
|
CallEntity callEntity = formEntity.getCall();
|
|
callEntity.setInitialForm(null);
|
|
callEntity.setFinalForm(null);
|
|
callRepository.save(callEntity);
|
|
return convertFormEntityToFormResponseBean(formEntity);
|
|
} else {
|
|
throw new CustomValidationException(
|
|
Status.BAD_REQUEST,
|
|
Translator.toLocale(GepafinConstant.UPDATING_FORM_VALUE_IMPACT_ON_FLOW, choosenField)
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
Utils.setIfUpdated(formEntity::getLabel, formEntity::setLabel, formRequest.getLabel());
|
|
Utils.setIfUpdated(formEntity::getContent, formEntity::setContent, setContentResponseBean(formRequest.getContent()));
|
|
formEntity.setUpdatedDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now()));
|
|
formEntity = saveFormEntity(formEntity);
|
|
return convertFormEntityToFormResponseBean(formEntity);
|
|
}
|
|
return convertFormEntityToFormResponseBean(formEntity);
|
|
}
|
|
|
|
public FormEntity validateForm(Long formId) {
|
|
FormEntity formEntity = formRepository.findById(formId)
|
|
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.FORM_NOT_FOUND)));
|
|
return formEntity;
|
|
}
|
|
|
|
public FormResponseBean getFormEntityById(Long formId) {
|
|
FormEntity formEntity = validateForm(formId);
|
|
return convertFormEntityToFormResponseBean(formEntity);
|
|
}
|
|
public void deleteFormById(Long formId){
|
|
FormEntity formEntity = validateForm(formId);
|
|
List<FlowDataEntity> flowDataEntities=flowDataRepository.findByCallId(formEntity.getCall().getId());
|
|
List<FlowEdgesEntity> flowEdgesEntities=flowEdgesRepository.findByCallId(formEntity.getCall().getId());
|
|
flowDataRepository.deleteAll(flowDataEntities);
|
|
flowEdgesRepository.deleteAll(flowEdgesEntities);
|
|
CallEntity callEntity=formEntity.getCall();
|
|
callEntity.setFinalForm(null);
|
|
callEntity.setInitialForm(null);
|
|
callRepository.save(callEntity);
|
|
formRepository.delete(formEntity);
|
|
}
|
|
public List<FormResponseBean> getFormsByCallId(Long callId){
|
|
CallEntity callEntity=callService.validateCall(callId);
|
|
if(callEntity== null){
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
|
Translator.toLocale(GepafinConstant.CALL_NOT_FOUND));
|
|
}
|
|
List<FormEntity> formEntities=formRepository.findByCallId(callId);
|
|
List<FormResponseBean> formResponseBeanList = formEntities.stream()
|
|
.map(req -> convertFormEntityToFormResponseBean(req))
|
|
.collect(Collectors.toList());
|
|
return formResponseBeanList;
|
|
}
|
|
public String setContentResponseBean(List<ContentRequestBean> contentRequestBeans){
|
|
return Utils.convertListToJsonString(contentRequestBeans);
|
|
}
|
|
|
|
public void validateFormField(List<ApplicationFormFieldRequestBean> applicationFormFieldRequestList, ApplicationEntity applicationEntity, FormEntity formEntity) {
|
|
Map<String, String> formFieldMap = new LinkedHashMap<String, String>();
|
|
for(ApplicationFormFieldRequestBean applicationFormFieldRequestBean:applicationFormFieldRequestList) {
|
|
formFieldMap.put(applicationFormFieldRequestBean.getFieldId(),applicationFormFieldRequestBean.getFieldValue());
|
|
}
|
|
|
|
FormResponseBean formResponseBean = convertFormEntityToFormResponseBean(formEntity);
|
|
ApplicationFormEntity applicationFormEntity=applicationFormRepository.findByApplicationIdAndFormId(applicationEntity.getId(),formEntity.getId());
|
|
Boolean isApplicationFormExist= getApplicationFormExist(applicationFormEntity);
|
|
FieldValidator validator = FieldValidator.create();
|
|
formResponseBean.getContent().forEach(contentResponseBean -> {
|
|
String fieldId = contentResponseBean.getId();
|
|
String value = formFieldMap.get(fieldId);
|
|
if(value == null && isApplicationFormExist) {
|
|
return;
|
|
}
|
|
FieldValidatorBean fieldValidatorBean = Utils.convertSourceObjectToDestinationObject(contentResponseBean, FieldValidatorBean.class);
|
|
validator
|
|
.notNull(value, fieldId)
|
|
.minLength(value, fieldValidatorBean.getMinLength(), fieldId) // Only applies if minLength is not null
|
|
.maxLength(value, fieldValidatorBean.getMaxLength(), fieldId) // Only applies if maxLength is not null
|
|
.matchesPattern(value, fieldValidatorBean.getPattern(), fieldId); // Only applies if pattern is present
|
|
});
|
|
|
|
validator.validate();
|
|
|
|
}
|
|
|
|
private Boolean getApplicationFormExist(ApplicationFormEntity applicationFormEntity) {
|
|
if(applicationFormEntity !=null) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|