use of org.activityinfo.model.form.FormEvalContext in project activityinfo by bedatadriven.
the class RelevanceHandler method applyRelevanceLogic.
private void applyRelevanceLogic(final FormField field) {
if (field.hasRelevanceConditionExpression()) {
try {
FormulaLexer lexer = new FormulaLexer(field.getRelevanceConditionExpression());
FormulaParser parser = new FormulaParser(lexer);
FormulaNode expr = parser.parse();
FieldContainer fieldContainer = simpleFormPanel.getWidgetCreator().get(field.getId());
if (fieldContainer != null) {
FormModel model = simpleFormPanel.getModel();
Optional<FormInstance> instance = model.getWorkingInstance(field.getId(), simpleFormPanel.getSelectedKey(field));
FormClass formClass = model.getClassByField(field.getId());
boolean relevant;
if (instance.isPresent()) {
relevant = expr.evaluateAsBoolean(new FormEvalContext(formClass, instance.get()));
} else {
relevant = expr.evaluateAsBoolean(new FormEvalContext(formClass, new FormInstance(ResourceId.generateSubmissionId(formClass), formClass.getId())));
}
fieldContainer.getFieldWidget().setReadOnly(!relevant);
if (!relevant) {
if (resettingValues) {
// we are in resetting state -> handle nested relevance
fieldContainer.getFieldWidget().clearValue();
fieldContainer.getFieldWidget().fireValueChanged();
}
fieldsWithAppliedRelevance.add(fieldContainer);
} else {
fieldsWithAppliedRelevance.remove(fieldContainer);
}
} else {
Log.error("Can't find container for fieldId: " + field.getId() + ", fieldName: " + field.getLabel() + ", expression: " + field.getRelevanceConditionExpression());
}
} catch (Exception e) {
Log.error("Error: Unable to apply relevance logic. FieldId: " + field.getId() + ", fieldName: " + field.getLabel() + ", expression: " + field.getRelevanceConditionExpression(), e);
}
}
}
use of org.activityinfo.model.form.FormEvalContext in project activityinfo by bedatadriven.
the class RelevanceExpressionTest method eval.
private void eval(String relevanceExpression, boolean expectedValue, FormInstance instance) {
FormulaLexer lexer = new FormulaLexer(relevanceExpression);
FormulaParser parser = new FormulaParser(lexer);
FormulaNode expr = parser.parse();
Assert.assertEquals(relevanceExpression, expectedValue, Casting.toBoolean(expr.evaluate(new FormEvalContext(formClass, instance))));
}
use of org.activityinfo.model.form.FormEvalContext in project activityinfo by bedatadriven.
the class FormEvalContextTest method circularRefs.
@Test
public void circularRefs() {
FormField a = new FormField(ResourceId.generateId());
a.setCode("A");
a.setType(new CalculatedFieldType("B+1"));
a.setLabel("A");
FormField b = new FormField(ResourceId.generateId());
b.setCode("B");
b.setType(new CalculatedFieldType("A/50"));
b.setLabel("B");
FormClass formClass = new FormClass(ResourceId.generateId());
formClass.addElement(a);
formClass.addElement(b);
FormEvalContext context = new FormEvalContext(formClass);
context.setInstance(new FormInstance(ResourceId.generateSubmissionId(formClass), formClass.getId()));
assertThat(context.getFieldValue(a.getId()), instanceOf(ErrorValue.class));
}
use of org.activityinfo.model.form.FormEvalContext in project activityinfo by bedatadriven.
the class SelectionViewModel method evalPermission.
private static boolean evalPermission(FormMetadata form, FormInstance record, Operation operation) {
if (!form.getPermissions().isAllowed(operation)) {
return false;
}
if (!form.getPermissions().isFiltered(operation)) {
return true;
}
String filter = form.getPermissions().getFilter(operation);
try {
FormEvalContext context = new FormEvalContext(form.getSchema(), record);
FormulaNode formula = FormulaParser.parse(filter);
FieldValue result = formula.evaluate(context);
return result == BooleanFieldValue.TRUE;
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to evaluate permission filter '" + filter + "'", e);
return false;
}
}
use of org.activityinfo.model.form.FormEvalContext in project activityinfo by bedatadriven.
the class SkipExpressionTest method eval.
private void eval(String relevanceExpression, boolean expectedValue, FormInstance instance) {
FormulaLexer lexer = new FormulaLexer(relevanceExpression);
FormulaParser parser = new FormulaParser(lexer);
FormulaNode expr = parser.parse();
Assert.assertEquals(relevanceExpression, expectedValue, Casting.toBoolean(expr.evaluate(new FormEvalContext(formClass, instance))));
}
Aggregations