Search in sources :

Example 31 with Relationship

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());
        }
    }
}
Also used : PersonService(org.openmrs.api.PersonService) Relationship(org.openmrs.Relationship) HashSet(java.util.HashSet)

Example 32 with Relationship

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);
    }
}
Also used : Relationship(org.openmrs.Relationship) HL7Exception(ca.uhn.hl7v2.HL7Exception) RelationshipType(org.openmrs.RelationshipType) Person(org.openmrs.Person) HashSet(java.util.HashSet)

Example 33 with Relationship

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;
}
Also used : HashMap(java.util.HashMap) Relationship(org.openmrs.Relationship) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.openmrs.Person) Transactional(org.springframework.transaction.annotation.Transactional)

Example 34 with Relationship

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());
}
Also used : Relationship(org.openmrs.Relationship) RelationshipType(org.openmrs.RelationshipType) Patient(org.openmrs.Patient) Person(org.openmrs.Person) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Example 35 with Relationship

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"));
}
Also used : Errors(org.springframework.validation.Errors) Relationship(org.openmrs.Relationship) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

Relationship (org.openmrs.Relationship)55 Test (org.junit.Test)49 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)49 Person (org.openmrs.Person)19 Date (java.util.Date)14 Patient (org.openmrs.Patient)13 RelationshipType (org.openmrs.RelationshipType)13 HashMap (java.util.HashMap)7 PersonService (org.openmrs.api.PersonService)6 MapBindingResult (org.springframework.validation.MapBindingResult)6 Message (ca.uhn.hl7v2.model.Message)4 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)4 ORU_R01 (ca.uhn.hl7v2.model.v25.message.ORU_R01)3 NK1 (ca.uhn.hl7v2.model.v25.segment.NK1)3 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)2 HashSet (java.util.HashSet)2 List (java.util.List)2 User (org.openmrs.User)2 HL7Exception (ca.uhn.hl7v2.HL7Exception)1