use of com.walmartlabs.concord.server.process.form.FormUtils.ValidationException in project concord by walmartlabs.
the class FormResourceV1 method submit.
/**
* Submit form instance's data, potentially resuming a suspended process.
*/
@POST
@ApiOperation(value = "Submit JSON form data")
@Path("/{processInstanceId}/form/{formName}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public FormSubmitResponse submit(@ApiParam @PathParam("processInstanceId") UUID processInstanceId, @ApiParam @PathParam("formName") String formName, @ApiParam Map<String, Object> data) {
PartialProcessKey processKey = PartialProcessKey.from(processInstanceId);
Form form = formService.get(processKey, formName);
if (form == null) {
throw new ConcordApplicationException("Form " + formName + " not found. Process ID: " + processInstanceId, Status.NOT_FOUND);
}
try {
data = FormUtils.convert(validatorLocale, form, data);
} catch (ValidationException e) {
Map<String, String> errors = Collections.singletonMap(e.getField().getName(), e.getMessage());
return new FormSubmitResponse(processInstanceId, errors);
}
FormSubmitResult result = formService.submit(processKey, formName, data);
Map<String, String> errors = FormUtils.mergeErrors(result.getErrors());
return new FormSubmitResponse(result.getProcessInstanceId(), errors);
}
use of com.walmartlabs.concord.server.process.form.FormUtils.ValidationException in project concord by walmartlabs.
the class CustomFormServiceV1 method continueSession.
private Response continueSession(UriInfo uriInfo, HttpHeaders headers, ProcessKey processKey, String formName, Map<String, Object> data) {
// TODO locking
Form form = assertForm(processKey, formName);
// TODO constants
Map<String, Object> opts = form.getOptions();
boolean yield = opts != null && (boolean) opts.getOrDefault("yield", false);
Path dst = cfg.getBaseDir().resolve(processKey.toString()).resolve(formName);
Path formDir = dst.resolve(FORM_DIR_NAME);
try {
Map<String, Object> m = new HashMap<>();
try {
m = FormUtils.convert(new ExternalFileFormValidatorLocale(processKey, formName, stateManager), form, data);
FormSubmitResult r = formService.submit(processKey, formName, m);
if (r.isValid()) {
if (yield) {
// this was the last "interactive" form. The process will continue in "background"
// and users should get a success page.
writeData(formDir, success(form, m));
} else {
while (true) {
ProcessStatus s = queueDao.getStatus(processKey);
if (s == ProcessStatus.SUSPENDED) {
String nextFormId = formService.nextFormId(processKey);
if (nextFormId == null) {
writeData(formDir, success(form, m));
break;
} else {
FormSessionResponse nextSession = startSession(processKey, nextFormId);
return redirectTo(nextSession.getUri());
}
} else if (s == ProcessStatus.FAILED || s == ProcessStatus.CANCELLED || s == ProcessStatus.TIMED_OUT) {
writeData(formDir, processFailed(form, m));
break;
} else if (s == ProcessStatus.FINISHED) {
writeData(formDir, success(form, m));
break;
}
try {
// TODO exp back off?
Thread.sleep(STATUS_REFRESH_DELAY);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
} else {
writeData(formDir, prepareData(form, m, r.getErrors()));
}
} catch (ValidationException e) {
ValidationError err = ValidationError.of(e.getField().getName(), e.getMessage());
FormData d = prepareData(form, m, Collections.singletonList(err));
writeData(formDir, d);
}
} catch (Exception e) {
throw new ConcordApplicationException("Error while submitting a form", e);
}
return redirectToForm(uriInfo, headers, processKey, formName);
}
Aggregations