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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations