Merge pull request #21 from Kitzanos/updated-form-api
Applied validation in PUT API of form
This commit is contained in:
@@ -22,4 +22,9 @@ public class Translator {
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
return messageSource.getMessage(msgCode, null, locale);
|
||||
}
|
||||
|
||||
public static String toLocale(String key, Object... args) {
|
||||
return messageSource.getMessage(key, args, LocaleContextHolder.getLocale());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -132,6 +132,6 @@ public class GepafinConstant {
|
||||
public static final String APPLICATION_ALREADY_SUBMITTED="application.already.submitted";
|
||||
public static final String INITAL_AND_FINAL_FORM_CANNOT_NULL="initial.and.final.form.cannot.null";
|
||||
public static final String APPLICATION_FORM_NOT_FOUND="application.form.not.found";
|
||||
|
||||
public static final String UPDATING_FORM_VALUE_IMPACT_ON_FLOW="updating.form.value.impact.on.flow";
|
||||
|
||||
}
|
||||
|
||||
@@ -135,9 +135,13 @@ public class FlowDao {
|
||||
public FlowDataEntity createFlowDataEntity(FlowDataRequestBean flowDataRequestBean,CallEntity call) {
|
||||
FlowDataEntity flowDataEntity = new FlowDataEntity();
|
||||
flowDataEntity.setFormId(flowDataRequestBean.getFormId());
|
||||
if(Boolean.FALSE.equals(flowDataRequestBean.getChosenField().isEmpty()) || flowDataRequestBean.getChosenField()!=null){
|
||||
flowDataEntity.setChoosenField(flowDataRequestBean.getChosenField());
|
||||
flowDataEntity.setChoosenValue(flowDataRequestBean.getChosenValue());
|
||||
flowDataEntity.setCallId(call.getId());
|
||||
}
|
||||
if(Boolean.FALSE.equals(flowDataRequestBean.getChosenValue().isEmpty()) || flowDataRequestBean.getChosenValue()!=null) {
|
||||
flowDataEntity.setChoosenValue(flowDataRequestBean.getChosenValue());
|
||||
}
|
||||
flowDataEntity.setCallId(call.getId());
|
||||
return flowDataEntity;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,14 +3,10 @@ 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.ApplicationFormFieldRequestBean;
|
||||
import net.gepafin.tendermanagement.model.request.ContentRequestBean;
|
||||
import net.gepafin.tendermanagement.model.request.FieldValidatorBean;
|
||||
import net.gepafin.tendermanagement.model.request.FormRequest;
|
||||
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.ApplicationFormRepository;
|
||||
import net.gepafin.tendermanagement.repositories.FormRepository;
|
||||
import net.gepafin.tendermanagement.repositories.*;
|
||||
import net.gepafin.tendermanagement.service.CallService;
|
||||
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
||||
import net.gepafin.tendermanagement.util.FieldValidator;
|
||||
@@ -21,6 +17,7 @@ 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;
|
||||
@@ -42,6 +39,15 @@ public class FormDao {
|
||||
@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;
|
||||
@@ -66,6 +72,16 @@ public class FormDao {
|
||||
}
|
||||
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);
|
||||
}
|
||||
@@ -74,14 +90,55 @@ 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){
|
||||
public FormResponseBean updateForm(Long formId, FormRequest formRequest,Boolean forceDeleteFlow){
|
||||
ContentRequestBean contentRequestBean2=null;
|
||||
FormEntity formEntity = validateForm(formId);
|
||||
callDao.validateUpdate(formEntity.getCall());
|
||||
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);
|
||||
callDao.validateUpdate(formEntity.getCall());
|
||||
FlowDataEntity flowDataEntity=flowDataRepository.findByFormIdAndCallId(formEntity.getId(),formEntity.getCall().getId());
|
||||
String choosenField=flowDataEntity.getChoosenField();
|
||||
List<ContentRequestBean> contentRequestBean=Utils.convertJsonStringToList(formEntity.getContent(),ContentRequestBean.class);
|
||||
for (ContentRequestBean contentRequestBean1:contentRequestBean){
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public FormEntity validateForm(Long formId) {
|
||||
@@ -96,6 +153,14 @@ public class FormDao {
|
||||
}
|
||||
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){
|
||||
|
||||
@@ -6,8 +6,7 @@ public enum ApplicationStatusTypeEnum {
|
||||
|
||||
DRAFT("DRAFT"),
|
||||
SUBMIT("SUBMIT"),
|
||||
EXPIRED("EXPIRED"),
|
||||
READY_TO_SUBMIT("READY_TO_SUBMIT");
|
||||
DISCARD("DISCARD");
|
||||
|
||||
private String value;
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ public interface FormService {
|
||||
|
||||
public FormResponseBean createForm(HttpServletRequest request,Long callId,FormRequest formRequest);
|
||||
|
||||
public FormResponseBean updateForm(HttpServletRequest request, Long formId, FormRequest formRequest);
|
||||
public FormResponseBean updateForm(HttpServletRequest request, Long formId, FormRequest formRequest,Boolean forceDeleteFlow);
|
||||
|
||||
public FormResponseBean getFormById(HttpServletRequest request, Long formId);
|
||||
|
||||
|
||||
@@ -26,8 +26,8 @@ public class FormServiceImpl implements FormService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public FormResponseBean updateForm(HttpServletRequest request, Long formId, FormRequest formRequest) {
|
||||
return formDao.updateForm(formId,formRequest);
|
||||
public FormResponseBean updateForm(HttpServletRequest request, Long formId, FormRequest formRequest,Boolean forceDeleteFlow) {
|
||||
return formDao.updateForm(formId,formRequest,forceDeleteFlow);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,6 +14,7 @@ import net.gepafin.tendermanagement.model.util.Response;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.ErrorConstants;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -33,6 +34,7 @@ public interface FlowApi {
|
||||
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
|
||||
@PutMapping(value = "/call/{callId}",
|
||||
produces = { "application/json" })
|
||||
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
|
||||
ResponseEntity<Response<FlowResponseBean>> createOrUpdateFlow(HttpServletRequest request,
|
||||
@Parameter(description = " Flow request object", required = true) @Valid @RequestBody FlowRequestBean flowRequestBean,
|
||||
@Parameter(description = "The call ID", required = true) @PathVariable("callId") Long callId);
|
||||
|
||||
@@ -13,6 +13,7 @@ import net.gepafin.tendermanagement.model.util.Response;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.ErrorConstants;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@@ -31,6 +32,7 @@ public interface FormApi {
|
||||
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) }))
|
||||
})
|
||||
@PostMapping(value = "/call/{callId}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
|
||||
public ResponseEntity<Response<FormResponseBean>> createForm(HttpServletRequest request,@Parameter(description = "The call ID", required = true) @PathVariable("callId") Long callId,
|
||||
@Parameter(description = "form request object", required = true)
|
||||
@Valid @RequestBody FormRequest formRequest);
|
||||
@@ -49,7 +51,7 @@ public interface FormApi {
|
||||
produces = { "application/json" })
|
||||
ResponseEntity<Response<FormResponseBean>> updateForm(HttpServletRequest request,
|
||||
@Parameter(description = "The form ID", required = true) @PathVariable("formId") Long formId,
|
||||
@Parameter(description = "form request object", required = true) @Valid @RequestBody FormRequest formRequest);
|
||||
@Parameter(description = "form request object", required = true) @Valid @RequestBody FormRequest formRequest,@Parameter(description = "force delete flow ",required = true)@RequestParam(value = "forceDeleteFlow",required = true)Boolean forceDeleteFlow);
|
||||
|
||||
@Operation(summary = "Api to get form by id",
|
||||
responses = {
|
||||
|
||||
@@ -32,8 +32,8 @@ public class FormApiController implements FormApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Response<FormResponseBean>> updateForm(HttpServletRequest request, Long formId, FormRequest formRequest) {
|
||||
FormResponseBean formResponseBean = formService.updateForm(request, formId, formRequest);
|
||||
public ResponseEntity<Response<FormResponseBean>> updateForm(HttpServletRequest request, Long formId, FormRequest formRequest,Boolean forceDeleteFlow) {
|
||||
FormResponseBean formResponseBean = formService.updateForm(request, formId, formRequest,forceDeleteFlow);
|
||||
return ResponseEntity.status(HttpStatus.CREATED)
|
||||
.body(new Response<>(formResponseBean, Status.SUCCESS, Translator.toLocale(GepafinConstant.FORM_UPDATED_SUCCESSFULLY)));
|
||||
}
|
||||
|
||||
@@ -166,3 +166,6 @@ validation.message=Validation messages.
|
||||
action.required=Action field required.
|
||||
call.not.published=Call is not published.
|
||||
application.form.not.found=Application form not found.
|
||||
|
||||
updating.form.value.impact.on.flow=Updating this value of form {0} can make impact on flow.
|
||||
|
||||
|
||||
@@ -160,4 +160,6 @@ flow.not.found=Flow not found.
|
||||
validation.message=Messaggi di convalida.
|
||||
action.required=Campo azione obbligatorio.
|
||||
call.not.published=La chiamata non è stata pubblicata.
|
||||
application.form.not.found=Modulo di domanda non trovato.
|
||||
application.form.not.found=Modulo di domanda non trovato.
|
||||
|
||||
updating.form.value.impact.on.flow=L'aggiornamento di questo valore del modulo {0} può avere un impatto sul flusso.
|
||||
Reference in New Issue
Block a user