Search in sources :

Example 1 with FormEvalContext

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);
        }
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) FormulaLexer(org.activityinfo.model.formula.FormulaLexer) FormClass(org.activityinfo.model.form.FormClass) FormulaParser(org.activityinfo.model.formula.FormulaParser) FormInstance(org.activityinfo.model.form.FormInstance) FormEvalContext(org.activityinfo.model.form.FormEvalContext)

Example 2 with FormEvalContext

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))));
}
Also used : FormEvalContext(org.activityinfo.model.form.FormEvalContext)

Example 3 with FormEvalContext

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));
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) FormClass(org.activityinfo.model.form.FormClass) ErrorValue(org.activityinfo.model.type.ErrorValue) FormInstance(org.activityinfo.model.form.FormInstance) FormField(org.activityinfo.model.form.FormField) FormEvalContext(org.activityinfo.model.form.FormEvalContext) Test(org.junit.Test)

Example 4 with FormEvalContext

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;
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) FieldValue(org.activityinfo.model.type.FieldValue) BooleanFieldValue(org.activityinfo.model.type.primitive.BooleanFieldValue) FormEvalContext(org.activityinfo.model.form.FormEvalContext)

Example 5 with FormEvalContext

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))));
}
Also used : FormEvalContext(org.activityinfo.model.form.FormEvalContext)

Aggregations

FormEvalContext (org.activityinfo.model.form.FormEvalContext)5 FormClass (org.activityinfo.model.form.FormClass)2 FormInstance (org.activityinfo.model.form.FormInstance)2 FormulaNode (org.activityinfo.model.formula.FormulaNode)2 FormField (org.activityinfo.model.form.FormField)1 FormulaLexer (org.activityinfo.model.formula.FormulaLexer)1 FormulaParser (org.activityinfo.model.formula.FormulaParser)1 ErrorValue (org.activityinfo.model.type.ErrorValue)1 FieldValue (org.activityinfo.model.type.FieldValue)1 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)1 BooleanFieldValue (org.activityinfo.model.type.primitive.BooleanFieldValue)1 Test (org.junit.Test)1