Search in sources :

Example 31 with Relationship

use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.

the class RelationshipRowCallbackHandler method getRelationship.

private Relationship getRelationship(ResultSet rs) throws SQLException {
    Relationship relationship = new Relationship();
    relationship.setRelationship(rs.getString("rel_uid"));
    relationship.setRelationshipType(rs.getString("reltype_uid"));
    relationship.setRelationshipName(rs.getString("reltype_name"));
    relationship.setFrom(createItem(rs.getString("from_uid")));
    relationship.setTo(createItem(rs.getString("to_uid")));
    relationship.setBidirectional(rs.getBoolean("reltype_bi"));
    relationship.setCreated(DateUtils.getIso8601NoTz(rs.getTimestamp("created")));
    relationship.setLastUpdated(DateUtils.getIso8601NoTz(rs.getTimestamp("lastupdated")));
    return relationship;
}
Also used : Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship)

Example 32 with Relationship

use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.

the class RelationshipRowCallbackHandler method createItem.

/**
 * The SQL query that generates the ResultSet used by this
 * {@see RowCallbackHandler} fetches both sides of a relationship: since
 * each side can be a Tracked Entity Instance, a Program Instance or a
 * Program Stage Instance, the query adds an "hint" to the final result to
 * help this Handler to correctly associate the type to the left or right
 * side of the relationship. The "typeWithUid" variable contains the UID of
 * the object and a string representing the type. E.g.
 *
 * tei|dj3382832 psi|332983893
 *
 * This function parses the string and extract the type and the uid, in
 * order to instantiate the appropriate object and assign it to the
 * {@see RelationshipItem}
 *
 * @param typeWithUid a String containing the object type and the UID of the
 *        object, separated by | (pipe)
 * @return a {@see RelationshipItem}
 */
private RelationshipItem createItem(String typeWithUid) {
    if (StringUtils.isEmpty(typeWithUid)) {
        return new RelationshipItem();
    }
    RelationshipItem ri = new RelationshipItem();
    final String type = typeWithUid.split("\\|")[0];
    final String uid = typeWithUid.split("\\|")[1];
    switch(type) {
        case "tei":
            TrackedEntityInstance tei = new TrackedEntityInstance();
            tei.clear();
            tei.setTrackedEntityInstance(uid);
            ri.setTrackedEntityInstance(tei);
            break;
        case "pi":
            Enrollment pi = new Enrollment();
            pi.setEnrollment(uid);
            ri.setEnrollment(pi);
            break;
        case "psi":
            Event psi = new Event();
            psi.setEvent(uid);
            ri.setEvent(psi);
            break;
        default:
            log.warn("Expecting tei|psi|pi as type when fetching a relationship, got: " + type);
    }
    return ri;
}
Also used : Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) Event(org.hisp.dhis.dxf2.events.event.Event) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) RelationshipItem(org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem)

Example 33 with Relationship

use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.

the class AbstractStore method getRelationshipsByIdsPartitioned.

private Multimap<String, Relationship> getRelationshipsByIdsPartitioned(List<Long> ids) {
    if (!ids.isEmpty()) {
        RelationshipRowCallbackHandler handler = new RelationshipRowCallbackHandler();
        jdbcTemplate.query(GET_RELATIONSHIP_BY_RELATIONSHIP_ID, createIdsParam(ids), handler);
        return handler.getItems();
    }
    return ArrayListMultimap.create();
}
Also used : RelationshipRowCallbackHandler(org.hisp.dhis.dxf2.events.trackedentity.store.mapper.RelationshipRowCallbackHandler)

Example 34 with Relationship

use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.

the class AbstractStore method getRelationshipsPartitioned.

private Multimap<String, Relationship> getRelationshipsPartitioned(List<Long> ids) {
    String getRelationshipsHavingIdSQL = String.format(GET_RELATIONSHIP_ID_BY_ENTITY_ID_SQL, getRelationshipEntityColumn(), getRelationshipEntityColumn());
    // Get all the relationship ids that have at least one relationship item
    // having
    // the ids in the tei|pi|psi column (depending on the subclass)
    List<Long> relationshipIds = getRelationshipIds(getRelationshipsHavingIdSQL, createIdsParam(ids));
    if (!relationshipIds.isEmpty()) {
        RelationshipRowCallbackHandler handler = new RelationshipRowCallbackHandler();
        jdbcTemplate.query(GET_RELATIONSHIP_BY_RELATIONSHIP_ID, createIdsParam(relationshipIds), handler);
        return handler.getItems();
    }
    return ArrayListMultimap.create();
}
Also used : RelationshipRowCallbackHandler(org.hisp.dhis.dxf2.events.trackedentity.store.mapper.RelationshipRowCallbackHandler)

Example 35 with Relationship

use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.

the class JacksonRelationshipServiceTest method verifyRelationshipIsNotImportedWhenDoesExist.

@Test
void verifyRelationshipIsNotImportedWhenDoesExist() {
    org.hisp.dhis.relationship.Relationship daoRelationship = new org.hisp.dhis.relationship.Relationship();
    daoRelationship.setUid("12345");
    when(relationshipService.getRelationshipByRelationship(any(org.hisp.dhis.relationship.Relationship.class))).thenReturn(Optional.of(daoRelationship));
    ImportSummary importSummary = subject.addRelationship(relationship, rnd.nextObject(ImportOptions.class));
    assertThat(importSummary.getStatus(), is(ImportStatus.ERROR));
    assertThat(importSummary.getImportCount().getImported(), is(0));
    assertThat(importSummary.getReference(), is(daoRelationship.getUid()));
    assertThat(importSummary.getDescription(), is("Relationship " + daoRelationship.getUid() + " already exists"));
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) Test(org.junit.jupiter.api.Test)

Aggregations

Relationship (org.hisp.dhis.dxf2.events.trackedentity.Relationship)30 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)14 ArrayList (java.util.ArrayList)11 Transactional (org.springframework.transaction.annotation.Transactional)10 TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)9 Test (org.junit.jupiter.api.Test)9 List (java.util.List)8 RelationshipType (org.hisp.dhis.relationship.RelationshipType)7 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)6 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)6 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)6 Optional (java.util.Optional)5 Set (java.util.Set)5 Event (org.hisp.dhis.dxf2.events.event.Event)5 Lists (com.google.common.collect.Lists)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 CurrentUserService (org.hisp.dhis.user.CurrentUserService)4