Search in sources :

Example 36 with FormTree

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));
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) FormClass(org.activityinfo.model.form.FormClass) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) ReferenceValue(org.activityinfo.model.type.ReferenceValue) FieldPath(org.activityinfo.model.formTree.FieldPath) RecordRef(org.activityinfo.model.type.RecordRef) FormInstance(org.activityinfo.model.form.FormInstance) LocalDate(org.activityinfo.model.type.time.LocalDate) Test(org.junit.Test)

Example 37 with FormTree

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);
    }
}
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 38 with FormTree

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;
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass)

Example 39 with FormTree

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;
        }
    });
}
Also used : DimensionCategory(org.activityinfo.legacy.shared.reports.content.DimensionCategory) FormTree(org.activityinfo.model.formTree.FormTree) ColumnSet(org.activityinfo.model.query.ColumnSet) ColumnModel(org.activityinfo.model.query.ColumnModel) QueryModel(org.activityinfo.model.query.QueryModel) Nullable(javax.annotation.Nullable)

Example 40 with FormTree

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;
        }
    });
}
Also used : DimensionCategory(org.activityinfo.legacy.shared.reports.content.DimensionCategory) FormTree(org.activityinfo.model.formTree.FormTree) ColumnSet(org.activityinfo.model.query.ColumnSet) ColumnModel(org.activityinfo.model.query.ColumnModel) QueryModel(org.activityinfo.model.query.QueryModel) Nullable(javax.annotation.Nullable)

Aggregations

FormTree (org.activityinfo.model.formTree.FormTree)55 Test (org.junit.Test)35 ResourceId (org.activityinfo.model.resource.ResourceId)12 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)10 FormClass (org.activityinfo.model.form.FormClass)9 ColumnSet (org.activityinfo.model.query.ColumnSet)9 QueryModel (org.activityinfo.model.query.QueryModel)8 ImportModel (org.activityinfo.ui.client.component.importDialog.model.ImportModel)8 PastedTable (org.activityinfo.ui.client.component.importDialog.model.source.PastedTable)8 FormField (org.activityinfo.model.form.FormField)7 RecordRef (org.activityinfo.model.type.RecordRef)7 LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)6 FormulaNode (org.activityinfo.model.formula.FormulaNode)6 ReferenceType (org.activityinfo.model.type.ReferenceType)6 LookupKey (org.activityinfo.model.formTree.LookupKey)5 ValidatedRowTable (org.activityinfo.ui.client.component.importDialog.model.validation.ValidatedRowTable)5 Optional (com.google.common.base.Optional)4 ColumnView (org.activityinfo.model.query.ColumnView)4 Nullable (javax.annotation.Nullable)3 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)3