Search in sources :

Example 1 with CategoryCombo

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

use of org.hisp.dhis.android.core.category.CategoryCombo in project dhis2-android-sdk by dhis2.

the class CategoryComboShould method map_from_json_string.

@Test
public void map_from_json_string() throws IOException, ParseException {
    ObjectMapper objectMapper = Inject.objectMapper();
    CategoryCombo combo = objectMapper.readValue("{" + "\"code\":\"BIRTHS\"," + "\"created\":\"2011-12-24T12:24:25.203\"," + "\"lastUpdated\":\"2016-04-18T16:04:34.745\"," + "\"name\":\"Births\"," + "\"id\":\"m2jTvAj5kkm\"," + "\"dataDimensionType\":\"DISAGGREGATION\"," + "\"displayName\":\"Births\"," + "\"publicAccess\":\"rw------\"," + "\"externalAccess\":false," + "\"isDefault\":false," + "\"skipTotal\":false," + "\"user\":{\"id\":\"GOLswS44mh8\"}," + "\"userGroupAccesses\":[]," + "\"attributeValues\":[]," + "\"categoryOptionCombos\":[" + "{\"id\":\"b19Ye0TWs1D\"}," + "{\"id\":\"YEmiuCcgNQI\"}," + "{\"id\":\"vP9xV78M67W\"}]," + "\"categories\":[" + "{\"id\":\"KfdsGBcoiCa\"}," + "{\"id\":\"cX5k9anHEHd\"}" + "]" + "}", CategoryCombo.class);
    assertThat(combo.uid()).isEqualTo("m2jTvAj5kkm");
    assertThat(combo.code()).isEqualTo("BIRTHS");
    assertThat(combo.created()).isEqualTo(BaseIdentifiableObject.DATE_FORMAT.parse("2011-12-24T12:24:25.203"));
    assertThat(combo.lastUpdated()).isEqualTo(BaseIdentifiableObject.DATE_FORMAT.parse("2016-04-18T16:04:34.745"));
    assertThat(combo.name()).isEqualTo("Births");
    assertThat(combo.displayName()).isEqualTo("Births");
    assertThat(combo.isDefault()).isEqualTo(false);
    // categories
    assertThat(combo.categories().get(0).uid()).isEqualTo("KfdsGBcoiCa");
    assertThat(combo.categories().get(1).uid()).isEqualTo("cX5k9anHEHd");
}
Also used : CategoryCombo(org.hisp.dhis.android.core.category.CategoryCombo) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with CategoryCombo

use of org.hisp.dhis.android.core.category.CategoryCombo in project dhis2-android-sdk by dhis2.

the class EventCallFactory method create.

public static EventEndPointCall create(Retrofit retrofit, DatabaseAdapter databaseAdapter, String orgUnit, int pageLimit, String categoryComboUID, String categoryOptionUID) {
    EventService eventService = retrofit.create(EventService.class);
    EventHandler eventHandler = HandlerFactory.createEventHandler(databaseAdapter);
    ResourceHandler resourceHandler = HandlerFactory.createResourceHandler(databaseAdapter);
    CategoryCombo categoryCombo = CategoryCombo.builder().uid(categoryComboUID).build();
    CategoryOption categoryOption = CategoryOption.builder().uid(categoryOptionUID).build();
    EventQuery eventQuery = EventQuery.Builder.create().withOrgUnit(orgUnit).withPageLimit(pageLimit).withCategoryComboAndCategoryOption(categoryCombo, categoryOption).build();
    EventEndPointCall eventEndPointCall = new EventEndPointCall(eventService, databaseAdapter, resourceHandler, eventHandler, new Date(), eventQuery);
    return eventEndPointCall;
}
Also used : CategoryCombo(org.hisp.dhis.android.core.category.CategoryCombo) EventQuery(org.hisp.dhis.android.core.event.EventQuery) EventHandler(org.hisp.dhis.android.core.event.EventHandler) CategoryOption(org.hisp.dhis.android.core.category.CategoryOption) EventService(org.hisp.dhis.android.core.event.EventService) ResourceHandler(org.hisp.dhis.android.core.resource.ResourceHandler) EventEndPointCall(org.hisp.dhis.android.core.event.EventEndPointCall) Date(java.util.Date)

Example 4 with CategoryCombo

use of org.hisp.dhis.android.core.category.CategoryCombo in project dhis2-android-sdk by dhis2.

the class CategoryComboCallFactory method create.

@NonNull
public static CategoryComboEndpointCall create(Retrofit retrofit, DatabaseAdapter databaseAdapter) {
    CategoryComboService categoryComboService = retrofit.create(CategoryComboService.class);
    ResponseValidator<CategoryCombo> categoryComboResponseValidator = new ResponseValidator<>();
    CategoryComboHandler categoryComboHandler = HandlerFactory.createCategoryComboHandler(databaseAdapter);
    ResourceHandler resourceHandler = HandlerFactory.createResourceHandler(databaseAdapter);
    CategoryComboQuery categoryComboQuery = CategoryComboQuery.defaultQuery();
    CategoryComboEndpointCall categoryComboEndpointCall = new CategoryComboEndpointCall(categoryComboQuery, categoryComboService, categoryComboResponseValidator, categoryComboHandler, resourceHandler, databaseAdapter, new Date());
    return categoryComboEndpointCall;
}
Also used : ResponseValidator(org.hisp.dhis.android.core.category.ResponseValidator) CategoryCombo(org.hisp.dhis.android.core.category.CategoryCombo) CategoryComboService(org.hisp.dhis.android.core.category.CategoryComboService) ResourceHandler(org.hisp.dhis.android.core.resource.ResourceHandler) CategoryComboQuery(org.hisp.dhis.android.core.category.CategoryComboQuery) CategoryComboEndpointCall(org.hisp.dhis.android.core.category.CategoryComboEndpointCall) CategoryComboHandler(org.hisp.dhis.android.core.category.CategoryComboHandler) Date(java.util.Date) NonNull(android.support.annotation.NonNull)

Aggregations

CategoryCombo (org.hisp.dhis.android.core.category.CategoryCombo)4 Date (java.util.Date)2 CategoryOption (org.hisp.dhis.android.core.category.CategoryOption)2 ResourceHandler (org.hisp.dhis.android.core.resource.ResourceHandler)2 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)1 NonNull (android.support.annotation.NonNull)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CategoryComboEndpointCall (org.hisp.dhis.android.core.category.CategoryComboEndpointCall)1 CategoryComboHandler (org.hisp.dhis.android.core.category.CategoryComboHandler)1 CategoryComboQuery (org.hisp.dhis.android.core.category.CategoryComboQuery)1 CategoryComboService (org.hisp.dhis.android.core.category.CategoryComboService)1 ResponseValidator (org.hisp.dhis.android.core.category.ResponseValidator)1 Payload (org.hisp.dhis.android.core.common.Payload)1 Transaction (org.hisp.dhis.android.core.data.database.Transaction)1 EventEndPointCall (org.hisp.dhis.android.core.event.EventEndPointCall)1 EventHandler (org.hisp.dhis.android.core.event.EventHandler)1 EventQuery (org.hisp.dhis.android.core.event.EventQuery)1 EventService (org.hisp.dhis.android.core.event.EventService)1 Test (org.junit.Test)1