use of org.hisp.dhis.relationship.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.relationship.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;
}
use of org.hisp.dhis.relationship.RelationshipItem in project dhis2-core by dhis2.
the class TrackerTest method persistRelationship.
public Relationship persistRelationship(TrackedEntityInstance tei, ProgramInstance pi) {
RelationshipItem from = new RelationshipItem();
from.setTrackedEntityInstance(tei);
RelationshipItem to = new RelationshipItem();
to.setProgramInstance(pi);
return _persistRelationship(from, to);
}
use of org.hisp.dhis.relationship.RelationshipItem in project dhis2-core by dhis2.
the class DeduplicationHelper method getInvalidReferenceErrors.
public String getInvalidReferenceErrors(DeduplicationMergeParams params) {
TrackedEntityInstance original = params.getOriginal();
TrackedEntityInstance duplicate = params.getDuplicate();
MergeObject mergeObject = params.getMergeObject();
/*
* Step 1: Make sure all uids in mergeObject is valid
*/
List<String> uids = ListUtils.distinctUnion(mergeObject.getTrackedEntityAttributes(), mergeObject.getEnrollments(), mergeObject.getRelationships());
for (String uid : uids) {
if (!CodeGenerator.isValidUid(uid)) {
return "Invalid uid '" + uid + "'.";
}
}
/*
* Step 2: Make sure all references objects exists in duplicate
*/
Set<String> validTrackedEntityAttributes = duplicate.getTrackedEntityAttributeValues().stream().map(teav -> teav.getAttribute().getUid()).collect(Collectors.toSet());
Set<String> validRelationships = duplicate.getRelationshipItems().stream().map(rel -> rel.getRelationship().getUid()).collect(Collectors.toSet());
Set<String> validEnrollments = duplicate.getProgramInstances().stream().map(BaseIdentifiableObject::getUid).collect(Collectors.toSet());
for (String tea : mergeObject.getTrackedEntityAttributes()) {
if (!validTrackedEntityAttributes.contains(tea)) {
return "Duplicate has no attribute '" + tea + "'.";
}
}
String rel = mergeObject.getRelationships().stream().filter(r -> !validRelationships.contains(r)).findFirst().orElse(null);
if (rel != null) {
return "Duplicate has no relationship '" + rel + "'.";
}
for (String enrollmentUid : mergeObject.getEnrollments()) {
if (!validEnrollments.contains(enrollmentUid)) {
return "Duplicate has no enrollment '" + enrollmentUid + "'.";
}
}
/*
* Step 3: Duplicate Relationships and Enrollments
*/
Set<Relationship> relationshipsToMerge = params.getDuplicate().getRelationshipItems().stream().map(RelationshipItem::getRelationship).filter(r -> mergeObject.getRelationships().contains(r.getUid())).collect(Collectors.toSet());
String duplicateRelationshipError = getDuplicateRelationshipError(params.getOriginal(), relationshipsToMerge);
if (duplicateRelationshipError != null) {
return "Invalid relationship '" + duplicateRelationshipError + "'. A similar relationship already exists on original.";
}
Set<String> programUidOfExistingEnrollments = original.getProgramInstances().stream().map(ProgramInstance::getProgram).map(BaseIdentifiableObject::getUid).collect(Collectors.toSet());
String duplicateEnrollment = duplicate.getProgramInstances().stream().filter(pi -> mergeObject.getEnrollments().contains(pi.getUid())).map(ProgramInstance::getProgram).map(BaseIdentifiableObject::getUid).filter(programUidOfExistingEnrollments::contains).findAny().orElse(null);
if (duplicateEnrollment != null) {
return "Invalid enrollment '" + duplicateEnrollment + "'. A similar enrollment already exists on original.";
}
/*
* Step 4: Make sure no relationships will become self-referencing.
*/
Set<String> relationshipsToMergeUids = relationshipsToMerge.stream().map(BaseIdentifiableObject::getUid).collect(Collectors.toSet());
String selfReferencingRelationship = original.getRelationshipItems().stream().map(RelationshipItem::getRelationship).map(BaseIdentifiableObject::getUid).filter(relationshipsToMergeUids::contains).findFirst().orElse(null);
if (selfReferencingRelationship != null) {
return "Invalid relationship '" + selfReferencingRelationship + "'. Relationship is between original and duplicate.";
}
return null;
}
use of org.hisp.dhis.relationship.RelationshipItem in project dhis2-core by dhis2.
the class MaintenanceServiceTest method testDeleteSoftDeletedProgramStageInstanceLinkedToARelationshipItem.
@Test
void testDeleteSoftDeletedProgramStageInstanceLinkedToARelationshipItem() {
RelationshipType rType = createRelationshipType('A');
rType.getFromConstraint().setRelationshipEntity(RelationshipEntity.PROGRAM_STAGE_INSTANCE);
rType.getFromConstraint().setProgram(program);
rType.getFromConstraint().setProgramStage(program.getProgramStageByStage(1));
rType.getToConstraint().setRelationshipEntity(RelationshipEntity.TRACKED_ENTITY_INSTANCE);
rType.getFromConstraint().setTrackedEntityType(entityInstance.getTrackedEntityType());
relationshipTypeService.addRelationshipType(rType);
ProgramStageInstance programStageInstanceA = new ProgramStageInstance(programInstance, program.getProgramStageByStage(1));
programStageInstanceA.setDueDate(enrollmentDate);
programStageInstanceA.setUid("UID-A");
long idA = programStageInstanceService.addProgramStageInstance(programStageInstanceA);
Relationship r = new Relationship();
RelationshipItem rItem1 = new RelationshipItem();
rItem1.setProgramStageInstance(programStageInstanceA);
RelationshipItem rItem2 = new RelationshipItem();
rItem2.setTrackedEntityInstance(entityInstance);
r.setFrom(rItem1);
r.setTo(rItem2);
r.setRelationshipType(rType);
r.setKey(RelationshipUtils.generateRelationshipKey(r));
r.setInvertedKey(RelationshipUtils.generateRelationshipInvertedKey(r));
relationshipService.addRelationship(r);
assertNotNull(programStageInstanceService.getProgramStageInstance(idA));
assertNotNull(relationshipService.getRelationship(r.getId()));
programStageInstanceService.deleteProgramStageInstance(programStageInstanceA);
assertNull(programStageInstanceService.getProgramStageInstance(idA));
assertNull(relationshipService.getRelationship(r.getId()));
assertTrue(programStageInstanceService.programStageInstanceExistsIncludingDeleted(programStageInstanceA.getUid()));
maintenanceService.deleteSoftDeletedProgramStageInstances();
assertFalse(programStageInstanceService.programStageInstanceExistsIncludingDeleted(programStageInstanceA.getUid()));
}
Aggregations