diff --git a/src/main/java/net/gepafin/tendermanagement/dao/FormDao.java b/src/main/java/net/gepafin/tendermanagement/dao/FormDao.java index ab882abd..59d1b802 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/FormDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/FormDao.java @@ -90,55 +90,65 @@ public class FormDao { throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.REQUIRED_PARAMETER_NOT_FOUND_FOR_FORM)); } } - public FormResponseBean updateForm(Long formId, FormRequest formRequest,Boolean forceDeleteFlow){ + public FormResponseBean updateForm(Long formId, FormRequest formRequest,Boolean forceDeleteFlow){ ContentRequestBean contentRequestBean2=null; + String choosenField=null; FormEntity formEntity = validateForm(formId); - callDao.validateUpdate(formEntity.getCall()); - FlowDataEntity flowDataEntity=flowDataRepository.findByFormIdAndCallId(formEntity.getId(),formEntity.getCall().getId()); - String choosenField=flowDataEntity.getChoosenField(); - List contentRequestBean=Utils.convertJsonStringToList(formEntity.getContent(),ContentRequestBean.class); - for (ContentRequestBean contentRequestBean1:contentRequestBean){ - if(Boolean.TRUE.equals(contentRequestBean1.getId().equals(choosenField))){ - contentRequestBean2=contentRequestBean1; - break; + callDao.validateUpdate(formEntity.getCall()); + List 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 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 flowDataEntities=flowDataRepository.findByCallId(formEntity.getCall().getId()); - List 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) - ); - } + if (contentRequestBean2 != null) { + List 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 flowDataEntities = flowDataRepository.findByCallId(formEntity.getCall().getId()); + List 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 null; + return null; } public FormEntity validateForm(Long formId) { diff --git a/src/main/java/net/gepafin/tendermanagement/repositories/FlowDataRepository.java b/src/main/java/net/gepafin/tendermanagement/repositories/FlowDataRepository.java index 8f752aa9..dffaee53 100644 --- a/src/main/java/net/gepafin/tendermanagement/repositories/FlowDataRepository.java +++ b/src/main/java/net/gepafin/tendermanagement/repositories/FlowDataRepository.java @@ -17,4 +17,7 @@ public interface FlowDataRepository extends JpaRepository { public Optional findByChoosenValueAndFormIdIn(String fieldValue, List nextFormIds); public List findByFormIdInAndCallId(List previousFormIds, Long callId); + + public FlowDataEntity findByFormIdAndChoosenField(Long formId, String choosenField); + }