use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class PivotAdapter method executeIndicatorValuesQuery.
private void executeIndicatorValuesQuery(final Activity activity, LinkedActivity linkedActivity) throws SQLException {
// Double check that this activity has not been deleted
if (isDeleted(activity, linkedActivity)) {
return;
}
// Query the SOURCE form tree
FormTree formTree = formTrees.get(linkedActivity.getLeafFormClassId());
Preconditions.checkNotNull(formTree, "No form tree for form " + linkedActivity.getLeafFormClassId());
QueryModel queryModel = new QueryModel(linkedActivity.getLeafFormClassId());
List<ActivityField> indicators = selectedIndicators(activity);
// Add Indicators to the query
// Keep track of alias to destination map
final Multimap<String, ActivityField> aliasToIndicator = HashMultimap.create();
for (ActivityField indicator : indicators) {
Collection<Integer> sourceIndicatorIds = linkedActivity.getSourceIndicatorIdsFor(indicator.getId());
for (Integer sourceIndicatorId : sourceIndicatorIds) {
String alias = "I" + sourceIndicatorId;
queryModel.selectExpr(fieldExpression(sourceIndicatorId)).as(alias);
aliasToIndicator.put(alias, indicator);
}
}
// These are the columns we will use for grouping
for (DimBinding dimension : groupBy) {
for (ColumnModel columnModel : dimension.getColumnQuery(formTree)) {
queryModel.addColumn(columnModel);
}
}
// if any of the indicators are "site count" indicators, then we need
// to query the site id as well
addSiteIdToQuery(activity, queryModel);
// declare the filter
queryModel.setFilter(composeFilter(formTree));
// Query the table
enqueueQuery(queryModel, new Function<ColumnSet, Void>() {
@Nullable
@Override
public Void apply(@Nullable ColumnSet columnSet) {
// Now add the results to the buckets
int[] siteIds = extractSiteIds(columnSet);
DimensionCategory[][] categories = extractCategories(activity, columnSet);
for (String sourceAlias : aliasToIndicator.keySet()) {
ColumnView measureView = columnSet.getColumnView(sourceAlias);
// A single source indicator may be mapped to multiple destination Indicators
for (ActivityField destinationIndicator : aliasToIndicator.get(sourceAlias)) {
DimensionCategory indicatorCategory = null;
if (indicatorDimension.isPresent()) {
indicatorCategory = indicatorDimension.get().category(destinationIndicator);
}
for (int i = 0; i < columnSet.getNumRows(); i++) {
if (destinationIndicator.getAggregation() == IndicatorDTO.AGGREGATE_SITE_COUNT) {
Map<Dimension, DimensionCategory> key = bucketKey(i, categories, indicatorCategory);
Accumulator bucket = bucketForKey(key, destinationIndicator.getAggregation());
bucket.addSite(siteIds[i]);
} else {
double value = measureView.getDouble(i);
if (!Double.isNaN(value) && !Double.isInfinite(value)) {
Map<Dimension, DimensionCategory> key = bucketKey(i, categories, indicatorCategory);
if (command.getValueType() == PivotSites.ValueType.INDICATOR) {
Accumulator bucket = bucketForKey(key, destinationIndicator.getAggregation());
bucket.addValue(value);
} else {
Accumulator bucket = bucketForKey(key, IndicatorDTO.AGGREGATE_SITE_COUNT);
bucket.addSite(siteIds[i]);
}
}
}
}
}
}
return null;
}
});
}
use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class HierarchyClassImporter method prepare.
@Override
public Promise<Void> prepare(ResourceLocator locator, List<? extends SourceRow> batch) {
final List<Promise<Void>> promises = Lists.newArrayList();
for (final ResourceId formId : rootField.getRange()) {
if (isMatchableAtLevel(formId, referenceFields.keySet())) {
QueryModel queryModel = new QueryModel(formId);
queryModel.selectResourceId().as("_id");
for (FieldPath referenceFieldPath : referenceFields.keySet()) {
queryModel.selectField(referenceFieldPath).as(referenceFieldPath.toString());
}
promises.add(locator.queryTable(queryModel).then(new Function<ColumnSet, Void>() {
@Override
public Void apply(ColumnSet columnSet) {
scoreSources.put(formId, new InstanceScoreSourceBuilder(formId, referenceFields, sourceColumns).build(columnSet));
return null;
}
}));
}
}
return Promise.waitAll(promises);
}
use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class SingleClassImporter method prepare.
@Override
public Promise<Void> prepare(ResourceLocator locator, List<? extends SourceRow> batch) {
QueryModel queryModel = new QueryModel(rangeFormId);
queryModel.selectResourceId().as("_id");
for (FieldPath fieldPath : referenceFields.keySet()) {
queryModel.selectField(fieldPath).as(fieldPath.toString());
}
return locator.queryTable(queryModel).then(new Function<ColumnSet, Void>() {
@Nullable
@Override
public Void apply(@Nullable ColumnSet input) {
scoreSource = new InstanceScoreSourceBuilder(rangeFormId, referenceFields, sources).build(input);
instanceScorer = new InstanceScorer(scoreSource);
return null;
}
});
}
use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class LookupKeySetTest method overlappingHierarchiesKeyQuery.
@Test
public void overlappingHierarchiesKeyQuery() {
TestingStorageProvider catalog = setup.getCatalog();
LocaliteForm localiteForm = catalog.getLocaliteForm();
FormTree formTree = setup.getFormTree(localiteForm.getFormId());
LookupKeySet lookupKeySet = new LookupKeySet(formTree, localiteForm.getAdminField());
Map<LookupKey, FormulaNode> formulas = lookupKeySet.getKeyFormulas(localiteForm.getAdminField().getId());
QueryModel queryModel = new QueryModel(localiteForm.getFormId());
queryModel.selectResourceId().as("id");
queryModel.selectExpr(formulas.get(lookupKeySet.getKey(0))).as("province");
queryModel.selectExpr(formulas.get(lookupKeySet.getKey(1))).as("territory");
queryModel.selectExpr(formulas.get(lookupKeySet.getKey(2))).as("zs");
Connection<ColumnSet> table = setup.connect(setup.getFormStore().query(queryModel));
ColumnSet columnSet = table.assertLoaded();
ColumnView id = columnSet.getColumnView("id");
ColumnView province = columnSet.getColumnView("province");
ColumnView territory = columnSet.getColumnView("territory");
ColumnView zs = columnSet.getColumnView("zs");
// First row references a Zone de Sante
assertThat(id.getString(0), equalTo("c0"));
assertThat(province.getString(0), equalTo("Province 14"));
assertThat(territory.getString(0), nullValue());
assertThat(zs.getString(0), equalTo("Zone de Sante 56"));
// Second row references only a Province
assertThat(id.getString(1), equalTo("c1"));
assertThat(province.getString(1), equalTo("Province 8"));
assertThat(territory.getString(1), nullValue());
assertThat(zs.getString(1), nullValue());
// Fifth row references a territory
assertThat(id.getString(4), equalTo("c4"));
assertThat(province.getString(4), equalTo("Province 15"));
assertThat(territory.getString(4), equalTo("Territory 42"));
assertThat(zs.getString(4), nullValue());
}
use of org.activityinfo.model.query.ColumnSet in project activityinfo by bedatadriven.
the class LookupViewModelTest method nullKeys.
@Test
public void nullKeys() {
FormClass keyForm = new FormClass(ResourceId.valueOf("FORM1"));
keyForm.addField(ResourceId.valueOf("PROVINCE")).setLabel("Province").setKey(true).setRequired(true).setType(TextType.SIMPLE);
keyForm.addField(ResourceId.valueOf("SCHOOL")).setLabel("School").setKey(true).setRequired(true).setType(TextType.SIMPLE);
FormClass form = new FormClass(ResourceId.valueOf("FORM2"));
FormField referenceField = new FormField(ResourceId.generateFieldId(ReferenceType.TYPE_CLASS));
ReferenceType referenceType = new ReferenceType(Cardinality.SINGLE, ResourceId.valueOf("FORM1"));
referenceField.setType(referenceType);
form.addField(ResourceId.valueOf("PROJECT")).setLabel("Project name").setType(referenceType);
FormTreeBuilder treeBuilder = new FormTreeBuilder(new FormMetadataProviderStub(form, keyForm));
FormTree formTree = treeBuilder.queryTree(form.getId());
Map<String, ColumnView> columnSet = new HashMap<>();
columnSet.put("id", new StringArrayColumnView(Arrays.asList("R1", "R2", "R3", "R4")));
columnSet.put("k1", new StringArrayColumnView(Arrays.asList("PZ", null, "PA", "PA")));
columnSet.put("k2", new StringArrayColumnView(Arrays.asList("S1", "S2", null, "S3")));
FormSource formSource = EasyMock.createMock(FormSource.class);
EasyMock.expect(formSource.query(EasyMock.anyObject(QueryModel.class))).andReturn(Observable.just(new ColumnSet(3, columnSet))).anyTimes();
EasyMock.replay(formSource);
LookupViewModel viewModel = new LookupViewModel(formSource, formTree, referenceField);
LookupKeyViewModel provinceKey = viewModel.getLookupKeys().get(0);
assertThat(provinceKey.getChoices().get(), contains("PA", "PZ"));
viewModel.select(provinceKey.getLookupKey(), "PA");
LookupKeyViewModel schoolKey = viewModel.getLookupKeys().get(1);
Connection<List<String>> schoolChoices = new Connection<>(schoolKey.getChoices());
assertThat(schoolChoices.assertLoaded(), contains("S3"));
}
Aggregations