use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.
the class PermissionFilters method getReferenceBaseFilter.
/**
* Returns a filter for records referenced by the given field.
*/
public Optional<FormulaNode> getReferenceBaseFilter(ResourceId fieldId) {
FormulaNode filter = fieldFilters.get(fieldId);
if (filter == null) {
return Optional.absent();
}
SymbolNode fieldExpr = new SymbolNode(fieldId);
return Optional.of(filter.transform(x -> {
if (x.equals(fieldExpr)) {
return new SymbolNode(ColumnModel.ID_SYMBOL);
} else {
return x;
}
}));
}
use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.
the class FormInputViewModelBuilder method buildRelevanceCalculator.
private void buildRelevanceCalculator(FormTree.Node node) {
String formula = node.getField().getRelevanceConditionExpression();
FormulaNode rootNode;
try {
rootNode = FormulaParser.parse(formula);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Invalid relevance formula: " + formula, e);
return;
}
relevanceCalculators.put(node.getFieldId(), instance -> {
evalContext.setInstance(instance);
try {
return rootNode.evaluateAsBoolean(evalContext);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to evaluate relevance condition", e);
return true;
}
});
}
use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.
the class LookupKeySetTest method multipleTextKeysTest.
@Test
public void multipleTextKeysTest() {
TestingStorageProvider catalog = setup.getCatalog();
SimpleReferenceForm refForm = catalog.getSimpleReferenceForm();
MultipleTextKeysForm multTextKeyForm = catalog.getMultipleTextKeysForm();
FormField refField = refForm.getRefField();
CompoundExpr pathToKey1 = new CompoundExpr(refField.getId(), MultipleTextKeysForm.FIRST_TEXT_KEY_ID.asString());
CompoundExpr pathToKey2 = new CompoundExpr(refField.getId(), MultipleTextKeysForm.SECOND_TEXT_KEY_ID.asString());
FormTree formTree = catalog.getFormTree(multTextKeyForm.getFormId());
LookupKeySet lookupKeySet = new LookupKeySet(formTree, refField);
Map<LookupKey, FormulaNode> formulas = lookupKeySet.getKeyFormulas(refField.getId());
assertThat(formulas.values(), containsInAnyOrder(pathToKey1, pathToKey2));
}
use of org.activityinfo.model.formula.FormulaNode 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.FormulaNode 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