Search in sources :

Example 1 with FormField

use of io.takari.bpm.model.form.FormField in project concord by walmartlabs.

the class FormUtils method convert.

// TODO this probably should be a part of the bpm engine's FormService
public static Map<String, Object> convert(ConcordFormValidatorLocale locale, Form form, Map<String, Object> m) throws ValidationException {
    FormDefinition fd = form.getFormDefinition();
    Map<String, String> tmpFiles = new HashMap<>();
    Map<String, Object> m2 = new HashMap<>();
    m2.put(Constants.Files.FORM_FILES, tmpFiles);
    Map<String, Object> defaultData = FormUtils.values(form);
    for (FormField f : fd.getFields()) {
        String k = f.getName();
        Object v = Boolean.TRUE.equals(f.getOption(READ_ONLY)) ? defaultData.get(k) : m.get(k);
        boolean isOptional = f.getCardinality() == FormField.Cardinality.ONE_OR_NONE || f.getCardinality() == FormField.Cardinality.ANY;
        if (v == null && !isOptional) {
            Map<String, Object> allowedValues = form.getAllowedValues();
            if (allowedValues == null) {
                allowedValues = Collections.emptyMap();
            }
            v = allowedValueAsFieldValue(allowedValues.get(f.getName()));
        }
        /*
             * Use cardinality as an indicator to convert single value (coming as a string) into an array
             * for the scenario when only one value was provided by the user
             */
        if (v instanceof String && (f.getCardinality() == FormField.Cardinality.ANY || f.getCardinality() == FormField.Cardinality.AT_LEAST_ONE)) {
            v = Collections.singletonList(v);
        }
        v = convert(locale, fd.getName(), f, null, v);
        if (v == null && !isOptional) {
            continue;
        }
        if (v != null && f.getType().equals(ConcordFormFields.FileField.TYPE)) {
            String wsFileName = "_form_files/" + form.getFormDefinition().getName() + "/" + f.getName();
            tmpFiles.put(wsFileName, (String) v);
            m2.put(k, wsFileName);
        } else {
            m2.put(k, v);
        }
    }
    return m2;
}
Also used : FormDefinition(io.takari.bpm.model.form.FormDefinition) FormField(io.takari.bpm.model.form.FormField)

Example 2 with FormField

use of io.takari.bpm.model.form.FormField 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)

Example 3 with FormField

use of io.takari.bpm.model.form.FormField in project concord by walmartlabs.

the class AbstractYamlParserTest method setUp.

@BeforeEach
@SuppressWarnings("unchecked")
public void setUp() {
    taskRegistry = new TestServiceTaskRegistry();
    forms = new HashMap<>();
    FormStorage fs = new TestFormStorage(forms);
    ExpressionManager expressionManager = new DefaultExpressionManager(taskRegistry);
    DefaultExecutionContextFactory contextFactory = new DefaultExecutionContextFactory(expressionManager);
    DefaultFormService.ResumeHandler rs = (form, args) -> getEngine().resume(form.getProcessBusinessKey(), form.getEventName(), args);
    formService = new DefaultFormService(contextFactory, rs, fs) {

        @Override
        public FormField toFormField(Map<String, Object> m) {
            Map.Entry<String, Object> entry = m.entrySet().iterator().next();
            Map<String, Object> opts = (Map<String, Object>) entry.getValue();
            return new FormField.Builder(entry.getKey(), (String) opts.get("type")).build();
        }
    };
    ResourceResolver resourceResolver = ClassLoader::getSystemResourceAsStream;
    Configuration cfg = new Configuration();
    cfg.setInterpolateInputVariables(true);
    cfg.setWrapAllExceptionsAsBpmnErrors(true);
    cfg.setCopyAllCallActivityOutVariables(true);
    engine = new EngineBuilder().withDefinitionProvider(workflowProvider.processes()).withTaskRegistry(taskRegistry).withUserTaskHandler(new FormTaskHandler(contextFactory, workflowProvider.forms(), formService)).withResourceResolver(resourceResolver).withConfiguration(cfg).build();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Configuration(io.takari.bpm.Configuration) ImportManager(com.walmartlabs.concord.imports.ImportManager) ProcessDefinitionProvider(io.takari.bpm.ProcessDefinitionProvider) HashMap(java.util.HashMap) FormField(io.takari.bpm.model.form.FormField) UUID(java.util.UUID) DefaultExpressionManager(io.takari.bpm.el.DefaultExpressionManager) ExecutionException(io.takari.bpm.api.ExecutionException) ExpressionManager(io.takari.bpm.el.ExpressionManager) EngineBuilder(io.takari.bpm.EngineBuilder) ServiceTaskRegistry(io.takari.bpm.task.ServiceTaskRegistry) DefaultExecutionContextFactory(io.takari.bpm.context.DefaultExecutionContextFactory) Engine(io.takari.bpm.api.Engine) ProcessDefinition(io.takari.bpm.model.ProcessDefinition) ProjectDefinition(com.walmartlabs.concord.project.model.ProjectDefinition) Map(java.util.Map) FormDefinition(io.takari.bpm.model.form.FormDefinition) ResourceResolver(io.takari.bpm.resource.ResourceResolver) io.takari.bpm.form(io.takari.bpm.form) ProjectLoader(com.walmartlabs.concord.project.ProjectLoader) InputStream(java.io.InputStream) Mockito.mock(org.mockito.Mockito.mock) DefaultExpressionManager(io.takari.bpm.el.DefaultExpressionManager) ExpressionManager(io.takari.bpm.el.ExpressionManager) Configuration(io.takari.bpm.Configuration) EngineBuilder(io.takari.bpm.EngineBuilder) DefaultExecutionContextFactory(io.takari.bpm.context.DefaultExecutionContextFactory) DefaultExpressionManager(io.takari.bpm.el.DefaultExpressionManager) ResourceResolver(io.takari.bpm.resource.ResourceResolver) FormField(io.takari.bpm.model.form.FormField) HashMap(java.util.HashMap) Map(java.util.Map) EngineBuilder(io.takari.bpm.EngineBuilder) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with FormField

use of io.takari.bpm.model.form.FormField in project concord by walmartlabs.

the class YamlFormConverter method convert.

public static FormDefinition convert(String name, List<YamlFormField> fields) throws YamlConverterException {
    Map<String, Object> options = null;
    List<FormField> l = new ArrayList<>();
    for (YamlFormField f : fields) {
        if (f == null || f.getName() == null) {
            throw new YamlConverterException("Empty field definition in form '" + name + "'");
        }
        if (OPTIONS_FIELD_NAME.equals(f.getName())) {
            if (options != null) {
                throw new YamlConverterException("Duplicate options definition in form '" + name + "'");
            }
            options = f.getOptions();
            continue;
        }
        l.add(convert(f));
    }
    return new FormDefinition(name, l);
}
Also used : ArrayList(java.util.ArrayList) YamlFormField(com.walmartlabs.concord.project.yaml.model.YamlFormField) YamlFormDefinition(com.walmartlabs.concord.project.yaml.model.YamlFormDefinition) FormDefinition(io.takari.bpm.model.form.FormDefinition) FormField(io.takari.bpm.model.form.FormField) YamlFormField(com.walmartlabs.concord.project.yaml.model.YamlFormField)

Example 5 with FormField

use of io.takari.bpm.model.form.FormField in project concord by walmartlabs.

the class FormResourceV1 method get.

/**
 * Return the current state of a form instance.
 */
@GET
@ApiOperation("Get the current state of a form")
@Path("/{processInstanceId}/form/{formName}")
@Produces(MediaType.APPLICATION_JSON)
public FormInstanceEntry get(@ApiParam @PathParam("processInstanceId") UUID processInstanceId, @ApiParam @PathParam("formName") String formName) {
    PartialProcessKey processKey = PartialProcessKey.from(processInstanceId);
    Form form = formService.get(processKey, formName);
    if (form == null) {
        throw new ConcordApplicationException("Form " + formName + " not found. Process ID: " + processKey, Status.NOT_FOUND);
    }
    Map<String, Object> data = FormUtils.values(form);
    boolean yield = MapUtils.getBoolean(form.getOptions(), "yield", false);
    Map<String, Object> allowedValues = form.getAllowedValues();
    if (allowedValues == null) {
        allowedValues = Collections.emptyMap();
    }
    List<FormInstanceEntry.Field> fields = new ArrayList<>();
    FormDefinition fd = form.getFormDefinition();
    for (FormField f : fd.getFields()) {
        String fieldName = f.getName();
        FormInstanceEntry.Cardinality c = map(f.getCardinality());
        String type = f.getType();
        Object value = data.get(fieldName);
        Object allowedValue = allowedValues.get(fieldName);
        fields.add(new FormInstanceEntry.Field(fieldName, f.getLabel(), type, c, value, allowedValue, f.getOptions()));
    }
    String pbk = form.getProcessBusinessKey();
    String name = fd.getName();
    String resourcePath = FORMS_RESOURCES_PATH + "/" + name;
    boolean isCustomForm = formService.exists(processKey, resourcePath);
    return new FormInstanceEntry(pbk, name, fields, isCustomForm, yield);
}
Also used : PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) Form(io.takari.bpm.form.Form) FormField(io.takari.bpm.model.form.FormField) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) FormDefinition(io.takari.bpm.model.form.FormDefinition) FormField(io.takari.bpm.model.form.FormField) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

FormDefinition (io.takari.bpm.model.form.FormDefinition)5 FormField (io.takari.bpm.model.form.FormField)5 ValidationError (com.walmartlabs.concord.forms.ValidationError)1 ImportManager (com.walmartlabs.concord.imports.ImportManager)1 ProjectLoader (com.walmartlabs.concord.project.ProjectLoader)1 ProjectDefinition (com.walmartlabs.concord.project.model.ProjectDefinition)1 YamlFormDefinition (com.walmartlabs.concord.project.yaml.model.YamlFormDefinition)1 YamlFormField (com.walmartlabs.concord.project.yaml.model.YamlFormField)1 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)1 PartialProcessKey (com.walmartlabs.concord.server.sdk.PartialProcessKey)1 ApiOperation (io.swagger.annotations.ApiOperation)1 Configuration (io.takari.bpm.Configuration)1 EngineBuilder (io.takari.bpm.EngineBuilder)1 ProcessDefinitionProvider (io.takari.bpm.ProcessDefinitionProvider)1 Engine (io.takari.bpm.api.Engine)1 ExecutionException (io.takari.bpm.api.ExecutionException)1 DefaultExecutionContextFactory (io.takari.bpm.context.DefaultExecutionContextFactory)1 DefaultExpressionManager (io.takari.bpm.el.DefaultExpressionManager)1 ExpressionManager (io.takari.bpm.el.ExpressionManager)1 io.takari.bpm.form (io.takari.bpm.form)1