Updated endpoint to create flow
This commit is contained in:
@@ -20,11 +20,13 @@ import net.gepafin.tendermanagement.repositories.FlowEdgesRepository;
|
||||
import net.gepafin.tendermanagement.repositories.FlowEdgesRepository;
|
||||
import net.gepafin.tendermanagement.service.CallService;
|
||||
import net.gepafin.tendermanagement.service.FormService;
|
||||
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -47,10 +49,11 @@ public class FlowDao {
|
||||
@Autowired
|
||||
private FormService formService;
|
||||
|
||||
public FlowResponseBean createFlow(FlowRequestBean flowRequestBean, Long callId) {
|
||||
public FlowResponseBean createOrUpdateFlow(FlowRequestBean flowRequestBean, Long callId) {
|
||||
validateFlowRequestBean(flowRequestBean);
|
||||
CallEntity call = setInitialAndFinalFormInCall(flowRequestBean, callId);
|
||||
checkIfFlowExits(callId);
|
||||
CallEntity call = callService.validateCall(callId);
|
||||
checkIfFlowExits(call);
|
||||
call= setInitialAndFinalFormInCall(flowRequestBean, call);
|
||||
validateFlowRequest(flowRequestBean);
|
||||
List<FlowDataEntity> flowDataEntities = createFlowData(flowRequestBean,call);
|
||||
List<FlowEdgesEntity> flowEdgesEntities = createFlowEdges(flowRequestBean,call);
|
||||
@@ -59,23 +62,29 @@ public class FlowDao {
|
||||
}
|
||||
|
||||
public void validateFlowRequestBean(FlowRequestBean flowRequestBean){
|
||||
if(flowRequestBean.getFlowData()==null || flowRequestBean.getFlowEdges()==null){
|
||||
throw new CustomValidationException(Status.BAD_REQUEST,Translator.toLocale(GepafinConstant.FLOW_REQUEST_NOT_PROPER));
|
||||
if (flowRequestBean.getFlowEdges() == null || flowRequestBean.getFlowEdges().isEmpty()) {
|
||||
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.FLOW_REQUEST_NOT_PROPER));
|
||||
}
|
||||
}
|
||||
|
||||
public void checkIfFlowExits(Long callId) {
|
||||
List<FlowDataEntity> flowDataEntities = flowDataRepository.findByCallId(callId);
|
||||
List<FlowEdgesEntity> flowEdgesEntities = flowEdgesRepository.findByCallId(callId);
|
||||
if (!flowDataEntities.isEmpty() || !flowEdgesEntities.isEmpty()) {
|
||||
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.FLOW_ALREADY_EXISTS));
|
||||
public void checkIfFlowExits(CallEntity call) {
|
||||
List<FlowDataEntity> flowDataEntities = flowDataRepository.findByCallId(call.getId());
|
||||
List<FlowEdgesEntity> flowEdgesEntities = flowEdgesRepository.findByCallId(call.getId());
|
||||
if (Boolean.FALSE.equals(flowDataEntities.isEmpty()) || Boolean.FALSE.equals(flowEdgesEntities.isEmpty())) {
|
||||
call.setInitialForm(null);
|
||||
call.setFinalForm(null);
|
||||
call=callRepository.save(call);
|
||||
flowDataRepository.deleteAll(flowDataEntities);
|
||||
flowEdgesRepository.deleteAll(flowEdgesEntities);
|
||||
}
|
||||
}
|
||||
|
||||
public void validateFlowRequest(FlowRequestBean flowRequestBean) {
|
||||
formService.validateForm(flowRequestBean.getInitialForm());
|
||||
formService.validateForm(flowRequestBean.getFinalForm());
|
||||
flowRequestBean.getFlowData().forEach(flowData -> formService.validateForm(flowData.getFormId()));
|
||||
if(flowRequestBean.getFlowData()!=null && !flowRequestBean.getFlowData().isEmpty()) {
|
||||
flowRequestBean.getFlowData().forEach(flowData -> formService.validateForm(flowData.getFormId()));
|
||||
}
|
||||
}
|
||||
|
||||
private List<FlowEdgesResponseBean> createFlowEdgesResponseBean(List<FlowEdgesEntity> flowEdgesEntities) {
|
||||
@@ -96,19 +105,22 @@ public class FlowDao {
|
||||
return flowResponseBean;
|
||||
}
|
||||
|
||||
private CallEntity setInitialAndFinalFormInCall(FlowRequestBean flowRequestBean, Long callId) {
|
||||
CallEntity call = callService.validateCall(callId);
|
||||
private CallEntity setInitialAndFinalFormInCall(FlowRequestBean flowRequestBean, CallEntity call) {
|
||||
call.setInitialForm(flowRequestBean.getInitialForm());
|
||||
call.setFinalForm(flowRequestBean.getFinalForm());
|
||||
call.setUpdatedDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now()));
|
||||
call = callRepository.save(call);
|
||||
return call;
|
||||
}
|
||||
|
||||
public List<FlowDataEntity> createFlowData(FlowRequestBean flowRequestBean, CallEntity call) {
|
||||
List<FlowDataEntity> flowDataEntities = flowRequestBean.getFlowData().stream()
|
||||
.map(flowDataRequestBean -> createFlowDataEntity(flowDataRequestBean, call))
|
||||
.collect(Collectors.toList());
|
||||
return flowDataRepository.saveAll(flowDataEntities);
|
||||
if (flowRequestBean.getFlowData() != null || !flowRequestBean.getFlowEdges().isEmpty()) {
|
||||
List<FlowDataEntity> flowDataEntities = flowRequestBean.getFlowData().stream()
|
||||
.map(flowDataRequestBean -> createFlowDataEntity(flowDataRequestBean, call))
|
||||
.collect(Collectors.toList());
|
||||
return flowDataRepository.saveAll(flowDataEntities);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public FlowDataEntity createFlowDataEntity(FlowDataRequestBean flowDataRequestBean,CallEntity call) {
|
||||
@@ -129,6 +141,7 @@ public class FlowDao {
|
||||
|
||||
public FlowEdgesEntity createFlowEdgesEntity(FlowEdgesRequestBean flowEdgesRequestBean,CallEntity call) {
|
||||
FlowEdgesEntity flowEdgesEntity = new FlowEdgesEntity();
|
||||
flowEdgesEntity.setTrackingId(flowEdgesRequestBean.getId());
|
||||
flowEdgesEntity.setSourceId(Long.valueOf(flowEdgesRequestBean.getSource()));
|
||||
flowEdgesEntity.setTargetId(Long.valueOf(flowEdgesRequestBean.getTarget()));
|
||||
flowEdgesEntity.setType(flowEdgesRequestBean.getType());
|
||||
@@ -147,7 +160,7 @@ public class FlowDao {
|
||||
|
||||
public FlowEdgesResponseBean convertFlowEdgesEntityToFlowEdgesResponseBean(FlowEdgesEntity flowEdgesEntity) {
|
||||
FlowEdgesResponseBean flowEdgesResponseBean = new FlowEdgesResponseBean();
|
||||
flowEdgesResponseBean.setId(flowEdgesEntity.getId());
|
||||
flowEdgesResponseBean.setId(flowEdgesEntity.getTrackingId());
|
||||
flowEdgesResponseBean.setType(flowEdgesEntity.getType());
|
||||
flowEdgesResponseBean.setSource(String.valueOf(flowEdgesEntity.getSourceId()));
|
||||
flowEdgesResponseBean.setTarget(String.valueOf(flowEdgesEntity.getTargetId()));
|
||||
@@ -163,7 +176,7 @@ public class FlowDao {
|
||||
List<FlowEdgesResponseBean> flowEdgesResponseBeans=createFlowEdgesResponseBean(flowEdgesEntities);
|
||||
flowResponseBean.setFlowData(flowDataResponseBeans);
|
||||
flowResponseBean.setFlowEdges(flowEdgesResponseBeans);
|
||||
if(flowResponseBean.getFlowData().isEmpty() || flowResponseBean.getFlowEdges().isEmpty()){
|
||||
if( flowResponseBean.getFlowEdges().isEmpty()){
|
||||
return null;
|
||||
}
|
||||
flowResponseBean.setCallId(call.getId());
|
||||
|
||||
Reference in New Issue
Block a user