Search in sources :

Example 36 with Relationship

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();
}
Also used : Event(org.hisp.dhis.tracker.domain.Event) StdConverter(com.fasterxml.jackson.databind.util.StdConverter) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) HashMap(java.util.HashMap) Relationship(org.hisp.dhis.tracker.domain.Relationship) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) List(java.util.List) Note(org.hisp.dhis.tracker.domain.Note) Map(java.util.Map) CodeGenerator(org.hisp.dhis.common.CodeGenerator) HashMap(java.util.HashMap) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) ArrayList(java.util.ArrayList) Relationship(org.hisp.dhis.tracker.domain.Relationship) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Event(org.hisp.dhis.tracker.domain.Event)

Example 37 with Relationship

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())));
}
Also used : TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Relationship(org.hisp.dhis.tracker.domain.Relationship) ArrayList(java.util.ArrayList) Event(org.hisp.dhis.tracker.domain.Event) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Test(org.junit.jupiter.api.Test)

Example 38 with Relationship

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));
}
Also used : TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Relationship(org.hisp.dhis.tracker.domain.Relationship) ArrayList(java.util.ArrayList) Event(org.hisp.dhis.tracker.domain.Event) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Test(org.junit.jupiter.api.Test)

Aggregations

Relationship (org.hisp.dhis.tracker.domain.Relationship)38 Test (org.junit.jupiter.api.Test)34 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)23 RelationshipType (org.hisp.dhis.relationship.RelationshipType)14 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)9 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)7 ArrayList (java.util.ArrayList)6 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)6 Event (org.hisp.dhis.tracker.domain.Event)6 RelationshipConstraint (org.hisp.dhis.relationship.RelationshipConstraint)4 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)4 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)3 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)3 Collectors (java.util.stream.Collectors)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 CodeGenerator (org.hisp.dhis.common.CodeGenerator)2 ProgramInstance (org.hisp.dhis.program.ProgramInstance)2 TrackerImportStrategy (org.hisp.dhis.tracker.TrackerImportStrategy)2 ReferenceTrackerEntity (org.hisp.dhis.tracker.preheat.ReferenceTrackerEntity)2 TrackerErrorReport (org.hisp.dhis.tracker.report.TrackerErrorReport)2