Search in sources :

Example 6 with TrackedEntityInstance

use of org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance in project dhis2-android-sdk by dhis2.

the class TrackerDataCallMockIntegrationShould method givenATrackedEntityInstancesInDatabase.

private TrackedEntityInstance givenATrackedEntityInstancesInDatabase(String uid, State state) {
    Date date = new Date();
    String dateString = BaseIdentifiableObject.DATE_FORMAT.format(date);
    TrackedEntityInstance trackedEntityInstance = TrackedEntityInstance.create(uid, date, date, dateString, dateString, "DiszpKrYNg8", "nEenWmSyUEp", "[9,9]", FeatureType.POINT, false, null, null, null);
    trackedEntityInstanceStore.insert(trackedEntityInstance.uid(), trackedEntityInstance.created(), trackedEntityInstance.lastUpdated(), trackedEntityInstance.createdAtClient(), trackedEntityInstance.lastUpdatedAtClient(), trackedEntityInstance.organisationUnit(), trackedEntityInstance.trackedEntity(), trackedEntityInstance.coordinates(), trackedEntityInstance.featureType(), state);
    return trackedEntityInstance;
}
Also used : TrackedEntityInstance(org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance) Date(java.util.Date)

Example 7 with TrackedEntityInstance

use of org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance in project dhis2-android-sdk by dhis2.

the class TrackerDataCallMockIntegrationShould method download_all_synced_tracked_entity_instances_that_exists_in_database.

@Test
public void download_all_synced_tracked_entity_instances_that_exists_in_database() throws Exception {
    givenAMetadataInDatabase();
    List<TrackedEntityInstance> trackedEntityInstances = givenASyncedTrackedEntityInstancesInDatabase();
    dhis2MockServer.enqueueMockResponse("system_info.json");
    dhis2MockServer.enqueueMockResponse("tracked_entity_instance.json");
    dhis2MockServer.enqueueMockResponse("tracked_entity_instance_2.json");
    Response response = d2.syncTrackerData().call();
    verifyHaveSynchronized(response, trackedEntityInstances, Collections.EMPTY_LIST);
}
Also used : Response(retrofit2.Response) TrackedEntityInstance(org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance) Test(org.junit.Test)

Example 8 with TrackedEntityInstance

use of org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance in project dhis2-android-sdk by dhis2.

the class TrackerDataCallMockIntegrationShould method verifyHaveSynchronized.

private void verifyHaveSynchronized(Response response, List<TrackedEntityInstance> syncedExpected, List<TrackedEntityInstance> toPostExpected) {
    Map<String, TrackedEntityInstance> toPostInDatabase = trackedEntityInstanceStore.queryToPost();
    Map<String, TrackedEntityInstance> syncedInDatabase = trackedEntityInstanceStore.querySynced();
    String lastUpdated = resourceStore.getLastUpdated(ResourceModel.Type.TRACKED_ENTITY_INSTANCE);
    Map<String, List<Enrollment>> enrollmentsMap = enrollmentStore.queryAll();
    assertThat(response.isSuccessful(), is(true));
    assertThat(syncedInDatabase.size(), is(syncedExpected.size()));
    assertThat(lastUpdated, is(notNullValue()));
    assertThat(toPostInDatabase.size(), is(toPostExpected.size()));
    for (TrackedEntityInstance trackedEntityInstance : syncedExpected) {
        assertThat(syncedInDatabase.containsKey(trackedEntityInstance.uid()), is(true));
        assertThat(enrollmentsMap.get(trackedEntityInstance.uid()), is(notNullValue()));
    }
    for (TrackedEntityInstance trackedEntityInstance : toPostExpected) {
        assertThat(toPostInDatabase.containsKey(trackedEntityInstance.uid()), is(true));
    }
}
Also used : List(java.util.List) TrackedEntityInstance(org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance)

Example 9 with TrackedEntityInstance

use of org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance in project dhis2-android-sdk by dhis2.

the class TrackedEntityInstancePostCall method call.

@Override
public Response<WebResponse> call() throws Exception {
    synchronized (this) {
        if (isExecuted) {
            throw new IllegalStateException("Call is already executed");
        }
        isExecuted = true;
    }
    List<TrackedEntityInstance> trackedEntityInstancesToPost = queryDataToSync();
    // if size is 0, then no need to do network request
    if (trackedEntityInstancesToPost.isEmpty()) {
        return null;
    }
    TrackedEntityInstancePayload trackedEntityInstancePayload = new TrackedEntityInstancePayload();
    trackedEntityInstancePayload.trackedEntityInstances = trackedEntityInstancesToPost;
    Response<WebResponse> response = trackedEntityInstanceService.postTrackedEntityInstances(trackedEntityInstancePayload).execute();
    if (response.isSuccessful()) {
        handleWebResponse(response);
    }
    return response;
}
Also used : WebResponse(org.hisp.dhis.android.core.imports.WebResponse) TrackedEntityInstancePayload(org.hisp.dhis.android.core.trackedentity.TrackedEntityInstancePayload) TrackedEntityInstance(org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance)

Example 10 with TrackedEntityInstance

use of org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance in project dhis2-android-sdk by dhis2.

the class TrackedEntityInstancePostCall method queryDataToSync.

@NonNull
private List<TrackedEntityInstance> queryDataToSync() {
    Map<String, List<TrackedEntityDataValue>> dataValueMap = trackedEntityDataValueStore.queryTrackedEntityDataValues(Boolean.FALSE);
    Map<String, List<Event>> eventMap = eventStore.queryEventsAttachedToEnrollmentToPost();
    Map<String, List<Enrollment>> enrollmentMap = enrollmentStore.query();
    Map<String, List<TrackedEntityAttributeValue>> attributeValueMap = trackedEntityAttributeValueStore.query();
    Map<String, TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceStore.queryToPost();
    List<TrackedEntityInstance> trackedEntityInstancesRecreated = new ArrayList<>();
    List<Relationship> relationshipRecreated = new ArrayList<>();
    // EMPTY LISTS TO REPLACE NULL VALUES SO THAT API DOESN'T BREAK.
    List<TrackedEntityAttributeValue> emptyAttributeValueList = new ArrayList<>();
    for (Map.Entry<String, TrackedEntityInstance> teiUid : trackedEntityInstances.entrySet()) {
        List<Enrollment> enrollmentsRecreated = new ArrayList<>();
        List<Enrollment> enrollments = enrollmentMap.get(teiUid.getKey());
        // if enrollments is not null, then they exist for this tracked entity instance
        if (enrollments != null) {
            List<Event> eventRecreated = new ArrayList<>();
            // building enrollment
            int enrollmentSize = enrollments.size();
            for (int i = 0; i < enrollmentSize; i++) {
                Enrollment enrollment = enrollments.get(i);
                // building events for enrollment
                List<Event> eventsForEnrollment = eventMap.get(enrollment.uid());
                // if eventsForEnrollment is not null, then they exist for this enrollment
                if (eventsForEnrollment != null) {
                    int eventSize = eventsForEnrollment.size();
                    for (int j = 0; j < eventSize; j++) {
                        Event event = eventsForEnrollment.get(j);
                        List<TrackedEntityDataValue> dataValuesForEvent = dataValueMap.get(event.uid());
                        eventRecreated.add(Event.create(event.uid(), event.enrollmentUid(), event.created(), event.lastUpdated(), event.createdAtClient(), event.lastUpdatedAtClient(), event.program(), event.programStage(), event.organisationUnit(), event.eventDate(), event.status(), event.coordinates(), event.completedDate(), event.dueDate(), event.deleted(), dataValuesForEvent, event.attributeCategoryOptions(), event.attributeOptionCombo(), event.trackedEntityInstance()));
                    }
                }
                // TODO recreate with notes
                enrollmentsRecreated.add(Enrollment.create(enrollment.uid(), enrollment.created(), enrollment.lastUpdated(), enrollment.createdAtClient(), enrollment.lastUpdatedAtClient(), enrollment.organisationUnit(), enrollment.program(), enrollment.dateOfEnrollment(), enrollment.dateOfIncident(), enrollment.followUp(), enrollment.enrollmentStatus(), enrollment.trackedEntityInstance(), enrollment.coordinate(), enrollment.deleted(), eventRecreated, null));
            }
        }
        // Building TEI WITHOUT (new ArrayList) relationships
        List<TrackedEntityAttributeValue> attributeValues = attributeValueMap.get(teiUid.getKey());
        // Then we need to set it to empty arrayList so that API doesn't break
        if (attributeValues == null) {
            attributeValues = emptyAttributeValueList;
        }
        TrackedEntityInstance trackedEntityInstance = trackedEntityInstances.get(teiUid.getKey());
        trackedEntityInstancesRecreated.add(TrackedEntityInstance.create(trackedEntityInstance.uid(), trackedEntityInstance.created(), trackedEntityInstance.lastUpdated(), trackedEntityInstance.createdAtClient(), trackedEntityInstance.lastUpdatedAtClient(), trackedEntityInstance.organisationUnit(), trackedEntityInstance.trackedEntity(), trackedEntityInstance.coordinates(), trackedEntityInstance.featureType(), trackedEntityInstance.deleted(), attributeValues, relationshipRecreated, enrollmentsRecreated));
    }
    return trackedEntityInstancesRecreated;
}
Also used : TrackedEntityAttributeValue(org.hisp.dhis.android.core.trackedentity.TrackedEntityAttributeValue) ArrayList(java.util.ArrayList) TrackedEntityDataValue(org.hisp.dhis.android.core.trackedentity.TrackedEntityDataValue) TrackedEntityInstance(org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance) Relationship(org.hisp.dhis.android.core.relationship.Relationship) Enrollment(org.hisp.dhis.android.core.enrollment.Enrollment) Event(org.hisp.dhis.android.core.event.Event) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) NonNull(android.support.annotation.NonNull)

Aggregations

TrackedEntityInstance (org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance)10 Response (retrofit2.Response)4 Test (org.junit.Test)3 Date (java.util.Date)2 List (java.util.List)2 NonNull (android.support.annotation.NonNull)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Transaction (org.hisp.dhis.android.core.data.database.Transaction)1 Enrollment (org.hisp.dhis.android.core.enrollment.Enrollment)1 Event (org.hisp.dhis.android.core.event.Event)1 WebResponse (org.hisp.dhis.android.core.imports.WebResponse)1 Relationship (org.hisp.dhis.android.core.relationship.Relationship)1 SystemInfo (org.hisp.dhis.android.core.systeminfo.SystemInfo)1 SystemInfoCall (org.hisp.dhis.android.core.systeminfo.SystemInfoCall)1 TrackedEntityAttributeValue (org.hisp.dhis.android.core.trackedentity.TrackedEntityAttributeValue)1 TrackedEntityDataValue (org.hisp.dhis.android.core.trackedentity.TrackedEntityDataValue)1 TrackedEntityInstancePayload (org.hisp.dhis.android.core.trackedentity.TrackedEntityInstancePayload)1