Search in sources :

Example 1 with PROGRAM_INSTANCE

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

Aggregations

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 RelationshipParams (org.hisp.dhis.dxf2.events.RelationshipParams)1 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)1 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)1 EnrollmentService (org.hisp.dhis.dxf2.events.enrollment.EnrollmentService)1 Event (org.hisp.dhis.dxf2.events.event.Event)1