Search in sources :

Example 16 with RelationshipItem

use of org.hisp.dhis.relationship.RelationshipItem in project dhis2-core by dhis2.

the class TrackerTest method persistRelationship.

public Relationship persistRelationship(TrackedEntityInstance t1, TrackedEntityInstance t2) {
    RelationshipItem from = new RelationshipItem();
    from.setTrackedEntityInstance(t1);
    RelationshipItem to = new RelationshipItem();
    to.setTrackedEntityInstance(t2);
    return _persistRelationship(from, to);
}
Also used : RelationshipItem(org.hisp.dhis.relationship.RelationshipItem)

Example 17 with RelationshipItem

use of org.hisp.dhis.relationship.RelationshipItem in project dhis2-core by dhis2.

the class DefaultTrackerAccessManager method canWrite.

@Override
public List<String> canWrite(User user, Relationship relationship) {
    List<String> errors = new ArrayList<>();
    RelationshipType relationshipType;
    RelationshipItem from;
    RelationshipItem to;
    // always allow if user == null (internal process) or user is superuser
    if (user == null || user.isSuper() || relationship == null) {
        return errors;
    }
    relationshipType = relationship.getRelationshipType();
    if (!aclService.canDataWrite(user, relationshipType)) {
        errors.add("User has no data write access to relationshipType: " + relationshipType.getUid());
    }
    from = relationship.getFrom();
    to = relationship.getTo();
    errors.addAll(canWrite(user, from.getTrackedEntityInstance()));
    errors.addAll(canUpdate(user, from.getProgramInstance(), false));
    errors.addAll(canUpdate(user, from.getProgramStageInstance(), false));
    errors.addAll(canWrite(user, to.getTrackedEntityInstance()));
    errors.addAll(canUpdate(user, to.getProgramInstance(), false));
    errors.addAll(canUpdate(user, to.getProgramStageInstance(), false));
    return errors;
}
Also used : ArrayList(java.util.ArrayList) RelationshipType(org.hisp.dhis.relationship.RelationshipType) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem)

Example 18 with RelationshipItem

use of org.hisp.dhis.relationship.RelationshipItem in project dhis2-core by dhis2.

the class RelationshipSMSListener method createRelationshipItem.

private RelationshipItem createRelationshipItem(RelationshipType relType, RelationshipDir dir, Uid objId) {
    RelationshipItem relItem = new RelationshipItem();
    RelationshipEntity fromEnt = relType.getFromConstraint().getRelationshipEntity();
    RelationshipEntity toEnt = relType.getFromConstraint().getRelationshipEntity();
    RelationshipEntity relEnt = dir == RelationshipDir.FROM ? fromEnt : toEnt;
    switch(relEnt) {
        case TRACKED_ENTITY_INSTANCE:
            TrackedEntityInstance tei = trackedEntityInstanceService.getTrackedEntityInstance(objId.getUid());
            if (tei == null) {
                throw new SMSProcessingException(SmsResponse.INVALID_TEI.set(objId));
            }
            relItem.setTrackedEntityInstance(tei);
            break;
        case PROGRAM_INSTANCE:
            ProgramInstance progInst = programInstanceService.getProgramInstance(objId.getUid());
            if (progInst == null) {
                throw new SMSProcessingException(SmsResponse.INVALID_ENROLL.set(objId));
            }
            relItem.setProgramInstance(progInst);
            break;
        case PROGRAM_STAGE_INSTANCE:
            ProgramStageInstance stageInst = programStageInstanceService.getProgramStageInstance(objId.getUid());
            if (stageInst == null) {
                throw new SMSProcessingException(SmsResponse.INVALID_EVENT.set(objId));
            }
            relItem.setProgramStageInstance(stageInst);
            break;
    }
    return relItem;
}
Also used : ProgramInstance(org.hisp.dhis.program.ProgramInstance) RelationshipEntity(org.hisp.dhis.relationship.RelationshipEntity) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 19 with RelationshipItem

use of org.hisp.dhis.relationship.RelationshipItem in project dhis2-core by dhis2.

the class DeduplicationHelperTest method shouldFailGetDuplicateRelationshipErrorWithDuplicateRelationshipsWithTeis.

@Test
void shouldFailGetDuplicateRelationshipErrorWithDuplicateRelationshipsWithTeis() {
    TrackedEntityInstance teiA = getTeiA();
    TrackedEntityInstance teiB = getTeiB();
    TrackedEntityInstance teiC = getTeiC();
    // A->C, B->C
    RelationshipItem fromA = new RelationshipItem();
    RelationshipItem toA = new RelationshipItem();
    RelationshipItem fromB = new RelationshipItem();
    RelationshipItem toB = new RelationshipItem();
    fromA.setTrackedEntityInstance(teiA);
    toA.setTrackedEntityInstance(teiC);
    fromB.setTrackedEntityInstance(teiB);
    toB.setTrackedEntityInstance(teiC);
    Relationship relA = new Relationship();
    Relationship relB = new Relationship();
    relA.setAutoFields();
    relB.setAutoFields();
    relA.setRelationshipType(relationshipType);
    relB.setRelationshipType(relationshipType);
    relA.setFrom(fromA);
    relA.setTo(toA);
    relB.setFrom(fromB);
    relB.setTo(toB);
    fromA.setRelationship(relA);
    toA.setRelationship(relA);
    fromB.setRelationship(relB);
    toB.setRelationship(relB);
    teiA.getRelationshipItems().add(fromA);
    teiB.getRelationshipItems().add(fromB);
    assertNotNull(deduplicationHelper.getDuplicateRelationshipError(teiA, teiB.getRelationshipItems().stream().map(RelationshipItem::getRelationship).collect(Collectors.toSet())));
}
Also used : Relationship(org.hisp.dhis.relationship.Relationship) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 20 with RelationshipItem

use of org.hisp.dhis.relationship.RelationshipItem in project dhis2-core by dhis2.

the class DeduplicationHelperTest method testMergeObjectRelationship.

@Test
void testMergeObjectRelationship() throws PotentialDuplicateConflictException, PotentialDuplicateForbiddenException {
    TrackedEntityInstance original = getTeiA();
    TrackedEntityInstance another = getTeiA();
    TrackedEntityInstance duplicate = getTeiA();
    Relationship anotherBaseRelationship = getRelationship();
    RelationshipItem relationshipItemAnotherTo = getRelationshipItem(anotherBaseRelationship, another);
    RelationshipItem relationshipItemAnotherFrom = getRelationshipItem(anotherBaseRelationship, duplicate);
    Relationship anotherRelationship = getRelationship(relationshipItemAnotherTo, relationshipItemAnotherFrom);
    RelationshipItem anotherRelationshipItem = getRelationshipItem(anotherRelationship, duplicate);
    duplicate.getRelationshipItems().add(anotherRelationshipItem);
    MergeObject mergeObject = deduplicationHelper.generateMergeObject(original, duplicate);
    assertTrue(mergeObject.getTrackedEntityAttributes().isEmpty());
    assertFalse(mergeObject.getRelationships().isEmpty());
    mergeObject.getRelationships().forEach(r -> assertEquals(anotherRelationship.getUid(), r));
    Relationship baseRelationship = getRelationship();
    RelationshipItem relationshipItemTo = getRelationshipItem(baseRelationship, original);
    RelationshipItem relationshipItemFrom = getRelationshipItem(baseRelationship, duplicate);
    Relationship relationship = getRelationship(relationshipItemTo, relationshipItemFrom);
    RelationshipItem relationshipItem = getRelationshipItem(relationship, duplicate);
    duplicate.getRelationshipItems().add(relationshipItem);
    mergeObject = deduplicationHelper.generateMergeObject(original, duplicate);
    assertEquals(1, mergeObject.getRelationships().size());
}
Also used : Relationship(org.hisp.dhis.relationship.Relationship) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Aggregations

RelationshipItem (org.hisp.dhis.relationship.RelationshipItem)30 Relationship (org.hisp.dhis.relationship.Relationship)16 Test (org.junit.jupiter.api.Test)11 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)9 RelationshipType (org.hisp.dhis.relationship.RelationshipType)8 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)7 ArrayList (java.util.ArrayList)5 ProgramInstance (org.hisp.dhis.program.ProgramInstance)5 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)4 Date (java.util.Date)3 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 CodeGenerator (org.hisp.dhis.common.CodeGenerator)2 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)2 ProgramInstanceService (org.hisp.dhis.program.ProgramInstanceService)2 RelationshipService (org.hisp.dhis.relationship.RelationshipService)2