How to target specific health programs from the Dhis2 website, and fetch events or clients from it to the app

I learnt that health programs are first created on the dhis2 website, so the url is used in the app to login then health programs are fetched.

I’m trying to create a new Dhis2 application from the scratch using the Dhis2 sdk core, but i cannot seem to find a documentation that talks about targeting specfic health programs created on the dhis2 website.
I want to be able to fetch events, clients and even submit data to specific health programs i am targeting in code.

I am completely new to dhis2, and I will appreciate a guide on this request thank you.

2 Likes

@marcos.campos , @vgarciabnz or @ericampire might be able to help you with this

1 Like

Hi @samuel.awodire,
this DHIS2 Android sdk is designed to work in offline mode mainly. It means that metadata must be downloaded in first place. The sdk will download all the health programs that are accessible to the user, which means:

  • these programs must be assigned to any organisationUnit assgined to the user
  • the user must have data write access to the program and its program stages

You can check the “workflow” section in the documentation for more details on this (Workflow - DHIS2 Documentation).

Please let me know if that works for you

2 Likes

Hi, @vgarciabnz I can target all programs using Sdk.d2().programModule().programs()

will this work for a specific program Sdk.d2().programModule().programs().byOrganisationUnitUid(“program unique Id”).byRelatedProgramUid().toString();

Well, if you want to get a specific program by uid you can call the method like
Sdk.d2().programModule().programs().uid("program_uid").blockingGet(),

or if you need to get the list of programs assigned to a particular orgunit, then
Sdk.d2().programModule().programs().byOrganisationUnitUid("ou_uid").blockingGet()

Then, you can use the programUid to filter TrackedEntityInstances or Events in their respective repositories.

1 Like

@vgarciabnz Thanks a lot :raised_hands:.
sorry :pray: one more thing, how do i submit an enrollment form to a specific program using the uid, and which format does Dhis2 support e.g JSON

You can use the repositories to create new data locally and upload it to the server. For example, if you want to create an enrollment you can write

String enrollmentUid = d2.enrollmentModule().enrollments().blockingAdd(
                EnrollmentCreateProjection.create("ou_uid", "program_uid", "tei_uid")
);

and then use the enrollmentUid to create events or modify the enrollment, like for example:

d2.enrollmentModule().enrollments().uid(enrollmentUid).setStatus(EnrollmentStatus.COMPLETED);

Then, you can call the upload method in trackedEntityInstances repository to upload the data that has been created or modified locally

d2.trackedEntityModule().trackedEntityInstances().blockingUpload();

You have more examples and details about the data flow in the docs Workflow - DHIS2 Documentation.

Hope it helps

1 Like

@vgarciabnz Thanks for the help so far, i have really made a lot of progress on my app development.
But, i still need your help on a method, i read the doc but i didn’t find the best way to get this method.
Please, how do i get the number of tracked entities or events to a program either by program uid or program name?
the blockCount() stuff

1 Like

Hi @samuel.awodire,

the easiest way to get the count of TEIs is using the TEI repository and filtering by programUid. Assuming you are using Java, somthing like this:

d2.trackedEntityModule().trackedEntityInstances()
            .byProgramUids(Collections.singletonList("program_uid"))
            .blockingCount();

This method includes the deleted enrollment in the count. If you want to exclude them, it is better to use the TEI query repository (similar to TEI repository, but has advanced filtering methods).

d2.trackedEntityModule().trackedEntityInstanceQuery()
            .byProgram().eq("program_uid")
            .byIncludeDeleted().eq(false)
            .blockingCount();

For events, you can use the event repository:

d2.eventModule().events()
            .byProgramUid().eq("program_uid")
            .byDeleted().isFalse()
            .blockingCount();

In all the previous cases, you might replace the string “program_uid” by the actual uid or by a query to the program repository filtering by name. For example:

d2.eventModule().events()
            .byProgramUid().eq(
                d2.programModule().programs().byName().eq("program_name").one().blockingGet().uid() 
            )
            .byDeleted().isFalse()
            .blockingCount();

If you are using Java, please check null-safety in case the program does not exist.

Hope it helps

3 Likes