Skip to content

Commit

Permalink
feat: add /tracker/events?categoryOptionComboIdScheme
Browse files Browse the repository at this point in the history
  • Loading branch information
teleivo committed Nov 8, 2024
1 parent b23a605 commit 38b035b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,18 @@ class JdbcEventStore implements EventStore {
private static final String COLUMN_PROGRAM_UID = "p_uid";
private static final String COLUMN_PROGRAM_CODE = "p_code";
private static final String COLUMN_PROGRAM_NAME = "p_name";
private static final String COLUMN_PROGRAM_ATTRIBUTE_VALUES = "p_attribute_values";
private static final String COLUMN_PROGRAM_ATTRIBUTE_VALUES = "p_attributevalues";
private static final String COLUMN_PROGRAM_STAGE_UID = "ps_uid";
private static final String COLUMN_PROGRAM_STAGE_CODE = "ps_code";
private static final String COLUMN_PROGRAM_STAGE_NAME = "ps_name";
private static final String COLUMN_PROGRAM_STAGE_ATTRIBUTE_VALUES = "ps_attribute_values";
private static final String COLUMN_PROGRAM_STAGE_ATTRIBUTE_VALUES = "ps_attributevalues";
private static final String COLUMN_ENROLLMENT_UID = "en_uid";
private static final String COLUMN_ENROLLMENT_STATUS = "en_status";
private static final String COLUMN_ENROLLMENT_DATE = "en_enrollmentdate";
private static final String COLUMN_ORG_UNIT_UID = "orgunit_uid";
private static final String COLUMN_ORG_UNIT_CODE = "orgunit_code";
private static final String COLUMN_ORG_UNIT_NAME = "orgunit_name";
private static final String COLUMN_ORG_UNIT_ATTRIBUTE_VALUES = "orgunit_attribute_values";
private static final String COLUMN_ORG_UNIT_ATTRIBUTE_VALUES = "orgunit_attributevalues";
private static final String COLUMN_TRACKEDENTITY_UID = "te_uid";
private static final String COLUMN_EVENT_OCCURRED_DATE = "ev_occurreddate";
private static final String COLUMN_ENROLLMENT_FOLLOWUP = "en_followup";
Expand All @@ -185,13 +185,17 @@ class JdbcEventStore implements EventStore {
private static final String COLUMN_EVENT_LAST_UPDATED_AT_CLIENT = "ev_lastupdatedatclient";
private static final String COLUMN_EVENT_COMPLETED_BY = "ev_completedby";
private static final String COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_UID = "coc_uid";
private static final String COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_NAME = "coc_name";
private static final String COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_CODE = "coc_code";
private static final String COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_ATTRIBUTE_VALUES =
"coc_attributevalues";
private static final String COLUMN_EVENT_COMPLETED_DATE = "ev_completeddate";
private static final String COLUMN_EVENT_DELETED = "ev_deleted";
private static final String COLUMN_EVENT_ASSIGNED_USER_USERNAME = "user_assigned_username";
private static final String COLUMN_EVENT_ASSIGNED_USER_DISPLAY_NAME = "user_assigned_name";
private static final String COLUMN_USER_UID = "u_uid";
private static final String COLUMN_ORG_UNIT_PATH = "ou_path";
private static final String DEFAULT_ORDER = COLUMN_EVENT_ID + " desc";
private static final String COLUMN_ORG_UNIT_PATH = "ou_path";
private static final String USER_SCOPE_ORG_UNIT_PATH_LIKE_MATCH_QUERY =
" ou.path like CONCAT(orgunit.path, '%') ";
private static final String CUSTOM_ORG_UNIT_PATH_LIKE_MATCH_QUERY =
Expand Down Expand Up @@ -340,6 +344,11 @@ private List<Event> fetchEvents(EventQueryParams queryParams, PageParams pagePar

CategoryOptionCombo coc = new CategoryOptionCombo();
coc.setUid(resultSet.getString(COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_UID));
coc.setCode(resultSet.getString(COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_CODE));
coc.setName(resultSet.getString(COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_NAME));
coc.setAttributeValues(
AttributeValues.of(
resultSet.getString(COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_ATTRIBUTE_VALUES)));
Set<CategoryOption> options =
Arrays.stream(resultSet.getString("co_uids").split(TextUtils.COMMA))
.map(
Expand Down Expand Up @@ -754,12 +763,15 @@ private String getEventSelectQuery(
"au.firstName as user_assigned_first_name, au.surName as user_assigned_surname, ")
.append("au.username as ")
.append(COLUMN_EVENT_ASSIGNED_USER_USERNAME)
.append(",")
.append("coc_agg.uid as ")
.append(", coc_agg.uid as ")
.append(COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_UID)
.append(", ")
.append("coc_agg.co_uids AS co_uids, ")
.append("coc_agg.co_count AS option_size, ");
.append(", coc_agg.code as ")
.append(COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_CODE)
.append(", coc_agg.name as ")
.append(COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_NAME)
.append(", coc_agg.attributevalues as ")
.append(COLUMN_EVENT_ATTRIBUTE_OPTION_COMBO_ATTRIBUTE_VALUES)
.append(", coc_agg.co_uids AS co_uids, coc_agg.co_count AS option_size, ");

for (Order order : params.getOrder()) {
if (order.getField() instanceof TrackedEntityAttribute tea)
Expand Down Expand Up @@ -1447,13 +1459,13 @@ private String addLastUpdatedFilters(
private String getCategoryOptionComboQuery(User user) {
String joinCondition =
"""
inner join (select coc.uid, coc.attributevalues, coc.code, coc.categoryoptioncomboid as\
id, string_agg(co.uid, ',') as co_uids, count(co.categoryoptionid) as co_count from\
categoryoptioncombo coc inner join categoryoptioncombos_categoryoptions cocco on\
coc.categoryoptioncomboid = cocco.categoryoptioncomboid inner join categoryoption\
co on cocco.categoryoptionid = co.categoryoptionid group by\
coc.categoryoptioncomboid \
""";
inner join (select coc.uid, coc.code, coc.name, coc.attributevalues, coc.categoryoptioncomboid as\
id, string_agg(co.uid, ',') as co_uids, count(co.categoryoptionid) as co_count from\
categoryoptioncombo coc inner join categoryoptioncombos_categoryoptions cocco on\
coc.categoryoptioncomboid = cocco.categoryoptioncomboid inner join categoryoption\
co on cocco.categoryoptionid = co.categoryoptionid group by\
coc.categoryoptioncomboid \
""";

if (!isSuper(user)) {
joinCondition =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"categoryOptionAttribute": true,
"categoryOptionComboAttribute": true,
"dataElementAttribute": true,
"name": "some attribute",
"name": "idScheme related attribute",
"organisationUnitAttribute": true,
"programAttribute": true,
"programStageAttribute": true,
Expand Down Expand Up @@ -104,6 +104,14 @@
"categoryOptionCombos": [
{
"id": "HllvX50cXC0",
"attributeValues": [
{
"attribute": {
"id": "j45AR9cBQKc"
},
"value": "categoryOptionCombo HllvX50cXC0"
}
],
"categoryCombo": {
"id": "bjDvmb4bfuf"
},
Expand All @@ -112,8 +120,8 @@
"id": "xYerKDKCefk"
}
],
"code": "default",
"name": "default"
"code": "HllvX50cXC0 code",
"name": "HllvX50cXC0 name"
},
{
"id": "tzsPhPtE94U",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.hisp.dhis.attribute.Attribute;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.common.IdentifiableObject;
import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle;
Expand Down Expand Up @@ -98,6 +99,7 @@ class IdSchemeExportControllerTest extends PostgresControllerIntegrationTestBase
private OrganisationUnit orgUnit;
private ProgramStage programStage;
private Program program;
private CategoryOptionCombo categoryOptionCombo;

private User importUser;

Expand Down Expand Up @@ -133,6 +135,7 @@ void setUp() throws IOException {
orgUnit = get(OrganisationUnit.class, "h4w96yEMlzO");
program = get(Program.class, "BFcipDERJnf");
programStage = get(ProgramStage.class, "NpsdDv6kKSO");
categoryOptionCombo = get(CategoryOptionCombo.class, "HllvX50cXC0");

manager.flush();
manager.clear();
Expand All @@ -143,13 +146,32 @@ void setUp() throws IOException {
void shouldExportMetadataUsingGivenIdScheme(TrackerIdSchemeParam idSchemeParam) {
switchContextToUser(importUser);

// maps JSON fields to idScheme request parameters
Map<String, String> idSchemeRequestParams =
Map.of(
"orgUnit",
"orgUnit",
"program",
"program",
"programStage",
"programStage",
"attributeOptionCombo",
"categoryOptionCombo");
// maps JSON fields to metadata
Map<String, IdentifiableObject> metadata =
Map.of("orgUnit", orgUnit, "program", program, "programStage", programStage);
Map.of(
"orgUnit",
orgUnit,
"program",
program,
"programStage",
programStage,
"attributeOptionCombo",
categoryOptionCombo);
String fields = metadata.keySet().stream().collect(Collectors.joining(","));
String idSchemes =
metadata.keySet().stream()
.map(m -> m + "IdScheme=" + idSchemeParam)
.map(m -> idSchemeRequestParams.get(m) + "IdScheme=" + idSchemeParam)
.collect(Collectors.joining("&"));
Event d9PbzJY8bJM = get(Event.class, "D9PbzJY8bJM");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package org.hisp.dhis.webapi.controller.tracker.export;

import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStage;
Expand All @@ -52,4 +53,10 @@ default String map(ProgramStage programStage, @Context TrackerIdSchemeParams idS
default String map(OrganisationUnit orgUnit, @Context TrackerIdSchemeParams idSchemeParams) {
return idSchemeParams.getOrgUnitIdScheme().getIdentifier(orgUnit);
}

@Named("categoryOptionComboToString")
default String map(
CategoryOptionCombo categoryOptionCombo, @Context TrackerIdSchemeParams idSchemeParams) {
return idSchemeParams.getCategoryOptionComboIdScheme().getIdentifier(categoryOptionCombo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ public interface EventMapper {
@Mapping(target = "createdAtClient", source = "createdAtClient")
@Mapping(target = "updatedAt", source = "lastUpdated")
@Mapping(target = "updatedAtClient", source = "lastUpdatedAtClient")
@Mapping(target = "attributeOptionCombo", source = "attributeOptionCombo.uid")
@Mapping(
target = "attributeOptionCombo",
source = "attributeOptionCombo",
qualifiedByName = "categoryOptionComboToString")
@Mapping(target = "attributeCategoryOptions", source = "attributeOptionCombo.categoryOptions")
@Mapping(target = "completedAt", source = "completedDate")
@Mapping(target = "createdBy", source = "createdByUserInfo")
Expand Down

0 comments on commit 38b035b

Please sign in to comment.