Search in sources :

Example 6 with Payload

use of org.hisp.dhis.android.core.common.Payload in project dhis2-android-sdk by dhis2.

the class TeisEndPointCall method call.

@Override
public Response<Payload<TrackedEntityInstance>> call() throws Exception {
    synchronized (this) {
        if (isExecuted) {
            throw new IllegalStateException("Already executed");
        }
        isExecuted = true;
    }
    String lastSyncedTEIs = resourceHandler.getLastUpdated(ResourceModel.Type.TRACKED_ENTITY_INSTANCE);
    Response<Payload<TrackedEntityInstance>> response;
    response = trackedEntityInstanceService.getTEIs(trackerQuery.getOrgUnit(), TrackedEntityInstance.lastUpdated.gt(lastSyncedTEIs), fields(), Boolean.TRUE, trackerQuery.getPage(), trackerQuery.getPageSize()).execute();
    if (response.isSuccessful() && response.body().items() != null && response.body().items().size() > 0) {
        List<TrackedEntityInstance> trackedEntityInstances = response.body().items();
        int size = trackedEntityInstances.size();
        if (trackerQuery.getPageLimit() > 0) {
            size = trackerQuery.getPageLimit();
        }
        for (int i = 0; i < size; i++) {
            Transaction transaction = databaseAdapter.beginNewTransaction();
            TrackedEntityInstance trackedEntityInstance = trackedEntityInstances.get(i);
            try {
                trackedEntityInstanceHandler.handle(trackedEntityInstance);
                transaction.setSuccessful();
            } catch (SQLiteConstraintException sql) {
                /*
                    This catch is necessary to ignore events with bad foreign keys exception
                    More info: If the foreign key have the flag
                    DEFERRABLE INITIALLY DEFERRED this exception will be throw in transaction
                    .end()
                    And the rollback will be executed only when the database is closed.
                    It is a reported as unfixed bug: https://issuetracker.google
                    .com/issues/37001653
                    */
                Log.d(this.getClass().getSimpleName(), sql.getMessage());
            } finally {
                transaction.end();
            }
        }
    }
    return response;
}
Also used : Transaction(org.hisp.dhis.android.core.data.database.Transaction) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) Payload(org.hisp.dhis.android.core.common.Payload)

Example 7 with Payload

use of org.hisp.dhis.android.core.common.Payload in project dhis2-android-sdk by dhis2.

the class ProgramModelPayloadShould method map_from_json_string.

@Test
public void map_from_json_string() throws Exception {
    ObjectMapper objectMapper = Inject.objectMapper();
    Payload<Program> payload = objectMapper.readValue("{\n" + "\n" + "    \"programs\": [\n" + "        {\n" + "            \"id\": \"IpHINAT79UW\",\n" + "            \"version\": 3,\n" + "            \"programStages\": [\n" + "                {\n" + "                    \"id\": \"A03MvHHogjR\",\n" + "                    \"programStageDataElements\": [\n" + "                        {\n" + "                            \"id\": \"LBNxoXdMnkv\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"a3kGcGDCuk6\"\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"yYMGxXpfl0Z\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"H6uSAMO5WLD\"\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"u2FvnCDCBcD\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"UXz7xuGCEhU\"\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"XLFc6tTftb5\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"wQLfBvPrXqq\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"f38bstJioPs\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"p8eX3rSkKN0\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"bx6fsa0t90x\"\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"O4dwFWakvGO\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"ebaJjqltK5N\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"kzgQRhOCadd\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"xtjAxBGQNNV\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"X8zyunlgUfM\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"x31y45jvIQL\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"JYyXbTmBBls\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"uf3svrmp8Oj\"\n" + "                            }\n" + "                        }\n" + "                    ]\n" + "                },\n" + "                {\n" + "                    \"id\": \"ZzYYXq4fJie\",\n" + "                    \"programStageDataElements\": [\n" + "                        {\n" + "                            \"id\": \"ztoQtbuXzsI\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"GQY2lXrypjO\"\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"vdc1saaN2ma\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"X8zyunlgUfM\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"x31y45jvIQL\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"Vpx18GqyLcK\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"FqlgKAG8HOu\"\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"WlYechRHVo3\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"vTUhAUZFoys\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"kzgQRhOCadd\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"WucAVPYvcEO\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"rxBfISxXS2U\"\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"EL5dr5x0WbZ\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"lNNb3truQoi\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"nH8Y04zS7UV\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"IpPWDRlHJSe\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"pOe0ogW4OWd\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"udkr3ihaeD3\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"xSTVGEIbarb\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"HLmTEmupdX0\"\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"YCO2FVT0wXL\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"cYGaxwK615G\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"oXR37f2wOb1\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"VlOvjLKnoyw\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"hDZbpskhqDd\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"OGmE3wUMEzu\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"rqmcdr07fxQ\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"sj3j9Hwc7so\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"dgsftM0rXu2\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"LfgZNmadu4W\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"aei1xRjSU2l\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"XdI8KRJiRoZ\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"sfYk4rKw18B\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"BeynU4L6VCQ\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"XdI8KRJiRoZ\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"LiV2YoatDud\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"OuJ6sgPyAbC\"\n" + "                            }\n" + "                        }\n" + "                    ]\n" + "                }\n" + "            ],\n" + "            \"programTrackedEntityAttributes\": [\n" + "                {\n" + "                    \"id\": \"K6BlNQdIxd1\",\n" + "                    \"trackedEntityAttribute\": {\n" + "                        \"id\": \"w75KJ2mc4zz\"\n" + "                    }\n" + "                },\n" + "                {\n" + "                    \"id\": \"oGv9KGJpqkl\",\n" + "                    \"trackedEntityAttribute\": {\n" + "                        \"id\": \"zDhUuAYrxNC\"\n" + "                    }\n" + "                },\n" + "                {\n" + "                    \"id\": \"YhqgQ6Iy4c4\",\n" + "                    \"trackedEntityAttribute\": {\n" + "                        \"id\": \"cejWyOfXge6\",\n" + "                        \"optionSet\": {\n" + "                            \"id\": \"pC3N9N77UmT\"\n" + "                        }\n" + "                    }\n" + "                }\n" + "            ]\n" + "        },\n" + "        {\n" + "            \"id\": \"q04UBOqq3rp\",\n" + "            \"version\": 1,\n" + "            \"programStages\": [\n" + "                {\n" + "                    \"id\": \"pSllsjpfLH2\",\n" + "                    \"programStageDataElements\": [\n" + "                        {\n" + "                            \"id\": \"apYCBRwEc44\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"f3Rn9XPEQuv\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"s3GoZHCmXL2\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"OOGft1qHHnN\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"Dv7iIitX44Y\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"VQ2lai3OfVG\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"ulVsVS36Zpm\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"UuL3eX8KJHY\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"PmE3Ev4ZcHX\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"o5M0cNMVKY3\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"deQEw93Vr4j\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"s3GoZHCmXL2\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"cLhp2K7tqAP\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"DvrjjquRrvF\",\n" + "                                \"optionSet\": {\n" + "                                    \"id\": \"Da7byW1wGzq\"\n" + "                                }\n" + "                            }\n" + "                        },\n" + "                        {\n" + "                            \"id\": \"BAZe68syer0\",\n" + "                            \"dataElement\": {\n" + "                                \"id\": \"lsJCUffec9h\"\n" + "                            }\n" + "                        }\n" + "                    ]\n" + "                }\n" + "            ],\n" + "            \"programTrackedEntityAttributes\": [ ]\n" + "        }\n" + "    ]\n" + "\n" + "}", new TypeReference<Payload<Program>>() {
    });
    List<Program> programs = payload.items();
    assertThat(programs).isNotNull();
    assertThat(programs).isNotEmpty();
    assertThat(programs.size()).isEqualTo(2);
    Program program = programs.get(0);
    assertThat(program.uid()).isEqualTo("IpHINAT79UW");
    assertThat(program.version()).isEqualTo(3);
    assertThat(program.programStages()).isNotNull();
    assertThat(program.programStages()).isNotEmpty();
    Program program1 = programs.get(1);
    assertThat(program1.uid()).isEqualTo("q04UBOqq3rp");
    assertThat(program1.version()).isEqualTo(1);
    assertThat(program1.programStages()).isNotNull();
    assertThat(program1.programStages()).isNotEmpty();
    ObjectWithUid programStage1 = program1.programStages().get(0);
    assertThat(programStage1.uid()).isEqualTo("pSllsjpfLH2");
}
Also used : ObjectWithUid(org.hisp.dhis.android.core.common.ObjectWithUid) Program(org.hisp.dhis.android.core.program.Program) Payload(org.hisp.dhis.android.core.common.Payload) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 8 with Payload

use of org.hisp.dhis.android.core.common.Payload in project dhis2-android-sdk by dhis2.

the class TrackedEntityCallUnitShould method invoke_insert_if_request_is_successful.

@Test
@SuppressWarnings("unchecked")
public void invoke_insert_if_request_is_successful() throws Exception {
    Headers headers = new Headers.Builder().add("Date", lastUpdated.toString()).build();
    when(payload.items()).thenReturn(Collections.singletonList(trackedEntity));
    Response<Payload<TrackedEntity>> response = Response.success(payload, headers);
    when(retrofitCall.execute()).thenReturn(response);
    call.call();
    verify(database, times(1)).beginNewTransaction();
    verify(transaction, times(1)).setSuccessful();
    verify(transaction, times(1)).end();
    verify(trackedEntityStore, times(1)).insert(anyString(), anyString(), anyString(), anyString(), any(Date.class), any(Date.class), anyString(), anyString(), anyString(), anyString());
    // TODO: after implementing the SystemInfoCall, tests..etc modify this to actually check the date:
    // Right now it only checks if: (Date) null is an instance of Date.class, not a terribly useful:
    verify(resourceStore, times(1)).insert(anyString(), any(Date.class));
}
Also used : Headers(okhttp3.Headers) Payload(org.hisp.dhis.android.core.common.Payload) Date(java.util.Date) Test(org.junit.Test)

Example 9 with Payload

use of org.hisp.dhis.android.core.common.Payload in project dhis2-android-sdk by dhis2.

the class CategoryComboEndpointCallRealIntegrationShould method download_categories_combos_and_relatives.

// @Test
public void download_categories_combos_and_relatives() throws Exception {
    Response responseLogIn = d2.logIn(RealServerMother.user, RealServerMother.password).call();
    Truth.assertThat(responseLogIn.isSuccessful()).isTrue();
    downloadCategories();
    assertNotCombosInDB();
    assertThereAreNotCategoryCombosLinkInDB();
    CategoryComboEndpointCall categoryComboEndpointCall = CategoryComboCallFactory.create(d2.retrofit(), databaseAdapter());
    Response<Payload<CategoryCombo>> responseCategory = categoryComboEndpointCall.call();
    assertResponseIsCorrect(responseCategory);
    assertDataIsProperlyParsedAndInsertedInTheDB();
}
Also used : Response(retrofit2.Response) Payload(org.hisp.dhis.android.core.common.Payload)

Example 10 with Payload

use of org.hisp.dhis.android.core.common.Payload in project dhis2-android-sdk by dhis2.

the class CategoryEndpointCallRealIntegrationShould method call_categories_endpoint.

@Test
public void call_categories_endpoint() throws Exception {
    Response responseLogIn = d2.logIn(RealServerMother.user, RealServerMother.password).call();
    Truth.assertThat(responseLogIn.isSuccessful()).isTrue();
    CategoryEndpointCall categoryEndpointCall = CategoryCallFactory.create(d2.retrofit(), databaseAdapter());
    Response<Payload<Category>> responseCategory = categoryEndpointCall.call();
    assertTrue(responseCategory.isSuccessful());
    assertTrue(hasCategories(responseCategory));
}
Also used : Response(retrofit2.Response) Payload(org.hisp.dhis.android.core.common.Payload) Test(org.junit.Test)

Aggregations

Payload (org.hisp.dhis.android.core.common.Payload)13 Transaction (org.hisp.dhis.android.core.data.database.Transaction)6 Test (org.junit.Test)4 ResourceHandler (org.hisp.dhis.android.core.resource.ResourceHandler)3 Response (retrofit2.Response)3 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Headers (okhttp3.Headers)2 Program (org.hisp.dhis.android.core.program.Program)2 Date (java.util.Date)1 CategoryCombo (org.hisp.dhis.android.core.category.CategoryCombo)1 CategoryOption (org.hisp.dhis.android.core.category.CategoryOption)1 GenericCallData (org.hisp.dhis.android.core.common.GenericCallData)1 ObjectWithUid (org.hisp.dhis.android.core.common.ObjectWithUid)1 AssetsFileReader (org.hisp.dhis.android.core.data.file.AssetsFileReader)1 DataElementEndpointCall (org.hisp.dhis.android.core.dataelement.DataElementEndpointCall)1 Indicator (org.hisp.dhis.android.core.indicator.Indicator)1 IndicatorEndpointCall (org.hisp.dhis.android.core.indicator.IndicatorEndpointCall)1 IndicatorTypeEndpointCall (org.hisp.dhis.android.core.indicator.IndicatorTypeEndpointCall)1 OptionSetCall (org.hisp.dhis.android.core.option.OptionSetCall)1