Trying to create TEI using the dhis2-java-client

Hi Everyone,

I’m working with the dhis2-java-client (latest version from maven repo) and while it works like a charm to create objects like OrgUnits, I’m not finding how to create TEI through the java client.

Am I doing it wrong ? Is there a separate client or sdk for the tracker part of the API ?

thanks in advance

Hi @Arnaud_Demarcq

Thanks for your post. It’s not common to see developers asking about the DHIS2 Java SDK so I’m really curious in knowing why use it instead of using the API directly (which is the more common case).

There is no separate SDK for the tracker. Are you getting any exception or error that you could share? Could you share the part of the script where you are trying to create a TEI?

Thank you!

Hi.

Thanks for your reply. We ended up calling the API directly as you suggest.

Thanks !

1 Like

I’m late to the party. I generally recommend using the DHIS2 Java SDK instead of the DHIS2 Java Client since the former is lighter in terms of dependencies so it’s easier to embed within applications.

The SDK hides a lot of the nuts and bolts that you would typically need to implement yourself when directly calling the DHIS2 Web API. For instance, with the SDK, you don’t need to worry about pagination or (de)serialisation. Here’s an example of how one would create a tracked entity in DHIS2 using the DHIS2 Java SDK:

Dhis2Client dhis2Client = Dhis2ClientBuilder.newClient( "https://play.im.dhis2.org/stable-2-40-6/api", "admin", "district" ).build();

dhis2Client.post( "trackedEntityTypes" ).
  withResource( new TrackedEntityType().
    withId("MCPQUTHX1Ze").
    withName( "Person" ).
    withCode( "GEN_LIB_TRK_PERSON" ) ).
  transfer().close();

dhis2Client.post( "trackedEntityAttributes" ).
  withResource( new TrackedEntityAttribute().withId( "HlKXyR5qr2e").
    withValueType( TrackedEntityAttribute.ValueTypeRef.TEXT ).
    withShortName( "Patient UID" ).
    withName( "Patient UID" ).
    withUnique( true ).
    withFormName( "Patient UID" ).
    withGenerated( true ).
    withPattern( "RANDOM(XXX######)" ).
    withCode( "IDS_AFI_PATIENT_UID" ).
    withAggregationType( TrackedEntityAttribute.AggregationTypeRef.COUNT ) ).
  transfer().close();
1 Like