use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.
the class DefaultAnalyticsService method getAggregatedDataValuesTableLayout.
/**
* Returns a Grid with aggregated data in table layout.
*
* @param params the {@link DataQueryParams}.
* @param columns the column dimensions.
* @param rows the row dimensions.
* @return a Grid with aggregated data in table layout.
*/
private Grid getAggregatedDataValuesTableLayout(DataQueryParams params, List<String> columns, List<String> rows) {
params.setOutputIdScheme(null);
Grid grid = getAggregatedDataValues(params);
ListUtils.removeEmptys(columns);
ListUtils.removeEmptys(rows);
queryPlanner.validateTableLayout(params, columns, rows);
ReportTable reportTable = new ReportTable();
List<DimensionalItemObject[]> tableColumns = new ArrayList<>();
List<DimensionalItemObject[]> tableRows = new ArrayList<>();
if (columns != null) {
for (String dimension : columns) {
reportTable.getColumnDimensions().add(dimension);
tableColumns.add(params.getDimensionItemArrayExplodeCoc(dimension));
}
}
if (rows != null) {
for (String dimension : rows) {
reportTable.getRowDimensions().add(dimension);
tableRows.add(params.getDimensionItemArrayExplodeCoc(dimension));
}
}
reportTable.setGridTitle(IdentifiableObjectUtils.join(params.getFilterItems())).setGridColumns(new CombinationGenerator<>(tableColumns.toArray(IRT2D)).getCombinations()).setGridRows(new CombinationGenerator<>(tableRows.toArray(IRT2D)).getCombinations());
addListIfEmpty(reportTable.getGridColumns());
addListIfEmpty(reportTable.getGridRows());
reportTable.setHideEmptyRows(params.isHideEmptyRows());
reportTable.setHideEmptyColumns(params.isHideEmptyColumns());
reportTable.setShowHierarchy(params.isShowHierarchy());
Map<String, Object> valueMap = AnalyticsUtils.getAggregatedDataValueMapping(grid);
return reportTable.getGrid(new ListGrid(grid.getMetaData(), grid.getInternalMetaData()), valueMap, params.getDisplayProperty(), false);
}
use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.
the class AnalyticsUtilsTest method testGetDataValueSetFromGridWithDuplicates.
@Test
public void testGetDataValueSetFromGridWithDuplicates() {
Grid grid = new ListGrid();
grid.addHeader(new GridHeader(DimensionalObject.DATA_X_DIM_ID));
grid.addHeader(new GridHeader(DimensionalObject.ORGUNIT_DIM_ID));
grid.addHeader(new GridHeader(DimensionalObject.PERIOD_DIM_ID));
grid.addHeader(new GridHeader(DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID));
grid.addHeader(new GridHeader(DimensionalObject.ATTRIBUTEOPTIONCOMBO_DIM_ID));
grid.addHeader(new GridHeader(VALUE_ID, VALUE_HEADER_NAME, ValueType.NUMBER, Double.class.getName(), false, false));
grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouA", "peA", null, null, 1d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouA", "peB", null, null, 2d));
// Duplicate
grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouA", "peB", null, null, 2d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouB", "peA", null, null, 3d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouB", "peB", null, null, 4d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxB", "ouA", "peA", null, null, 5d));
// Duplicate
grid.addRow().addValuesAsList(Lists.newArrayList("dxB", "ouA", "peA", null, null, 5d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxB", "ouA", "peB", null, null, 6d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxC", "ouA", "peA", null, null, 7d));
// Duplicate
grid.addRow().addValuesAsList(Lists.newArrayList("dxC", "ouA", "peA", null, null, 7d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxC", "ouA", "peB", null, null, 8d));
DataValueSet dvs = AnalyticsUtils.getDataValueSetFromGrid(DataQueryParams.newBuilder().withDuplicatesOnly(true).build(), grid);
assertNotNull(dvs);
assertNotNull(dvs.getDataValues());
assertEquals(3, dvs.getDataValues().size());
}
use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.
the class AnalyticsUtilsTest method testGetDataValueSetFromGrid.
@Test
public void testGetDataValueSetFromGrid() {
Grid grid = new ListGrid();
grid.addHeader(new GridHeader(DimensionalObject.DATA_X_DIM_ID));
grid.addHeader(new GridHeader(DimensionalObject.ORGUNIT_DIM_ID));
grid.addHeader(new GridHeader(DimensionalObject.PERIOD_DIM_ID));
grid.addHeader(new GridHeader(DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID));
grid.addHeader(new GridHeader(DimensionalObject.ATTRIBUTEOPTIONCOMBO_DIM_ID));
grid.addHeader(new GridHeader(VALUE_ID, VALUE_HEADER_NAME, ValueType.NUMBER, Double.class.getName(), false, false));
grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouA", "peA", "coA", "aoA", 1d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouA", "peB", null, null, 2d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouB", "peA", null, null, 3d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxA", "ouB", "peB", null, null, 4d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxB", "ouA", "peA", "coA", null, 5d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxB", "ouA", "peB", "coA", "aoB", 6d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxC", "ouA", "peA", null, "aoA", 7));
grid.addRow().addValuesAsList(Lists.newArrayList("dxC", "ouA", "peB", null, null, 8d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxD", "ouA", "peA", "coB", null, 9d));
grid.addRow().addValuesAsList(Lists.newArrayList("dxE", "ouA", "peB", null, null, 10));
DataValueSet dvs = AnalyticsUtils.getDataValueSetFromGrid(DataQueryParams.newBuilder().build(), grid);
assertNotNull(dvs);
assertNotNull(dvs.getDataValues());
assertEquals(10, dvs.getDataValues().size());
assertEquals("dxA", dvs.getDataValues().get(1).getDataElement());
assertEquals("ouA", dvs.getDataValues().get(1).getOrgUnit());
assertEquals("peB", dvs.getDataValues().get(1).getPeriod());
assertNull(dvs.getDataValues().get(1).getCategoryOptionCombo());
assertNull(dvs.getDataValues().get(1).getAttributeOptionCombo());
assertEquals("2.0", dvs.getDataValues().get(1).getValue());
assertEquals("dxB", dvs.getDataValues().get(4).getDataElement());
assertEquals("ouA", dvs.getDataValues().get(4).getOrgUnit());
assertEquals("peA", dvs.getDataValues().get(4).getPeriod());
assertEquals("coA", dvs.getDataValues().get(4).getCategoryOptionCombo());
assertNull(dvs.getDataValues().get(4).getAttributeOptionCombo());
assertEquals("5.0", dvs.getDataValues().get(4).getValue());
assertEquals("dxC", dvs.getDataValues().get(6).getDataElement());
assertEquals("ouA", dvs.getDataValues().get(6).getOrgUnit());
assertEquals("peA", dvs.getDataValues().get(6).getPeriod());
assertNull(dvs.getDataValues().get(6).getCategoryOptionCombo());
assertEquals("aoA", dvs.getDataValues().get(6).getAttributeOptionCombo());
assertEquals("7", dvs.getDataValues().get(6).getValue());
assertEquals("dxD", dvs.getDataValues().get(8).getDataElement());
assertEquals("ouA", dvs.getDataValues().get(8).getOrgUnit());
assertEquals("peA", dvs.getDataValues().get(8).getPeriod());
assertEquals("coB", dvs.getDataValues().get(8).getCategoryOptionCombo());
assertNull(dvs.getDataValues().get(8).getAttributeOptionCombo());
assertEquals("9.0", dvs.getDataValues().get(8).getValue());
assertEquals("dxE", dvs.getDataValues().get(9).getDataElement());
assertEquals("ouA", dvs.getDataValues().get(9).getOrgUnit());
assertEquals("peB", dvs.getDataValues().get(9).getPeriod());
assertNull(dvs.getDataValues().get(9).getCategoryOptionCombo());
assertNull(dvs.getDataValues().get(9).getAttributeOptionCombo());
assertEquals("10", dvs.getDataValues().get(9).getValue());
}
use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.
the class DefaultTrackedEntityInstanceService method getTrackedEntityInstancesGrid.
// TODO lower index on attribute value?
@Override
public Grid getTrackedEntityInstancesGrid(TrackedEntityInstanceQueryParams params) {
decideAccess(params);
validate(params);
handleAttributes(params);
params.setUser(currentUserService.getCurrentUser());
// ---------------------------------------------------------------------
// Conform parameters
// ---------------------------------------------------------------------
params.conform();
// ---------------------------------------------------------------------
// Grid headers
// ---------------------------------------------------------------------
Grid grid = new ListGrid();
grid.addHeader(new GridHeader(TRACKED_ENTITY_INSTANCE_ID, "Instance"));
grid.addHeader(new GridHeader(CREATED_ID, "Created"));
grid.addHeader(new GridHeader(LAST_UPDATED_ID, "Last updated"));
grid.addHeader(new GridHeader(ORG_UNIT_ID, "Organisation unit"));
grid.addHeader(new GridHeader(ORG_UNIT_NAME, "Organisation unit name"));
grid.addHeader(new GridHeader(TRACKED_ENTITY_ID, "Tracked entity"));
grid.addHeader(new GridHeader(INACTIVE_ID, "Inactive"));
if (params.isIncludeDeleted()) {
grid.addHeader(new GridHeader(DELETED, "Deleted", ValueType.BOOLEAN, "boolean", false, false));
}
for (QueryItem item : params.getAttributes()) {
grid.addHeader(new GridHeader(item.getItem().getUid(), item.getItem().getName()));
}
List<Map<String, String>> entities = trackedEntityInstanceStore.getTrackedEntityInstancesGrid(params);
// ---------------------------------------------------------------------
// Grid rows
// ---------------------------------------------------------------------
Set<String> tes = new HashSet<>();
for (Map<String, String> entity : entities) {
grid.addRow();
grid.addValue(entity.get(TRACKED_ENTITY_INSTANCE_ID));
grid.addValue(entity.get(CREATED_ID));
grid.addValue(entity.get(LAST_UPDATED_ID));
grid.addValue(entity.get(ORG_UNIT_ID));
grid.addValue(entity.get(ORG_UNIT_NAME));
grid.addValue(entity.get(TRACKED_ENTITY_ID));
grid.addValue(entity.get(INACTIVE_ID));
if (params.isIncludeDeleted()) {
grid.addValue(entity.get(DELETED));
}
tes.add(entity.get(TRACKED_ENTITY_ID));
for (QueryItem item : params.getAttributes()) {
grid.addValue(entity.get(item.getItemId()));
}
}
Map<String, Object> metaData = new HashMap<>();
if (params.isPaging()) {
int count = 0;
if (params.isTotalPages()) {
count = trackedEntityInstanceStore.getTrackedEntityInstanceCount(params);
}
Pager pager = new Pager(params.getPageWithDefault(), count, params.getPageSizeWithDefault());
metaData.put(PAGER_META_KEY, pager);
}
if (!params.isSkipMeta()) {
Map<String, String> names = new HashMap<>();
for (String te : tes) {
TrackedEntity entity = trackedEntityService.getTrackedEntity(te);
names.put(te, entity != null ? entity.getDisplayName() : null);
}
metaData.put(META_DATA_NAMES_KEY, names);
}
grid.setMetaData(metaData);
return grid;
}
use of org.hisp.dhis.system.grid.ListGrid in project dhis2-core by dhis2.
the class ReportTableTest method testGetGridShowHierarchy.
@Test
public void testGetGridShowHierarchy() {
ReportTable reportTable = new ReportTable("Grid table", dataElements, new ArrayList<>(), new ArrayList<>(), periods, Lists.newArrayList(unitB, unitC), true, true, false, null, null, null);
reportTable.setShowHierarchy(true);
reportTable.init(null, null, null, null, null, i18nFormat);
Map<Object, List<?>> ancestorMap = new HashMap<>();
ancestorMap.put(unitB.getUid(), unitB.getAncestorNames(null, true));
ancestorMap.put(unitC.getUid(), unitC.getAncestorNames(null, true));
Map<String, Object> metaData = new HashMap<>();
Map<String, Object> internalMetaData = new HashMap<>();
internalMetaData.put(AnalyticsMetaDataKey.ORG_UNIT_ANCESTORS.getKey(), ancestorMap);
Map<String, Object> valueMap = new HashMap<>();
valueMap.put(dataElementA.getDimensionItem() + DIMENSION_SEP + periodA.getDimensionItem() + DIMENSION_SEP + unitB.getDimensionItem(), 11);
valueMap.put(dataElementA.getDimensionItem() + DIMENSION_SEP + periodA.getDimensionItem() + DIMENSION_SEP + unitC.getDimensionItem(), 21);
valueMap.put(dataElementA.getDimensionItem() + DIMENSION_SEP + periodB.getDimensionItem() + DIMENSION_SEP + unitB.getDimensionItem(), 12);
valueMap.put(dataElementA.getDimensionItem() + DIMENSION_SEP + periodB.getDimensionItem() + DIMENSION_SEP + unitC.getDimensionItem(), 22);
valueMap.put(dataElementB.getDimensionItem() + DIMENSION_SEP + periodA.getDimensionItem() + DIMENSION_SEP + unitB.getDimensionItem(), 13);
valueMap.put(dataElementB.getDimensionItem() + DIMENSION_SEP + periodA.getDimensionItem() + DIMENSION_SEP + unitC.getDimensionItem(), 23);
valueMap.put(dataElementB.getDimensionItem() + DIMENSION_SEP + periodB.getDimensionItem() + DIMENSION_SEP + unitB.getDimensionItem(), 14);
valueMap.put(dataElementB.getDimensionItem() + DIMENSION_SEP + periodB.getDimensionItem() + DIMENSION_SEP + unitC.getDimensionItem(), 24);
DisplayProperty property = DisplayProperty.NAME;
Grid grid = reportTable.getGrid(new ListGrid(metaData, internalMetaData), valueMap, property, false);
assertEquals(10, grid.getWidth());
assertEquals(10, grid.getHeaders().size());
assertEquals(2, grid.getHeight());
assertEquals(unitA.getDisplayName(), grid.getValue(0, 0));
assertEquals(unitB.getDisplayName(), grid.getValue(0, 1));
assertEquals(unitB.getUid(), grid.getValue(0, 2));
assertEquals(unitB.getDisplayProperty(property), grid.getValue(0, 3));
assertEquals(unitB.getCode(), grid.getValue(0, 4));
assertEquals(unitA.getDisplayName(), grid.getValue(1, 0));
assertEquals(unitC.getDisplayName(), grid.getValue(1, 1));
assertEquals(unitC.getUid(), grid.getValue(1, 2));
assertEquals(unitC.getDisplayProperty(property), grid.getValue(1, 3));
assertEquals(unitC.getCode(), grid.getValue(1, 4));
}
Aggregations