Search in sources :

Example 1 with Payload

use of org.hisp.dhis.android.core.common.Payload 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 Payload

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

the class DataSetParentCall method callBody.

@Override
public Response callBody() throws Exception {
    DataSetAccessEndpointCall dataSetAccessEndpointCall = dataSetAccessCallFactory.create(data);
    Response<Payload<DataSet>> dataSetAccessResponse = dataSetAccessEndpointCall.call();
    List<DataSet> dataSetsWithAccess = dataSetAccessResponse.body().items();
    Set<String> dataSetUids = DataSetParentUidsHelper.getAssignedDataSetUids(dataSetsWithAccess);
    DataSetEndpointCall dataSetEndpointCall = dataSetCallFactory.create(data, dataSetUids);
    Response<Payload<DataSet>> dataSetResponse = dataSetEndpointCall.call();
    List<DataSet> dataSets = dataSetResponse.body().items();
    DataElementEndpointCall dataElementEndpointCall = dataElementCallFactory.create(data, DataSetParentUidsHelper.getDataElementUids(dataSets));
    Response<Payload<DataElement>> dataElementResponse = dataElementEndpointCall.call();
    IndicatorEndpointCall indicatorEndpointCall = indicatorCallFactory.create(data, DataSetParentUidsHelper.getIndicatorUids(dataSets));
    Response<Payload<Indicator>> indicatorResponse = indicatorEndpointCall.call();
    List<Indicator> indicators = indicatorResponse.body().items();
    IndicatorTypeEndpointCall indicatorTypeEndpointCall = indicatorTypeCallFactory.create(data, DataSetParentUidsHelper.getIndicatorTypeUids(indicators));
    indicatorTypeEndpointCall.call();
    periodHandler.generateAndPersist();
    linkManager.saveDataSetDataElementAndIndicatorLinks(dataSets);
    linkManager.saveDataSetOrganisationUnitLinks(organisationUnits, dataSetUids);
    return dataElementResponse;
}
Also used : IndicatorEndpointCall(org.hisp.dhis.android.core.indicator.IndicatorEndpointCall) IndicatorTypeEndpointCall(org.hisp.dhis.android.core.indicator.IndicatorTypeEndpointCall) Indicator(org.hisp.dhis.android.core.indicator.Indicator) DataElementEndpointCall(org.hisp.dhis.android.core.dataelement.DataElementEndpointCall) Payload(org.hisp.dhis.android.core.common.Payload)

Example 3 with Payload

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

the class TrackedEntityCall method call.

@Override
public Response<Payload<TrackedEntity>> call() throws Exception {
    synchronized (this) {
        if (isExecuted) {
            throw new IllegalStateException("Already executed");
        }
        isExecuted = true;
    }
    if (uidSet.size() > MAX_UIDS) {
        throw new IllegalArgumentException("Can't handle the amount of tracked entities: " + uidSet.size() + ". " + "Max size is: " + MAX_UIDS);
    }
    ResourceHandler resourceHandler = new ResourceHandler(resourceStore);
    String lastUpdated = resourceHandler.getLastUpdated(resourceType);
    Response<Payload<TrackedEntity>> response = getTrackedEntities(lastUpdated);
    TrackedEntityHandler trackedEntityHandler = new TrackedEntityHandler(trackedEntityStore);
    Transaction transaction = databaseAdapter.beginNewTransaction();
    try {
        if (response != null && response.isSuccessful()) {
            List<TrackedEntity> trackedEntities = response.body().items();
            int size = trackedEntities.size();
            for (int i = 0; i < size; i++) {
                TrackedEntity trackedEntity = trackedEntities.get(i);
                trackedEntityHandler.handleTrackedEntity(trackedEntity);
            }
            resourceHandler.handleResource(resourceType, serverDate);
            transaction.setSuccessful();
        }
    } finally {
        transaction.end();
    }
    return response;
}
Also used : Transaction(org.hisp.dhis.android.core.data.database.Transaction) ResourceHandler(org.hisp.dhis.android.core.resource.ResourceHandler) Payload(org.hisp.dhis.android.core.common.Payload)

Example 4 with Payload

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

the class OrganisationUnitCall method call.

@Override
public Response<Payload<OrganisationUnit>> call() throws Exception {
    synchronized (this) {
        if (isExecuted) {
            throw new IllegalArgumentException("AlreadyExecuted");
        }
        isExecuted = true;
    }
    Response<Payload<OrganisationUnit>> response = null;
    Response<Payload<OrganisationUnit>> totalResponse = null;
    Transaction transaction = genericCallData.databaseAdapter().beginNewTransaction();
    OrganisationUnitModelBuilder modelBuilder = new OrganisationUnitModelBuilder();
    try {
        Set<String> rootOrgUnitUids = findRoots(user.organisationUnits());
        Filter<OrganisationUnit, String> lastUpdatedFilter = OrganisationUnit.lastUpdated.gt(genericCallData.resourceHandler().getLastUpdated(ResourceModel.Type.ORGANISATION_UNIT));
        // Call OrganisationUnitService for each tree root & try to handleTrackedEntity sub-tree:
        for (String uid : rootOrgUnitUids) {
            response = getOrganisationUnit(uid, lastUpdatedFilter);
            if (response.isSuccessful()) {
                if (totalResponse == null) {
                    totalResponse = response;
                } else {
                    totalResponse.body().items().addAll(response.body().items());
                }
                organisationUnitHandler.handleMany(response.body().items(), modelBuilder);
            } else {
                totalResponse = response;
                // stop early unsuccessful:
                break;
            }
        }
        if (response != null && response.isSuccessful()) {
            genericCallData.resourceHandler().handleResource(ResourceModel.Type.ORGANISATION_UNIT, genericCallData.serverDate());
            transaction.setSuccessful();
        }
    } finally {
        transaction.end();
    }
    return totalResponse;
}
Also used : Transaction(org.hisp.dhis.android.core.data.database.Transaction) Payload(org.hisp.dhis.android.core.common.Payload)

Example 5 with Payload

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

the class ProgramCall method call.

@Override
public Response<Payload<Program>> call() throws Exception {
    synchronized (this) {
        if (isExecuted) {
            throw new IllegalStateException("Already executed");
        }
        isExecuted = true;
    }
    if (uids.size() > MAX_UIDS) {
        throw new IllegalArgumentException("Can't handle the amount of programs: " + uids.size() + ". " + "Max size is: " + MAX_UIDS);
    }
    ResourceHandler resourceHandler = new ResourceHandler(resourceStore);
    String lastSyncedPrograms = resourceHandler.getLastUpdated(ResourceModel.Type.PROGRAM);
    Response<Payload<Program>> programsByLastUpdated = programService.getPrograms(getFields(), Program.lastUpdated.gt(lastSyncedPrograms), Program.uid.in(uids), Boolean.FALSE).execute();
    if (programsByLastUpdated.isSuccessful()) {
        Transaction transaction = databaseAdapter.beginNewTransaction();
        try {
            List<Program> programs = programsByLastUpdated.body().items();
            int size = programs.size();
            for (int i = 0; i < size; i++) {
                Program program = programs.get(i);
                programHandler.handleProgram(program);
            }
            resourceHandler.handleResource(ResourceModel.Type.PROGRAM, serverDate);
            transaction.setSuccessful();
        } finally {
            transaction.end();
        }
    }
    return programsByLastUpdated;
}
Also used : Transaction(org.hisp.dhis.android.core.data.database.Transaction) ResourceHandler(org.hisp.dhis.android.core.resource.ResourceHandler) Payload(org.hisp.dhis.android.core.common.Payload)

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