use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class PatientServiceImpl method mergeRelationships.
private void mergeRelationships(Patient preferred, Patient notPreferred, PersonMergeLogData mergedData) {
// copy all relationships
PersonService personService = Context.getPersonService();
Set<String> existingRelationships = new HashSet<>();
// fill in the existing relationships with hashes
for (Relationship rel : personService.getRelationshipsByPerson(preferred)) {
existingRelationships.add(relationshipHash(rel, preferred));
}
// iterate over notPreferred's relationships and only copy them if they are needed
for (Relationship rel : personService.getRelationshipsByPerson(notPreferred)) {
if (!rel.getVoided()) {
boolean personAisPreferred = rel.getPersonA().equals(preferred);
boolean personAisNotPreferred = rel.getPersonA().equals(notPreferred);
boolean personBisPreferred = rel.getPersonB().equals(preferred);
boolean personBisNotPreferred = rel.getPersonB().equals(notPreferred);
String relHash = relationshipHash(rel, notPreferred);
if ((personAisPreferred && personBisNotPreferred) || (personBisPreferred && personAisNotPreferred)) {
// void this relationship if it's between the preferred and notPreferred patients
personService.voidRelationship(rel, "person " + (personAisNotPreferred ? "A" : "B") + " was merged to person " + (personAisPreferred ? "A" : "B"));
} else if (existingRelationships.contains(relHash)) {
// void this relationship if it already exists between preferred and the other side
personService.voidRelationship(rel, "person " + (personAisNotPreferred ? "A" : "B") + " was merged and a relationship already exists");
} else {
// copy this relationship and replace notPreferred with preferred
Relationship tmpRel = rel.copy();
if (personAisNotPreferred) {
tmpRel.setPersonA(preferred);
}
if (personBisNotPreferred) {
tmpRel.setPersonB(preferred);
}
log.debug("Copying relationship " + rel.getRelationshipId() + " to " + preferred.getPatientId());
Relationship persisted = personService.saveRelationship(tmpRel);
mergedData.addCreatedRelationship(persisted.getUuid());
// void the existing relationship to the notPreferred
personService.voidRelationship(rel, "person " + (personAisNotPreferred ? "A" : "B") + " was merged, relationship copied to #" + tmpRel.getRelationshipId());
// add the relationship hash to existing relationships
existingRelationships.add(relHash);
}
mergedData.addVoidedRelationship(rel.getUuid());
}
}
}
use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class ORUR01Handler method processNK1.
/**
* process an NK1 segment and add relationships if needed
*
* @param patient
* @param nk1
* @throws HL7Exception
* @should create a relationship from a NK1 segment
* @should not create a relationship if one exists
* @should create a person if the relative is not found
* @should fail if the coding system is not 99REL
* @should fail if the relationship identifier is formatted improperly
* @should fail if the relationship type is not found
*/
protected void processNK1(Patient patient, NK1 nk1) throws HL7Exception {
// guarantee we are working with our custom coding system
String relCodingSystem = nk1.getRelationship().getNameOfCodingSystem().getValue();
if (!relCodingSystem.equals(HL7Constants.HL7_LOCAL_RELATIONSHIP)) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.relationshipCoding", new Object[] { relCodingSystem }, null));
}
// get the relationship type identifier
String relIdentifier = nk1.getRelationship().getIdentifier().getValue();
// validate the format of the relationship identifier
if (!Pattern.matches("[0-9]+[AB]", relIdentifier)) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.relationshipType", new Object[] { relIdentifier }, null));
}
// get the type ID
Integer relTypeId;
try {
relTypeId = Integer.parseInt(relIdentifier.substring(0, relIdentifier.length() - 1));
} catch (NumberFormatException e) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.relationshipType", new Object[] { relIdentifier }, null));
}
// find the relationship type
RelationshipType relType = Context.getPersonService().getRelationshipType(relTypeId);
if (relType == null) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.relationshipTypeNotFound", new Object[] { relTypeId }, null));
}
// find the relative
Person relative = getRelative(nk1);
// determine if the patient is person A or B; the relIdentifier indicates
// the relative's side of the relationship, so the patient is the inverse
boolean patientIsPersonA = relIdentifier.endsWith("B");
boolean patientCanBeEitherPerson = relType.getbIsToA().equals(relType.getaIsToB());
// look at existing relationships to determine if a new one is needed
Set<Relationship> rels = new HashSet<>();
if (relative != null) {
if (patientCanBeEitherPerson || patientIsPersonA) {
rels.addAll(Context.getPersonService().getRelationships(patient, relative, relType));
}
if (patientCanBeEitherPerson || !patientIsPersonA) {
rels.addAll(Context.getPersonService().getRelationships(relative, patient, relType));
}
}
// create a relationship if none is found
if (rels.isEmpty()) {
// check the relative's existence
if (relative == null) {
// create one based on NK1 information
relative = Context.getHL7Service().createPersonFromNK1(nk1);
if (relative == null) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.relativeNotCreated"));
}
}
// create the relationship
Relationship relation = new Relationship();
if (patientCanBeEitherPerson || patientIsPersonA) {
relation.setPersonA(patient);
relation.setPersonB(relative);
} else {
relation.setPersonA(relative);
relation.setPersonB(patient);
}
relation.setRelationshipType(relType);
Context.getPersonService().saveRelationship(relation);
}
}
use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class PersonServiceImpl method getRelationshipMap.
/**
* @see org.openmrs.api.PersonService#getRelationshipMap(org.openmrs.RelationshipType)
*/
@Override
@Transactional(readOnly = true)
public Map<Person, List<Person>> getRelationshipMap(RelationshipType relType) throws APIException {
// get all relationships with this type
List<Relationship> relationships = Context.getPersonService().getRelationships(null, null, relType);
// the map to return
Map<Person, List<Person>> ret = new HashMap<>();
if (relationships != null) {
for (Relationship rel : relationships) {
Person from = rel.getPersonA();
Person to = rel.getPersonB();
List<Person> relList = ret.get(from);
if (relList == null) {
relList = new ArrayList<>();
}
relList.add(to);
ret.put(from, relList);
}
}
return ret;
}
use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class PatientServiceTest method mergePatients_shouldNotVoidRelationshipsForSameTypeAndSideWithDifferentRelatives.
/**
* @see PatientService#mergePatients(Patient,Patient)
*/
@Test
public void mergePatients_shouldNotVoidRelationshipsForSameTypeAndSideWithDifferentRelatives() throws Exception {
executeDataSet(PATIENT_RELATIONSHIPS_XML);
Patient preferred = patientService.getPatient(999);
Patient notPreferred = patientService.getPatient(2);
voidOrders(Collections.singleton(notPreferred));
// expected relationships before merge:
// * 2->1 (type 2)
// * 999->2 (type 5)
// * 999->1 (type 2)
// * 7->999 (type 4)
// * 502->2 (type 1)
// * 7->999 (type 1)
patientService.mergePatients(preferred, notPreferred);
// expected relationships after merge:
// * 999->1 (type 2)
// * 7->999 (type 4)
// * 502->999 (type 1)
// * 7->999 (type 1)
// check for relationships that should not be removed: 7->999 (type 4)
// and 7->999 (type 1)
List<Relationship> rels = personService.getRelationships(new Person(7), preferred, new RelationshipType(4));
assertEquals("7->999 (type 4) was removed", 1, rels.size());
rels = personService.getRelationships(new Person(7), preferred, new RelationshipType(1));
assertEquals("7->999 (type 1) was removed", 1, rels.size());
}
use of org.openmrs.Relationship in project openmrs-core by openmrs.
the class RelationshipValidatorTest method validate_shouldFailValidationIfFieldLengthsAreNotCorrect.
/**
* @see RelationshipValidator#validate(Object,Errors)
*/
@Test
public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() {
Relationship relationship = new Relationship(1);
relationship.setVoidReason("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text");
Errors errors = new BindException(relationship, "relationship");
new RelationshipValidator().validate(relationship, errors);
Assert.assertEquals(true, errors.hasFieldErrors("voidReason"));
}
Aggregations