use of org.hisp.dhis.android.core.trackedentity.TrackedEntityDataValue 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;
}
Aggregations