Search in sources :

Example 1 with TrackedEntityInstance

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

the class TrackerDataCall method call.

@Override
public Response call() throws Exception {
    synchronized (this) {
        if (isExecuted) {
            throw new IllegalStateException("Already executed");
        }
        isExecuted = true;
    }
    Response response = null;
    Map<String, TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceStore.querySynced();
    if (!trackedEntityInstances.isEmpty()) {
        Transaction transaction = databaseAdapter.beginNewTransaction();
        try {
            response = new SystemInfoCall(databaseAdapter, systemInfoStore, systemInfoService, resourceStore).call();
            if (!response.isSuccessful()) {
                return response;
            }
            SystemInfo systemInfo = (SystemInfo) response.body();
            Date serverDate = systemInfo.serverDate();
            response = trackedEntityInstanceCall(serverDate, trackedEntityInstances);
            transaction.setSuccessful();
        } finally {
            transaction.end();
        }
    }
    return response;
}
Also used : Response(retrofit2.Response) SystemInfo(org.hisp.dhis.android.core.systeminfo.SystemInfo) Transaction(org.hisp.dhis.android.core.data.database.Transaction) SystemInfoCall(org.hisp.dhis.android.core.systeminfo.SystemInfoCall) TrackedEntityInstance(org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance) Date(java.util.Date)

Example 2 with TrackedEntityInstance

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

the class TrackerDataCallMockIntegrationShould method not_download_tracked_entity_instances_if_does_not_exists_synced_in_database.

@Test
public void not_download_tracked_entity_instances_if_does_not_exists_synced_in_database() throws Exception {
    givenAMetadataInDatabase();
    TrackedEntityInstance toPostTrackedEntityInstance = givenAToPostTrackedEntityInstanceInDatabase();
    Response response = d2.syncTrackerData().call();
    verifyHaveNotSynchronized(response, Arrays.asList(toPostTrackedEntityInstance));
}
Also used : Response(retrofit2.Response) TrackedEntityInstance(org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance) Test(org.junit.Test)

Example 3 with TrackedEntityInstance

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

the class TrackerDataCallMockIntegrationShould method givenASyncedTrackedEntityInstancesInDatabase.

private List<TrackedEntityInstance> givenASyncedTrackedEntityInstancesInDatabase() {
    TrackedEntityInstance ngelehunGunnarson = givenATrackedEntityInstancesInDatabase("PgmUFEQYZdt", State.SYNCED);
    TrackedEntityInstance mikeBensinger = givenATrackedEntityInstancesInDatabase("IaxoagO9899", State.SYNCED);
    return Arrays.asList(mikeBensinger, ngelehunGunnarson);
}
Also used : TrackedEntityInstance(org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance)

Example 4 with TrackedEntityInstance

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

the class TrackerDataCallMockIntegrationShould method only_download_synced_tracked_entity_instance_that_exists_in_database.

@Test
public void only_download_synced_tracked_entity_instance_that_exists_in_database() throws Exception {
    givenAMetadataInDatabase();
    TrackedEntityInstance toPostTrackedEntityInstance = givenAToPostTrackedEntityInstanceInDatabase();
    TrackedEntityInstance syncedTrackedEntityInstance = givenASyncedTrackedEntityInstanceInDatabase();
    dhis2MockServer.enqueueMockResponse("system_info.json");
    dhis2MockServer.enqueueMockResponse("tracked_entity_instance.json");
    Response response = d2.syncTrackerData().call();
    verifyHaveSynchronized(response, Arrays.asList(syncedTrackedEntityInstance), Arrays.asList(toPostTrackedEntityInstance));
}
Also used : Response(retrofit2.Response) TrackedEntityInstance(org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance) Test(org.junit.Test)

Example 5 with TrackedEntityInstance

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

the class TrackerDataCallMockIntegrationShould method verifyHaveNotSynchronized.

private void verifyHaveNotSynchronized(Response response, List<TrackedEntityInstance> expectedToPost) {
    Map<String, TrackedEntityInstance> inDatabaseToPost = trackedEntityInstanceStore.queryToPost();
    Map<String, TrackedEntityInstance> inDatabaseSynced = trackedEntityInstanceStore.querySynced();
    String lastUpdated = resourceStore.getLastUpdated(ResourceModel.Type.TRACKED_ENTITY_INSTANCE);
    assertThat(response, is(nullValue()));
    assertThat(inDatabaseSynced.size(), is(0));
    assertThat(lastUpdated, is(nullValue()));
    assertThat(inDatabaseToPost.size(), is(expectedToPost.size()));
    for (TrackedEntityInstance trackedEntityInstance : expectedToPost) {
        assertThat(inDatabaseToPost.containsKey(trackedEntityInstance.uid()), is(true));
    }
}
Also used : TrackedEntityInstance(org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance)

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