Skip to content

Commit

Permalink
feat: add /tracker/events?categoryOptionComboIdScheme DHIS2-14968 (#1…
Browse files Browse the repository at this point in the history
…9102)

* feat: add /tracker/events?categoryOptionComboIdScheme

* test: use merged simple_metadata.json
  • Loading branch information
teleivo authored Nov 11, 2024
1 parent 601ceb4 commit da859c7
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 250 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 @@ -221,6 +221,14 @@
},
{
"id": "SeWJkpLAyLt",
"attributeValues": [
{
"attribute": {
"id": "j45AR9cBQKc"
},
"value": "categoryOptionCombo SeWJkpLAyLt"
}
],
"categoryCombo": {
"id": "O4VaNks6tta"
},
Expand Down Expand Up @@ -672,21 +680,21 @@
"attribute": {
"id": "j45AR9cBQKc"
},
"value": "multi-org-attribute"
"value": "orgUnit DiszpKrYNg8"
}
],
"code": "org3",
"code": "DiszpKrYNg8 code",
"created": "2020-05-31T08:56:15.922",
"description": "test-orgunit-3",
"description": "DiszpKrYNg8 description",
"lastUpdated": "2020-05-31T11:41:22.384",
"lastUpdatedBy": {
"id": "tTgjgobT1oS"
},
"level": 1,
"name": "test-orgunit-3",
"name": "DiszpKrYNg8 name",
"openingDate": "2020-05-31T00:00:00.000",
"path": "/DiszpKrYNg8",
"shortName": "test-orgunit-3",
"shortName": "DiszpKrYNg8 shortName",
"user": {
"id": "tTgjgobT1oS"
}
Expand Down Expand Up @@ -1009,7 +1017,7 @@
"attribute": {
"id": "j45AR9cBQKc"
},
"value": "multi-program-stage-attribute"
"value": "programStage qLZC0lvvxQH"
}
],
"autoGenerateEvent": true,
Expand Down Expand Up @@ -1421,13 +1429,13 @@
"attribute": {
"id": "j45AR9cBQKc"
},
"value": "multi-program-attribute"
"value": "program iS7eutanDry"
}
],
"categoryCombo": {
"id": "O4VaNks6tta"
},
"code": "multi-program",
"code": "iS7eutanDry code",
"completeEventsExpiryDays": 0,
"created": "2022-04-22T05:57:48.409",
"displayIncidentDate": true,
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 @@ -130,9 +132,10 @@ void setUp() throws IOException {
assertNoErrors(
trackerImportService.importTracker(params, fromJson("tracker/event_and_enrollment.json")));
get(Attribute.class, METADATA_ATTRIBUTE); // ensure this is created in setup
orgUnit = get(OrganisationUnit.class, "h4w96yEMlzO");
program = get(Program.class, "BFcipDERJnf");
programStage = get(ProgramStage.class, "NpsdDv6kKSO");
orgUnit = get(OrganisationUnit.class, "DiszpKrYNg8");
program = get(Program.class, "iS7eutanDry");
programStage = get(ProgramStage.class, "qLZC0lvvxQH");
categoryOptionCombo = get(CategoryOptionCombo.class, "SeWJkpLAyLt");

manager.flush();
manager.clear();
Expand All @@ -143,20 +146,39 @@ 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");
Event qRYjLTiJTrA = get(Event.class, "QRYjLTiJTrA");

JsonEvent actual =
GET(
"/tracker/events/{id}?fields={fields}&{idSchemes}",
d9PbzJY8bJM.getUid(),
qRYjLTiJTrA.getUid(),
fields,
idSchemes)
.content(HttpStatus.OK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class TrackedEntitiesExportControllerPostgresTest extends PostgresControllerInte
@BeforeEach
void setUp() throws IOException {
this.renderService = _renderService;
setUpMetadata("tracker/simple_metadata_changelog.json");
setUpMetadata("tracker/simple_metadata.json");

User importUser = userService.getUser("tTgjgobT1oS");
injectSecurityContextUser(importUser);
Expand Down
Loading

0 comments on commit da859c7

Please sign in to comment.