Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading