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;
}
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;
}
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();
}
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();
}
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"));
}
Aggregations