Search in sources :

Example 1 with ValidationError

use of com.walmartlabs.concord.forms.ValidationError in project concord by walmartlabs.

the class CustomFormServiceV2 method continueSession.

private Response continueSession(UriInfo uriInfo, HttpHeaders headers, ProcessKey processKey, String formName, Map<String, Object> data) {
    // TODO locking
    Form form = assertForm(processKey, formName);
    boolean yield = form.options().yield();
    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 ExternalFileFormValidatorLocaleV2(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, processKey.getInstanceId()));
                } 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, processKey.getInstanceId()));
                                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, processKey.getInstanceId()));
                            break;
                        } else if (s == ProcessStatus.FINISHED) {
                            writeData(formDir, success(form, m, processKey.getInstanceId()));
                            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(), processKey.getInstanceId()));
            }
        } catch (FormUtils.ValidationException e) {
            ValidationError err = ValidationError.of(e.getField().name(), e.getMessage());
            FormData d = prepareData(form, m, Collections.singletonList(err), processKey.getInstanceId());
            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) FormData(com.walmartlabs.concord.server.console.CustomFormServiceV1.FormData) Form(com.walmartlabs.concord.forms.Form) ProcessStatus(com.walmartlabs.concord.server.sdk.ProcessStatus) FormSubmitResult(com.walmartlabs.concord.server.process.form.FormSubmitResult) FormUtils(com.walmartlabs.concord.forms.FormUtils) IOException(java.io.IOException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ValidationError(com.walmartlabs.concord.forms.ValidationError) ExternalFileFormValidatorLocaleV2(com.walmartlabs.concord.server.process.form.ExternalFileFormValidatorLocaleV2)

Example 2 with ValidationError

use of com.walmartlabs.concord.forms.ValidationError in project concord by walmartlabs.

the class CustomFormServiceV2 method prepareData.

private FormData prepareData(boolean success, boolean processFailed, Form form, Map<String, Object> overrides, boolean skipMissingOverrides, List<ValidationError> errors, UUID processInstanceId) {
    // TODO merge with FormResource
    Map<String, FormDataDefinition> _definitions = new HashMap<>();
    // the order of precedence should be:
    // submitted value > form call value > field's default value > environment value
    Map<String, Object> _values = new HashMap<>(form.options().extraValues());
    Map<String, String> _errors = new HashMap<>();
    form.fields().stream().filter(f -> f.defaultValue() != null).forEach(f -> _values.put(f.name(), f.defaultValue()));
    List<String> fields = form.fields().stream().map(FormField::name).collect(Collectors.toList());
    for (FormField f : form.fields()) {
        Object allowedValue = f.allowedValue();
        _definitions.put(f.name(), new FormDataDefinition(f.label(), f.type(), convert(f.cardinality()), allowedValue));
        Object v = overrides != null ? overrides.get(f.name()) : null;
        if (v == null && skipMissingOverrides) {
            continue;
        }
        if (v == null) {
            continue;
        }
        _values.put(f.name(), v);
    }
    if (errors != null) {
        for (ValidationError e : errors) {
            _errors.put(e.fieldName(), e.error());
        }
    }
    String submitUrl = String.format(FORM_WIZARD_CONTINUE_URL_TEMPLATE, processInstanceId, form.name());
    return new FormData(success, processFailed, submitUrl, fields, _definitions, _values, _errors);
}
Also used : PathParam(javax.ws.rs.PathParam) java.util(java.util) FormField(com.walmartlabs.concord.forms.FormField) Produces(javax.ws.rs.Produces) ProcessStateManager.copyTo(com.walmartlabs.concord.server.process.state.ProcessStateManager.copyTo) LoggerFactory(org.slf4j.LoggerFactory) MultipartInput(org.jboss.resteasy.plugins.providers.multipart.MultipartInput) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) ProcessKeyCache(com.walmartlabs.concord.server.process.queue.ProcessKeyCache) Singleton(javax.inject.Singleton) ValidationError(com.walmartlabs.concord.forms.ValidationError) Inject(javax.inject.Inject) MultipartUtils(com.walmartlabs.concord.server.MultipartUtils) ProcessQueueDao(com.walmartlabs.concord.server.process.queue.ProcessQueueDao) Consumes(javax.ws.rs.Consumes) FormSubmitResult(com.walmartlabs.concord.server.process.form.FormSubmitResult) Named(javax.inject.Named) URI(java.net.URI) Status(javax.ws.rs.core.Response.Status) Path(java.nio.file.Path) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ProcessStateManager(com.walmartlabs.concord.server.process.state.ProcessStateManager) FormUtils(com.walmartlabs.concord.forms.FormUtils) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) FormData(com.walmartlabs.concord.server.console.CustomFormServiceV1.FormData) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Validate(org.sonatype.siesta.Validate) StandardOpenOption(java.nio.file.StandardOpenOption) ExternalFileFormValidatorLocaleV2(com.walmartlabs.concord.server.process.form.ExternalFileFormValidatorLocaleV2) ProcessStatus(com.walmartlabs.concord.server.sdk.ProcessStatus) IOException(java.io.IOException) javax.ws.rs.core(javax.ws.rs.core) Collectors(java.util.stream.Collectors) CustomFormConfiguration(com.walmartlabs.concord.server.cfg.CustomFormConfiguration) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) Form(com.walmartlabs.concord.forms.Form) FormDataDefinition(com.walmartlabs.concord.server.console.CustomFormServiceV1.FormDataDefinition) FormServiceV1(com.walmartlabs.concord.server.process.form.FormServiceV1) FormServiceV2(com.walmartlabs.concord.server.process.form.FormServiceV2) FormData(com.walmartlabs.concord.server.console.CustomFormServiceV1.FormData) ValidationError(com.walmartlabs.concord.forms.ValidationError) FormDataDefinition(com.walmartlabs.concord.server.console.CustomFormServiceV1.FormDataDefinition) FormField(com.walmartlabs.concord.forms.FormField)

Example 3 with ValidationError

use of com.walmartlabs.concord.forms.ValidationError 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)

Example 4 with ValidationError

use of com.walmartlabs.concord.forms.ValidationError in project concord by walmartlabs.

the class CustomFormServiceV1 method prepareData.

private FormData prepareData(boolean success, boolean processFailed, Form form, Map<String, Object> overrides, boolean skipMissingOverrides, List<ValidationError> errors) {
    String processInstanceId = form.getProcessBusinessKey();
    String formName = form.getFormDefinition().getName();
    String submitUrl = String.format(FORM_WIZARD_CONTINUE_URL_TEMPLATE, processInstanceId, formName);
    // TODO merge with FormResource
    Map<String, FormDataDefinition> _definitions = new HashMap<>();
    // the order of precedence should be:
    // submitted value > form call value > field's default value > environment value
    Map<String, String> _errors = new HashMap<>();
    Map<String, Object> data = FormUtils.values(form);
    Map<String, Object> extra = FormUtils.extraValues(form);
    Map<String, Object> _values = new HashMap<>(extra);
    Map<String, Object> allowedValues = form.getAllowedValues();
    if (allowedValues == null) {
        allowedValues = Collections.emptyMap();
    }
    FormDefinition fd = form.getFormDefinition();
    List<String> fields = fd.getFields().stream().map(FormField::getName).collect(Collectors.toList());
    for (FormField f : fd.getFields()) {
        Object allowedValue = allowedValues.get(f.getName());
        _definitions.put(f.getName(), new FormDataDefinition(f.getLabel(), f.getType(), f.getCardinality(), allowedValue));
        Object v = overrides != null ? overrides.get(f.getName()) : null;
        if (v == null && skipMissingOverrides) {
            continue;
        }
        if (v == null) {
            v = data.get(f.getName());
        }
        if (v == null) {
            continue;
        }
        _values.put(f.getName(), v);
    }
    if (errors != null) {
        for (ValidationError e : errors) {
            _errors.put(e.fieldName(), e.error());
        }
    }
    return new FormData(success, processFailed, submitUrl, fields, _definitions, _values, _errors);
}
Also used : ValidationError(com.walmartlabs.concord.forms.ValidationError) FormDefinition(io.takari.bpm.model.form.FormDefinition) FormField(io.takari.bpm.model.form.FormField)

Aggregations

ValidationError (com.walmartlabs.concord.forms.ValidationError)4 FormSubmitResult (com.walmartlabs.concord.server.process.form.FormSubmitResult)3 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)3 ProcessStatus (com.walmartlabs.concord.server.sdk.ProcessStatus)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 Form (com.walmartlabs.concord.forms.Form)2 FormUtils (com.walmartlabs.concord.forms.FormUtils)2 FormData (com.walmartlabs.concord.server.console.CustomFormServiceV1.FormData)2 ExternalFileFormValidatorLocaleV2 (com.walmartlabs.concord.server.process.form.ExternalFileFormValidatorLocaleV2)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 FormField (com.walmartlabs.concord.forms.FormField)1 MultipartUtils (com.walmartlabs.concord.server.MultipartUtils)1 CustomFormConfiguration (com.walmartlabs.concord.server.cfg.CustomFormConfiguration)1 FormDataDefinition (com.walmartlabs.concord.server.console.CustomFormServiceV1.FormDataDefinition)1 ExternalFileFormValidatorLocale (com.walmartlabs.concord.server.process.form.ExternalFileFormValidatorLocale)1 FormServiceV1 (com.walmartlabs.concord.server.process.form.FormServiceV1)1 FormServiceV2 (com.walmartlabs.concord.server.process.form.FormServiceV2)1 ValidationException (com.walmartlabs.concord.server.process.form.FormUtils.ValidationException)1