Search in sources :

Example 1 with Transaction

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

the class EventEndPointCall method call.

@Override
public Response<Payload<Event>> call() throws Exception {
    synchronized (this) {
        if (isExecuted) {
            throw new IllegalStateException("Already executed");
        }
        isExecuted = true;
    }
    String lastSyncedEvents = resourceHandler.getLastUpdated(ResourceModel.Type.EVENT);
    Response<Payload<Event>> eventsByLastUpdated;
    if (eventQuery.getCategoryCombo() == null || eventQuery.getCategoryOption() == null) {
        eventsByLastUpdated = eventService.getEvents(eventQuery.getOrgUnit(), eventQuery.getProgram(), eventQuery.getTrackedEntityInstance(), getSingleFields(), Event.lastUpdated.gt(lastSyncedEvents), Event.uid.in(eventQuery.getUIds()), Boolean.TRUE, eventQuery.getPage(), eventQuery.getPageSize()).execute();
    } else {
        CategoryCombo categoryCombo = eventQuery.getCategoryCombo();
        CategoryOption categoryOption = eventQuery.getCategoryOption();
        eventsByLastUpdated = eventService.getEvents(eventQuery.getOrgUnit(), eventQuery.getProgram(), eventQuery.getTrackedEntityInstance(), getSingleFields(), Event.lastUpdated.gt(lastSyncedEvents), Event.uid.in(eventQuery.getUIds()), Boolean.TRUE, eventQuery.getPage(), eventQuery.getPageSize(), categoryCombo.uid(), categoryOption.uid()).execute();
    }
    if (eventsByLastUpdated.isSuccessful() && eventsByLastUpdated.body().items() != null) {
        List<Event> events = eventsByLastUpdated.body().items();
        int size = events.size();
        if (eventQuery.getPageLimit() > 0) {
            size = eventQuery.getPageLimit();
        }
        for (int i = 0; i < size; i++) {
            Transaction transaction = databaseAdapter.beginNewTransaction();
            Event event = events.get(i);
            try {
                eventHandler.handle(event);
                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();
            }
        }
        resourceHandler.handleResource(ResourceModel.Type.EVENT, serverDate);
    }
    return eventsByLastUpdated;
}
Also used : Transaction(org.hisp.dhis.android.core.data.database.Transaction) CategoryCombo(org.hisp.dhis.android.core.category.CategoryCombo) CategoryOption(org.hisp.dhis.android.core.category.CategoryOption) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) Payload(org.hisp.dhis.android.core.common.Payload)

Example 2 with Transaction

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

the class GenericEndpointCallImpl method persist.

private void persist(Response<Payload<P>> response) {
    if (response == null) {
        throw new RuntimeException("Trying to process call without download data");
    }
    List<P> pojoList = response.body().items();
    if (pojoList != null && !pojoList.isEmpty()) {
        Transaction transaction = data.databaseAdapter().beginNewTransaction();
        try {
            handler.handleMany(pojoList, modelBuilder);
            data.resourceHandler().handleResource(resourceType, data.serverDate());
            transaction.setSuccessful();
        } finally {
            transaction.end();
        }
    }
}
Also used : Transaction(org.hisp.dhis.android.core.data.database.Transaction)

Example 3 with Transaction

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

the class SingleDataCall 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 = eventCall(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 4 with Transaction

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

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

the class TransactionalCall method call.

@Override
public final Response call() throws Exception {
    synchronized (this) {
        if (isExecuted) {
            throw new IllegalStateException("Already executed");
        }
        isExecuted = true;
    }
    Transaction transaction = data.databaseAdapter().beginNewTransaction();
    try {
        Response response = callBody();
        transaction.setSuccessful();
        return response;
    } finally {
        transaction.end();
    }
}
Also used : Response(retrofit2.Response) Transaction(org.hisp.dhis.android.core.data.database.Transaction)

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