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