use of org.hisp.dhis.relationship.RelationshipItem in project dhis2-core by dhis2.
the class RelationshipSMSListener method postProcess.
@Override
protected SmsResponse postProcess(IncomingSms sms, SmsSubmission submission) throws SMSProcessingException {
RelationshipSmsSubmission subm = (RelationshipSmsSubmission) submission;
Uid fromid = subm.getFrom();
Uid toid = subm.getTo();
Uid typeid = subm.getRelationshipType();
RelationshipType relType = relationshipTypeService.getRelationshipType(typeid.getUid());
if (relType == null) {
throw new SMSProcessingException(SmsResponse.INVALID_RELTYPE.set(typeid));
}
RelationshipItem fromItem = createRelationshipItem(relType, RelationshipDir.FROM, fromid);
RelationshipItem toItem = createRelationshipItem(relType, RelationshipDir.TO, toid);
Relationship rel = new Relationship();
// auto-generated
if (subm.getRelationship() != null) {
rel.setUid(subm.getRelationship().getUid());
}
rel.setRelationshipType(relType);
rel.setFrom(fromItem);
rel.setTo(toItem);
rel.setCreated(new Date());
rel.setLastUpdated(new Date());
// TODO: Are there values we need to account for in relationships?
relationshipService.addRelationship(rel);
return SmsResponse.SUCCESS;
}
use of org.hisp.dhis.relationship.RelationshipItem in project dhis2-core by dhis2.
the class DeduplicationHelperTest method shouldFailGetDuplicateRelationshipErrorWithDuplicateRelationshipsWithTeisBidirectional.
@Test
void shouldFailGetDuplicateRelationshipErrorWithDuplicateRelationshipsWithTeisBidirectional() {
TrackedEntityInstance teiA = getTeiA();
TrackedEntityInstance teiB = getTeiB();
TrackedEntityInstance teiC = getTeiC();
// A->C, B->C
RelationshipItem fromA = new RelationshipItem();
RelationshipItem toA = new RelationshipItem();
RelationshipItem fromB = new RelationshipItem();
RelationshipItem toB = new RelationshipItem();
fromA.setTrackedEntityInstance(teiC);
toA.setTrackedEntityInstance(teiA);
fromB.setTrackedEntityInstance(teiB);
toB.setTrackedEntityInstance(teiC);
Relationship relA = new Relationship();
Relationship relB = new Relationship();
relA.setAutoFields();
relB.setAutoFields();
relA.setRelationshipType(relationshipTypeBidirectional);
relB.setRelationshipType(relationshipTypeBidirectional);
relA.setFrom(fromA);
relA.setTo(toA);
relB.setFrom(fromB);
relB.setTo(toB);
fromA.setRelationship(relA);
toA.setRelationship(relA);
fromB.setRelationship(relB);
toB.setRelationship(relB);
teiA.getRelationshipItems().add(fromA);
teiB.getRelationshipItems().add(fromB);
assertNotNull(deduplicationHelper.getDuplicateRelationshipError(teiA, teiB.getRelationshipItems().stream().map(RelationshipItem::getRelationship).collect(Collectors.toSet())));
}
use of org.hisp.dhis.relationship.RelationshipItem in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method getTei.
private TrackedEntityInstance getTei(org.hisp.dhis.trackedentity.TrackedEntityInstance daoTrackedEntityInstance, Set<TrackedEntityAttribute> readableAttributes, TrackedEntityInstanceParams params, User user) {
if (daoTrackedEntityInstance == null) {
return null;
}
TrackedEntityInstance trackedEntityInstance = new TrackedEntityInstance();
trackedEntityInstance.setTrackedEntityInstance(daoTrackedEntityInstance.getUid());
trackedEntityInstance.setOrgUnit(daoTrackedEntityInstance.getOrganisationUnit().getUid());
trackedEntityInstance.setTrackedEntityType(daoTrackedEntityInstance.getTrackedEntityType().getUid());
trackedEntityInstance.setCreated(DateUtils.getIso8601NoTz(daoTrackedEntityInstance.getCreated()));
trackedEntityInstance.setCreatedAtClient(DateUtils.getIso8601NoTz(daoTrackedEntityInstance.getCreatedAtClient()));
trackedEntityInstance.setLastUpdated(DateUtils.getIso8601NoTz(daoTrackedEntityInstance.getLastUpdated()));
trackedEntityInstance.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(daoTrackedEntityInstance.getLastUpdatedAtClient()));
trackedEntityInstance.setInactive(Optional.ofNullable(daoTrackedEntityInstance.isInactive()).orElse(false));
trackedEntityInstance.setGeometry(daoTrackedEntityInstance.getGeometry());
trackedEntityInstance.setDeleted(daoTrackedEntityInstance.isDeleted());
trackedEntityInstance.setPotentialDuplicate(daoTrackedEntityInstance.isPotentialDuplicate());
trackedEntityInstance.setStoredBy(daoTrackedEntityInstance.getStoredBy());
trackedEntityInstance.setCreatedByUserInfo(daoTrackedEntityInstance.getCreatedByUserInfo());
trackedEntityInstance.setLastUpdatedByUserInfo(daoTrackedEntityInstance.getLastUpdatedByUserInfo());
if (daoTrackedEntityInstance.getGeometry() != null) {
Geometry geometry = daoTrackedEntityInstance.getGeometry();
FeatureType featureType = FeatureType.getTypeFromName(geometry.getGeometryType());
trackedEntityInstance.setFeatureType(featureType);
trackedEntityInstance.setCoordinates(GeoUtils.getCoordinatesFromGeometry(geometry));
}
if (params.isIncludeRelationships()) {
for (RelationshipItem relationshipItem : daoTrackedEntityInstance.getRelationshipItems()) {
org.hisp.dhis.relationship.Relationship daoRelationship = relationshipItem.getRelationship();
if (trackerAccessManager.canRead(user, daoRelationship).isEmpty()) {
Relationship relationship = relationshipService.getRelationship(relationshipItem.getRelationship(), RelationshipParams.FALSE, user);
trackedEntityInstance.getRelationships().add(relationship);
}
}
}
if (params.isIncludeEnrollments()) {
for (ProgramInstance programInstance : daoTrackedEntityInstance.getProgramInstances()) {
if (trackerAccessManager.canRead(user, programInstance, false).isEmpty() && (params.isIncludeDeleted() || !programInstance.isDeleted())) {
trackedEntityInstance.getEnrollments().add(enrollmentService.getEnrollment(user, programInstance, params, true));
}
}
}
if (params.isIncludeProgramOwners()) {
for (TrackedEntityProgramOwner programOwner : daoTrackedEntityInstance.getProgramOwners()) {
trackedEntityInstance.getProgramOwners().add(new ProgramOwner(programOwner));
}
}
Set<TrackedEntityAttribute> readableAttributesCopy = filterOutSkipSyncAttributesIfApplies(params, trackedEntityInstance, readableAttributes);
for (TrackedEntityAttributeValue attributeValue : daoTrackedEntityInstance.getTrackedEntityAttributeValues()) {
if (readableAttributesCopy.contains(attributeValue.getAttribute())) {
Attribute attribute = new Attribute();
attribute.setCreated(DateUtils.getIso8601NoTz(attributeValue.getCreated()));
attribute.setLastUpdated(DateUtils.getIso8601NoTz(attributeValue.getLastUpdated()));
attribute.setDisplayName(attributeValue.getAttribute().getDisplayName());
attribute.setAttribute(attributeValue.getAttribute().getUid());
attribute.setValueType(attributeValue.getAttribute().getValueType());
attribute.setCode(attributeValue.getAttribute().getCode());
attribute.setValue(attributeValue.getValue());
attribute.setStoredBy(attributeValue.getStoredBy());
attribute.setSkipSynchronization(attributeValue.getAttribute().getSkipSynchronization());
trackedEntityInstance.getAttributes().add(attribute);
}
}
return trackedEntityInstance;
}
use of org.hisp.dhis.relationship.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;
}
use of org.hisp.dhis.relationship.RelationshipItem in project dhis2-core by dhis2.
the class RelationshipStoreTest method getByProgramStageInstance.
@Test
void getByProgramStageInstance() {
Program programA = createProgram('A', new HashSet<>(), organisationUnit);
programService.addProgram(programA);
ProgramInstance programInstance = new ProgramInstance();
programInstance.setProgram(programA);
programInstance.setAutoFields();
programInstance.setEnrollmentDate(new Date());
programInstance.setIncidentDate(new Date());
programInstance.setStatus(ProgramStatus.ACTIVE);
programInstanceService.addProgramInstance(programInstance);
ProgramStage programStageA = createProgramStage('S', programA);
programStageA.setProgram(programA);
programStageService.saveProgramStage(programStageA);
programA.getProgramStages().add(programStageA);
ProgramStageInstance programStageInstance = new ProgramStageInstance();
programStageInstance.setOrganisationUnit(organisationUnit);
programStageInstance.setProgramStage(programStageA);
programStageInstance.setProgramInstance(programInstance);
programStageInstance.setAutoFields();
programStageInstanceService.addProgramStageInstance(programStageInstance);
RelationshipItem relationshipItemFrom = new RelationshipItem();
relationshipItemFrom.setTrackedEntityInstance(trackedEntityInstanceA);
RelationshipItem relationshipItemTo = new RelationshipItem();
relationshipItemTo.setProgramStageInstance(programStageInstance);
Relationship relationshipA = new Relationship();
relationshipA.setRelationshipType(relationshipType);
relationshipA.setFrom(relationshipItemFrom);
relationshipA.setTo(relationshipItemTo);
relationshipA.setKey(RelationshipUtils.generateRelationshipKey(relationshipA));
relationshipA.setInvertedKey(RelationshipUtils.generateRelationshipInvertedKey(relationshipA));
relationshipService.addRelationship(relationshipA);
List<Relationship> relationshipList = relationshipService.getRelationshipsByProgramStageInstance(programStageInstance, true);
assertEquals(1, relationshipList.size());
assertTrue(relationshipList.contains(relationshipA));
assertTrue(relationshipService.getRelationshipByRelationship(relationshipA).isPresent());
}
Aggregations