Search in sources :

Example 1 with FormulaParser

use of org.activityinfo.model.formula.FormulaParser 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 FormulaParser

use of org.activityinfo.model.formula.FormulaParser in project activityinfo by bedatadriven.

the class CalculatedTypeEditor method validateExpression.

private void validateExpression() {
    String expression = expressionTextBox.getValue();
    if (Strings.isNullOrEmpty(expression)) {
        expressionGroup.setShowValidationMessage(false);
        expressionGroup.validationStateType(ValidationStateType.NONE);
        return;
    }
    try {
        FormulaLexer lexer = new FormulaLexer(expression);
        FormulaParser parser = new FormulaParser(lexer);
        FormulaNode expr = parser.parse();
    } catch (Exception e) {
        expressionGroup.setShowValidationMessage(true);
        expressionGroup.setValidationMessage(I18N.CONSTANTS.calculationExpressionIsInvalid());
        expressionGroup.validationStateType(ValidationStateType.ERROR);
        return;
    }
    // Ok
    expressionGroup.setShowValidationMessage(false);
    expressionGroup.validationStateType(ValidationStateType.SUCCESS);
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) FormulaLexer(org.activityinfo.model.formula.FormulaLexer) FormulaParser(org.activityinfo.model.formula.FormulaParser)

Aggregations

FormulaLexer (org.activityinfo.model.formula.FormulaLexer)2 FormulaNode (org.activityinfo.model.formula.FormulaNode)2 FormulaParser (org.activityinfo.model.formula.FormulaParser)2 FormClass (org.activityinfo.model.form.FormClass)1 FormEvalContext (org.activityinfo.model.form.FormEvalContext)1 FormInstance (org.activityinfo.model.form.FormInstance)1