Search in sources :

Example 6 with RelationshipItem

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

the class RelationshipSMSListener method postProcess.

@Override
protected SmsResponse postProcess(IncomingSms sms, SmsSubmission submission) throws SMSProcessingException {
    RelationshipSmsSubmission subm = (RelationshipSmsSubmission) submission;
    Uid fromid = subm.getFrom();
    Uid toid = subm.getTo();
    Uid typeid = subm.getRelationshipType();
    RelationshipType relType = relationshipTypeService.getRelationshipType(typeid.getUid());
    if (relType == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_RELTYPE.set(typeid));
    }
    RelationshipItem fromItem = createRelationshipItem(relType, RelationshipDir.FROM, fromid);
    RelationshipItem toItem = createRelationshipItem(relType, RelationshipDir.TO, toid);
    Relationship rel = new Relationship();
    // auto-generated
    if (subm.getRelationship() != null) {
        rel.setUid(subm.getRelationship().getUid());
    }
    rel.setRelationshipType(relType);
    rel.setFrom(fromItem);
    rel.setTo(toItem);
    rel.setCreated(new Date());
    rel.setLastUpdated(new Date());
    // TODO: Are there values we need to account for in relationships?
    relationshipService.addRelationship(rel);
    return SmsResponse.SUCCESS;
}
Also used : Uid(org.hisp.dhis.smscompression.models.Uid) RelationshipSmsSubmission(org.hisp.dhis.smscompression.models.RelationshipSmsSubmission) Relationship(org.hisp.dhis.relationship.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) Date(java.util.Date)

Example 7 with RelationshipItem

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

the class DeduplicationHelperTest method shouldFailGetDuplicateRelationshipErrorWithDuplicateRelationshipsWithTeisBidirectional.

@Test
void shouldFailGetDuplicateRelationshipErrorWithDuplicateRelationshipsWithTeisBidirectional() {
    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(teiC);
    toA.setTrackedEntityInstance(teiA);
    fromB.setTrackedEntityInstance(teiB);
    toB.setTrackedEntityInstance(teiC);
    Relationship relA = new Relationship();
    Relationship relB = new Relationship();
    relA.setAutoFields();
    relB.setAutoFields();
    relA.setRelationshipType(relationshipTypeBidirectional);
    relB.setRelationshipType(relationshipTypeBidirectional);
    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 8 with RelationshipItem

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

the class AbstractTrackedEntityInstanceService method getTei.

private TrackedEntityInstance getTei(org.hisp.dhis.trackedentity.TrackedEntityInstance daoTrackedEntityInstance, Set<TrackedEntityAttribute> readableAttributes, TrackedEntityInstanceParams params, User user) {
    if (daoTrackedEntityInstance == null) {
        return null;
    }
    TrackedEntityInstance trackedEntityInstance = new TrackedEntityInstance();
    trackedEntityInstance.setTrackedEntityInstance(daoTrackedEntityInstance.getUid());
    trackedEntityInstance.setOrgUnit(daoTrackedEntityInstance.getOrganisationUnit().getUid());
    trackedEntityInstance.setTrackedEntityType(daoTrackedEntityInstance.getTrackedEntityType().getUid());
    trackedEntityInstance.setCreated(DateUtils.getIso8601NoTz(daoTrackedEntityInstance.getCreated()));
    trackedEntityInstance.setCreatedAtClient(DateUtils.getIso8601NoTz(daoTrackedEntityInstance.getCreatedAtClient()));
    trackedEntityInstance.setLastUpdated(DateUtils.getIso8601NoTz(daoTrackedEntityInstance.getLastUpdated()));
    trackedEntityInstance.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(daoTrackedEntityInstance.getLastUpdatedAtClient()));
    trackedEntityInstance.setInactive(Optional.ofNullable(daoTrackedEntityInstance.isInactive()).orElse(false));
    trackedEntityInstance.setGeometry(daoTrackedEntityInstance.getGeometry());
    trackedEntityInstance.setDeleted(daoTrackedEntityInstance.isDeleted());
    trackedEntityInstance.setPotentialDuplicate(daoTrackedEntityInstance.isPotentialDuplicate());
    trackedEntityInstance.setStoredBy(daoTrackedEntityInstance.getStoredBy());
    trackedEntityInstance.setCreatedByUserInfo(daoTrackedEntityInstance.getCreatedByUserInfo());
    trackedEntityInstance.setLastUpdatedByUserInfo(daoTrackedEntityInstance.getLastUpdatedByUserInfo());
    if (daoTrackedEntityInstance.getGeometry() != null) {
        Geometry geometry = daoTrackedEntityInstance.getGeometry();
        FeatureType featureType = FeatureType.getTypeFromName(geometry.getGeometryType());
        trackedEntityInstance.setFeatureType(featureType);
        trackedEntityInstance.setCoordinates(GeoUtils.getCoordinatesFromGeometry(geometry));
    }
    if (params.isIncludeRelationships()) {
        for (RelationshipItem relationshipItem : daoTrackedEntityInstance.getRelationshipItems()) {
            org.hisp.dhis.relationship.Relationship daoRelationship = relationshipItem.getRelationship();
            if (trackerAccessManager.canRead(user, daoRelationship).isEmpty()) {
                Relationship relationship = relationshipService.getRelationship(relationshipItem.getRelationship(), RelationshipParams.FALSE, user);
                trackedEntityInstance.getRelationships().add(relationship);
            }
        }
    }
    if (params.isIncludeEnrollments()) {
        for (ProgramInstance programInstance : daoTrackedEntityInstance.getProgramInstances()) {
            if (trackerAccessManager.canRead(user, programInstance, false).isEmpty() && (params.isIncludeDeleted() || !programInstance.isDeleted())) {
                trackedEntityInstance.getEnrollments().add(enrollmentService.getEnrollment(user, programInstance, params, true));
            }
        }
    }
    if (params.isIncludeProgramOwners()) {
        for (TrackedEntityProgramOwner programOwner : daoTrackedEntityInstance.getProgramOwners()) {
            trackedEntityInstance.getProgramOwners().add(new ProgramOwner(programOwner));
        }
    }
    Set<TrackedEntityAttribute> readableAttributesCopy = filterOutSkipSyncAttributesIfApplies(params, trackedEntityInstance, readableAttributes);
    for (TrackedEntityAttributeValue attributeValue : daoTrackedEntityInstance.getTrackedEntityAttributeValues()) {
        if (readableAttributesCopy.contains(attributeValue.getAttribute())) {
            Attribute attribute = new Attribute();
            attribute.setCreated(DateUtils.getIso8601NoTz(attributeValue.getCreated()));
            attribute.setLastUpdated(DateUtils.getIso8601NoTz(attributeValue.getLastUpdated()));
            attribute.setDisplayName(attributeValue.getAttribute().getDisplayName());
            attribute.setAttribute(attributeValue.getAttribute().getUid());
            attribute.setValueType(attributeValue.getAttribute().getValueType());
            attribute.setCode(attributeValue.getAttribute().getCode());
            attribute.setValue(attributeValue.getValue());
            attribute.setStoredBy(attributeValue.getStoredBy());
            attribute.setSkipSynchronization(attributeValue.getAttribute().getSkipSynchronization());
            trackedEntityInstance.getAttributes().add(attribute);
        }
    }
    return trackedEntityInstance;
}
Also used : TrackedEntityProgramOwner(org.hisp.dhis.trackedentity.TrackedEntityProgramOwner) FeatureType(org.hisp.dhis.organisationunit.FeatureType) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Geometry(org.locationtech.jts.geom.Geometry) TrackedEntityProgramOwner(org.hisp.dhis.trackedentity.TrackedEntityProgramOwner) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem)

Example 9 with RelationshipItem

use of org.hisp.dhis.relationship.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;
}
Also used : TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) Event(org.hisp.dhis.dxf2.events.event.Event) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem)

Example 10 with RelationshipItem

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

the class RelationshipStoreTest method getByProgramStageInstance.

@Test
void getByProgramStageInstance() {
    Program programA = createProgram('A', new HashSet<>(), organisationUnit);
    programService.addProgram(programA);
    ProgramInstance programInstance = new ProgramInstance();
    programInstance.setProgram(programA);
    programInstance.setAutoFields();
    programInstance.setEnrollmentDate(new Date());
    programInstance.setIncidentDate(new Date());
    programInstance.setStatus(ProgramStatus.ACTIVE);
    programInstanceService.addProgramInstance(programInstance);
    ProgramStage programStageA = createProgramStage('S', programA);
    programStageA.setProgram(programA);
    programStageService.saveProgramStage(programStageA);
    programA.getProgramStages().add(programStageA);
    ProgramStageInstance programStageInstance = new ProgramStageInstance();
    programStageInstance.setOrganisationUnit(organisationUnit);
    programStageInstance.setProgramStage(programStageA);
    programStageInstance.setProgramInstance(programInstance);
    programStageInstance.setAutoFields();
    programStageInstanceService.addProgramStageInstance(programStageInstance);
    RelationshipItem relationshipItemFrom = new RelationshipItem();
    relationshipItemFrom.setTrackedEntityInstance(trackedEntityInstanceA);
    RelationshipItem relationshipItemTo = new RelationshipItem();
    relationshipItemTo.setProgramStageInstance(programStageInstance);
    Relationship relationshipA = new Relationship();
    relationshipA.setRelationshipType(relationshipType);
    relationshipA.setFrom(relationshipItemFrom);
    relationshipA.setTo(relationshipItemTo);
    relationshipA.setKey(RelationshipUtils.generateRelationshipKey(relationshipA));
    relationshipA.setInvertedKey(RelationshipUtils.generateRelationshipInvertedKey(relationshipA));
    relationshipService.addRelationship(relationshipA);
    List<Relationship> relationshipList = relationshipService.getRelationshipsByProgramStageInstance(programStageInstance, true);
    assertEquals(1, relationshipList.size());
    assertTrue(relationshipList.contains(relationshipA));
    assertTrue(relationshipService.getRelationshipByRelationship(relationshipA).isPresent());
}
Also used : Program(org.hisp.dhis.program.Program) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Relationship(org.hisp.dhis.relationship.Relationship) ProgramStage(org.hisp.dhis.program.ProgramStage) Date(java.util.Date) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

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