Search in sources :

Example 1 with ValidationException

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);
}
Also used : PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ValidationException(com.walmartlabs.concord.server.process.form.FormUtils.ValidationException) Form(io.takari.bpm.form.Form) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ApiOperation(io.swagger.annotations.ApiOperation)

Example 2 with ValidationException

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);
}
Also used : Path(java.nio.file.Path) ValidationException(com.walmartlabs.concord.server.process.form.FormUtils.ValidationException) Form(io.takari.bpm.form.Form) ProcessStatus(com.walmartlabs.concord.server.sdk.ProcessStatus) FormSubmitResult(com.walmartlabs.concord.server.process.form.FormSubmitResult) ValidationException(com.walmartlabs.concord.server.process.form.FormUtils.ValidationException) IOException(java.io.IOException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ValidationError(com.walmartlabs.concord.forms.ValidationError) ExternalFileFormValidatorLocale(com.walmartlabs.concord.server.process.form.ExternalFileFormValidatorLocale)

Aggregations

ValidationException (com.walmartlabs.concord.server.process.form.FormUtils.ValidationException)2 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)2 Form (io.takari.bpm.form.Form)2 ValidationError (com.walmartlabs.concord.forms.ValidationError)1 ExternalFileFormValidatorLocale (com.walmartlabs.concord.server.process.form.ExternalFileFormValidatorLocale)1 FormSubmitResult (com.walmartlabs.concord.server.process.form.FormSubmitResult)1 PartialProcessKey (com.walmartlabs.concord.server.sdk.PartialProcessKey)1 ProcessStatus (com.walmartlabs.concord.server.sdk.ProcessStatus)1 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1