Search in sources :

Example 1 with FormDefinition

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

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

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

the class YamlProjectConverter method convert.

public static ProjectDefinition convert(YamlProject project) throws YamlConverterException {
    Map<String, ProcessDefinition> flows = convertFlows(project.getFlows());
    Set<String> publicFlows = project.getPublicFlows();
    Map<String, FormDefinition> forms = convertForms(project.getForms());
    Map<String, Object> cfg = project.getConfiguration();
    Map<String, Profile> profiles = convertProfiles(project.getProfiles());
    List<Trigger> triggers = YamlTriggersConverter.convert(project.getTriggers());
    Imports imports = convertImports(project.getImports());
    Resources resources = convertResources(project.getResources());
    return new ProjectDefinition(flows, publicFlows, forms, cfg, profiles, triggers, imports, resources);
}
Also used : ProcessDefinition(io.takari.bpm.model.ProcessDefinition) Imports(com.walmartlabs.concord.imports.Imports) YamlImportConverter.convertImports(com.walmartlabs.concord.project.yaml.YamlImportConverter.convertImports) Profile(com.walmartlabs.concord.project.model.Profile) YamlProfile(com.walmartlabs.concord.project.yaml.model.YamlProfile) ProjectDefinition(com.walmartlabs.concord.project.model.ProjectDefinition) Trigger(com.walmartlabs.concord.project.model.Trigger) Resources(com.walmartlabs.concord.project.model.Resources) FormDefinition(io.takari.bpm.model.form.FormDefinition)

Example 4 with FormDefinition

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

use of io.takari.bpm.model.form.FormDefinition 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)4 ValidationError (com.walmartlabs.concord.forms.ValidationError)1 Imports (com.walmartlabs.concord.imports.Imports)1 Profile (com.walmartlabs.concord.project.model.Profile)1 ProjectDefinition (com.walmartlabs.concord.project.model.ProjectDefinition)1 Resources (com.walmartlabs.concord.project.model.Resources)1 Trigger (com.walmartlabs.concord.project.model.Trigger)1 YamlImportConverter.convertImports (com.walmartlabs.concord.project.yaml.YamlImportConverter.convertImports)1 YamlFormDefinition (com.walmartlabs.concord.project.yaml.model.YamlFormDefinition)1 YamlFormField (com.walmartlabs.concord.project.yaml.model.YamlFormField)1 YamlProfile (com.walmartlabs.concord.project.yaml.model.YamlProfile)1 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)1 PartialProcessKey (com.walmartlabs.concord.server.sdk.PartialProcessKey)1 ApiOperation (io.swagger.annotations.ApiOperation)1 Form (io.takari.bpm.form.Form)1 ProcessDefinition (io.takari.bpm.model.ProcessDefinition)1 ArrayList (java.util.ArrayList)1