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;
}
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();
}
}
}
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();
}
}
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;
}
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();
}
}
Aggregations