Search in sources :

Example 11 with Transaction

use of org.hisp.dhis.android.core.data.database.Transaction 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 12 with Transaction

use of org.hisp.dhis.android.core.data.database.Transaction in project dhis2-android-sdk by dhis2.

the class ProgramStageSectionProgramIndicatorLinkStoreShould method insert_deferred_in_data_base_when_insert.

@Test
public void insert_deferred_in_data_base_when_insert() {
    String deferredProgramStageSection = "deferredProgramStageSection";
    String deferredProgramIndicator = "deferredProgramIndicator";
    Transaction transaction = databaseAdapter().beginNewTransaction();
    store.insert(deferredProgramStageSection, deferredProgramIndicator);
    database().insert(ProgramStageSectionModel.TABLE, null, CreateProgramStageSectionUtils.create(2L, deferredProgramStageSection, PROGRAM_STAGE));
    database().insert(ProgramIndicatorModel.TABLE, null, CreateProgramIndicatorUtils.create(2L, deferredProgramIndicator, PROGRAM));
    transaction.setSuccessful();
    transaction.end();
    Cursor cursor = database().query(ProgramStageSectionProgramIndicatorLinkModel.TABLE, PROJECTION, null, null, null, null, null);
    assertThatCursor(cursor).hasRow(deferredProgramStageSection, deferredProgramIndicator).isExhausted();
}
Also used : Transaction(org.hisp.dhis.android.core.data.database.Transaction) CursorAssert.assertThatCursor(org.hisp.dhis.android.core.data.database.CursorAssert.assertThatCursor) Cursor(android.database.Cursor) Test(org.junit.Test)

Example 13 with Transaction

use of org.hisp.dhis.android.core.data.database.Transaction in project dhis2-android-sdk by dhis2.

the class MetadataCall method call.

@SuppressWarnings("PMD.NPathComplexity")
@Override
public Response call() throws Exception {
    synchronized (this) {
        if (isExecuted) {
            throw new IllegalStateException("Already executed");
        }
        isExecuted = true;
    }
    Response response = null;
    Transaction transaction = databaseAdapter.beginNewTransaction();
    try {
        response = new SystemInfoCall(databaseAdapter, systemInfoStore, systemInfoService, resourceStore).call();
        if (!response.isSuccessful()) {
            return response;
        }
        GenericCallData data = GenericCallData.create(databaseAdapter, new ResourceHandler(resourceStore), retrofit);
        Response<User> userResponse = new UserCall(userService, databaseAdapter, userStore, userCredentialsStore, userRoleStore, resourceStore, data.serverDate()).call();
        response = userResponse;
        if (!response.isSuccessful()) {
            return response;
        }
        response = downloadCategories(data.serverDate());
        if (!response.isSuccessful()) {
            return response;
        }
        response = downloadCategoryCombos(data.serverDate());
        if (!response.isSuccessful()) {
            return response;
        }
        Response<Payload<Program>> programAccessResponse = ProgramAccessEndpointCall.FACTORY.create(data, programService).call();
        response = programAccessResponse;
        if (!response.isSuccessful()) {
            return response;
        }
        Set<String> programUids = getProgramUidsWithDataReadAccess(programAccessResponse.body().items());
        response = new ProgramCall(programService, databaseAdapter, resourceStore, programUids, programStore, data.serverDate(), trackedEntityAttributeStore, programTrackedEntityAttributeStore, programRuleVariableStore, programIndicatorStore, programStageSectionProgramIndicatorLinkStore, programRuleActionStore, programRuleStore, relationshipStore, styleHandler, renderTypeHandler).call();
        if (!response.isSuccessful()) {
            return response;
        }
        List<Program> programs = ((Response<Payload<Program>>) response).body().items();
        Set<String> assignedProgramStageUids = getAssignedProgramStageUids(programs);
        Response programStageResponse = programStageCallFactory.create(data, assignedProgramStageUids).call();
        if (!programStageResponse.isSuccessful()) {
            return programStageResponse;
        }
        Set<String> trackedEntityUids = getAssignedTrackedEntityUids(programs);
        response = new TrackedEntityCall(trackedEntityUids, databaseAdapter, trackedEntityStore, resourceStore, trackedEntityService, data.serverDate()).call();
        if (!response.isSuccessful()) {
            return response;
        }
        User user = userResponse.body();
        Response<Payload<OrganisationUnit>> organisationUnitResponse = organisationUnitCallFactory.create(data, user, programUids).call();
        if (!organisationUnitResponse.isSuccessful()) {
            return organisationUnitResponse;
        }
        List<ProgramStage> programStages = ((Response<Payload<ProgramStage>>) programStageResponse).body().items();
        Set<String> optionSetUids = getAssignedOptionSetUids(programs, programStages);
        response = new OptionSetCall(data, optionSetService, optionSetHandler, optionSetUids).call();
        if (!response.isSuccessful()) {
            return response;
        }
        List<OrganisationUnit> organisationUnits = organisationUnitResponse.body().items();
        response = dataSetParentCallFactory.create(user, data, organisationUnits).call();
        if (!response.isSuccessful()) {
            return response;
        }
        transaction.setSuccessful();
        return response;
    } finally {
        transaction.end();
    }
}
Also used : OrganisationUnit(org.hisp.dhis.android.core.organisationunit.OrganisationUnit) User(org.hisp.dhis.android.core.user.User) Program(org.hisp.dhis.android.core.program.Program) TrackedEntityCall(org.hisp.dhis.android.core.trackedentity.TrackedEntityCall) ResourceHandler(org.hisp.dhis.android.core.resource.ResourceHandler) GenericCallData(org.hisp.dhis.android.core.common.GenericCallData) Response(retrofit2.Response) Transaction(org.hisp.dhis.android.core.data.database.Transaction) SystemInfoCall(org.hisp.dhis.android.core.systeminfo.SystemInfoCall) UserCall(org.hisp.dhis.android.core.user.UserCall) Payload(org.hisp.dhis.android.core.common.Payload) ProgramStage(org.hisp.dhis.android.core.program.ProgramStage) OptionSetCall(org.hisp.dhis.android.core.option.OptionSetCall) ProgramCall(org.hisp.dhis.android.core.program.ProgramCall)

Example 14 with Transaction

use of org.hisp.dhis.android.core.data.database.Transaction in project dhis2-android-sdk by dhis2.

the class TrackerEntitiesDataCall method call.

@Override
public Response call() throws Exception {
    synchronized (this) {
        if (isExecuted) {
            throw new IllegalStateException("Already executed");
        }
        isExecuted = true;
    }
    Response response = null;
    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 = trackerCall(serverDate);
        if (response == null || !response.isSuccessful()) {
            return response;
        }
        transaction.setSuccessful();
        return response;
    } finally {
        transaction.end();
    }
}
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) Date(java.util.Date)

Example 15 with Transaction

use of org.hisp.dhis.android.core.data.database.Transaction in project dhis2-android-sdk by dhis2.

the class TrackedEntityInstanceEndPointCall method call.

@Override
public Response call() throws Exception {
    synchronized (this) {
        if (isExecuted) {
            throw new IllegalStateException("Already executed");
        }
        isExecuted = true;
    }
    Response<TrackedEntityInstance> response = trackedEntityInstanceService.trackedEntityInstance(trackedEntityInstanceUid, fields(), true).execute();
    if (response == null || !response.isSuccessful()) {
        return response;
    }
    Transaction transaction = databaseAdapter.beginNewTransaction();
    try {
        TrackedEntityInstance trackedEntityInstance = response.body();
        trackedEntityInstanceHandler.handle(trackedEntityInstance);
        resourceHandler.handleResource(TRACKED_ENTITY_INSTANCE, serverDate);
        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)

Aggregations

Transaction (org.hisp.dhis.android.core.data.database.Transaction)18 Payload (org.hisp.dhis.android.core.common.Payload)6 ResourceHandler (org.hisp.dhis.android.core.resource.ResourceHandler)5 Response (retrofit2.Response)5 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)4 SystemInfoCall (org.hisp.dhis.android.core.systeminfo.SystemInfoCall)4 Date (java.util.Date)3 SystemInfo (org.hisp.dhis.android.core.systeminfo.SystemInfo)3 Cursor (android.database.Cursor)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 CursorAssert.assertThatCursor (org.hisp.dhis.android.core.data.database.CursorAssert.assertThatCursor)1 OptionSetCall (org.hisp.dhis.android.core.option.OptionSetCall)1 OrganisationUnit (org.hisp.dhis.android.core.organisationunit.OrganisationUnit)1 Program (org.hisp.dhis.android.core.program.Program)1 ProgramCall (org.hisp.dhis.android.core.program.ProgramCall)1 ProgramStage (org.hisp.dhis.android.core.program.ProgramStage)1 TrackedEntityCall (org.hisp.dhis.android.core.trackedentity.TrackedEntityCall)1 TrackedEntityInstance (org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance)1