Search in sources :

Example 1 with RelationshipItem

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;
}
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 2 with 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;
}
Also used : Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) RelationshipItem(org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem)

Example 3 with RelationshipItem

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;
}
Also used : Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) RelationshipItem(org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem)

Example 4 with RelationshipItem

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;
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) Authorities(org.hisp.dhis.security.Authorities) Date(java.util.Date) Restrictions(org.hisp.dhis.query.Restrictions) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) TrackedEntityInstanceAuditService(org.hisp.dhis.trackedentity.TrackedEntityInstanceAuditService) ReservedValueService(org.hisp.dhis.reservedvalue.ReservedValueService) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) StringUtils(org.apache.commons.lang3.StringUtils) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) RelationshipService(org.hisp.dhis.relationship.RelationshipService) FileResourceService(org.hisp.dhis.fileresource.FileResourceService) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) TrackedEntityAttributeStore(org.hisp.dhis.trackedentity.TrackedEntityAttributeStore) EnrollmentService(org.hisp.dhis.dxf2.events.enrollment.EnrollmentService) Query(org.hisp.dhis.query.Query) UserService(org.hisp.dhis.user.UserService) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Collection(java.util.Collection) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) QueryService(org.hisp.dhis.query.QueryService) TrackedEntityProgramOwner(org.hisp.dhis.trackedentity.TrackedEntityProgramOwner) ImportReportMode(org.hisp.dhis.dxf2.metadata.feedback.ImportReportMode) Objects(java.util.Objects) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) TEA_VALUE_MAX_LENGTH(org.hisp.dhis.trackedentity.TrackedEntityAttributeService.TEA_VALUE_MAX_LENGTH) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) Optional(java.util.Optional) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) Geometry(org.locationtech.jts.geom.Geometry) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityInstanceAggregate(org.hisp.dhis.dxf2.events.aggregates.TrackedEntityInstanceAggregate) TrackedEntityTypeService(org.hisp.dhis.trackedentity.TrackedEntityTypeService) Constants(org.hisp.dhis.dxf2.Constants) AuditType(org.hisp.dhis.common.AuditType) GeoUtils(org.hisp.dhis.system.util.GeoUtils) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Notifier(org.hisp.dhis.system.notification.Notifier) RelationshipParams(org.hisp.dhis.dxf2.events.RelationshipParams) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) User(org.hisp.dhis.user.User) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) TrackedEntityInstanceAudit(org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit) IdSchemes(org.hisp.dhis.common.IdSchemes) FileResource(org.hisp.dhis.fileresource.FileResource) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) FeatureType(org.hisp.dhis.organisationunit.FeatureType) NotificationLevel(org.hisp.dhis.system.notification.NotificationLevel) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CachingMap(org.hisp.dhis.commons.collection.CachingMap) ERROR(org.hisp.dhis.system.notification.NotificationLevel.ERROR) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Collections(java.util.Collections) DateUtils(org.hisp.dhis.util.DateUtils) Transactional(org.springframework.transaction.annotation.Transactional) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ArrayList(java.util.ArrayList) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem)

Example 5 with RelationshipItem

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;
}
Also used : Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem)

Aggregations

Relationship (org.hisp.dhis.dxf2.events.trackedentity.Relationship)4 RelationshipItem (org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem)4 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)3 Event (org.hisp.dhis.dxf2.events.event.Event)3 RelationshipItem (org.hisp.dhis.relationship.RelationshipItem)3 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)2 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1