use of org.hisp.dhis.tracker.domain.Relationship in project dhis2-core by dhis2.
the class TrackerBundleParamsConverter method convert.
/**
* Iterates over the collections of a dataBundle. If any objects in those
* collections have objects nested within them, they are extracted. For each
* object we process, we make sure all references are valid as well.
*
* @param dataBundle containing collections to check and update.
* @return a dataBundle with a flattened data structure, and valid uid
* references.
*/
@Override
public TrackerBundleParams convert(TrackerBundleParams dataBundle) {
Map<String, TrackedEntity> trackedEntityMap = new HashMap<>();
Map<String, Enrollment> enrollmentHashMap = new HashMap<>();
Map<String, Event> eventHashMap = new HashMap<>();
Map<String, Relationship> relationshipHashMap = new HashMap<>();
// Extract all enrollments and relationships, and set parent reference.
for (TrackedEntity te : dataBundle.getTrackedEntities()) {
updateTrackedEntityReferences(te);
trackedEntityMap.put(te.getTrackedEntity(), te);
extractEnrollments(te).forEach(enrollment -> enrollmentHashMap.put(enrollment.getEnrollment(), enrollment));
extractRelationships(te).forEach(relationship -> relationshipHashMap.put(relationship.getRelationship(), relationship));
}
// Set UID for all enrollments and notes
dataBundle.getEnrollments().stream().peek(enrollment -> updateEnrollmentReferences(enrollment, enrollment.getTrackedEntity())).forEach(enrollment -> enrollmentHashMap.put(enrollment.getEnrollment(), enrollment));
// Extract all events and relationships, and set parent references
for (Enrollment enrollment : enrollmentHashMap.values()) {
extractEvents(enrollment).forEach(event -> eventHashMap.put(event.getEvent(), event));
extractRelationships(enrollment).forEach(relationship -> relationshipHashMap.put(relationship.getRelationship(), relationship));
enrollment.setNotes(enrollment.getNotes().stream().filter(note -> !StringUtils.isEmpty(note.getValue())).peek(this::updateNoteReferences).collect(Collectors.toList()));
}
// Set UID for all events and notes
dataBundle.getEvents().stream().peek(event -> updateEventReferences(event, event.getEnrollment())).forEach(event -> eventHashMap.put(event.getEvent(), event));
// Extract all relationships
for (Event event : eventHashMap.values()) {
extractRelationships(event).forEach(relationship -> relationshipHashMap.put(relationship.getRelationship(), relationship));
event.setNotes(event.getNotes().stream().filter(note -> !StringUtils.isEmpty(note.getValue())).peek(this::updateNoteReferences).collect(Collectors.toList()));
}
// Set UID for all relationships
dataBundle.getRelationships().stream().peek(this::updateRelationshipReferences).forEach(relationship -> relationshipHashMap.put(relationship.getRelationship(), relationship));
return TrackerBundleParams.builder().trackedEntities(new ArrayList<>(trackedEntityMap.values())).enrollments(new ArrayList<>(enrollmentHashMap.values())).events(new ArrayList<>(eventHashMap.values())).relationships(new ArrayList<>(relationshipHashMap.values())).build();
}
use of org.hisp.dhis.tracker.domain.Relationship in project dhis2-core by dhis2.
the class TrackerBundleParamsConverterTest method verifyUidIsAssignedWhenMissing.
@Test
void verifyUidIsAssignedWhenMissing() throws IOException {
List<Relationship> relationships1 = createRelationships(2, null);
List<Relationship> relationships2 = createRelationships(2, "rel2");
List<Event> events1 = createEvent(3, null, null);
List<Event> events2 = createEvent(7, null, null);
List<Enrollment> enrollments = new ArrayList<>();
Enrollment enrollment1 = createEnrollment(null, null, events1);
Enrollment enrollment2 = createEnrollment(null, null, events2);
enrollment1.setRelationships(relationships1);
enrollment2.setRelationships(relationships2);
enrollments.add(enrollment1);
enrollments.add(enrollment2);
TrackedEntity trackedEntity = createTrackedEntity(null, enrollments);
trackedEntity.setRelationships(relationships1);
TrackerBundleParams build = TrackerBundleParams.builder().trackedEntities(Collections.singletonList(trackedEntity)).build();
String jsonPayload = toJson(build);
TrackerBundleParams b2 = this.objectMapper.readValue(jsonPayload, TrackerBundleParams.class);
// TEI has uid
assertThat(b2.getTrackedEntities().get(0).getTrackedEntity(), is(notNullValue()));
// Also check parent uid is set
assertThat(b2.getEnrollments().get(0).getTrackedEntity(), is(b2.getTrackedEntities().get(0).getTrackedEntity()));
assertThat(b2.getEnrollments().get(0).getEnrollment(), is(notNullValue()));
// Also check parent uid is set
assertThat(b2.getEnrollments().get(1).getTrackedEntity(), is(b2.getTrackedEntities().get(0).getTrackedEntity()));
assertThat(b2.getEnrollments().get(1).getEnrollment(), is(notNullValue()));
assertThat(b2.getEvents().get(0).getEvent(), is(notNullValue()));
assertThat(b2.getEvents().get(1).getEvent(), is(notNullValue()));
assertThat(b2.getRelationships(), hasSize(4));
b2.getRelationships().stream().forEach(r -> assertThat(r.getRelationship(), is(notNullValue())));
}
use of org.hisp.dhis.tracker.domain.Relationship in project dhis2-core by dhis2.
the class TrackerBundleParamsConverterTest method verifyNestedTeiStructureHasNestedDataClearedAfterFlattening.
@Test
void verifyNestedTeiStructureHasNestedDataClearedAfterFlattening() throws IOException {
List<Relationship> relationships1 = createRelationships(2, "rel1");
List<Relationship> relationships2 = createRelationships(2, "rel2");
List<Event> events1 = createEvent(3, "ev1", "enr1");
List<Event> events2 = createEvent(7, "ev2", "enr2");
List<Enrollment> enrollments = new ArrayList<>();
Enrollment enrollment1 = createEnrollment("enr1", "teiABC", events1);
Enrollment enrollment2 = createEnrollment("enr2", "teiABC", events2);
enrollment1.setRelationships(relationships2);
enrollment2.setRelationships(relationships1);
enrollments.add(enrollment1);
enrollments.add(enrollment2);
TrackedEntity trackedEntity = createTrackedEntity("teiABC", enrollments);
trackedEntity.setRelationships(relationships1);
TrackerBundleParams build = TrackerBundleParams.builder().trackedEntities(Collections.singletonList(trackedEntity)).build();
String jsonPayload = toJson(build);
TrackerBundleParams b2 = this.objectMapper.readValue(jsonPayload, TrackerBundleParams.class);
assertThat(b2.getTrackedEntities().get(0).getEnrollments(), hasSize(0));
assertThat(b2.getTrackedEntities().get(0).getRelationships(), hasSize(0));
assertThat(b2.getEnrollments().get(0).getEvents(), hasSize(0));
assertThat(b2.getEnrollments().get(0).getRelationships(), hasSize(0));
assertThat(b2.getEvents().get(0).getRelationships(), hasSize(0));
}
Aggregations