Skip to content

Commit

Permalink
feat: Metadata for user orgunits in analytics CPL response [2.41-DHIS…
Browse files Browse the repository at this point in the history
…2-16025] (#15476)

* feat: Metadata for user orgunits in analytics CPL response [2.41-DHIS2-16025]

* clean coding

* clean coding

* clean coding
  • Loading branch information
d-bernat authored Oct 25, 2023
1 parent 5c9c0f9 commit 027e318
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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<AnalyticsMetaDataKey> userOrgUnitMetaDataKeys =
getUserOrgUnitsMetadataKeys(commonParams);
Map<String, Object> 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));

Expand Down Expand Up @@ -129,6 +140,35 @@ private List<OrganisationUnit> 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<AnalyticsMetaDataKey> 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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void addMetaData(DataQueryParams params, Grid grid) {

Map<String, Object> items = new HashMap<>(getDimensionMetadataItemMap(params, grid));

AnalyticsOrganisationUnitUtils.getUserOrganisationUnitsUidList(
AnalyticsOrganisationUnitUtils.getUserOrganisationUnitItems(
currentUserService.getCurrentUser(), params.getUserOrganisationUnitsCriteria())
.forEach(items::putAll);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ protected void addMetadata(
}

Map<String, Object> items = new HashMap<>();
AnalyticsOrganisationUnitUtils.getUserOrganisationUnitsUidList(
AnalyticsOrganisationUnitUtils.getUserOrganisationUnitItems(
currentUserService.getCurrentUser(), params.getUserOrganisationUnitsCriteria())
.forEach(items::putAll);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, Object>> getUserOrganisationUnitsUidList(
public static Collection<Map<String, Object>> getUserOrganisationUnitItems(
User currentUser, List<AnalyticsMetaDataKey> userOrganisationUnitsCriteria) {
List<Map<String, Object>> userOrganisations = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -77,7 +77,7 @@ void testAnalyticsOrganisationUnitUtils_All() {
// when
when(user.getOrganisationUnits()).thenReturn(Set.of(orgA));
List<Map<String, Object>> uidMapList =
getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList();
getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList();

// then
assertEquals(3, uidMapList.size());
Expand All @@ -100,7 +100,7 @@ void testAnalyticsOrganisationUnitUtils_Parent() {
// when
when(user.getOrganisationUnits()).thenReturn(Set.of(orgA));
List<Map<String, Object>> uidMapList =
getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList();
getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList();

// then
assertEquals(1, uidMapList.size());
Expand All @@ -117,7 +117,7 @@ void testAnalyticsOrganisationUnitUtils_Children() {
// when
when(user.getOrganisationUnits()).thenReturn(Set.of(orgA));
List<Map<String, Object>> uidMapList =
getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList();
getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList();

// then
assertEquals(1, uidMapList.size());
Expand All @@ -134,7 +134,7 @@ void testAnalyticsOrganisationUnitUtils_Grandchildren() {
// when
when(user.getOrganisationUnits()).thenReturn(Set.of(orgA));
List<Map<String, Object>> uidMapList =
getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList();
getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList();

// then
assertEquals(1, uidMapList.size());
Expand All @@ -152,7 +152,7 @@ void testAnalyticsOrganisationUnitUtils_All_Empty() {
// when
when(user.getOrganisationUnits()).thenReturn(Set.of());
List<Map<String, Object>> uidMapList =
getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList();
getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList();

// then
assertEquals(3, uidMapList.size());
Expand All @@ -174,7 +174,7 @@ void testAnalyticsOrganisationUnitUtils_Parent_Empty() {
// when
when(user.getOrganisationUnits()).thenReturn(Set.of());
List<Map<String, Object>> uidMapList =
getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList();
getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList();

// then
assertEquals(1, uidMapList.size());
Expand All @@ -190,7 +190,7 @@ void testAnalyticsOrganisationUnitUtils_Children_Empty() {
// when
when(user.getOrganisationUnits()).thenReturn(Set.of());
List<Map<String, Object>> uidMapList =
getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList();
getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList();

// then
assertEquals(1, uidMapList.size());
Expand All @@ -207,7 +207,7 @@ void testAnalyticsOrganisationUnitUtils_Grandchildren_Empty() {
// when
when(user.getOrganisationUnits()).thenReturn(Set.of());
List<Map<String, Object>> uidMapList =
getUserOrganisationUnitsUidList(user, userOrganisationUnitsCriteria).stream().toList();
getUserOrganisationUnitItems(user, userOrganisationUnitsCriteria).stream().toList();

// then
assertEquals(1, uidMapList.size());
Expand Down

0 comments on commit 027e318

Please sign in to comment.