use of com.walmartlabs.concord.forms.Form in project concord by walmartlabs.
the class FormCallCommand method execute.
@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
String eventRef = UUID.randomUUID().toString();
Context ctx = runtime.getService(Context.class);
ExpressionEvaluator expressionEvaluator = runtime.getService(ExpressionEvaluator.class);
EvalContext evalContext = EvalContextFactory.global(ctx);
FormCall call = getStep();
String formName = expressionEvaluator.eval(evalContext, call.getName(), String.class);
ProcessDefinition processDefinition = runtime.getService(ProcessDefinition.class);
ProcessConfiguration processConfiguration = runtime.getService(ProcessConfiguration.class);
List<FormField> fields = assertFormFields(expressionEvaluator, evalContext, processConfiguration, processDefinition, formName, call);
Form form = Form.builder().name(formName).eventName(eventRef).options(buildFormOptions(expressionEvaluator, evalContext, call)).fields(buildFormFields(expressionEvaluator, evalContext, fields, Objects.requireNonNull(call.getOptions()).values())).build();
FormService formService = runtime.getService(FormService.class);
formService.save(form);
state.peekFrame(threadId).pop();
state.setEventRef(threadId, eventRef);
state.setStatus(threadId, ThreadStatus.SUSPENDED);
}
use of com.walmartlabs.concord.forms.Form in project concord by walmartlabs.
the class FormResourceV2 method get.
/**
* Return the current state of a form instance.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public FormInstanceEntry get(UUID processInstanceId, 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);
}
List<FormInstanceEntry.Field> fields = new ArrayList<>();
for (FormField f : form.fields()) {
String fieldName = f.name();
FormInstanceEntry.Cardinality c = map(f.cardinality());
String type = f.type();
Serializable value = f.defaultValue();
Serializable allowedValue = f.allowedValue();
Map options = f.options();
fields.add(new FormInstanceEntry.Field(fieldName, f.label(), type, c, value, allowedValue, options));
}
String name = form.name();
boolean yield = form.options().yield();
String resourcePath = FORMS_RESOURCES_PATH + "/" + name;
boolean isCustomForm = formService.exists(processKey, resourcePath);
return new FormInstanceEntry(processInstanceId.toString(), name, fields, isCustomForm, yield);
}
use of com.walmartlabs.concord.forms.Form 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);
}
use of com.walmartlabs.concord.forms.Form in project concord by walmartlabs.
the class CustomFormServiceV2 method startSession.
private FormSessionResponse startSession(ProcessKey processKey, String formName) {
// TODO locking
Form form = assertForm(processKey, formName);
Path dst = cfg.getBaseDir().resolve(processKey.toString()).resolve(formName);
try {
Path formDir = dst.resolve(FORM_DIR_NAME);
if (!Files.exists(formDir)) {
Files.createDirectories(formDir);
}
String resource = FormServiceV1.FORMS_RESOURCES_PATH + "/" + form.name();
// copy original branding files into the target directory
boolean branded = stateManager.exportDirectory(processKey, resource, copyTo(formDir));
if (!branded) {
// not branded, redirect to the default wizard
String uri = String.format(NON_BRANDED_FORM_URL_TEMPLATE, processKey, formName);
return new FormSessionResponse(uri);
}
// create JS file containing the form's data
writeData(formDir, initialData(form, processKey.getInstanceId()));
// copy shared resources (if present)
copySharedResources(processKey, dst);
} catch (IOException e) {
log.warn("startSession ['{}', '{}'] -> error while preparing a custom form: {}", processKey, formName, e);
throw new ConcordApplicationException("Error while preparing a custom form", e);
}
return new FormSessionResponse(formPath(processKey, formName));
}
use of com.walmartlabs.concord.forms.Form 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);
}
Aggregations