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