use of org.hisp.dhis.common.MetadataItem in project dhis2-core by dhis2.
the class AbstractAnalyticsService method getMetadataItems.
/**
* Returns a map of metadata item identifiers and {@link MetadataItem}.
*
* @param params the data query parameters.
* @return a map.
*/
private Map<String, MetadataItem> getMetadataItems(EventQueryParams params, List<DimensionItemKeywords.Keyword> periodKeywords, List<Option> itemOptions) {
Map<String, MetadataItem> metadataItemMap = AnalyticsUtils.getDimensionMetadataItemMap(params);
boolean includeDetails = params.isIncludeMetadataDetails();
if (params.hasValueDimension()) {
DimensionalItemObject value = params.getValue();
metadataItemMap.put(value.getUid(), new MetadataItem(value.getDisplayProperty(params.getDisplayProperty()), includeDetails ? value.getUid() : null, value.getCode()));
}
params.getItemLegends().stream().filter(Objects::nonNull).forEach(legend -> metadataItemMap.put(legend.getUid(), new MetadataItem(legend.getDisplayName(), includeDetails ? legend.getUid() : null, legend.getCode())));
addMetadataItems(metadataItemMap, params, itemOptions);
params.getItemsAndItemFilters().stream().filter(Objects::nonNull).forEach(item -> metadataItemMap.put(getItemIdMaybeWithProgramStageIdPrefix(item), new MetadataItem(item.getItem().getDisplayName(), includeDetails ? item.getItem() : null)));
if (hasPeriodKeywords(periodKeywords)) {
for (DimensionItemKeywords.Keyword keyword : periodKeywords) {
if (keyword.getMetadataItem() != null) {
metadataItemMap.put(keyword.getKey(), new MetadataItem(keyword.getMetadataItem().getName()));
}
}
}
return metadataItemMap;
}
use of org.hisp.dhis.common.MetadataItem in project dhis2-core by dhis2.
the class EventAnalyticsServiceMetadataTest method testGetQueryItemMetadata.
@Test
@SuppressWarnings("unchecked")
void testGetQueryItemMetadata() {
DimensionalObject periods = new BaseDimensionalObject(DimensionalObject.PERIOD_DIM_ID, DimensionType.PERIOD, Lists.newArrayList(peA));
DimensionalObject orgUnits = new BaseDimensionalObject(DimensionalObject.ORGUNIT_DIM_ID, DimensionType.ORGANISATION_UNIT, Lists.newArrayList(ouA));
QueryItem qiA = new QueryItem(deA, deA.getLegendSet(), deA.getValueType(), deA.getAggregationType(), null);
QueryItem qiB = new QueryItem(deE, null, deE.getValueType(), deE.getAggregationType(), deE.getOptionSet());
EventQueryParams params = new EventQueryParams.Builder().withProgram(prA).addDimension(periods).addDimension(orgUnits).addItem(qiA).addItemFilter(qiB).withSkipData(true).withSkipMeta(false).withApiVersion(DhisApiVersion.V29).build();
Grid grid = eventAnalyticsService.getAggregatedEventData(params);
Map<String, Object> metadata = grid.getMetaData();
Map<String, MetadataItem> itemMap = (Map<String, MetadataItem>) metadata.get(AnalyticsMetaDataKey.ITEMS.getKey());
assertNotNull(itemMap.get(DimensionalObject.PERIOD_DIM_ID));
assertNotNull(itemMap.get(DimensionalObject.ORGUNIT_DIM_ID));
for (Legend legend : deA.getLegendSet().getLegends()) {
assertNotNull(itemMap.get(legend.getUid()));
}
for (Option option : deE.getOptionSet().getOptions()) {
assertNotNull(itemMap.get(option.getUid()));
}
assertNotNull(itemMap.get(deA.getUid()));
assertNotNull(itemMap.get(deE.getUid()));
}
use of org.hisp.dhis.common.MetadataItem in project dhis2-core by dhis2.
the class DefaultEventAnalyticsService method addEventReportDimensionalItems.
/**
* Adds dimensional items to the given list of objects. Send in a list of
* {@link EventAnalyticsDimensionalItem} and add properties from
* {@link ValueTypedDimensionalItemObject} parameter.
*
* @param eventDimensionalItemObject the
* {@link ValueTypedDimensionalItemObject} object to get properties
* from.
* @param objects the list of {@link EventAnalyticsDimensionalItem} objects.
* @param grid the {@link Grid} from the event analytics request.
* @param dimension the dimension identifier.
*/
@SuppressWarnings("unchecked")
private void addEventReportDimensionalItems(ValueTypedDimensionalItemObject eventDimensionalItemObject, List<EventAnalyticsDimensionalItem> objects, Grid grid, String dimension) {
Preconditions.checkNotNull(eventDimensionalItemObject, String.format("Data dimension '%s' is invalid", dimension));
String parentUid = eventDimensionalItemObject.getUid();
if (eventDimensionalItemObject.getValueType() == BOOLEAN) {
objects.add(new EventAnalyticsDimensionalItem(OPT_TRUE, parentUid));
objects.add(new EventAnalyticsDimensionalItem(OPT_FALSE, parentUid));
}
if (eventDimensionalItemObject.hasOptionSet()) {
for (Option option : eventDimensionalItemObject.getOptionSet().getOptions()) {
objects.add(new EventAnalyticsDimensionalItem(option, parentUid));
}
} else if (eventDimensionalItemObject.hasLegendSet()) {
List<String> legendOptions = (List<String>) ((Map<String, Object>) grid.getMetaData().get(DIMENSIONS.getKey())).get(dimension);
if (legendOptions.isEmpty()) {
List<Legend> legends = eventDimensionalItemObject.getLegendSet().getSortedLegends();
for (Legend legend : legends) {
for (int i = legend.getStartValue().intValue(); i < legend.getEndValue().intValue(); i++) {
objects.add(new EventAnalyticsDimensionalItem(new Option(String.valueOf(i), String.valueOf(i)), parentUid));
}
}
} else {
for (String legend : legendOptions) {
MetadataItem metadataItem = (MetadataItem) ((Map<String, Object>) grid.getMetaData().get(ITEMS.getKey())).get(legend);
objects.add(new EventAnalyticsDimensionalItem(new Option(metadataItem.getName(), legend), parentUid));
}
}
}
}
use of org.hisp.dhis.common.MetadataItem in project dhis2-core by dhis2.
the class DefaultEventAnalyticsService method generateOutputGrid.
/**
* Generates an output grid for event analytics download based on input
* parameters.
*
* @param grid the result grid.
* @param params the {@link EventQueryParams}.
* @param rowPermutations the row permutations
* @param columnPermutations the column permutations.
* @param rowDimensions the row dimensions.
* @return grid with table layout.
*/
@SuppressWarnings("unchecked")
private Grid generateOutputGrid(Grid grid, EventQueryParams params, List<Map<String, EventAnalyticsDimensionalItem>> rowPermutations, List<Map<String, EventAnalyticsDimensionalItem>> columnPermutations, List<String> rowDimensions) {
Grid outputGrid = new ListGrid();
outputGrid.setTitle(IdentifiableObjectUtils.join(params.getFilterItems()));
for (String row : rowDimensions) {
MetadataItem metadataItem = (MetadataItem) ((Map<String, Object>) grid.getMetaData().get(ITEMS.getKey())).get(row);
String name = StringUtils.defaultIfEmpty(metadataItem.getName(), row);
String col = StringUtils.defaultIfEmpty(COLUMN_NAMES.get(row), row);
outputGrid.addHeader(new GridHeader(name, col, ValueType.TEXT, false, true));
}
columnPermutations.forEach(permutation -> {
StringBuilder builder = new StringBuilder();
permutation.forEach((key, value) -> {
if (!key.equals(ORGUNIT_DIM_ID) && !key.equals(PERIOD_DIM_ID)) {
builder.append(key).append(SPACE);
}
builder.append(value.getDisplayProperty(params.getDisplayProperty())).append(DASH_PRETTY_SEPARATOR);
});
String display = builder.length() > 0 ? builder.substring(0, builder.lastIndexOf(DASH_PRETTY_SEPARATOR)) : TOTAL_COLUMN_PRETTY_NAME;
outputGrid.addHeader(new GridHeader(display, display, ValueType.NUMBER, false, false));
});
for (Map<String, EventAnalyticsDimensionalItem> rowCombination : rowPermutations) {
outputGrid.addRow();
List<List<String>> ids = new ArrayList<>();
Map<String, EventAnalyticsDimensionalItem> displayObjects = new HashMap<>();
boolean fillDisplayList = true;
for (Map<String, EventAnalyticsDimensionalItem> columnCombination : columnPermutations) {
List<String> idList = new ArrayList<>();
boolean finalFillDisplayList = fillDisplayList;
rowCombination.forEach((key, value) -> {
idList.add(value.toString());
if (finalFillDisplayList) {
displayObjects.put(value.getParentUid(), value);
}
});
columnCombination.forEach((key, value) -> idList.add(value.toString()));
ids.add(idList);
fillDisplayList = false;
}
rowDimensions.forEach(dimension -> outputGrid.addValue(displayObjects.get(dimension).getDisplayProperty(params.getDisplayProperty())));
EventAnalyticsUtils.addValues(ids, grid, outputGrid);
}
return outputGrid;
}
use of org.hisp.dhis.common.MetadataItem in project dhis2-core by dhis2.
the class AnalyticsUtils method getDimensionMetadataItemMap.
/**
* Returns a mapping between identifiers and meta data items for the given
* query.
*
* @param params the data query parameters.
* @return a mapping between identifiers and meta data items.
*/
public static Map<String, MetadataItem> getDimensionMetadataItemMap(DataQueryParams params) {
List<DimensionalObject> dimensions = params.getDimensionsAndFilters();
Map<String, MetadataItem> map = new HashMap<>();
Calendar calendar = PeriodType.getCalendar();
boolean includeMetadataDetails = params.isIncludeMetadataDetails();
for (DimensionalObject dimension : dimensions) {
for (DimensionalItemObject item : dimension.getItems()) {
if (DimensionType.PERIOD == dimension.getDimensionType() && !calendar.isIso8601()) {
Period period = (Period) item;
DateTimeUnit dateTimeUnit = calendar.fromIso(period.getStartDate());
String isoDate = period.getPeriodType().getIsoDate(dateTimeUnit);
map.put(isoDate, new MetadataItem(period.getDisplayName(), includeMetadataDetails ? period : null));
} else {
map.put(item.getDimensionItem(), new MetadataItem(item.getDisplayProperty(params.getDisplayProperty()), includeMetadataDetails ? item : null));
}
if (DimensionType.ORGANISATION_UNIT == dimension.getDimensionType() && params.isHierarchyMeta()) {
OrganisationUnit unit = (OrganisationUnit) item;
for (OrganisationUnit ancestor : unit.getAncestors()) {
map.put(ancestor.getUid(), new MetadataItem(ancestor.getDisplayProperty(params.getDisplayProperty()), includeMetadataDetails ? ancestor : null));
}
}
if (DimensionItemType.DATA_ELEMENT == item.getDimensionItemType()) {
DataElement dataElement = (DataElement) item;
for (CategoryOptionCombo coc : dataElement.getCategoryOptionCombos()) {
map.put(coc.getUid(), new MetadataItem(coc.getDisplayProperty(params.getDisplayProperty()), includeMetadataDetails ? coc : null));
}
}
}
map.put(dimension.getDimension(), new MetadataItem(dimension.getDisplayProperty(params.getDisplayProperty()), includeMetadataDetails ? dimension : null));
if (dimension.getDimensionItemKeywords() != null) {
dimension.getDimensionItemKeywords().getKeywords().forEach(b -> map.put(b.getKey(), b.getMetadataItem()));
}
}
Program program = params.getProgram();
ProgramStage stage = params.getProgramStage();
if (ObjectUtils.allNotNull(program)) {
map.put(program.getUid(), new MetadataItem(program.getDisplayProperty(params.getDisplayProperty()), includeMetadataDetails ? program : null));
if (stage != null) {
map.put(stage.getUid(), new MetadataItem(stage.getDisplayName(), includeMetadataDetails ? stage : null));
} else {
for (ProgramStage ps : program.getProgramStages()) {
map.put(ps.getUid(), new MetadataItem(ps.getDisplayName(), includeMetadataDetails ? ps : null));
}
}
}
return map;
}
Aggregations