Search in sources :

Example 1 with CategoryOption

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

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

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

the class CategoryOptionShould method map_from_json_string.

@Test
public void map_from_json_string() throws IOException, ParseException {
    ObjectMapper objectMapper = Inject.objectMapper();
    CategoryOption option = objectMapper.readValue("{" + "\"lastUpdated\":\"2016-08-08T11:17:59.448\"," + "\"id\":\"cQYFfHX9oIT\"," + "\"created\":\"2016-08-08T11:17:59.448\"," + "\"name\":\"Green\"," + "\"shortName\":\"Green\"," + "\"displayName\":\"Green\"," + "\"displayShortName\":\"Green\"," + "\"startDate\":\"2016-04-01T00:00:00.000\"," + "\"endDate\":\"2016-05-01T00:00:00.000\"," + "\"externalAccess\":false," + "\"dimensionItem\":\"cQYFfHX9oIT\"," + "\"dimensionItemType\":\"CATEGORY_OPTION\"," + "\"categories\":[]," + "\"organisationUnits\":[]," + "\"categoryOptionCombos\":[" + "{\"id\":\"S34ULMcHMca\"}," + "{\"id\":\"sqGRzCziswD\"}," + "{\"id\":\"WVzTbHctjok\"}," + "{\"id\":\"QFcGyiRFFH5\"}" + "]," + "\"categoryOptionGroups\":[]" + "}", CategoryOption.class);
    assertThat(option.uid()).isEqualTo("cQYFfHX9oIT");
    assertThat(option.created()).isEqualTo(BaseIdentifiableObject.DATE_FORMAT.parse("2016-08-08T11:17:59.448"));
    assertThat(option.lastUpdated()).isEqualTo(BaseIdentifiableObject.DATE_FORMAT.parse("2016-08-08T11:17:59.448"));
    assertThat(option.name()).isEqualTo("Green");
    assertThat(option.shortName()).isEqualTo("Green");
    assertThat(option.displayName()).isEqualTo("Green");
    assertThat(option.displayShortName()).isEqualTo("Green");
    assertThat(option.startDate()).isEqualTo(BaseIdentifiableObject.DATE_FORMAT.parse("2016-04-01T00:00:00.000"));
    assertThat(option.endDate()).isEqualTo(BaseIdentifiableObject.DATE_FORMAT.parse("2016-05-01T00:00:00.000"));
    // check if list maintains order of the items in payload
    assertThat(option.categoryOptionCombos().get(0).uid()).isEqualTo("S34ULMcHMca");
    assertThat(option.categoryOptionCombos().get(1).uid()).isEqualTo("sqGRzCziswD");
    assertThat(option.categoryOptionCombos().get(2).uid()).isEqualTo("WVzTbHctjok");
    assertThat(option.categoryOptionCombos().get(3).uid()).isEqualTo("QFcGyiRFFH5");
}
Also used : CategoryOption(org.hisp.dhis.android.core.category.CategoryOption) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

CategoryOption (org.hisp.dhis.android.core.category.CategoryOption)3 CategoryCombo (org.hisp.dhis.android.core.category.CategoryCombo)2 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Date (java.util.Date)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 ResourceHandler (org.hisp.dhis.android.core.resource.ResourceHandler)1 Test (org.junit.Test)1