Search in sources :

Example 1 with ExternalFileFormValidatorLocale

use of com.walmartlabs.concord.server.process.form.ExternalFileFormValidatorLocale 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

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 ValidationException (com.walmartlabs.concord.server.process.form.FormUtils.ValidationException)1 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)1 ProcessStatus (com.walmartlabs.concord.server.sdk.ProcessStatus)1 Form (io.takari.bpm.form.Form)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1