Search in sources :

Example 51 with ColumnView

use of org.activityinfo.model.query.ColumnView in project activityinfo by bedatadriven.

the class FormScan method getValuesToCache.

public Map<String, Object> getValuesToCache() {
    Map<String, Object> toPut = new HashMap<>();
    for (Map.Entry<FormulaNode, PendingSlot<ColumnView>> column : columnMap.entrySet()) {
        ColumnView value;
        try {
            value = column.getValue().get();
        } catch (IllegalStateException e) {
            throw new IllegalStateException(column.getKey().toString(), e);
        }
        toPut.put(fieldCacheKey(column.getKey()), value);
    }
    for (Map.Entry<ForeignKeyId, PendingSlot<ForeignKey>> fk : foreignKeyMap.entrySet()) {
        toPut.put(fkCacheKey(fk.getKey()), fk.getValue().get());
    }
    if (!columnMap.isEmpty()) {
        toPut.put(rowCountKey(), rowCountFromColumn(columnMap));
    } else if (rowCount != null) {
        toPut.put(rowCountKey(), rowCount.get());
    }
    return toPut;
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) ForeignKeyId(org.activityinfo.store.query.shared.join.ForeignKeyId) ColumnView(org.activityinfo.model.query.ColumnView)

Example 52 with ColumnView

use of org.activityinfo.model.query.ColumnView in project activityinfo by bedatadriven.

the class FormScan method updateFromCache.

public void updateFromCache(Map<String, Object> cached) {
    // See which columns we could retrieve from cache
    for (FormulaNode fieldId : Lists.newArrayList(columnMap.keySet())) {
        ColumnView view = (ColumnView) cached.get(fieldCacheKey(fieldId));
        if (view != null) {
            // populate the pending result slot with the view from the cache
            columnMap.get(fieldId).set(view);
            // remove this column from the list of columns to fetch
            columnMap.remove(fieldId);
            // resolve the rowCount slot if still needed
            if (rowCount != null) {
                rowCount.set(view.numRows());
                rowCount = null;
            }
        }
    }
    // And which foreign keys...
    for (ForeignKeyId keyId : Lists.newArrayList(foreignKeyMap.keySet())) {
        ForeignKey map = (ForeignKey) cached.get(fkCacheKey(keyId));
        if (map != null) {
            foreignKeyMap.get(keyId).set(map);
            foreignKeyMap.remove(keyId);
        }
    }
    // Do we need a row count?
    if (rowCount != null) {
        Integer count = (Integer) cached.get(rowCountKey());
        if (count != null) {
            rowCount.set(count);
        }
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) ForeignKeyId(org.activityinfo.store.query.shared.join.ForeignKeyId) ColumnView(org.activityinfo.model.query.ColumnView) ForeignKey(org.activityinfo.store.query.shared.columns.ForeignKey)

Example 53 with ColumnView

use of org.activityinfo.model.query.ColumnView in project activityinfo by bedatadriven.

the class FormScanBatch method computePermissionFilter.

private Slot<TableFilter> computePermissionFilter(ResourceId formId) {
    FormPermissions permissions = supervisor.getFormPermissions(formId);
    if (!permissions.isVisible()) {
        return new PendingSlot<>(TableFilter.NONE_SELECTED);
    }
    if (!permissions.hasVisibilityFilter()) {
        return new PendingSlot<>(TableFilter.ALL_SELECTED);
    }
    // Otherwise apply per-record permissions
    try {
        FormTreeBuilder formTreeBuilder = new FormTreeBuilder(formClassProvider);
        FormTree formTree = formTreeBuilder.queryTree(formId);
        FormulaNode formula = FormulaParser.parse(permissions.getViewFilter());
        QueryEvaluator evaluator = new QueryEvaluator(FilterLevel.NONE, formTree, this);
        Slot<ColumnView> filterView = evaluator.evaluateExpression(formula);
        return new MemoizedSlot<>(filterView, new Function<ColumnView, TableFilter>() {

            @Override
            public TableFilter apply(ColumnView columnView) {
                return new TableFilter(columnView);
            }
        });
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failed to parse visibility filter", e);
        LOGGER.severe("Error parsing visibility filter '" + permissions.getViewFilter() + " in form " + formId + ": " + e.getMessage() + ". " + "For security reasons, no results will be shown");
        return new PendingSlot<>(TableFilter.NONE_SELECTED);
    }
}
Also used : FormPermissions(org.activityinfo.model.form.FormPermissions) ColumnView(org.activityinfo.model.query.ColumnView) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) FormulaNode(org.activityinfo.model.formula.FormulaNode) FormTree(org.activityinfo.model.formTree.FormTree)

Example 54 with ColumnView

use of org.activityinfo.model.query.ColumnView in project activityinfo by bedatadriven.

the class FunctionCallSlot method get.

@Override
public ColumnView get() {
    List<ColumnView> arguments = Lists.newArrayList();
    for (Slot<ColumnView> argument : argumentSlots) {
        ColumnView view = argument.get();
        if (view == null) {
            throw new IllegalStateException();
        }
        arguments.add(view);
    }
    try {
        return function.columnApply(arguments.get(0).numRows(), arguments);
    } catch (FormulaException e) {
        int numRows = arguments.get(0).numRows();
        return new EmptyColumnView(ColumnType.STRING, numRows);
    }
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView) EmptyColumnView(org.activityinfo.model.query.EmptyColumnView) EmptyColumnView(org.activityinfo.model.query.EmptyColumnView) FormulaException(org.activityinfo.model.formula.diagnostic.FormulaException)

Example 55 with ColumnView

use of org.activityinfo.model.query.ColumnView in project activityinfo by bedatadriven.

the class QueryEvaluatorTest method circularReference.

@Test
public void circularReference() throws Exception {
    final FormClass formClass = new FormClass(ResourceId.valueOf("XYZ"));
    formClass.addField(ResourceId.valueOf("FA")).setCode("A").setLabel("Field A").setType(new CalculatedFieldType("B"));
    formClass.addField(ResourceId.valueOf("FB")).setCode("B").setLabel("Field B").setType(new CalculatedFieldType("A"));
    FormStorageProviderStub catalog = new FormStorageProviderStub();
    catalog.addForm(formClass).withRowCount(10);
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormScanCache(), new NullFormSupervisor());
    FormScanBatch batch = builder.createNewBatch();
    QueryEvaluator evaluator = new QueryEvaluator(FilterLevel.BASE, catalog.getTree(formClass.getId()), batch);
    Slot<ColumnView> a = evaluator.evaluateExpression(new SymbolNode("A"));
    Slot<ColumnView> aPlusOne = evaluator.evaluateExpression(FormulaParser.parse("A+1"));
    builder.execute(batch);
    assertThat(a.get().numRows(), equalTo(10));
    assertThat(a.get().getString(0), nullValue());
    assertThat(aPlusOne.get().getString(0), nullValue());
    assertThat(aPlusOne.get().getDouble(0), equalTo(1d));
}
Also used : ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) SymbolNode(org.activityinfo.model.formula.SymbolNode) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) FormClass(org.activityinfo.model.form.FormClass) ColumnView(org.activityinfo.model.query.ColumnView) Test(org.junit.Test)

Aggregations

ColumnView (org.activityinfo.model.query.ColumnView)67 ColumnSet (org.activityinfo.model.query.ColumnSet)22 Test (org.junit.Test)22 QueryModel (org.activityinfo.model.query.QueryModel)21 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)8 ResourceId (org.activityinfo.model.resource.ResourceId)7 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)7 DoubleArrayColumnView (org.activityinfo.model.query.DoubleArrayColumnView)6 NullFormScanCache (org.activityinfo.store.query.shared.NullFormScanCache)6 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)5 FormClass (org.activityinfo.model.form.FormClass)5 FormTree (org.activityinfo.model.formTree.FormTree)5 FormulaNode (org.activityinfo.model.formula.FormulaNode)4 Nullable (javax.annotation.Nullable)3 FormulaSyntaxException (org.activityinfo.model.formula.diagnostic.FormulaSyntaxException)3 ColumnModel (org.activityinfo.model.query.ColumnModel)3 RecordRef (org.activityinfo.model.type.RecordRef)3 PrintWriter (java.io.PrintWriter)2 HashSet (java.util.HashSet)2 EntityCategory (org.activityinfo.legacy.shared.reports.content.EntityCategory)2