use of org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem in project dhis2-core by dhis2.
the class AbstractRelationshipService method includeRelationshipItem.
private org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem includeRelationshipItem(RelationshipItem dao, boolean uidOnly) {
TrackedEntityInstanceParams teiParams = TrackedEntityInstanceParams.FALSE;
org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem relationshipItem = new org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem();
if (dao.getTrackedEntityInstance() != null) {
org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance tei = new org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance();
String uid = dao.getTrackedEntityInstance().getUid();
if (uidOnly) {
tei.clear();
tei.setTrackedEntityInstance(uid);
} else {
tei = trackedEntityInstanceService.getTrackedEntityInstance(dao.getTrackedEntityInstance(), teiParams);
}
relationshipItem.setTrackedEntityInstance(tei);
} else if (dao.getProgramInstance() != null) {
Enrollment enrollment = new Enrollment();
String uid = dao.getProgramInstance().getUid();
if (uidOnly) {
enrollment.clear();
enrollment.setEnrollment(uid);
} else {
enrollment = enrollmentService.getEnrollment(dao.getProgramInstance(), teiParams);
}
relationshipItem.setEnrollment(enrollment);
} else if (dao.getProgramStageInstance() != null) {
Event event = new Event();
String uid = dao.getProgramStageInstance().getUid();
if (uidOnly) {
event.clear();
event.setEvent(uid);
} else {
event = eventService.getEvent(dao.getProgramStageInstance());
event.setRelationships(null);
}
relationshipItem.setEvent(event);
}
return relationshipItem;
}
use of org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem in project dhis2-core by dhis2.
the class HandleRelationshipsTrackedEntityInstanceServiceTest method createTeiToTeiRelationship.
private Relationship createTeiToTeiRelationship(char key, RelationshipType relationshipType, TrackedEntityInstance trackedEntityInstanceA, TrackedEntityInstance trackedEntityInstanceB) {
RelationshipItem relationshipItemTeiA = new RelationshipItem();
relationshipItemTeiA.setTrackedEntityInstance(trackedEntityInstanceA);
RelationshipItem relationshipItemTeiB = new RelationshipItem();
relationshipItemTeiB.setTrackedEntityInstance(trackedEntityInstanceB);
Relationship relationship = new Relationship();
relationship.setFrom(relationshipItemTeiA);
relationship.setTo(relationshipItemTeiB);
relationship.setRelationshipType(relationshipType.getUid());
relationship.setRelationship("UID_" + key);
relationship.setRelationshipName("Person to person");
relationship.setBidirectional(relationshipType.isBidirectional());
return relationship;
}
use of org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem in project dhis2-core by dhis2.
the class JacksonRelationshipServiceTest method createTei2TeiRelationship.
private Relationship createTei2TeiRelationship(RelationshipType relationshipType) {
Relationship relationship = new Relationship();
RelationshipItem from = new RelationshipItem();
from.setTrackedEntityInstance(rnd.nextObject(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance.class));
RelationshipItem to = new RelationshipItem();
to.setTrackedEntityInstance(rnd.nextObject(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance.class));
relationship.setFrom(from);
relationship.setTo(to);
relationship.setRelationshipType(relationshipType.getUid());
return relationship;
}
use of org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method handleRelationships.
private ImportSummaries handleRelationships(TrackedEntityInstance dtoEntityInstance, org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance, ImportOptions importOptions) {
ImportSummaries importSummaries = new ImportSummaries();
List<Relationship> create = new ArrayList<>();
List<Relationship> update = new ArrayList<>();
List<String> relationshipUids = dtoEntityInstance.getRelationships().stream().map(Relationship::getRelationship).collect(Collectors.toList());
List<Relationship> delete = daoEntityInstance.getRelationshipItems().stream().map(RelationshipItem::getRelationship).filter(relationship -> trackerAccessManager.canWrite(importOptions.getUser(), relationship).isEmpty()).filter(relationship -> isTeiPartOfRelationship(relationship, daoEntityInstance)).map(org.hisp.dhis.relationship.Relationship::getUid).filter((uid) -> !relationshipUids.contains(uid)).map(uid -> {
Relationship relationship = new Relationship();
relationship.setRelationship(uid);
return relationship;
}).collect(Collectors.toList());
for (Relationship relationship : dtoEntityInstance.getRelationships()) {
if (importOptions.getImportStrategy() == ImportStrategy.SYNC && dtoEntityInstance.isDeleted()) {
delete.add(relationship);
} else if (relationship.getRelationship() == null) {
org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem relationshipItem = new org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem();
if (!isTeiPartOfRelationship(relationship, daoEntityInstance)) {
relationshipItem.setTrackedEntityInstance(dtoEntityInstance);
relationship.setFrom(relationshipItem);
}
create.add(relationship);
} else {
if (isTeiPartOfRelationship(relationship, daoEntityInstance)) {
if (_relationshipService.relationshipExists(relationship.getRelationship())) {
update.add(relationship);
} else {
create.add(relationship);
}
} else {
String message = String.format("Can't update relationship '%s': TrackedEntityInstance '%s' is not the owner of the relationship", relationship.getRelationship(), daoEntityInstance.getUid());
importSummaries.addImportSummary(new ImportSummary(ImportStatus.ERROR, message).setReference(relationship.getRelationship()).incrementIgnored());
}
}
}
importSummaries.addImportSummaries(relationshipService.addRelationships(create, importOptions));
importSummaries.addImportSummaries(relationshipService.updateRelationships(update, importOptions));
importSummaries.addImportSummaries(relationshipService.deleteRelationships(delete, importOptions));
return importSummaries;
}
use of org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem in project dhis2-core by dhis2.
the class AbstractRelationshipService method createDAORelationship.
private org.hisp.dhis.relationship.Relationship createDAORelationship(Relationship relationship) {
RelationshipType relationshipType = relationshipTypeCache.get(relationship.getRelationshipType());
org.hisp.dhis.relationship.Relationship daoRelationship = new org.hisp.dhis.relationship.Relationship();
RelationshipItem fromItem = null;
RelationshipItem toItem = null;
daoRelationship.setRelationshipType(relationshipType);
if (relationship.getRelationship() != null) {
daoRelationship.setUid(relationship.getRelationship());
}
// FROM
if (relationshipType.getFromConstraint().getRelationshipEntity().equals(TRACKED_ENTITY_INSTANCE)) {
fromItem = new RelationshipItem();
fromItem.setTrackedEntityInstance(trackedEntityInstanceCache.get(getUidOfRelationshipItem(relationship.getFrom())));
} else if (relationshipType.getFromConstraint().getRelationshipEntity().equals(PROGRAM_INSTANCE)) {
fromItem = new RelationshipItem();
fromItem.setProgramInstance(programInstanceCache.get(getUidOfRelationshipItem(relationship.getFrom())));
} else if (relationshipType.getFromConstraint().getRelationshipEntity().equals(PROGRAM_STAGE_INSTANCE)) {
fromItem = new RelationshipItem();
fromItem.setProgramStageInstance(programStageInstanceCache.get(getUidOfRelationshipItem(relationship.getFrom())));
}
// TO
if (relationshipType.getToConstraint().getRelationshipEntity().equals(TRACKED_ENTITY_INSTANCE)) {
toItem = new RelationshipItem();
toItem.setTrackedEntityInstance(trackedEntityInstanceCache.get(getUidOfRelationshipItem(relationship.getTo())));
} else if (relationshipType.getToConstraint().getRelationshipEntity().equals(PROGRAM_INSTANCE)) {
toItem = new RelationshipItem();
toItem.setProgramInstance(programInstanceCache.get(getUidOfRelationshipItem(relationship.getTo())));
} else if (relationshipType.getToConstraint().getRelationshipEntity().equals(PROGRAM_STAGE_INSTANCE)) {
toItem = new RelationshipItem();
toItem.setProgramStageInstance(programStageInstanceCache.get(getUidOfRelationshipItem(relationship.getTo())));
}
daoRelationship.setFrom(fromItem);
daoRelationship.setTo(toItem);
daoRelationship.setKey(RelationshipUtils.generateRelationshipKey(daoRelationship));
daoRelationship.setInvertedKey(RelationshipUtils.generateRelationshipInvertedKey(daoRelationship));
return daoRelationship;
}
Aggregations