Merge pull request #19 from Kitzanos/updated-flow-api

Updated flow endpoint
This commit is contained in:
rajeshkhore
2024-09-12 20:11:30 +05:30
committed by GitHub
9 changed files with 56 additions and 34 deletions

View File

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

View File

@@ -25,4 +25,7 @@ public class FlowEdgesEntity extends BaseEntity {
@Column(name="CALL_ID") @Column(name="CALL_ID")
private Long callId; private Long callId;
@Column(name="TRACKING_ID")
private String trackingId;
} }

View File

@@ -5,6 +5,8 @@ import lombok.Data;
@Data @Data
public class FlowEdgesRequestBean { public class FlowEdgesRequestBean {
private String id;
private String source; private String source;
private String target; private String target;

View File

@@ -6,7 +6,7 @@ import org.springframework.security.core.parameters.P;
@Data @Data
public class FlowEdgesResponseBean { public class FlowEdgesResponseBean {
private Long id; private String id;
private String source; private String source;

View File

@@ -6,7 +6,7 @@ import net.gepafin.tendermanagement.model.response.FlowResponseBean;
public interface FlowService { public interface FlowService {
public FlowResponseBean createFlow(HttpServletRequest httpServletRequest, FlowRequestBean flowRequestBean, Long callId); public FlowResponseBean createOrUpdateFlow(HttpServletRequest httpServletRequest, FlowRequestBean flowRequestBean, Long callId);
public FlowResponseBean getFlowByCallId(HttpServletRequest request, Long callId); public FlowResponseBean getFlowByCallId(HttpServletRequest request, Long callId);
} }

View File

@@ -7,6 +7,7 @@ import net.gepafin.tendermanagement.model.response.FlowResponseBean;
import net.gepafin.tendermanagement.service.FlowService; import net.gepafin.tendermanagement.service.FlowService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service @Service
public class FlowServiceImpl implements FlowService { public class FlowServiceImpl implements FlowService {
@@ -15,11 +16,13 @@ public class FlowServiceImpl implements FlowService {
private FlowDao flowDao; private FlowDao flowDao;
@Override @Override
public FlowResponseBean createFlow(HttpServletRequest httpServletRequest, FlowRequestBean flowRequestBean, Long callId) { @Transactional(rollbackFor = Exception.class)
return flowDao.createFlow(flowRequestBean,callId); public FlowResponseBean createOrUpdateFlow(HttpServletRequest httpServletRequest, FlowRequestBean flowRequestBean, Long callId) {
return flowDao.createOrUpdateFlow(flowRequestBean,callId);
} }
@Override @Override
@org.springframework.transaction.annotation.Transactional(readOnly = true)
public FlowResponseBean getFlowByCallId(HttpServletRequest request, Long callId) { public FlowResponseBean getFlowByCallId(HttpServletRequest request, Long callId) {
return flowDao.getFlowByCallId(callId); return flowDao.getFlowByCallId(callId);
} }

View File

@@ -15,17 +15,14 @@ import net.gepafin.tendermanagement.web.rest.api.errors.ErrorConstants;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List; import java.util.List;
@Validated @Validated
public interface FlowApi { public interface FlowApi {
@Operation(summary = "Api to create flow", @Operation(summary = "Api to create or update flow",
responses = { responses = {
@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@@ -34,9 +31,9 @@ public interface FlowApi {
@ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE) })), @ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) }) @ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
@PostMapping(value = "/call/{callId}", @PutMapping(value = "/call/{callId}",
produces = { "application/json" }) produces = { "application/json" })
ResponseEntity<Response<FlowResponseBean>> createFlow(HttpServletRequest request, ResponseEntity<Response<FlowResponseBean>> createOrUpdateFlow(HttpServletRequest request,
@Parameter(description = " Flow request object", required = true) @Valid @RequestBody FlowRequestBean flowRequestBean, @Parameter(description = " Flow request object", required = true) @Valid @RequestBody FlowRequestBean flowRequestBean,
@Parameter(description = "The call ID", required = true) @PathVariable("callId") Long callId); @Parameter(description = "The call ID", required = true) @PathVariable("callId") Long callId);

View File

@@ -26,15 +26,13 @@ public class FlowApiController implements FlowApi {
private FlowService flowService; private FlowService flowService;
@Override @Override
@Transactional(rollbackFor=Exception.class) public ResponseEntity<Response<FlowResponseBean>> createOrUpdateFlow(HttpServletRequest httpServletRequest, FlowRequestBean flowRequestBean, Long callId) {
public ResponseEntity<Response<FlowResponseBean>> createFlow(HttpServletRequest httpServletRequest, FlowRequestBean flowRequestBean, Long callId) { FlowResponseBean flowResponseBean=flowService.createOrUpdateFlow(httpServletRequest,flowRequestBean,callId);
FlowResponseBean flowResponseBean=flowService.createFlow(httpServletRequest,flowRequestBean,callId);
return ResponseEntity.status(HttpStatus.CREATED) return ResponseEntity.status(HttpStatus.CREATED)
.body(new Response<>(flowResponseBean, Status.SUCCESS, Translator.toLocale(GepafinConstant.FLOW_CREATED_SUCCESSFULLY))); .body(new Response<>(flowResponseBean, Status.SUCCESS, Translator.toLocale(GepafinConstant.FLOW_CREATED_SUCCESSFULLY)));
} }
@Override @Override
@Transactional(rollbackFor=Exception.class)
public ResponseEntity<Response<FlowResponseBean>> getFlowByCallId(HttpServletRequest request, Long callId) { public ResponseEntity<Response<FlowResponseBean>> getFlowByCallId(HttpServletRequest request, Long callId) {
FlowResponseBean flowResponseBean=flowService.getFlowByCallId(request,callId); FlowResponseBean flowResponseBean=flowService.getFlowByCallId(request,callId);
return ResponseEntity.status(HttpStatus.OK) return ResponseEntity.status(HttpStatus.OK)

View File

@@ -633,4 +633,10 @@
<column name="final_form" type="INTEGER"></column> <column name="final_form" type="INTEGER"></column>
</addColumn> </addColumn>
</changeSet> </changeSet>
<changeSet id="12-09-2024_1" author="Rajesh Khore">
<addColumn tableName="flow_edges">
<column name="tracking_id" type="VARCHAR(255)"></column>
</addColumn>
</changeSet>
</databaseChangeLog> </databaseChangeLog>