diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/processing/MetadataParamsHandler.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/processing/MetadataParamsHandler.java index b45ce476d8af..22696c9589f5 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/processing/MetadataParamsHandler.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/processing/MetadataParamsHandler.java @@ -35,12 +35,16 @@ import static org.hisp.dhis.analytics.AnalyticsMetaDataKey.ORG_UNIT_NAME_HIERARCHY; import static org.hisp.dhis.analytics.AnalyticsMetaDataKey.PAGER; import static org.hisp.dhis.analytics.orgunit.OrgUnitHelper.getActiveOrganisationUnits; +import static org.hisp.dhis.analytics.util.AnalyticsOrganisationUnitUtils.getUserOrganisationUnitItems; import static org.hisp.dhis.common.DimensionalObjectUtils.asTypedList; import static org.hisp.dhis.organisationunit.OrganisationUnit.getParentGraphMap; import static org.hisp.dhis.organisationunit.OrganisationUnit.getParentNameGraphMap; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Set; +import org.hisp.dhis.analytics.AnalyticsMetaDataKey; import org.hisp.dhis.analytics.common.MetadataInfo; import org.hisp.dhis.analytics.common.params.AnalyticsPagingParams; import org.hisp.dhis.analytics.common.params.CommonParams; @@ -63,6 +67,7 @@ @Component public class MetadataParamsHandler { private static final String DOT = "."; + private static final String ORG_UNIT_DIM = "ou"; /** * Appends the metadata to the given {@link Grid} based on the given arguments. @@ -73,10 +78,16 @@ public class MetadataParamsHandler { */ public void handle(Grid grid, CommonParams commonParams, User user, long rowsCount) { if (!commonParams.isSkipMeta()) { - MetadataInfo metadataInfo = new MetadataInfo(); // Dimensions. - metadataInfo.put(ITEMS.getKey(), new MetadataItemsHandler().handle(grid, commonParams)); + List userOrgUnitMetaDataKeys = + getUserOrgUnitsMetadataKeys(commonParams); + Map items = + new HashMap<>(new MetadataItemsHandler().handle(grid, commonParams)); + getUserOrganisationUnitItems(user, userOrgUnitMetaDataKeys).forEach(items::putAll); + MetadataInfo metadataInfo = new MetadataInfo(); + metadataInfo.put(ITEMS.getKey(), items); + metadataInfo.put( DIMENSIONS.getKey(), new MetadataDimensionsHandler().handle(grid, commonParams)); @@ -129,6 +140,35 @@ private List getActiveOrgUnits(Grid grid, CommonParams commonP return getActiveOrganisationUnits(grid, organisationUnits); } + /** + * Retrieve the analytics metadata keys belong to user organisation unit dimension group + * + * @param commonParams the {@link CommonParams}. + * @return list of the {@link AnalyticsMetaDataKey} + */ + private static List getUserOrgUnitsMetadataKeys(CommonParams commonParams) { + return commonParams.getDimensionIdentifiers().stream() + .filter(dimensionIdentifier -> dimensionIdentifier.toString().equals(ORG_UNIT_DIM)) + .flatMap(dimensionIdentifier -> dimensionIdentifier.getDimension().getItems().stream()) + .flatMap(item -> item.getValues().stream()) + .filter( + item -> + item.equals(AnalyticsMetaDataKey.USER_ORGUNIT.getKey()) + || item.equals(AnalyticsMetaDataKey.USER_ORGUNIT_CHILDREN.getKey()) + || item.equals(AnalyticsMetaDataKey.USER_ORGUNIT_GRANDCHILDREN.getKey())) + .map( + item -> { + if (item.equals(AnalyticsMetaDataKey.USER_ORGUNIT.getKey())) { + return AnalyticsMetaDataKey.USER_ORGUNIT; + } + if (item.equals(AnalyticsMetaDataKey.USER_ORGUNIT_CHILDREN.getKey())) { + return AnalyticsMetaDataKey.USER_ORGUNIT_CHILDREN; + } + return AnalyticsMetaDataKey.USER_ORGUNIT_GRANDCHILDREN; + }) + .toList(); + } + /** * Returns the query {@link QueryItem} identifier. It may be prefixed with its program stage * identifier (if one exists). diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/handler/MetadataHandler.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/handler/MetadataHandler.java index 68f043c5ac48..5e3208c47c52 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/handler/MetadataHandler.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/handler/MetadataHandler.java @@ -94,7 +94,7 @@ public void addMetaData(DataQueryParams params, Grid grid) { Map items = new HashMap<>(getDimensionMetadataItemMap(params, grid)); - AnalyticsOrganisationUnitUtils.getUserOrganisationUnitsUidList( + AnalyticsOrganisationUnitUtils.getUserOrganisationUnitItems( currentUserService.getCurrentUser(), params.getUserOrganisationUnitsCriteria()) .forEach(items::putAll); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/AbstractAnalyticsService.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/AbstractAnalyticsService.java index 33c4ac8a9eef..071e76fc4b69 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/AbstractAnalyticsService.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/AbstractAnalyticsService.java @@ -393,7 +393,7 @@ protected void addMetadata( } Map items = new HashMap<>(); - AnalyticsOrganisationUnitUtils.getUserOrganisationUnitsUidList( + AnalyticsOrganisationUnitUtils.getUserOrganisationUnitItems( currentUserService.getCurrentUser(), params.getUserOrganisationUnitsCriteria()) .forEach(items::putAll); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/AnalyticsOrganisationUnitUtils.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/AnalyticsOrganisationUnitUtils.java index e5efe029d2db..a85492c0c23f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/AnalyticsOrganisationUnitUtils.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/AnalyticsOrganisationUnitUtils.java @@ -52,7 +52,7 @@ public class AnalyticsOrganisationUnitUtils { * @param currentUser the {@link org.hisp.dhis.user.CurrentUser}. * @return intersection of requested user organisation units and all units in response grid. */ - public static Collection> getUserOrganisationUnitsUidList( + public static Collection> getUserOrganisationUnitItems( User currentUser, List userOrganisationUnitsCriteria) { List> userOrganisations = new ArrayList<>(); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsOrganisationUnitUtilsTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsOrganisationUnitUtilsTest.java index 415daed25dc0..820f2a9edb6d 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsOrganisationUnitUtilsTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsOrganisationUnitUtilsTest.java @@ -31,7 +31,7 @@ import static org.hisp.dhis.analytics.AnalyticsMetaDataKey.USER_ORGUNIT; import static org.hisp.dhis.analytics.AnalyticsMetaDataKey.USER_ORGUNIT_CHILDREN; import static org.hisp.dhis.analytics.AnalyticsMetaDataKey.USER_ORGUNIT_GRANDCHILDREN; -import static org.hisp.dhis.analytics.util.AnalyticsOrganisationUnitUtils.getUserOrganisationUnitsUidList; +import static org.hisp.dhis.analytics.util.AnalyticsOrganisationUnitUtils.getUserOrganisationUnitItems; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; @@ -77,7 +77,7 @@ void testAnalyticsOrganisationUnitUtils_All() { // when when(user.getOrganisationUnits()).thenReturn(Set.of(orgA)); List> uidMapList = - getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList(); + getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList(); // then assertEquals(3, uidMapList.size()); @@ -100,7 +100,7 @@ void testAnalyticsOrganisationUnitUtils_Parent() { // when when(user.getOrganisationUnits()).thenReturn(Set.of(orgA)); List> uidMapList = - getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList(); + getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList(); // then assertEquals(1, uidMapList.size()); @@ -117,7 +117,7 @@ void testAnalyticsOrganisationUnitUtils_Children() { // when when(user.getOrganisationUnits()).thenReturn(Set.of(orgA)); List> uidMapList = - getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList(); + getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList(); // then assertEquals(1, uidMapList.size()); @@ -134,7 +134,7 @@ void testAnalyticsOrganisationUnitUtils_Grandchildren() { // when when(user.getOrganisationUnits()).thenReturn(Set.of(orgA)); List> uidMapList = - getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList(); + getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList(); // then assertEquals(1, uidMapList.size()); @@ -152,7 +152,7 @@ void testAnalyticsOrganisationUnitUtils_All_Empty() { // when when(user.getOrganisationUnits()).thenReturn(Set.of()); List> uidMapList = - getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList(); + getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList(); // then assertEquals(3, uidMapList.size()); @@ -174,7 +174,7 @@ void testAnalyticsOrganisationUnitUtils_Parent_Empty() { // when when(user.getOrganisationUnits()).thenReturn(Set.of()); List> uidMapList = - getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList(); + getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList(); // then assertEquals(1, uidMapList.size()); @@ -190,7 +190,7 @@ void testAnalyticsOrganisationUnitUtils_Children_Empty() { // when when(user.getOrganisationUnits()).thenReturn(Set.of()); List> uidMapList = - getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList(); + getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList(); // then assertEquals(1, uidMapList.size()); @@ -207,7 +207,7 @@ void testAnalyticsOrganisationUnitUtils_Grandchildren_Empty() { // when when(user.getOrganisationUnits()).thenReturn(Set.of()); List> uidMapList = - getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList(); + getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList(); // then assertEquals(1, uidMapList.size());