Skip to content

Commit

Permalink
fix: Enrollment dim. restriction SQL [DHIS2-16349] (#17393) (#17411)
Browse files Browse the repository at this point in the history
* fix: Enrollment dim. restriction SQL [DHIS2-16349] (#17393)

* fix: Enrollment dim. restriction SQL [DHIS2-16349]

* trigger Github actions

(cherry picked from commit 232cd91)

* spotless fix

* Replace missing isAttributeCategory method

* fix testEnrollmentWithCategoryDimensionRestriction

(cherry picked from commit 05d6b7b)
  • Loading branch information
jimgrace committed May 14, 2024
1 parent b7097de commit 23caed1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import static org.hisp.dhis.analytics.util.AnalyticsSqlUtils.quote;
import static org.hisp.dhis.analytics.util.AnalyticsSqlUtils.quoteAlias;
import static org.hisp.dhis.analytics.util.AnalyticsUtils.withExceptionHandling;
import static org.hisp.dhis.common.DataDimensionType.ATTRIBUTE;
import static org.hisp.dhis.common.DimensionItemType.DATA_ELEMENT;
import static org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID;
import static org.hisp.dhis.common.IdentifiableObjectUtils.getUids;
Expand All @@ -56,6 +57,7 @@
import org.hisp.dhis.analytics.event.EnrollmentAnalyticsManager;
import org.hisp.dhis.analytics.event.EventQueryParams;
import org.hisp.dhis.analytics.event.ProgramIndicatorSubqueryBuilder;
import org.hisp.dhis.category.CategoryOption;
import org.hisp.dhis.common.DimensionType;
import org.hisp.dhis.common.DimensionalItemObject;
import org.hisp.dhis.common.DimensionalObject;
Expand Down Expand Up @@ -342,18 +344,25 @@ protected String getWhereClause(EventQueryParams params) {
}

// ---------------------------------------------------------------------
// Organisation unit group sets
// Categories (enrollments don't have attribute categories)
// ---------------------------------------------------------------------

List<DimensionalObject> dynamicDimensions =
params.getDimensionsAndFilters(Sets.newHashSet(DimensionType.CATEGORY));

for (DimensionalObject dim : dynamicDimensions) {
String col = quoteAlias(dim.getDimensionName());
if (!isAttributeCategory(dim)) {
String col = quoteAlias(dim.getDimensionName());

sql += "and " + col + " in (" + getQuotedCommaDelimitedString(getUids(dim.getItems())) + ") ";
sql +=
"and " + col + " in (" + getQuotedCommaDelimitedString(getUids(dim.getItems())) + ") ";
}
}

// ---------------------------------------------------------------------
// Organisation unit group sets
// ---------------------------------------------------------------------

dynamicDimensions =
params.getDimensionsAndFilters(Sets.newHashSet(DimensionType.ORGANISATION_UNIT_GROUP_SET));

Expand Down Expand Up @@ -629,6 +638,20 @@ protected String getColumn(QueryItem item) {
return getColumn(item, "");
}

/**
* Is a category dimension an attribute category (rather than a disaggregation category)?
* Attribute categories are not included in enrollment tables, so category user dimension
* restrictions (which use attribute categories) do not apply.
*/
private boolean isAttributeCategory(DimensionalObject categoryDim) {
return ((CategoryOption) categoryDim.getItems().get(0))
.getCategories()
.iterator()
.next()
.getDataDimensionType()
== ATTRIBUTE;
}

private static String getExecutionDateFilter(Date startDate, Date endDate) {
StringBuilder sb = new StringBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,20 @@ void testDimensionRestrictionWhenUserCannotReadCategoryOptions() {
"Current user is constrained by a dimension but has access to no dimension items"));
}

@Test
void testEnrollmentWithCategoryDimensionRestriction() {
injectSecurityContext(userA);
EventQueryParams params = getEnrollmentQueryBuilderA().build();
Grid grid = enrollmentTarget.getEnrollments(params);

assertGridContains(
// Headers
List.of("enrollmentdate", "ou", "tei", "teaAttribuU"),
// Grid
List.of(List.of("2017-01-01 00:00:00.0", "ouabcdefghE", "trackEntInA", "ouabcdefghF")),
grid);
}

// -------------------------------------------------------------------------
// Test getAggregatedEventData with OrgUnitFields
// -------------------------------------------------------------------------
Expand Down

0 comments on commit 23caed1

Please sign in to comment.