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