use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.
the class SitePersisterTest method test.
@Test
public void test() {
FormClass formClass = assertResolves(locator.getFormClass(PEAR_Activity));
FormInstance siteFormInstance = new FormInstance(CuidAdapter.generateSiteCuid(), formClass.getId());
siteFormInstance.set(CONTENU_DI_KIT_FIELD, new EnumValue(CONTENU_DI_KIT_FIELD_ATTR_VALUE));
// built-in values
siteFormInstance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.PARTNER_FIELD), new ReferenceValue(new RecordRef(CuidAdapter.partnerFormId(DATABASE_ID), CuidAdapter.partnerRecordId(1))));
siteFormInstance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.START_DATE_FIELD), new LocalDate(2014, 1, 1));
siteFormInstance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.END_DATE_FIELD), new LocalDate(2014, 1, 2));
siteFormInstance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.LOCATION_FIELD), new ReferenceValue(new RecordRef(CuidAdapter.locationFormClass(1), CuidAdapter.locationInstanceId(1))));
assertResolves(locator.persist(siteFormInstance));
// query by id
FormInstance fromServer = assertResolves(locator.getFormInstance(siteFormInstance.getFormId(), siteFormInstance.getId()));
Assert.assertNotNull(fromServer);
Assert.assertEquals(fromServer.get(CONTENU_DI_KIT_FIELD), new EnumValue(CONTENU_DI_KIT_FIELD_ATTR_VALUE));
FormTree formTree = assertResolves(locator.getFormTree(formClass.getId()));
final List<FieldPath> paths = Lists.newArrayList(formTree.getRootPaths());
Assert.assertTrue(paths.contains(new FieldPath(CONTENU_DI_KIT_FIELD)));
// query projection
FormInstance instance = assertResolves(locator.getFormInstance(formClass.getId(), siteFormInstance.getId()));
EnumValue fieldValue = (EnumValue) instance.get(CONTENU_DI_KIT_FIELD);
assertThat(fieldValue.getValueId(), equalTo(CONTENU_DI_KIT_FIELD_ATTR_VALUE));
}
use of org.activityinfo.model.formTree.FormTree 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.formTree.FormTree in project activityinfo by bedatadriven.
the class FormForest method getReferencedForms.
public List<FormClass> getReferencedForms() {
Set<ResourceId> visited = new HashSet<>();
List<FormClass> list = new ArrayList<>();
// Depth-first search for natural ordering
for (FormTree formTree : trees.values()) {
collectReferenced(formTree, formTree.getRootFields(), visited, list);
}
return list;
}
use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.
the class PivotAdapter method executeSiteCountQueryOnSite.
private void executeSiteCountQueryOnSite(final Activity activity, LinkedActivity linkedActivity) {
Preconditions.checkState(!indicatorDimension.isPresent());
FormTree formTree = formTrees.get(linkedActivity.getSiteFormClassId());
QueryModel queryModel = new QueryModel(linkedActivity.getSiteFormClassId());
queryModel.setFilter(composeFilter(formTree));
// Add dimensions columns as needed
for (DimBinding dimension : groupBy) {
for (ColumnModel columnModel : dimension.getColumnQuery(formTree)) {
queryModel.addColumn(columnModel);
}
}
// Query the table
enqueueQuery(queryModel, new Function<ColumnSet, Void>() {
@Nullable
@Override
public Void apply(@Nullable ColumnSet columnSet) {
// Now add the counts to the buckets
DimensionCategory[][] categories = extractCategories(activity, columnSet);
for (int i = 0; i < columnSet.getNumRows(); i++) {
Map<Dimension, DimensionCategory> key = bucketKey(i, categories, null);
Accumulator bucket = bucketForKey(key, IndicatorDTO.AGGREGATE_SITE_COUNT);
bucket.addCount(1);
}
return null;
}
});
}
use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.
the class PivotAdapter method executeSiteCountQueryOnReportingPeriod.
private void executeSiteCountQueryOnReportingPeriod(final Activity activity, LinkedActivity linkedActivity) {
Preconditions.checkArgument(activity.isMonthly());
// Query the linked activity
FormTree formTree = formTrees.get(linkedActivity.getLeafFormClassId());
QueryModel queryModel = new QueryModel(activity.getLeafFormClassId());
queryModel.setFilter(composeFilter(formTree));
// Add dimensions columns as needed
for (DimBinding dimension : groupBy) {
for (ColumnModel columnModel : dimension.getColumnQuery(formTree)) {
queryModel.addColumn(columnModel);
}
}
addSiteIdToQuery(activity, queryModel);
// Query the table
enqueueQuery(queryModel, new Function<ColumnSet, Void>() {
@Nullable
@Override
public Void apply(@Nullable ColumnSet columnSet) {
// Now add the counts to the buckets
DimensionCategory[][] categories = extractCategories(activity, columnSet);
int[] siteId = extractSiteIds(columnSet);
for (int i = 0; i < columnSet.getNumRows(); i++) {
Map<Dimension, DimensionCategory> key = bucketKey(i, categories, null);
Accumulator bucket = bucketForKey(key, IndicatorDTO.AGGREGATE_SITE_COUNT);
bucket.addSite(siteId[i]);
}
return null;
}
});
}
Aggregations