Search in sources :

Example 11 with FormulaNode

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

the class PermissionsEnforcer method can.

public boolean can(FormInstance record, Operation operation) {
    FormPermissions formPermissions = supervisor.getFormPermissions(record.getFormId());
    if (!formPermissions.isAllowed(operation)) {
        return false;
    }
    if (!formPermissions.isFiltered(operation)) {
        return true;
    }
    FormulaNode filter = parseFilter(record.getFormId(), formPermissions.getFilter(operation));
    return evalFilter(record, filter);
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode)

Example 12 with FormulaNode

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

the class TableViewModel method parentFormula.

private String parentFormula(ParsedFormula formula) {
    if (!formula.isValid()) {
        return formula.getFormula();
    }
    SymbolNode parentSymbol = new SymbolNode(ColumnModel.PARENT_SYMBOL);
    FormulaNode transformed = formula.getRootNode().transform(node -> {
        if (node instanceof SymbolNode) {
            // A -> parent.A
            return new CompoundExpr(parentSymbol, (SymbolNode) node);
        } else {
            return node;
        }
    });
    return transformed.asExpression();
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) SymbolNode(org.activityinfo.model.formula.SymbolNode) FormulaNode(org.activityinfo.model.formula.FormulaNode)

Example 13 with FormulaNode

use of org.activityinfo.model.formula.FormulaNode 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 14 with FormulaNode

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

the class FieldPath method toExpr.

public FormulaNode toExpr() {
    Iterator<ResourceId> it = path.iterator();
    FormulaNode expr = new SymbolNode(it.next());
    while (it.hasNext()) {
        expr = new CompoundExpr(expr, new SymbolNode(it.next()));
    }
    return expr;
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) SymbolNode(org.activityinfo.model.formula.SymbolNode) FormulaNode(org.activityinfo.model.formula.FormulaNode) ResourceId(org.activityinfo.model.resource.ResourceId)

Example 15 with FormulaNode

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

the class AttributeFieldBinding method createAttributeColumns.

private List<ColumnModel> createAttributeColumns(EnumItem[] items) {
    List<ColumnModel> columns = new ArrayList<>(items.length);
    for (int i = 0; i < items.length; i++) {
        String itemId = items[i].getId().asString();
        FormulaNode expr = new CompoundExpr(attrField.getId(), itemId);
        columns.add(new ColumnModel().setFormula(expr).as(itemId));
    }
    return columns;
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) FormulaNode(org.activityinfo.model.formula.FormulaNode) ArrayList(java.util.ArrayList)

Aggregations

FormulaNode (org.activityinfo.model.formula.FormulaNode)27 SymbolNode (org.activityinfo.model.formula.SymbolNode)8 FormTree (org.activityinfo.model.formTree.FormTree)7 CompoundExpr (org.activityinfo.model.formula.CompoundExpr)7 Test (org.junit.Test)7 LookupKey (org.activityinfo.model.formTree.LookupKey)5 LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)5 ColumnView (org.activityinfo.model.query.ColumnView)4 FormulaParser (org.activityinfo.model.formula.FormulaParser)3 ResourceId (org.activityinfo.model.resource.ResourceId)3 ForeignKeyId (org.activityinfo.store.query.shared.join.ForeignKeyId)3 FormEvalContext (org.activityinfo.model.form.FormEvalContext)2 FormField (org.activityinfo.model.form.FormField)2 FormPermissions (org.activityinfo.model.form.FormPermissions)2 FormulaLexer (org.activityinfo.model.formula.FormulaLexer)2 FieldValue (org.activityinfo.model.type.FieldValue)2 JoinNode (org.activityinfo.store.query.shared.join.JoinNode)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Optional (com.google.common.base.Optional)1 HashMultimap (com.google.common.collect.HashMultimap)1