use of org.hisp.dhis.analytics.EventAnalyticsDimensionalItem in project dhis2-core by dhis2.
the class DefaultEventAnalyticsService method addEventDataObjects.
/**
* Puts elements into the mapping table. The elements are fetched from the
* query parameters.
*
* @param grid the {@link Grid}.
* @param params the {@link EventQueryParams}.
* @param table the map to add elements to.
* @param dimension the dimension identifier.
*/
private void addEventDataObjects(Grid grid, EventQueryParams params, Map<String, List<EventAnalyticsDimensionalItem>> table, String dimension) {
List<EventAnalyticsDimensionalItem> objects = params.getEventReportDimensionalItemArrayExploded(dimension);
if (objects.isEmpty()) {
ValueTypedDimensionalItemObject eventDimensionalItemObject = dataElementService.getDataElement(dimension);
if (eventDimensionalItemObject == null) {
eventDimensionalItemObject = trackedEntityAttributeService.getTrackedEntityAttribute(dimension);
}
addEventReportDimensionalItems(eventDimensionalItemObject, objects, grid, dimension);
table.put(eventDimensionalItemObject.getDisplayProperty(params.getDisplayProperty()), objects);
} else {
table.put(dimension, objects);
}
}
use of org.hisp.dhis.analytics.EventAnalyticsDimensionalItem in project dhis2-core by dhis2.
the class EventAnalyticsUtils method recurse.
/**
* A recursive method which finds all permutations of the elements in map.
*
* @param map the map with all values
* @param iter iterator with keys
* @param cur the current map
* @param list the resulting list
*/
public static void recurse(Map<String, List<EventAnalyticsDimensionalItem>> map, ListIterator<String> iter, TreeMap<String, EventAnalyticsDimensionalItem> cur, List<Map<String, EventAnalyticsDimensionalItem>> list) {
if (!iter.hasNext()) {
Map<String, EventAnalyticsDimensionalItem> entry = new HashMap<>();
for (String key : cur.keySet()) {
entry.put(key, cur.get(key));
}
list.add(entry);
} else {
String key = iter.next();
List<EventAnalyticsDimensionalItem> set = map.get(key);
for (EventAnalyticsDimensionalItem value : set) {
cur.put(key, value);
recurse(map, iter, cur, list);
cur.remove(key);
}
iter.previous();
}
}
use of org.hisp.dhis.analytics.EventAnalyticsDimensionalItem in project dhis2-core by dhis2.
the class EventAnalyticsUtilsTest method testGenerateEventDataPermutations.
@Test
void testGenerateEventDataPermutations() {
Map<String, List<EventAnalyticsDimensionalItem>> tableRows = new LinkedHashMap<>();
Grid grid = new ListGrid();
DataElement deA = createDataElement('A');
deA.setValueType(ValueType.BOOLEAN);
grid.addMetaData(deA.getUid(), deA);
TrackedEntityAttribute trackedEntityAttribute = createTrackedEntityAttribute('B');
OptionSet optionSet = new OptionSet();
optionSet.addOption(new Option("name", "code"));
trackedEntityAttribute.setOptionSet(optionSet);
grid.addMetaData(trackedEntityAttribute.getUid(), trackedEntityAttribute);
List<EventAnalyticsDimensionalItem> objects = new ArrayList<>();
Option t = new Option();
t.setCode("1");
t.setName("Yes");
Option f = new Option();
f.setCode("0");
f.setName("No");
objects.add(new EventAnalyticsDimensionalItem(t, deA.getUid()));
objects.add(new EventAnalyticsDimensionalItem(f, deA.getUid()));
objects.add(new EventAnalyticsDimensionalItem(new Option("name", "code"), trackedEntityAttribute.getUid()));
tableRows.put(deA.getUid(), objects);
tableRows.put(trackedEntityAttribute.getDimensionItem(), objects);
List<Map<String, EventAnalyticsDimensionalItem>> rowPermutations = EventAnalyticsUtils.generateEventDataPermutations(tableRows);
assertEquals(9, rowPermutations.size());
}
use of org.hisp.dhis.analytics.EventAnalyticsDimensionalItem 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.analytics.EventAnalyticsDimensionalItem 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;
}
Aggregations