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