Search in sources :

Example 1 with RelationshipItem

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

the class TranslationServiceTest method testFormNameTranslationForRelationShip.

@Test
void testFormNameTranslationForRelationShip() {
    RelationshipType relationshipType = createRelationshipType('A');
    OrganisationUnit organisationUnit = createOrganisationUnit('A');
    TrackedEntityAttribute attribute = createTrackedEntityAttribute('A');
    manager.save(relationshipType);
    manager.save(organisationUnit);
    manager.save(attribute);
    TrackedEntityInstance trackedEntityInstance = createTrackedEntityInstance('A', organisationUnit, attribute);
    manager.save(trackedEntityInstance);
    Relationship relationship = new Relationship();
    RelationshipItem from = new RelationshipItem();
    from.setTrackedEntityInstance(trackedEntityInstance);
    RelationshipItem to = new RelationshipItem();
    to.setTrackedEntityInstance(trackedEntityInstance);
    relationship.setFrom(from);
    relationship.setTo(to);
    relationship.setRelationshipType(relationshipType);
    relationship.setKey(RelationshipUtils.generateRelationshipKey(relationship));
    relationship.setInvertedKey(RelationshipUtils.generateRelationshipInvertedKey(relationship));
    manager.save(relationship);
    String translatedValue = "RelationShip FormName Translated";
    Set<Translation> translations = new HashSet<>(relationship.getTranslations());
    translations.add(new Translation(locale.getLanguage(), "FORM_NAME", translatedValue));
    manager.updateTranslations(relationship, translations);
    assertEquals(translatedValue, relationship.getDisplayFormName());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Relationship(org.hisp.dhis.relationship.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 2 with RelationshipItem

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

the class RelationshipUtilsTest method testGenerateRelationshipInvertedKey.

@Test
void testGenerateRelationshipInvertedKey() {
    RelationshipItem from = new RelationshipItem();
    RelationshipItem to = new RelationshipItem();
    from.setTrackedEntityInstance(teiA);
    to.setTrackedEntityInstance(teiB);
    Relationship relationship = new Relationship();
    relationship.setRelationshipType(relationshipType);
    relationship.setFrom(from);
    relationship.setTo(to);
    String invertedKey = relationshipType.getUid() + "_" + teiB.getUid() + "_" + teiA.getUid();
    assertEquals(invertedKey, RelationshipUtils.generateRelationshipInvertedKey(relationship));
}
Also used : Relationship(org.hisp.dhis.relationship.Relationship) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 3 with RelationshipItem

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

the class MaintenanceServiceTest method testDeleteSoftDeletedProgramInstanceLinkedToARelationshipItem.

@Test
void testDeleteSoftDeletedProgramInstanceLinkedToARelationshipItem() {
    RelationshipType rType = createRelationshipType('A');
    rType.getFromConstraint().setRelationshipEntity(RelationshipEntity.PROGRAM_INSTANCE);
    rType.getFromConstraint().setProgram(program);
    rType.getToConstraint().setRelationshipEntity(RelationshipEntity.TRACKED_ENTITY_INSTANCE);
    rType.getFromConstraint().setTrackedEntityType(entityInstance.getTrackedEntityType());
    relationshipTypeService.addRelationshipType(rType);
    Relationship r = new Relationship();
    RelationshipItem rItem1 = new RelationshipItem();
    rItem1.setProgramInstance(programInstance);
    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(programInstanceService.getProgramInstance(programInstance.getId()));
    assertNotNull(relationshipService.getRelationship(r.getId()));
    programInstanceService.deleteProgramInstance(programInstance);
    assertNull(programInstanceService.getProgramInstance(programInstance.getId()));
    assertNull(relationshipService.getRelationship(r.getId()));
    assertTrue(programInstanceService.programInstanceExistsIncludingDeleted(programInstance.getUid()));
    maintenanceService.deleteSoftDeletedProgramInstances();
    assertFalse(programInstanceService.programInstanceExistsIncludingDeleted(programInstance.getUid()));
}
Also used : Relationship(org.hisp.dhis.relationship.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) Test(org.junit.jupiter.api.Test)

Example 4 with RelationshipItem

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

the class DeduplicationHelper method getMergeableRelationships.

private List<String> getMergeableRelationships(TrackedEntityInstance original, TrackedEntityInstance duplicate) {
    List<String> relationships = new ArrayList<>();
    for (RelationshipItem ri : duplicate.getRelationshipItems()) {
        TrackedEntityInstance from = ri.getRelationship().getFrom().getTrackedEntityInstance();
        TrackedEntityInstance to = ri.getRelationship().getTo().getTrackedEntityInstance();
        if ((from != null && from.getUid().equals(original.getUid())) || (to != null && to.getUid().equals(original.getUid()))) {
            continue;
        }
        boolean duplicateRelationship = false;
        for (RelationshipItem ri2 : original.getRelationshipItems()) {
            TrackedEntityInstance originalFrom = ri2.getRelationship().getFrom().getTrackedEntityInstance();
            TrackedEntityInstance originalTo = ri2.getRelationship().getTo().getTrackedEntityInstance();
            if ((originalFrom != null && originalFrom.getUid().equals(duplicate.getUid())) || (originalTo != null && originalTo.getUid().equals(duplicate.getUid()))) {
                continue;
            }
            if (isSameRelationship(ri2.getRelationship(), ri.getRelationship())) {
                duplicateRelationship = true;
                break;
            }
        }
        if (duplicateRelationship) {
            continue;
        }
        relationships.add(ri.getRelationship().getUid());
    }
    return relationships;
}
Also used : ArrayList(java.util.ArrayList) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem)

Example 5 with RelationshipItem

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

the class DefaultTrackerAccessManager method canRead.

@Override
public List<String> canRead(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.canDataRead(user, relationshipType)) {
        errors.add("User has no data read access to relationshipType: " + relationshipType.getUid());
    }
    from = relationship.getFrom();
    to = relationship.getTo();
    errors.addAll(canRead(user, from.getTrackedEntityInstance()));
    errors.addAll(canRead(user, from.getProgramInstance(), false));
    errors.addAll(canRead(user, from.getProgramStageInstance(), false));
    errors.addAll(canRead(user, to.getTrackedEntityInstance()));
    errors.addAll(canRead(user, to.getProgramInstance(), false));
    errors.addAll(canRead(user, to.getProgramStageInstance(), false));
    return errors;
}
Also used : ArrayList(java.util.ArrayList) RelationshipType(org.hisp.dhis.relationship.RelationshipType) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem)

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