Search in sources :

Example 16 with RelationshipType

use of org.openmrs.RelationshipType in project openmrs-core by openmrs.

the class ORUR01HandlerTest method processNK1_shouldCreateAPersonIfTheRelativeIsNotFound.

/**
 * @see ORUR01Handler#processNK1(Patient,NK1)
 */
@Test
public void processNK1_shouldCreateAPersonIfTheRelativeIsNotFound() throws Exception {
    // process a message with an invalid relative identifier
    PersonService personService = Context.getPersonService();
    // the patient that is the focus of
    Patient patient = new Patient(3);
    // this hl7 message
    String hl7String = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20090728170332||ORU^R01|gu99yBh4loLX2mh9cHaV|P|2.5|1||||||||4^AMRS.ELD.FORMID\r" + "PID|||3^^^^||Beren^John^Bondo||\r" + "NK1|1|Jones^Jane^Lee^^RN|3A^Parent^99REL||||||||||||F|19751016|||||||||||||||||2178037d-f86b-4f12-8d8b-be3ebc220029^^^UUID^v4\r" + "PV1||O|1^Unknown||||1^Super User (admin)|||||||||||||||||||||||||||||||||||||20090714|||||||V\r" + "ORC|RE||||||||20090728165937|1^Super User\r" + "OBR|1|||1238^MEDICAL RECORD OBSERVATIONS^99DCT\r" + "OBX|2|NM|5497^CD4 COUNT^99DCT||123|||||||||20090714\r" + "OBR|3|||23^FOOD CONSTRUCT^99DCT\r" + "OBX|1|CWE|21^FOOD ASSISTANCE FOR ENTIRE FAMILY^99DCT||22^UNKNOWN^99DCT^2471^UNKNOWN^99NAM|||||||||20090714";
    ORUR01Handler oruHandler = new ORUR01Handler();
    Message hl7message = parser.parse(hl7String);
    ORU_R01 oru = (ORU_R01) hl7message;
    List<NK1> nk1List = oruHandler.getNK1List(oru);
    for (NK1 nk1 : nk1List) oruHandler.processNK1(patient, nk1);
    // find the relative in the database
    Person relative = personService.getPersonByUuid("2178037d-f86b-4f12-8d8b-be3ebc220029");
    Assert.assertNotNull("a new person was not created", relative);
    // see if the relative made it into the relationship properly
    List<Relationship> rels = personService.getRelationships(relative, patient, new RelationshipType(3));
    Assert.assertTrue("new relationship was not created", !rels.isEmpty() && rels.size() == 1);
}
Also used : Message(ca.uhn.hl7v2.model.Message) ORU_R01(ca.uhn.hl7v2.model.v25.message.ORU_R01) NK1(ca.uhn.hl7v2.model.v25.segment.NK1) PersonService(org.openmrs.api.PersonService) Relationship(org.openmrs.Relationship) RelationshipType(org.openmrs.RelationshipType) Patient(org.openmrs.Patient) Person(org.openmrs.Person) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 17 with RelationshipType

use of org.openmrs.RelationshipType in project openmrs-core by openmrs.

the class PatientServiceTest method mergePatients_shouldNotCreateDuplicateRelationships.

/**
 * @see PatientService#mergePatients(Patient,Patient)
 */
@Test
public void mergePatients_shouldNotCreateDuplicateRelationships() 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->2 (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 a relationship that should not be duplicated: 2->1 and
    // 999->1
    List<Relationship> rels = personService.getRelationships(preferred, new Person(1), new RelationshipType(2));
    assertEquals("duplicate relationships were not 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 18 with RelationshipType

use of org.openmrs.RelationshipType in project openmrs-module-coreapps by openmrs.

the class EditProviderPageController method get.

public void get(PageModel model, @MethodParam("getAccount") AccountDomainWrapper account, @ModelAttribute("patientId") @BindParams Patient patient, @SpringBean("patientService") PatientService patientService, @SpringBean("accountService") AccountService accountService, @SpringBean("adminService") AdministrationService administrationService, @SpringBean("providerManagementService") ProviderManagementService providerManagementService) throws PersonIsNotProviderException, InvalidRelationshipTypeException, SuggestionEvaluationException {
    model.addAttribute("account", account);
    List<ProviderRole> allProviderRoles = providerManagementService.getRestrictedProviderRoles(false);
    model.addAttribute("providerRoles", allProviderRoles);
    List<ProviderPersonRelationship> patientsList = new ArrayList<ProviderPersonRelationship>();
    List<RelationshipType> relationshipTypes = new ArrayList<RelationshipType>();
    Set<ProviderAttributeType> providerAttributeTypes = new HashSet<ProviderAttributeType>();
    List<ProviderPersonRelationship> supervisorsForProvider = null;
    List<ProviderPersonRelationship> superviseesForSupervisor = null;
    boolean isSupervisor = false;
    Provider provider = account.getProvider();
    if (provider != null) {
        if (!provider.getProviderRole().getSuperviseeProviderRoles().isEmpty()) {
            isSupervisor = true;
        }
        supervisorsForProvider = ProviderManagementUtils.getSupervisors(provider);
        superviseesForSupervisor = ProviderManagementUtils.getSupervisees(provider);
        ProviderRole providerRole = provider.getProviderRole();
        if (providerRole != null && providerRole.getRelationshipTypes() != null) {
            providerAttributeTypes = providerRole.getProviderAttributeTypes();
            Set<ProviderAttribute> attributes = provider.getAttributes();
            for (ProviderAttribute attribute : attributes) {
                // remove from the list of Attribute Types the ones that are already entered for this provider
                providerAttributeTypes.remove(attribute.getAttributeType());
            }
            for (RelationshipType relationshipType : provider.getProviderRole().getRelationshipTypes()) {
                if (!relationshipType.isRetired()) {
                    relationshipTypes.add(relationshipType);
                }
            }
            patientsList = ProviderManagementUtils.getAssignedPatients(provider);
        }
    } else {
        for (ProviderRole providerRole : allProviderRoles) {
            providerAttributeTypes.addAll(providerRole.getProviderAttributeTypes());
        }
    }
    boolean useAddressHierarchy = false;
    if (ModuleFactory.isModuleStarted("addresshierarchy")) {
        useAddressHierarchy = true;
    }
    model.addAttribute("relationshipTypes", relationshipTypes);
    model.addAttribute("patientsList", patientsList);
    model.addAttribute("providerAttributeTypes", providerAttributeTypes);
    model.addAttribute("isSupervisor", isSupervisor);
    model.addAttribute("supervisorsForProvider", supervisorsForProvider);
    model.addAttribute("superviseesForSupervisor", superviseesForSupervisor);
    model.addAttribute("superviseesForSupervisor", superviseesForSupervisor);
    model.addAttribute("useAddressHierarchy", useAddressHierarchy);
}
Also used : ProviderRole(org.openmrs.module.providermanagement.ProviderRole) ArrayList(java.util.ArrayList) RelationshipType(org.openmrs.RelationshipType) ProviderPersonRelationship(org.openmrs.module.providermanagement.relationship.ProviderPersonRelationship) Provider(org.openmrs.module.providermanagement.Provider) ProviderAttribute(org.openmrs.ProviderAttribute) ProviderAttributeType(org.openmrs.ProviderAttributeType) HashSet(java.util.HashSet)

Example 19 with RelationshipType

use of org.openmrs.RelationshipType in project openmrs-module-pihcore by PIH.

the class ProviderRoleBundle method install.

/**
 * Performs the installation of the metadata items
 *
 * @throws Exception if an error occurs
 */
@Override
public void install() throws Exception {
    Set<RelationshipType> relationshipTypes = null;
    RelationshipType vhwToPatient = personService.getRelationshipTypeByUuid(RelationshipTypeBundle.RelationshipTypes.CHW_TO_PATIENT);
    if (vhwToPatient != null) {
        relationshipTypes = new HashSet<RelationshipType>();
        relationshipTypes.add(vhwToPatient);
    }
    Set<ProviderAttributeType> providerAttributes = new HashSet<ProviderAttributeType>();
    ProviderAttributeType providerAttributeType = providerService.getProviderAttributeTypeByUuid(ProviderAttributeTypeBundle.ProviderAttributeTypes.DATE_HIRED);
    if (providerAttributeType != null) {
        providerAttributes.add(providerAttributeType);
    }
    providerAttributeType = providerService.getProviderAttributeTypeByUuid(ProviderAttributeTypeBundle.ProviderAttributeTypes.NUMBER_OF_HOUSEHOLDS);
    if (providerAttributeType != null) {
        providerAttributes.add(providerAttributeType);
    }
    ProviderRole vhwSupervisee = install(providerRole("CHW", null, relationshipTypes, providerAttributes, ProviderRoles.CHW));
    Set<ProviderRole> superviseeRoles = null;
    if (vhwSupervisee != null) {
        superviseeRoles = new HashSet<ProviderRole>();
        superviseeRoles.add(vhwSupervisee);
    }
    install(providerRole("CHW Supervisor", superviseeRoles, relationshipTypes, providerAttributes, ProviderRoles.CHW_SUPERVISOR));
}
Also used : RelationshipType(org.openmrs.RelationshipType) ProviderRole(org.openmrs.module.providermanagement.ProviderRole) ProviderAttributeType(org.openmrs.ProviderAttributeType) HashSet(java.util.HashSet)

Example 20 with RelationshipType

use of org.openmrs.RelationshipType 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)

Aggregations

RelationshipType (org.openmrs.RelationshipType)35 Test (org.junit.Test)29 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)29 Relationship (org.openmrs.Relationship)13 Person (org.openmrs.Person)8 BindException (org.springframework.validation.BindException)7 Errors (org.springframework.validation.Errors)7 Patient (org.openmrs.Patient)6 PersonService (org.openmrs.api.PersonService)5 Message (ca.uhn.hl7v2.model.Message)4 Date (java.util.Date)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 HashSet (java.util.HashSet)3 List (java.util.List)3 ProviderAttributeType (org.openmrs.ProviderAttributeType)2 Role (org.openmrs.Role)2 User (org.openmrs.User)2 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)2