Search in sources :

Example 1 with RelationshipEntity

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

the class RelationshipTypeJoinGeneratorTest method asserter.

private void asserter(RelationshipType relationshipType, AnalyticsType type) {
    RelationshipEntity from = relationshipType.getFromConstraint().getRelationshipEntity();
    RelationshipEntity to = relationshipType.getToConstraint().getRelationshipEntity();
    String expected = " ";
    expected += getFromRelationshipEntity(from, type);
    expected += RELATIONSHIP_JOIN;
    expected += getToRelationshipEntity(to);
    expected += addWhere(relationshipType);
    expected += (to.equals(TRACKED_ENTITY_INSTANCE) ? " AND tei.uid = ax.tei )" : (to.equals(PROGRAM_INSTANCE) ? " AND pi.uid = ax.pi )" : " AND psi.uid = ax.psi )"));
    assertEquals(expected, RelationshipTypeJoinGenerator.generate(ALIAS, relationshipType, type));
}
Also used : RelationshipEntity(org.hisp.dhis.relationship.RelationshipEntity)

Example 2 with RelationshipEntity

use of org.hisp.dhis.relationship.RelationshipEntity 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 3 with RelationshipEntity

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

the class AbstractRelationshipService method prepareCaches.

private void prepareCaches(List<Relationship> relationships, User user) {
    Map<RelationshipEntity, List<String>> relationshipEntities = new HashMap<>();
    Map<String, List<Relationship>> relationshipTypeMap = relationships.stream().collect(Collectors.groupingBy(Relationship::getRelationshipType));
    // Find all the RelationshipTypes first, so we know what the uids refer
    // to
    Query query = Query.from(schemaService.getDynamicSchema(RelationshipType.class));
    query.setUser(user);
    query.add(Restrictions.in("id", relationshipTypeMap.keySet()));
    queryService.query(query).forEach(rt -> relationshipTypeCache.put(rt.getUid(), (RelationshipType) rt));
    // Group all uids into their respective RelationshipEntities
    relationshipTypeCache.values().forEach(relationshipType -> {
        List<String> fromUids = relationshipTypeMap.get(relationshipType.getUid()).stream().map((r) -> getUidOfRelationshipItem(r.getFrom())).collect(Collectors.toList());
        List<String> toUids = relationshipTypeMap.get(relationshipType.getUid()).stream().map((r) -> getUidOfRelationshipItem(r.getTo())).collect(Collectors.toList());
        // Merge existing results with newly found ones.
        relationshipEntities.merge(relationshipType.getFromConstraint().getRelationshipEntity(), fromUids, (old, _new) -> ListUtils.union(old, _new));
        relationshipEntities.merge(relationshipType.getToConstraint().getRelationshipEntity(), toUids, (old, _new) -> ListUtils.union(old, _new));
    });
    // Find and put all Relationship members in their respective cache
    if (relationshipEntities.get(TRACKED_ENTITY_INSTANCE) != null) {
        teiDaoService.getTrackedEntityInstancesByUid(relationshipEntities.get(TRACKED_ENTITY_INSTANCE), user).forEach(tei -> trackedEntityInstanceCache.put(tei.getUid(), tei));
    }
    if (relationshipEntities.get(PROGRAM_INSTANCE) != null) {
        Query piQuery = Query.from(schemaService.getDynamicSchema(ProgramInstance.class));
        piQuery.setUser(user);
        piQuery.add(Restrictions.in("id", relationshipEntities.get(PROGRAM_INSTANCE)));
        queryService.query(piQuery).forEach(pi -> programInstanceCache.put(pi.getUid(), (ProgramInstance) pi));
    }
    if (relationshipEntities.get(PROGRAM_STAGE_INSTANCE) != null) {
        Query psiQuery = Query.from(schemaService.getDynamicSchema(ProgramStageInstance.class));
        psiQuery.setUser(user);
        psiQuery.add(Restrictions.in("id", relationshipEntities.get(PROGRAM_STAGE_INSTANCE)));
        queryService.query(psiQuery).forEach(psi -> programStageInstanceCache.put(psi.getUid(), (ProgramStageInstance) psi));
    }
}
Also used : EventService(org.hisp.dhis.dxf2.events.event.EventService) Restrictions(org.hisp.dhis.query.Restrictions) StringUtils(org.apache.commons.lang3.StringUtils) RelationshipUtils(org.hisp.dhis.commons.util.RelationshipUtils) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) RelationshipConstraint(org.hisp.dhis.relationship.RelationshipConstraint) PROGRAM_STAGE_INSTANCE(org.hisp.dhis.relationship.RelationshipEntity.PROGRAM_STAGE_INSTANCE) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) EnrollmentService(org.hisp.dhis.dxf2.events.enrollment.EnrollmentService) Query(org.hisp.dhis.query.Query) UserService(org.hisp.dhis.user.UserService) ListUtils(org.hisp.dhis.commons.collection.ListUtils) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) QueryService(org.hisp.dhis.query.QueryService) Event(org.hisp.dhis.dxf2.events.event.Event) ImportReportMode(org.hisp.dhis.dxf2.metadata.feedback.ImportReportMode) Objects(java.util.Objects) List(java.util.List) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) Optional(java.util.Optional) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) PagingAndSortingCriteriaAdapter(org.hisp.dhis.webapi.controller.event.webrequest.PagingAndSortingCriteriaAdapter) HashMap(java.util.HashMap) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Function(java.util.function.Function) PROGRAM_INSTANCE(org.hisp.dhis.relationship.RelationshipEntity.PROGRAM_INSTANCE) ArrayList(java.util.ArrayList) RelationshipParams(org.hisp.dhis.dxf2.events.RelationshipParams) Lists(com.google.common.collect.Lists) DbmsManager(org.hisp.dhis.dbms.DbmsManager) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) RelationshipType(org.hisp.dhis.relationship.RelationshipType) User(org.hisp.dhis.user.User) TRACKED_ENTITY_INSTANCE(org.hisp.dhis.relationship.RelationshipEntity.TRACKED_ENTITY_INSTANCE) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) RelationshipEntity(org.hisp.dhis.relationship.RelationshipEntity) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) CurrentUserService(org.hisp.dhis.user.CurrentUserService) DateUtils(org.hisp.dhis.util.DateUtils) Transactional(org.springframework.transaction.annotation.Transactional) Query(org.hisp.dhis.query.Query) HashMap(java.util.HashMap) ProgramInstance(org.hisp.dhis.program.ProgramInstance) RelationshipType(org.hisp.dhis.relationship.RelationshipType) List(java.util.List) ArrayList(java.util.ArrayList) RelationshipEntity(org.hisp.dhis.relationship.RelationshipEntity) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 4 with RelationshipEntity

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

the class AbstractRelationshipService method addRelationshipConstraintConflicts.

/**
 * Finds and adds any conflicts between relationship and relationship type
 *
 * @param constraint the constraint to check
 * @param relationshipItem the relationshipItem to check
 * @param relationshipUid the uid of the relationship
 */
private void addRelationshipConstraintConflicts(RelationshipConstraint constraint, org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem relationshipItem, String relationshipUid, ImportConflicts importConflicts) {
    RelationshipEntity entity = constraint.getRelationshipEntity();
    String itemUid = getUidOfRelationshipItem(relationshipItem);
    if (TRACKED_ENTITY_INSTANCE.equals(entity)) {
        TrackedEntityInstance tei = trackedEntityInstanceCache.get(itemUid);
        if (tei == null) {
            importConflicts.addConflict(relationshipUid, "TrackedEntityInstance '" + itemUid + "' not found.");
        } else if (!tei.getTrackedEntityType().equals(constraint.getTrackedEntityType())) {
            importConflicts.addConflict(relationshipUid, "TrackedEntityInstance '" + itemUid + "' has invalid TrackedEntityType.");
        }
    } else if (PROGRAM_INSTANCE.equals(entity)) {
        ProgramInstance pi = programInstanceCache.get(itemUid);
        if (pi == null) {
            importConflicts.addConflict(relationshipUid, "ProgramInstance '" + itemUid + "' not found.");
        } else if (!pi.getProgram().equals(constraint.getProgram())) {
            importConflicts.addConflict(relationshipUid, "ProgramInstance '" + itemUid + "' has invalid Program.");
        }
    } else if (PROGRAM_STAGE_INSTANCE.equals(entity)) {
        ProgramStageInstance psi = programStageInstanceCache.get(itemUid);
        if (psi == null) {
            importConflicts.addConflict(relationshipUid, "ProgramStageInstance '" + itemUid + "' not found.");
        } else {
            if (constraint.getProgram() != null && !psi.getProgramStage().getProgram().equals(constraint.getProgram())) {
                importConflicts.addConflict(relationshipUid, "ProgramStageInstance '" + itemUid + "' has invalid Program.");
            } else if (constraint.getProgramStage() != null && !psi.getProgramStage().equals(constraint.getProgramStage())) {
                importConflicts.addConflict(relationshipUid, "ProgramStageInstance '" + itemUid + "' has invalid ProgramStage.");
            }
        }
    }
}
Also used : ProgramInstance(org.hisp.dhis.program.ProgramInstance) RelationshipEntity(org.hisp.dhis.relationship.RelationshipEntity) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Aggregations

RelationshipEntity (org.hisp.dhis.relationship.RelationshipEntity)4 ProgramInstance (org.hisp.dhis.program.ProgramInstance)3 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)3 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)3 RelationshipItem (org.hisp.dhis.relationship.RelationshipItem)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Lists (com.google.common.collect.Lists)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 ListUtils (org.hisp.dhis.commons.collection.ListUtils)1 RelationshipUtils (org.hisp.dhis.commons.util.RelationshipUtils)1 DbmsManager (org.hisp.dhis.dbms.DbmsManager)1 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)1