use of org.hisp.dhis.common.DimensionalObject in project dhis2-core by dhis2.
the class DefaultAnalyticsService method addMetaData.
/**
* Adds meta data values to the given grid based on the given data query
* parameters.
*
* @param params the {@link DataQueryParams}.
* @param grid the grid.
*/
private void addMetaData(DataQueryParams params, Grid grid) {
if (!params.isSkipMeta()) {
Map<String, Object> metaData = new HashMap<>();
Map<String, Object> internalMetaData = new HashMap<>();
// -----------------------------------------------------------------
// Items / names element
// -----------------------------------------------------------------
Map<String, String> cocNameMap = AnalyticsUtils.getCocNameMap(params);
if (params.getApiVersion().ge(DhisApiVersion.V26)) {
metaData.put(AnalyticsMetaDataKey.ITEMS.getKey(), AnalyticsUtils.getDimensionMetadataItemMap(params));
} else {
Map<String, String> uidNameMap = AnalyticsUtils.getDimensionItemNameMap(params);
uidNameMap.putAll(cocNameMap);
uidNameMap.put(DATA_X_DIM_ID, DISPLAY_NAME_DATA_X);
metaData.put(AnalyticsMetaDataKey.NAMES.getKey(), uidNameMap);
}
// -----------------------------------------------------------------
// Item order elements
// -----------------------------------------------------------------
Map<String, Object> dimensionItems = new HashMap<>();
Calendar calendar = PeriodType.getCalendar();
List<String> periodUids = calendar.isIso8601() ? getDimensionalItemIds(params.getDimensionOrFilterItems(PERIOD_DIM_ID)) : getLocalPeriodIdentifiers(params.getDimensionOrFilterItems(PERIOD_DIM_ID), calendar);
dimensionItems.put(PERIOD_DIM_ID, periodUids);
dimensionItems.put(CATEGORYOPTIONCOMBO_DIM_ID, cocNameMap.keySet());
for (DimensionalObject dim : params.getDimensionsAndFilters()) {
if (!dimensionItems.keySet().contains(dim.getDimension())) {
dimensionItems.put(dim.getDimension(), getDimensionalItemIds(dim.getItems()));
}
}
if (params.getApiVersion().ge(DhisApiVersion.V26)) {
metaData.put(AnalyticsMetaDataKey.DIMENSIONS.getKey(), dimensionItems);
} else {
metaData.putAll(dimensionItems);
}
// -----------------------------------------------------------------
// Organisation unit hierarchy
// -----------------------------------------------------------------
User user = securityManager.getCurrentUser(params);
List<OrganisationUnit> organisationUnits = asTypedList(params.getDimensionOrFilterItems(ORGUNIT_DIM_ID));
Collection<OrganisationUnit> roots = user != null ? user.getOrganisationUnits() : null;
if (params.isHierarchyMeta()) {
metaData.put(AnalyticsMetaDataKey.ORG_UNIT_HIERARCHY.getKey(), getParentGraphMap(organisationUnits, roots));
}
if (params.isShowHierarchy()) {
Map<Object, List<?>> ancestorMap = organisationUnits.stream().collect(Collectors.toMap(OrganisationUnit::getUid, ou -> ou.getAncestorNames(roots, true)));
internalMetaData.put(AnalyticsMetaDataKey.ORG_UNIT_ANCESTORS.getKey(), ancestorMap);
metaData.put(AnalyticsMetaDataKey.ORG_UNIT_NAME_HIERARCHY.getKey(), getParentNameGraphMap(organisationUnits, roots, true));
}
grid.setMetaData(ImmutableMap.copyOf(metaData));
grid.setInternalMetaData(ImmutableMap.copyOf(internalMetaData));
}
}
use of org.hisp.dhis.common.DimensionalObject in project dhis2-core by dhis2.
the class AnalyticsUtilsTest method testGetDimensionalItemObjectMap.
@Test
public void testGetDimensionalItemObjectMap() {
DataElement deA = createDataElement('A');
Indicator inA = createIndicator('A', null);
DataSet dsA = createDataSet('A');
DimensionalObject dx = new BaseDimensionalObject(DimensionalObject.DATA_X_DIM_ID, DimensionType.DATA_X, DimensionalObjectUtils.getList(deA, inA, dsA));
DataQueryParams params = DataQueryParams.newBuilder().addDimension(dx).withDisplayProperty(DisplayProperty.NAME).build();
Map<String, DimensionalItemObject> map = AnalyticsUtils.getDimensionalItemObjectMap(params);
assertEquals(map.get(deA.getDimensionItem()), deA);
assertEquals(map.get(inA.getDimensionItem()), inA);
assertEquals(map.get(dsA.getDimensionItem()), dsA);
}
use of org.hisp.dhis.common.DimensionalObject in project dhis2-core by dhis2.
the class AnalyticsUtilsTest method testGetDimensionItemNameMap.
@Test
public void testGetDimensionItemNameMap() {
DataElement deA = createDataElement('A');
Indicator inA = createIndicator('A', null);
DataSet dsA = createDataSet('A');
OrganisationUnit ouA = createOrganisationUnit('A');
OrganisationUnit ouB = createOrganisationUnit('B');
DimensionalObject dx = new BaseDimensionalObject(DimensionalObject.DATA_X_DIM_ID, DimensionType.DATA_X, DimensionalObjectUtils.getList(deA, inA, dsA));
DimensionalObject ou = new BaseDimensionalObject(DimensionalObject.ORGUNIT_DIM_ID, DimensionType.ORGANISATION_UNIT, Lists.newArrayList(ouA, ouB));
DataQueryParams params = DataQueryParams.newBuilder().addDimension(dx).addDimension(ou).withDisplayProperty(DisplayProperty.NAME).build();
Map<String, String> map = AnalyticsUtils.getDimensionItemNameMap(params);
assertEquals(map.get(deA.getDimensionItem()), deA.getDisplayName());
assertEquals(map.get(inA.getDimensionItem()), inA.getDisplayName());
assertEquals(map.get(dsA.getDimensionItem()), dsA.getDisplayName());
assertEquals(map.get(ouA.getDimensionItem()), ouA.getDisplayName());
assertEquals(map.get(ouB.getDimensionItem()), ouB.getDisplayName());
}
use of org.hisp.dhis.common.DimensionalObject in project dhis2-core by dhis2.
the class DefaultDataQueryService method getFromAnalyticalObject.
@Override
public DataQueryParams getFromAnalyticalObject(AnalyticalObject object) {
Assert.notNull(object, "Analytical object cannot be null");
DataQueryParams.Builder params = DataQueryParams.newBuilder();
I18nFormat format = i18nManager.getI18nFormat();
IdScheme idScheme = IdScheme.UID;
Date date = object.getRelativePeriodDate();
String userOrgUnit = object.getRelativeOrganisationUnit() != null ? object.getRelativeOrganisationUnit().getUid() : null;
List<OrganisationUnit> userOrgUnits = getUserOrgUnits(null, userOrgUnit);
object.populateAnalyticalProperties();
for (DimensionalObject column : object.getColumns()) {
params.addDimension(getDimension(column.getDimension(), getDimensionalItemIds(column.getItems()), date, userOrgUnits, format, false, false, idScheme));
}
for (DimensionalObject row : object.getRows()) {
params.addDimension(getDimension(row.getDimension(), getDimensionalItemIds(row.getItems()), date, userOrgUnits, format, false, false, idScheme));
}
for (DimensionalObject filter : object.getFilters()) {
params.addFilter(getDimension(filter.getDimension(), getDimensionalItemIds(filter.getItems()), date, userOrgUnits, format, false, false, idScheme));
}
return params.build();
}
use of org.hisp.dhis.common.DimensionalObject in project dhis2-core by dhis2.
the class DefaultDataQueryService method getDimension.
// TODO optimize so that org unit levels + boundary are used in query instead of fetching all org units one by one
@Override
public DimensionalObject getDimension(String dimension, List<String> items, Date relativePeriodDate, List<OrganisationUnit> userOrgUnits, I18nFormat format, boolean allowNull, boolean allowAllPeriodItems, IdScheme inputIdScheme) {
final boolean allItems = items.isEmpty();
if (DATA_X_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> dataDimensionItems = new ArrayList<>();
for (String uid : items) {
if (uid.startsWith(KEY_DE_GROUP)) {
String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
DataElementGroup group = idObjectManager.getObject(DataElementGroup.class, inputIdScheme, groupUid);
if (group != null) {
dataDimensionItems.addAll(group.getMembers());
}
} else if (uid.startsWith(KEY_IN_GROUP)) {
String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
IndicatorGroup group = idObjectManager.getObject(IndicatorGroup.class, inputIdScheme, groupUid);
if (group != null) {
dataDimensionItems.addAll(group.getMembers());
}
} else {
DimensionalItemObject dimItemObject = dimensionService.getDataDimensionalItemObject(inputIdScheme, uid);
if (dimItemObject != null) {
dataDimensionItems.add(dimItemObject);
}
}
}
if (dataDimensionItems.isEmpty()) {
throw new IllegalQueryException("Dimension dx is present in query without any valid dimension options");
}
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.DATA_X, null, DISPLAY_NAME_DATA_X, dataDimensionItems);
return object;
} else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> cocs = new ArrayList<>();
for (String uid : items) {
DataElementCategoryOptionCombo coc = idObjectManager.getObject(DataElementCategoryOptionCombo.class, inputIdScheme, uid);
if (coc != null) {
cocs.add(coc);
}
}
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.CATEGORY_OPTION_COMBO, null, DISPLAY_NAME_CATEGORYOPTIONCOMBO, cocs);
return object;
} else if (ATTRIBUTEOPTIONCOMBO_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> aocs = new ArrayList<>();
for (String uid : items) {
DataElementCategoryOptionCombo aoc = idObjectManager.getObject(DataElementCategoryOptionCombo.class, inputIdScheme, uid);
if (aoc != null) {
aocs.add(aoc);
}
}
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.ATTRIBUTE_OPTION_COMBO, null, DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO, aocs);
return object;
} else if (PERIOD_DIM_ID.equals(dimension)) {
Calendar calendar = PeriodType.getCalendar();
List<Period> periods = new ArrayList<>();
for (String isoPeriod : items) {
if (RelativePeriodEnum.contains(isoPeriod)) {
RelativePeriodEnum relativePeriod = RelativePeriodEnum.valueOf(isoPeriod);
List<Period> relativePeriods = RelativePeriods.getRelativePeriodsFromEnum(relativePeriod, relativePeriodDate, format, true);
periods.addAll(relativePeriods);
} else {
Period period = PeriodType.getPeriodFromIsoString(isoPeriod);
if (period != null) {
periods.add(period);
}
}
}
// Remove duplicates
periods = periods.stream().distinct().collect(Collectors.toList());
if (periods.isEmpty() && !allowAllPeriodItems) {
throw new IllegalQueryException("Dimension pe is present in query without any valid dimension options");
}
for (Period period : periods) {
String name = format != null ? format.formatPeriod(period) : null;
period.setName(name);
period.setShortName(name);
if (!calendar.isIso8601()) {
period.setUid(getLocalPeriodIdentifier(period, calendar));
}
}
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.PERIOD, null, DISPLAY_NAME_PERIOD, asList(periods));
return object;
} else if (ORGUNIT_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> ous = new ArrayList<>();
List<Integer> levels = new ArrayList<>();
List<OrganisationUnitGroup> groups = new ArrayList<>();
for (String ou : items) {
if (KEY_USER_ORGUNIT.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
ous.addAll(userOrgUnits);
} else if (KEY_USER_ORGUNIT_CHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
ous.addAll(OrganisationUnit.getSortedChildren(userOrgUnits));
} else if (KEY_USER_ORGUNIT_GRANDCHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
ous.addAll(OrganisationUnit.getSortedGrandChildren(userOrgUnits));
} else if (ou != null && ou.startsWith(KEY_LEVEL)) {
int level = DimensionalObjectUtils.getLevelFromLevelParam(ou);
if (level > 0) {
levels.add(level);
}
} else if (ou != null && ou.startsWith(KEY_ORGUNIT_GROUP)) {
String uid = DimensionalObjectUtils.getUidFromGroupParam(ou);
OrganisationUnitGroup group = idObjectManager.getObject(OrganisationUnitGroup.class, inputIdScheme, uid);
if (group != null) {
groups.add(group);
}
} else if (!inputIdScheme.is(IdentifiableProperty.UID) || CodeGenerator.isValidUid(ou)) {
OrganisationUnit unit = idObjectManager.getObject(OrganisationUnit.class, inputIdScheme, ou);
if (unit != null) {
ous.add(unit);
}
}
}
// Remove duplicates
ous = ous.stream().distinct().collect(Collectors.toList());
List<DimensionalItemObject> orgUnits = new ArrayList<>();
List<OrganisationUnit> ousList = asTypedList(ous);
if (!levels.isEmpty()) {
orgUnits.addAll(sort(organisationUnitService.getOrganisationUnitsAtLevels(levels, ousList)));
}
if (!groups.isEmpty()) {
orgUnits.addAll(sort(organisationUnitService.getOrganisationUnits(groups, ousList)));
}
if (levels.isEmpty() && groups.isEmpty()) {
orgUnits.addAll(ous);
}
if (orgUnits.isEmpty()) {
throw new IllegalQueryException("Dimension ou is present in query without any valid dimension options");
}
// Remove duplicates
orgUnits = orgUnits.stream().distinct().collect(Collectors.toList());
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT, null, DISPLAY_NAME_ORGUNIT, orgUnits);
return object;
} else if (LONGITUDE_DIM_ID.contains(dimension)) {
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LONGITUDE, new ArrayList<>());
return object;
} else if (LATITUDE_DIM_ID.contains(dimension)) {
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LATITUDE, new ArrayList<>());
return object;
} else {
DimensionalObject dimObject = idObjectManager.get(DataQueryParams.DYNAMIC_DIM_CLASSES, inputIdScheme, dimension);
if (dimObject != null && dimObject.isDataDimension()) {
Class<?> dimClass = ReflectionUtils.getRealClass(dimObject.getClass());
Class<? extends DimensionalItemObject> itemClass = DimensionalObject.DIMENSION_CLASS_ITEM_CLASS_MAP.get(dimClass);
List<DimensionalItemObject> dimItems = !allItems ? asList(idObjectManager.getByUidOrdered(itemClass, items)) : dimObject.getItems();
DimensionalObject object = new BaseDimensionalObject(dimension, dimObject.getDimensionType(), null, dimObject.getName(), dimItems, allItems);
return object;
}
}
if (allowNull) {
return null;
}
throw new IllegalQueryException("Dimension identifier does not reference any dimension: " + dimension);
}
Aggregations