Skip to content

Commit

Permalink
fix: fixes period order in TE endpoint [DHIS2-16265] (#17915)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnespolino authored Jun 28, 2024
1 parent 6843e2d commit 85d2d49
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import static java.util.stream.Collectors.toSet;
import static org.hisp.dhis.common.DimensionalObject.DATA_X_DIM_ID;
import static org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID;
import static org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID;

import java.util.List;
import java.util.Objects;
Expand All @@ -42,6 +41,7 @@
import org.hisp.dhis.analytics.common.params.dimension.DimensionParam;
import org.hisp.dhis.common.BaseDimensionalObject;
import org.hisp.dhis.common.DimensionItemKeywords.Keyword;
import org.hisp.dhis.common.DimensionType;
import org.hisp.dhis.common.DimensionalItemObject;
import org.hisp.dhis.common.DimensionalObject;
import org.hisp.dhis.common.QueryItem;
Expand Down Expand Up @@ -145,7 +145,7 @@ public Set<DimensionalItemObject> getAllDimensionalItemObjects() {
* @return the list of {@link DimensionalItemObject}.
*/
public List<DimensionalItemObject> getPeriodDimensionOrFilterItems() {
return getDimensionOptions(PERIOD_DIM_ID, getAllDimensionalObjects());
return getPeriodDimensionOptions(getAllDimensionalObjects());
}

/**
Expand Down Expand Up @@ -226,4 +226,19 @@ public List<DimensionalItemObject> getDimensionOptions(

return index != -1 ? dimensions.get(index).getItems() : emptyList();
}

/**
* Retrieves a list of {@link DimensionalItemObject} objects for the given list of {@link
* DimensionalObject}, of type PERIOD. Returns an empty list if the dimension is not present.
*
* @param dimensions the list of {@link DimensionalObject}.
* @return the list of {@link DimensionalItemObject} or empty list.
*/
public List<DimensionalItemObject> getPeriodDimensionOptions(List<DimensionalObject> dimensions) {
return dimensions.stream()
.filter(d -> d.getDimensionType().equals(DimensionType.PERIOD))
.map(DimensionalObject::getItems)
.flatMap(List::stream)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.hisp.dhis.analytics.common.params.CommonParams;
import org.hisp.dhis.analytics.data.handler.MetadataHandler;
import org.hisp.dhis.calendar.Calendar;
import org.hisp.dhis.common.DimensionType;
import org.hisp.dhis.common.DimensionalItemObject;
import org.hisp.dhis.common.DimensionalObject;
import org.hisp.dhis.common.Grid;
Expand Down Expand Up @@ -71,8 +72,13 @@ public class MetadataDimensionsHandler {
*/
Map<String, List<String>> handle(Grid grid, CommonParams commonParams) {
List<QueryItem> items = commonParams.delegate().getAllItems();

List<DimensionalObject> allDimensionalObjects =
commonParams.delegate().getAllDimensionalObjects();
commonParams.delegate().getAllDimensionalObjects().stream()
// we're adding periods separately so we need to filter them out
.filter(MetadataDimensionsHandler::isNotPeriod)
.toList();

List<DimensionalItemObject> periodDimensionOrFilterItems =
commonParams.delegate().getPeriodDimensionOrFilterItems();
List<QueryItem> itemFilters = commonParams.delegate().getItemsAsFilters();
Expand All @@ -90,6 +96,10 @@ Map<String, List<String>> handle(Grid grid, CommonParams commonParams) {
return dimensionItems;
}

private static boolean isNotPeriod(DimensionalObject dimensionalObject) {
return !dimensionalObject.getDimensionType().equals(DimensionType.PERIOD);
}

/**
* Returns a list of distinct period UIDs. This method was extracted from the original code to be
* used in the {@link MetadataDimensionsHandler} and {@link MetadataHandler} classes.
Expand Down
Loading

0 comments on commit 85d2d49

Please sign in to comment.