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));
}
}
Aggregations