Search in sources :

Example 36 with OBR

use of ca.uhn.hl7v2.model.v26.segment.OBR in project openmrs-core by openmrs.

the class ORUR01HandlerTest method processMessage_shouldSetValue_NumericForObsIfQuestionDatatypeIsNumeric.

/**
 * @see ORUR01Handler#processMessage(Message)
 */
@Test
public void processMessage_shouldSetValue_NumericForObsIfQuestionDatatypeIsNumeric() throws Exception {
    ObsService os = Context.getObsService();
    String hl7string = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20080226102656||ORU^R01|JqnfhKKtouEz8kzTk6Zo|P|2.5|1||||||||16^AMRS.ELD.FORMID\r" + "PID|||7^^^^||Collet^Test^Chebaskwony||\r" + "PV1||O|1^Unknown Location||||1^Super User (1-8)|||||||||||||||||||||||||||||||||||||20080212|||||||V\r" + "ORC|RE||||||||20080226102537|1^Super User\r" + "OBR|1|||1238^MEDICAL RECORD OBSERVATIONS^99DCT\r" + "OBX|1|NM|5497^CD4, BY FACS^99DCT||450|||||||||20080206";
    // the expected question for the obs in the hl7 message has to be
    // numeric
    Assert.assertEquals("Numeric", Context.getConceptService().getConcept(5497).getDatatype().getName());
    List<Obs> oldList = os.getObservationsByPersonAndConcept(new Person(7), new Concept(5497));
    Message hl7message = parser.parse(hl7string);
    router.processMessage(hl7message);
    List<Obs> newList = os.getObservationsByPersonAndConcept(new Person(7), new Concept(5497));
    Obs newObservation = null;
    for (Obs newObs : newList) {
        if (!oldList.contains(newObs) && !newObs.isObsGrouping()) {
            newObservation = newObs;
        }
    }
    Assert.assertEquals(450, newObservation.getValueNumeric().intValue());
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) Message(ca.uhn.hl7v2.model.Message) ObsService(org.openmrs.api.ObsService) Person(org.openmrs.Person) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 37 with OBR

use of ca.uhn.hl7v2.model.v26.segment.OBR in project openmrs-core by openmrs.

the class ORUR01HandlerTest method processNK1_shouldFailIfTheRelationshipIdentifierIsFormattedImproperly.

/**
 * @see ORUR01Handler#processNK1(Patient,NK1)
 */
@Test(expected = HL7Exception.class)
public void processNK1_shouldFailIfTheRelationshipIdentifierIsFormattedImproperly() throws Exception {
    // process a message with an invalid relationship identifier format
    // 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|3C^Parent^99REL||||||||||||F|19751016|||||||||||||||||2^^^L^PI\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);
}
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) Patient(org.openmrs.Patient) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 38 with OBR

use of ca.uhn.hl7v2.model.v26.segment.OBR in project openmrs-core by openmrs.

the class ORUR01HandlerTest method processMessage_shouldCreateAnEncounterAndFindTheProviderByUuid.

/**
 * @see ORUR01Handler#processMessage(Message)
 */
@Test
public void processMessage_shouldCreateAnEncounterAndFindTheProviderByUuid() throws Exception {
    executeDataSet("org/openmrs/api/include/ProviderServiceTest-initial.xml");
    int patientId = 2;
    Patient patient = new Patient(patientId);
    List<Encounter> encForPatient1 = Context.getEncounterService().getEncountersByPatient(patient);
    ProviderService ps = Context.getProviderService();
    Provider provider = ps.getProviderByIdentifier("8a760");
    String hl7string = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20080226102656||ORU^R01|JqnfhKKtouEz8kzTk6Zo|P|2.5|1||||||||16^AMRS.ELD.FORMID\r" + "PID|||" + patientId + "^^^^||Hornblower^Horatio^Test||\r" + "PV1||O|1^Unknown Location||||ba4781f4-6b94-11e0-93c3-18a905e044dc^name^^^^^^^&" + HL7Constants.PROVIDER_ASSIGNING_AUTH_PROV_UUID + "&L|||||||||||||||||||||||||||||||||||||20080212|||||||V\r" + "ORC|RE||||||||20080226102537|1^Super User\r" + "OBR|1|||1238^MEDICAL RECORD OBSERVATIONS^99DCT";
    Message hl7message = parser.parse(hl7string);
    router.processMessage(hl7message);
    // check for the new encounter
    List<Encounter> encForPatient2 = Context.getEncounterService().getEncountersByPatient(patient);
    encForPatient2.removeAll(encForPatient1);
    Assert.assertTrue(encForPatient2.size() == 1);
    Provider newProvider = encForPatient2.get(0).getProvidersByRole(Context.getEncounterService().getEncounterRoleByUuid(EncounterRole.UNKNOWN_ENCOUNTER_ROLE_UUID)).iterator().next();
    Assert.assertEquals(4, newProvider.getId().intValue());
}
Also used : ProviderService(org.openmrs.api.ProviderService) Message(ca.uhn.hl7v2.model.Message) Patient(org.openmrs.Patient) Encounter(org.openmrs.Encounter) Provider(org.openmrs.Provider) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 39 with OBR

use of ca.uhn.hl7v2.model.v26.segment.OBR in project openmrs-core by openmrs.

the class ORUR01Handler method processORU_R01.

/**
 * Bulk of the processing done here. Called by the main processMessage method
 *
 * @param oru the message to process
 * @return the processed message
 * @throws HL7Exception
 * @should process multiple NK1 segments
 */
private Message processORU_R01(ORU_R01 oru) throws HL7Exception {
    // TODO: ideally, we would branch or alter our behavior based on the
    // sending application.
    // validate message
    validate(oru);
    // extract segments for convenient use below
    MSH msh = getMSH(oru);
    PID pid = getPID(oru);
    List<NK1> nk1List = getNK1List(oru);
    PV1 pv1 = getPV1(oru);
    // we're using the ORC assoc with first OBR to
    ORC orc = getORC(oru);
    // hold data enterer and date entered for now
    // Obtain message control id (unique ID for message from sending
    // application)
    String messageControlId = msh.getMessageControlID().getValue();
    if (log.isDebugEnabled()) {
        log.debug("Found HL7 message in inbound queue with control id = " + messageControlId);
    }
    // create the encounter
    Patient patient = getPatient(pid);
    if (log.isDebugEnabled()) {
        log.debug("Processing HL7 message for patient " + patient.getPatientId());
    }
    Encounter encounter = createEncounter(msh, patient, pv1, orc);
    // do the discharge to location logic
    try {
        updateHealthCenter(patient, pv1);
    } catch (Exception e) {
        log.error("Error while processing Discharge To Location (" + messageControlId + ")", e);
    }
    // process NK1 (relationship) segments
    for (NK1 nk1 : nk1List) {
        processNK1(patient, nk1);
    }
    // list of concepts proposed in the obs of this encounter.
    // these proposals need to be created after the encounter
    // has been created
    List<ConceptProposal> conceptProposals = new ArrayList<>();
    // create observations
    if (log.isDebugEnabled()) {
        log.debug("Creating observations for message " + messageControlId + "...");
    }
    // we ignore all MEDICAL_RECORD_OBSERVATIONS that are OBRs.  We do not
    // create obs_groups for them
    List<Integer> ignoredConceptIds = new ArrayList<>();
    String obrConceptId = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_MEDICAL_RECORD_OBSERVATIONS, "1238");
    if (StringUtils.hasLength(obrConceptId)) {
        ignoredConceptIds.add(Integer.valueOf(obrConceptId));
    }
    // we also ignore all PROBLEM_LIST that are OBRs
    String obrProblemListConceptId = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PROBLEM_LIST, "1284");
    if (StringUtils.hasLength(obrProblemListConceptId)) {
        ignoredConceptIds.add(Integer.valueOf(obrProblemListConceptId));
    }
    ORU_R01_PATIENT_RESULT patientResult = oru.getPATIENT_RESULT();
    int numObr = patientResult.getORDER_OBSERVATIONReps();
    for (int i = 0; i < numObr; i++) {
        if (log.isDebugEnabled()) {
            log.debug("Processing OBR (" + i + " of " + numObr + ")");
        }
        ORU_R01_ORDER_OBSERVATION orderObs = patientResult.getORDER_OBSERVATION(i);
        // the parent obr
        OBR obr = orderObs.getOBR();
        if (!StringUtils.hasText(obr.getUniversalServiceIdentifier().getIdentifier().getValue())) {
            throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.errorInvalidOBR ", new Object[] { messageControlId }, null));
        }
        // if we're not ignoring this obs group, create an
        // Obs grouper object that the underlying obs objects will use
        Obs obsGrouper = null;
        Concept obrConcept = getConcept(obr.getUniversalServiceIdentifier(), messageControlId);
        if (obrConcept != null && !ignoredConceptIds.contains(obrConcept.getId())) {
            // maybe check for a parent obs group from OBR-29 Parent ?
            // create an obs for this obs group too
            obsGrouper = new Obs();
            obsGrouper.setConcept(obrConcept);
            obsGrouper.setPerson(encounter.getPatient());
            obsGrouper.setEncounter(encounter);
            Date datetime = getDatetime(obr);
            if (datetime == null) {
                datetime = encounter.getEncounterDatetime();
            }
            obsGrouper.setObsDatetime(datetime);
            obsGrouper.setLocation(encounter.getLocation());
            obsGrouper.setCreator(encounter.getCreator());
            // set comments if there are any
            StringBuilder comments = new StringBuilder();
            ORU_R01_ORDER_OBSERVATION parent = (ORU_R01_ORDER_OBSERVATION) obr.getParent();
            int totalNTEs = parent.getNTEReps();
            for (int iNTE = 0; iNTE < totalNTEs; iNTE++) {
                for (FT obxComment : parent.getNTE(iNTE).getComment()) {
                    if (comments.length() > 0) {
                        comments.append(" ");
                    }
                    comments.append(obxComment.getValue());
                }
            }
            // only set comments if there are any
            if (StringUtils.hasText(comments.toString())) {
                obsGrouper.setComment(comments.toString());
            }
            // add this obs as another row in the obs table
            encounter.addObs(obsGrouper);
        }
        // loop over the obs and create each object, adding it to the encounter
        int numObs = orderObs.getOBSERVATIONReps();
        HL7Exception errorInHL7Queue = null;
        for (int j = 0; j < numObs; j++) {
            if (log.isDebugEnabled()) {
                log.debug("Processing OBS (" + j + " of " + numObs + ")");
            }
            OBX obx = orderObs.getOBSERVATION(j).getOBX();
            try {
                log.debug("Parsing observation");
                Obs obs = parseObs(encounter, obx, obr, messageControlId);
                if (obs != null) {
                    // the creator/dateCreated from the encounter
                    if (encounter.getEncounterId() != null) {
                        obs.setCreator(getEnterer(orc));
                        obs.setDateCreated(new Date());
                    }
                    // set the obsGroup on this obs
                    if (obsGrouper != null) {
                        // set the obs to the group.  This assumes the group is already
                        // on the encounter and that when the encounter is saved it will
                        // propagate to the children obs
                        obsGrouper.addGroupMember(obs);
                    } else {
                        // set this obs on the encounter object that we
                        // will be saving later
                        log.debug("Obs is not null. Adding to encounter object");
                        encounter.addObs(obs);
                        log.debug("Done with this obs");
                    }
                }
            } catch (ProposingConceptException proposingException) {
                Concept questionConcept = proposingException.getConcept();
                String value = proposingException.getValueName();
                // if the sender never specified any text for the proposed concept
                if (!StringUtils.isEmpty(value)) {
                    conceptProposals.add(createConceptProposal(encounter, questionConcept, value));
                } else {
                    errorInHL7Queue = new HL7Exception(Context.getMessageSourceService().getMessage("Hl7.proposed.concept.name.empty"), proposingException);
                    // stop any further processing of current message
                    break;
                }
            } catch (HL7Exception e) {
                errorInHL7Queue = e;
            } finally {
                // Handle obs-level exceptions
                if (errorInHL7Queue != null) {
                    throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.improperlyFormattedOBX", new Object[] { PipeParser.encode(obx, new EncodingCharacters('|', "^~\\&")) }, null), HL7Exception.DATA_TYPE_ERROR, errorInHL7Queue);
                }
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Finished creating observations");
        log.debug("Current thread: " + Thread.currentThread());
        log.debug("Creating the encounter object");
    }
    Context.getEncounterService().saveEncounter(encounter);
    // now that the encounter is saved
    for (ConceptProposal proposal : conceptProposals) {
        Context.getConceptService().saveConceptProposal(proposal);
    }
    return oru;
}
Also used : ORC(ca.uhn.hl7v2.model.v25.segment.ORC) ArrayList(java.util.ArrayList) FT(ca.uhn.hl7v2.model.v25.datatype.FT) NK1(ca.uhn.hl7v2.model.v25.segment.NK1) Encounter(org.openmrs.Encounter) OBR(ca.uhn.hl7v2.model.v25.segment.OBR) EncodingCharacters(ca.uhn.hl7v2.parser.EncodingCharacters) Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) MSH(ca.uhn.hl7v2.model.v25.segment.MSH) ORU_R01_PATIENT_RESULT(ca.uhn.hl7v2.model.v25.group.ORU_R01_PATIENT_RESULT) OBX(ca.uhn.hl7v2.model.v25.segment.OBX) Patient(org.openmrs.Patient) PID(ca.uhn.hl7v2.model.v25.segment.PID) PV1(ca.uhn.hl7v2.model.v25.segment.PV1) DataTypeException(ca.uhn.hl7v2.model.DataTypeException) HL7Exception(ca.uhn.hl7v2.HL7Exception) ApplicationException(ca.uhn.hl7v2.app.ApplicationException) Date(java.util.Date) ORU_R01_ORDER_OBSERVATION(ca.uhn.hl7v2.model.v25.group.ORU_R01_ORDER_OBSERVATION) ConceptProposal(org.openmrs.ConceptProposal) HL7Exception(ca.uhn.hl7v2.HL7Exception)

Example 40 with OBR

use of ca.uhn.hl7v2.model.v26.segment.OBR in project openmrs-core by openmrs.

the class HL7ServiceTest method resolveLocationId_shouldReturnInternalIdentifierOfLocationIfOnlyLocationIdIsSpecified.

/**
 * @throws HL7Exception
 * @see HL7Service#resolveLocationId(ca.uhn.hl7v2.model.v25.datatype.PL)
 */
@Test
public void resolveLocationId_shouldReturnInternalIdentifierOfLocationIfOnlyLocationIdIsSpecified() throws HL7Exception {
    executeDataSet("org/openmrs/hl7/include/ORUTest-initialData.xml");
    HL7Service hl7service = Context.getHL7Service();
    Message message = hl7service.parseHL7String("MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20080226102656||ORU^R01|JqnfhKKtouEz8kzTk6Zo|P|2.5|1||||||||16^AMRS.ELD.FORMID\r" + "PID|||3^^^^||John3^Doe^||\r" + "NK1|1|Hornblower^Horatio^L|2B^Sibling^99REL||||||||||||M|19410501|||||||||||||||||1000^^^L^PN||||\r" + "PV1||O|1^0^0^0&Test Location&0||||1^Super User (1-8)|||||||||||||||||||||||||||||||||||||20080212|||||||V\r" + "ORC|RE||||||||20080226102537|1^Super User\r" + "OBR|1|||1238^MEDICAL RECORD OBSERVATIONS^99DCT\r" + "OBX|1|NM|5497^CD4, BY FACS^99DCT||450|||||||||20080206\r" + "OBX|2|DT|5096^RETURN VISIT DATE^99DCT||20080229|||||||||20080212");
    ORU_R01 oru = (ORU_R01) message;
    PV1 pv1 = oru.getPATIENT_RESULT().getPATIENT().getVISIT().getPV1();
    Assert.assertNotNull("PV1 parsed as null", pv1);
    PL hl7Location = pv1.getAssignedPatientLocation();
    Integer locationId = hl7service.resolveLocationId(hl7Location);
    Assert.assertEquals("Resolved and given locationId shoud be equals", Integer.valueOf(1), locationId);
}
Also used : Message(ca.uhn.hl7v2.model.Message) ORU_R01(ca.uhn.hl7v2.model.v25.message.ORU_R01) PV1(ca.uhn.hl7v2.model.v25.segment.PV1) PL(ca.uhn.hl7v2.model.v25.datatype.PL) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

Message (ca.uhn.hl7v2.model.Message)60 Test (org.junit.Test)60 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)60 ORU_R01 (ca.uhn.hl7v2.model.v25.message.ORU_R01)29 NK1 (ca.uhn.hl7v2.model.v25.segment.NK1)22 Patient (org.openmrs.Patient)22 ORUR01Handler (org.openmrs.hl7.handler.ORUR01Handler)15 Concept (org.openmrs.Concept)14 Obs (org.openmrs.Obs)14 Person (org.openmrs.Person)14 ObsService (org.openmrs.api.ObsService)11 Encounter (org.openmrs.Encounter)9 CX (ca.uhn.hl7v2.model.v25.datatype.CX)5 Form (org.openmrs.Form)5 RelationshipType (org.openmrs.RelationshipType)5 HL7Exception (ca.uhn.hl7v2.HL7Exception)4 ORC (ca.uhn.hl7v2.model.v25.segment.ORC)4 PV1 (ca.uhn.hl7v2.model.v25.segment.PV1)4 Date (java.util.Date)4 Provider (org.openmrs.Provider)4