Search in sources :

Example 1 with Enrollment

use of org.hisp.dhis.android.core.enrollment.Enrollment in project dhis2-android-sdk by dhis2.

the class TrackedEntityInstancePostCallRealIntegrationShould method pull_event_after_push_tracked_entity_instance_with_that_event.

/*
    * If you want run this test you need config the correct uids in the server side.
    * At this moment is necessary add into the "child programme" program the category combo : Implementing Partner
    * */
// @Test
public void pull_event_after_push_tracked_entity_instance_with_that_event() throws Exception {
    downloadMetadata();
    createDummyDataToPost(orgUnitUid, programUid, programStageUid, trackedEntityUid, coordinates, featureType, eventUid, enrollmentUid, trackedEntityInstanceUid, trackedEntityAttributeUid, dataElementUid);
    postTrackedEntityInstances();
    TrackedEntityInstance pushedTrackedEntityInstance = getTrackedEntityInstanceFromDB(trackedEntityInstanceUid);
    Enrollment pushedEnrollment = getEnrollmentsByTrackedEntityInstanceFromDb(trackedEntityInstanceUid);
    Event pushedEvent = getEventsFromDb(eventUid);
    d2.wipeDB().call();
    downloadMetadata();
    TrackedEntityInstanceEndPointCall trackedEntityInstanceEndPointCall = TrackedEntityInstanceCallFactory.create(d2.retrofit(), databaseAdapter(), trackedEntityInstanceUid);
    trackedEntityInstanceEndPointCall.call();
    TrackedEntityInstance downloadedTrackedEntityInstance = getTrackedEntityInstanceFromDB(trackedEntityInstanceUid);
    Enrollment downloadedEnrollment = getEnrollmentsByTrackedEntityInstanceFromDb(trackedEntityInstanceUid);
    Event downloadedEvent = getEventsFromDb(eventUid);
    assertPushAndDownloadTrackedEntityInstances(pushedTrackedEntityInstance, pushedEnrollment, pushedEvent, downloadedTrackedEntityInstance, downloadedEnrollment, downloadedEvent);
}
Also used : Enrollment(org.hisp.dhis.android.core.enrollment.Enrollment) Event(org.hisp.dhis.android.core.event.Event)

Example 2 with Enrollment

use of org.hisp.dhis.android.core.enrollment.Enrollment in project dhis2-android-sdk by dhis2.

the class TrackedEntityInstancePostCallRealIntegrationShould method getEnrollmentsByTrackedEntityInstanceFromDb.

private Enrollment getEnrollmentsByTrackedEntityInstanceFromDb(String trackedEntityInstanceUid) {
    EnrollmentStoreImpl enrollmentStore = new EnrollmentStoreImpl(databaseAdapter());
    Enrollment enrollment = null;
    Map<String, List<Enrollment>> storedEnrollmentsByTrackedEntityInstance = enrollmentStore.queryAll();
    for (Enrollment storedEnrollment : storedEnrollmentsByTrackedEntityInstance.get(trackedEntityInstanceUid)) {
        if (storedEnrollment.uid().equals(enrollmentUid)) {
            enrollment = storedEnrollment;
        }
    }
    return enrollment;
}
Also used : EnrollmentStoreImpl(org.hisp.dhis.android.core.enrollment.EnrollmentStoreImpl) Enrollment(org.hisp.dhis.android.core.enrollment.Enrollment) List(java.util.List)

Example 3 with Enrollment

use of org.hisp.dhis.android.core.enrollment.Enrollment in project dhis2-android-sdk by dhis2.

the class TrackedEntityInstancePostCall method queryDataToSync.

@NonNull
private List<TrackedEntityInstance> queryDataToSync() {
    Map<String, List<TrackedEntityDataValue>> dataValueMap = trackedEntityDataValueStore.queryTrackedEntityDataValues(Boolean.FALSE);
    Map<String, List<Event>> eventMap = eventStore.queryEventsAttachedToEnrollmentToPost();
    Map<String, List<Enrollment>> enrollmentMap = enrollmentStore.query();
    Map<String, List<TrackedEntityAttributeValue>> attributeValueMap = trackedEntityAttributeValueStore.query();
    Map<String, TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceStore.queryToPost();
    List<TrackedEntityInstance> trackedEntityInstancesRecreated = new ArrayList<>();
    List<Relationship> relationshipRecreated = new ArrayList<>();
    // EMPTY LISTS TO REPLACE NULL VALUES SO THAT API DOESN'T BREAK.
    List<TrackedEntityAttributeValue> emptyAttributeValueList = new ArrayList<>();
    for (Map.Entry<String, TrackedEntityInstance> teiUid : trackedEntityInstances.entrySet()) {
        List<Enrollment> enrollmentsRecreated = new ArrayList<>();
        List<Enrollment> enrollments = enrollmentMap.get(teiUid.getKey());
        // if enrollments is not null, then they exist for this tracked entity instance
        if (enrollments != null) {
            List<Event> eventRecreated = new ArrayList<>();
            // building enrollment
            int enrollmentSize = enrollments.size();
            for (int i = 0; i < enrollmentSize; i++) {
                Enrollment enrollment = enrollments.get(i);
                // building events for enrollment
                List<Event> eventsForEnrollment = eventMap.get(enrollment.uid());
                // if eventsForEnrollment is not null, then they exist for this enrollment
                if (eventsForEnrollment != null) {
                    int eventSize = eventsForEnrollment.size();
                    for (int j = 0; j < eventSize; j++) {
                        Event event = eventsForEnrollment.get(j);
                        List<TrackedEntityDataValue> dataValuesForEvent = dataValueMap.get(event.uid());
                        eventRecreated.add(Event.create(event.uid(), event.enrollmentUid(), event.created(), event.lastUpdated(), event.createdAtClient(), event.lastUpdatedAtClient(), event.program(), event.programStage(), event.organisationUnit(), event.eventDate(), event.status(), event.coordinates(), event.completedDate(), event.dueDate(), event.deleted(), dataValuesForEvent, event.attributeCategoryOptions(), event.attributeOptionCombo(), event.trackedEntityInstance()));
                    }
                }
                // TODO recreate with notes
                enrollmentsRecreated.add(Enrollment.create(enrollment.uid(), enrollment.created(), enrollment.lastUpdated(), enrollment.createdAtClient(), enrollment.lastUpdatedAtClient(), enrollment.organisationUnit(), enrollment.program(), enrollment.dateOfEnrollment(), enrollment.dateOfIncident(), enrollment.followUp(), enrollment.enrollmentStatus(), enrollment.trackedEntityInstance(), enrollment.coordinate(), enrollment.deleted(), eventRecreated, null));
            }
        }
        // Building TEI WITHOUT (new ArrayList) relationships
        List<TrackedEntityAttributeValue> attributeValues = attributeValueMap.get(teiUid.getKey());
        // Then we need to set it to empty arrayList so that API doesn't break
        if (attributeValues == null) {
            attributeValues = emptyAttributeValueList;
        }
        TrackedEntityInstance trackedEntityInstance = trackedEntityInstances.get(teiUid.getKey());
        trackedEntityInstancesRecreated.add(TrackedEntityInstance.create(trackedEntityInstance.uid(), trackedEntityInstance.created(), trackedEntityInstance.lastUpdated(), trackedEntityInstance.createdAtClient(), trackedEntityInstance.lastUpdatedAtClient(), trackedEntityInstance.organisationUnit(), trackedEntityInstance.trackedEntity(), trackedEntityInstance.coordinates(), trackedEntityInstance.featureType(), trackedEntityInstance.deleted(), attributeValues, relationshipRecreated, enrollmentsRecreated));
    }
    return trackedEntityInstancesRecreated;
}
Also used : TrackedEntityAttributeValue(org.hisp.dhis.android.core.trackedentity.TrackedEntityAttributeValue) ArrayList(java.util.ArrayList) TrackedEntityDataValue(org.hisp.dhis.android.core.trackedentity.TrackedEntityDataValue) TrackedEntityInstance(org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance) Relationship(org.hisp.dhis.android.core.relationship.Relationship) Enrollment(org.hisp.dhis.android.core.enrollment.Enrollment) Event(org.hisp.dhis.android.core.event.Event) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) NonNull(android.support.annotation.NonNull)

Example 4 with Enrollment

use of org.hisp.dhis.android.core.enrollment.Enrollment in project dhis2-android-sdk by dhis2.

the class TrackedEntityInstanceCallMockIntegrationShould method createTei.

private TrackedEntityInstance createTei(TrackedEntityInstance downloadedTei, Map<String, List<TrackedEntityAttributeValue>> attValues, List<Enrollment> downloadedEnrollmentsWithoutEvents, List<Event> downloadedEventsWithoutValues, Map<String, List<TrackedEntityDataValue>> downloadedValues) {
    Map<String, List<Event>> downloadedEvents = new HashMap<>();
    List<Enrollment> downloadedEnrollments = new ArrayList<>();
    for (Event event : downloadedEventsWithoutValues) {
        event = Event.create(event.uid(), event.enrollmentUid(), event.created(), event.lastUpdated(), event.createdAtClient(), event.lastUpdatedAtClient(), event.program(), event.programStage(), event.organisationUnit(), event.eventDate(), event.status(), event.coordinates(), event.completedDate(), event.dueDate(), event.deleted(), downloadedValues.get(event.uid()), event.attributeCategoryOptions(), event.attributeOptionCombo(), event.trackedEntityInstance());
        if (downloadedEvents.get(event.enrollmentUid()) == null) {
            downloadedEvents.put(event.enrollmentUid(), new ArrayList<Event>());
        }
        downloadedEvents.get(event.enrollmentUid()).add(event);
    }
    for (Enrollment enrollment : downloadedEnrollmentsWithoutEvents) {
        enrollment = Enrollment.create(enrollment.uid(), enrollment.created(), enrollment.lastUpdated(), enrollment.createdAtClient(), enrollment.lastUpdatedAtClient(), enrollment.organisationUnit(), enrollment.program(), enrollment.dateOfEnrollment(), enrollment.dateOfIncident(), enrollment.followUp(), enrollment.enrollmentStatus(), downloadedTei.uid(), enrollment.coordinate(), enrollment.deleted(), downloadedEvents.get(enrollment.uid()), enrollment.notes());
        downloadedEnrollments.add(enrollment);
    }
    List<Relationship> relationships = new ArrayList<>();
    if (downloadedTei.relationships() != null) {
        relationships = downloadedTei.relationships();
    }
    downloadedTei = TrackedEntityInstance.create(downloadedTei.uid(), downloadedTei.created(), downloadedTei.lastUpdated(), downloadedTei.createdAtClient(), downloadedTei.lastUpdatedAtClient(), downloadedTei.organisationUnit(), downloadedTei.trackedEntity(), downloadedTei.coordinates(), downloadedTei.featureType(), downloadedTei.deleted(), attValues.get(downloadedTei.uid()), relationships, downloadedEnrollments);
    return downloadedTei;
}
Also used : HashMap(java.util.HashMap) Relationship(org.hisp.dhis.android.core.relationship.Relationship) ArrayList(java.util.ArrayList) Enrollment(org.hisp.dhis.android.core.enrollment.Enrollment) Event(org.hisp.dhis.android.core.event.Event) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with Enrollment

use of org.hisp.dhis.android.core.enrollment.Enrollment in project dhis2-android-sdk by dhis2.

the class TrackedEntityInstanceCallMockIntegrationShould method removeDeletedData.

@NonNull
private TrackedEntityInstance removeDeletedData(TrackedEntityInstance trackedEntityInstance) {
    Map<String, List<Event>> expectedEvents = new HashMap<>();
    List<Enrollment> expectedEnrollments = new ArrayList<>();
    for (Enrollment enrollment : trackedEntityInstance.enrollments()) {
        for (Event event : enrollment.events()) {
            if (!event.deleted()) {
                if (expectedEvents.get(event.enrollmentUid()) == null) {
                    expectedEvents.put(event.enrollmentUid(), new ArrayList<Event>());
                }
                expectedEvents.get(event.enrollmentUid()).add(event);
            }
        }
        if (!enrollment.deleted()) {
            enrollment = Enrollment.create(enrollment.uid(), enrollment.created(), enrollment.lastUpdated(), enrollment.createdAtClient(), enrollment.lastUpdatedAtClient(), enrollment.organisationUnit(), enrollment.program(), enrollment.dateOfEnrollment(), enrollment.dateOfIncident(), enrollment.followUp(), enrollment.enrollmentStatus(), trackedEntityInstance.uid(), enrollment.coordinate(), enrollment.deleted(), expectedEvents.get(enrollment.uid()), enrollment.notes());
            expectedEnrollments.add(enrollment);
        }
    }
    trackedEntityInstance = TrackedEntityInstance.create(trackedEntityInstance.uid(), trackedEntityInstance.created(), trackedEntityInstance.lastUpdated(), trackedEntityInstance.createdAtClient(), trackedEntityInstance.lastUpdatedAtClient(), trackedEntityInstance.organisationUnit(), trackedEntityInstance.trackedEntity(), trackedEntityInstance.coordinates(), trackedEntityInstance.featureType(), trackedEntityInstance.deleted(), trackedEntityInstance.trackedEntityAttributeValues(), trackedEntityInstance.relationships(), expectedEnrollments);
    return trackedEntityInstance;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Enrollment(org.hisp.dhis.android.core.enrollment.Enrollment) Event(org.hisp.dhis.android.core.event.Event) ArrayList(java.util.ArrayList) List(java.util.List) NonNull(android.support.annotation.NonNull)

Aggregations

Enrollment (org.hisp.dhis.android.core.enrollment.Enrollment)5 List (java.util.List)4 Event (org.hisp.dhis.android.core.event.Event)4 ArrayList (java.util.ArrayList)3 NonNull (android.support.annotation.NonNull)2 HashMap (java.util.HashMap)2 Relationship (org.hisp.dhis.android.core.relationship.Relationship)2 Map (java.util.Map)1 EnrollmentStoreImpl (org.hisp.dhis.android.core.enrollment.EnrollmentStoreImpl)1 TrackedEntityAttributeValue (org.hisp.dhis.android.core.trackedentity.TrackedEntityAttributeValue)1 TrackedEntityDataValue (org.hisp.dhis.android.core.trackedentity.TrackedEntityDataValue)1 TrackedEntityInstance (org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance)1