Search in sources :

Example 6 with FormulaNode

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

the class EffectiveTableModel method addKeyColumns.

private void addKeyColumns(List<EffectiveTableColumn> columns, FormTree.Node node) {
    LookupKeySet lookupKeySet = new LookupKeySet(formTree, node.getField());
    Map<LookupKey, FormulaNode> formulas = lookupKeySet.getKeyFormulas(node.getFieldId());
    for (LookupKey lookupKey : lookupKeySet.getLookupKeys()) {
        ImmutableTableColumn tableColumn = ImmutableTableColumn.builder().id(node.getFieldId().asString() + "_k" + lookupKey.getKeyIndex()).formula(formulas.get(lookupKey).toString()).label(lookupKey.getKeyLabel()).build();
        columns.add(new EffectiveTableColumn(formTree, tableColumn));
    }
}
Also used : LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) FormulaNode(org.activityinfo.model.formula.FormulaNode) LookupKey(org.activityinfo.model.formTree.LookupKey) ImmutableTableColumn(org.activityinfo.model.analysis.ImmutableTableColumn)

Example 7 with FormulaNode

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

the class FormScan method prepare.

/**
 * Prepares a column query based on the requested fields and formulas.
 */
public void prepare(ColumnQueryBuilder columnQueryBuilder) {
    // check to see if we still need to hit the database after being populated by the cache
    if (columnMap.isEmpty() && foreignKeyMap.isEmpty() && rowCount == null) {
        return;
    }
    // Build the query
    ExprQueryBuilder queryBuilder = new ExprQueryBuilder(columnFactory, formClass, columnQueryBuilder);
    for (Map.Entry<FormulaNode, PendingSlot<ColumnView>> column : columnMap.entrySet()) {
        if (column.getKey().equals(PK_COLUMN_KEY)) {
            queryBuilder.addResourceId(new IdColumnBuilder(column.getValue()));
        } else {
            queryBuilder.addExpr(column.getKey(), column.getValue());
        }
    }
    // Only add a row count observer IF it has been requested AND
    // it hasn't been loaded from the cache.
    RowCountBuilder rowCountBuilder = null;
    if (rowCount != null && !rowCount.isSet()) {
        rowCountBuilder = new RowCountBuilder(rowCount);
        queryBuilder.addResourceId(rowCountBuilder);
    }
    for (Map.Entry<ForeignKeyId, PendingSlot<ForeignKey>> fk : foreignKeyMap.entrySet()) {
        queryBuilder.addField(fk.getKey().getFieldId(), columnFactory.newForeignKeyBuilder(fk.getKey().getRightFormId(), fk.getValue()));
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) ForeignKeyId(org.activityinfo.store.query.shared.join.ForeignKeyId) IdColumnBuilder(org.activityinfo.store.query.shared.columns.IdColumnBuilder) RowCountBuilder(org.activityinfo.store.query.shared.columns.RowCountBuilder)

Example 8 with FormulaNode

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

the class NodeMatcherTest method resolve.

private Collection<String> resolve(String exprString) {
    FormTree tree = tree();
    symbolTable = new NodeMatcher(tree);
    FormulaNode expr = FormulaParser.parse(exprString);
    Collection<NodeMatch> matches;
    if (expr instanceof SymbolNode) {
        matches = symbolTable.resolveSymbol((SymbolNode) expr);
    } else if (expr instanceof CompoundExpr) {
        matches = symbolTable.resolveCompoundExpr((CompoundExpr) expr);
    } else {
        throw new IllegalArgumentException(exprString);
    }
    // Create a string that we can match against easily
    List<String> strings = Lists.newArrayList();
    for (NodeMatch match : matches) {
        strings.add(match.toDebugString());
    }
    System.out.println("Resolved " + exprString + " => " + strings);
    return strings;
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) SymbolNode(org.activityinfo.model.formula.SymbolNode) FormulaNode(org.activityinfo.model.formula.FormulaNode) FormTree(org.activityinfo.model.formTree.FormTree)

Example 9 with FormulaNode

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

the class Updater method computeSerialNumberPrefix.

private String computeSerialNumberPrefix(FormClass formClass, SerialNumberType type, FormInstance effectiveRecord) {
    if (!type.hasPrefix()) {
        return null;
    }
    try {
        FormEvalContext evalContext = new FormEvalContext(formClass);
        evalContext.setInstance(effectiveRecord);
        FormulaNode formula = FormulaParser.parse(type.getPrefixFormula());
        FieldValue prefixValue = formula.evaluate(evalContext);
        if (prefixValue instanceof TextValue) {
            return ((TextValue) prefixValue).asString();
        } else {
            throw new IllegalStateException("Prefix " + type.getPrefixFormula() + " resolves to type " + prefixValue.getTypeClass().getId());
        }
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failed to compute prefix for serial number", e);
        return null;
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) TextValue(org.activityinfo.model.type.primitive.TextValue) FieldValue(org.activityinfo.model.type.FieldValue) JsonMappingException(org.activityinfo.json.JsonMappingException)

Example 10 with FormulaNode

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

the class SiteFormStorage method getPermissions.

@Override
public FormPermissions getPermissions(int userId) {
    if (activity.getOwnerUserId() == userId) {
        return FormPermissions.owner();
    } else {
        UserPermission databasePermission = permissionsCache.getPermission(userId, activity.getDatabaseId());
        FormPermissions.Builder permissions = FormPermissions.builder();
        FormulaNode partnerFilter = Formulas.equals(new SymbolNode(CuidAdapter.partnerField(activity.getId())), new ConstantNode(CuidAdapter.partnerRecordId(databasePermission.getPartnerId()).asString()));
        if (databasePermission.isViewAll()) {
            permissions.allowView();
        } else if (databasePermission.isView()) {
            permissions.allowFilteredView(partnerFilter.asExpression());
        }
        if (databasePermission.isEditAll()) {
            permissions.allowEdit();
        } else if (databasePermission.isEdit()) {
            permissions.allowFilteredEdit(partnerFilter.asExpression());
        }
        if (databasePermission.isDesign()) {
            permissions.allowSchemaUpdate();
        }
        // published property of activity overrides user permissions
        if (activity.isPublished()) {
            permissions.allowUnfilteredView();
        }
        return permissions.build();
    }
}
Also used : SymbolNode(org.activityinfo.model.formula.SymbolNode) FormulaNode(org.activityinfo.model.formula.FormulaNode) ConstantNode(org.activityinfo.model.formula.ConstantNode) UserPermission(org.activityinfo.store.mysql.metadata.UserPermission)

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