Search in sources :

Example 56 with HL7Exception

use of ca.uhn.hl7v2.HL7Exception in project openmrs-core by openmrs.

the class HL7ServiceImpl method resolvePersonId.

/**
 * @see org.openmrs.hl7.HL7Service#resolvePersonId(ca.uhn.hl7v2.model.v25.datatype.XCN)
 */
@Override
@Transactional(readOnly = true)
public Integer resolvePersonId(XCN xcn) throws HL7Exception {
    String idNumber = xcn.getIDNumber().getValue();
    String familyName = xcn.getFamilyName().getSurname().getValue();
    String givenName = xcn.getGivenName().getValue();
    if (idNumber != null && idNumber.length() > 0) {
        try {
            Person person = Context.getPersonService().getPerson(Integer.valueOf(idNumber));
            return person.getPersonId();
        } catch (Exception e) {
            log.error("Invalid person ID '" + idNumber + "'", e);
            return null;
        }
    } else {
        List<Person> persons = Context.getPersonService().getPeople(givenName + " " + familyName, null);
        if (persons.size() == 1) {
            return persons.get(0).getPersonId();
        } else if (persons.isEmpty()) {
            log.error("Couldn't find a person named " + givenName + " " + familyName);
            return null;
        } else {
            log.error("Found more than one person named " + givenName + " " + familyName);
            return null;
        }
    }
}
Also used : Person(org.openmrs.Person) URISyntaxException(java.net.URISyntaxException) DAOException(org.openmrs.api.db.DAOException) FileNotFoundException(java.io.FileNotFoundException) APIException(org.openmrs.api.APIException) HL7Exception(ca.uhn.hl7v2.HL7Exception) EncodingNotSupportedException(ca.uhn.hl7v2.parser.EncodingNotSupportedException) IOException(java.io.IOException) PatientIdentifierException(org.openmrs.api.PatientIdentifierException) ApplicationException(ca.uhn.hl7v2.app.ApplicationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 57 with HL7Exception

use of ca.uhn.hl7v2.HL7Exception in project openmrs-core by openmrs.

the class HL7ServiceImpl method processHL7InQueue.

/**
 * @see org.openmrs.hl7.HL7Service#processHL7InQueue(org.openmrs.hl7.HL7InQueue)
 */
@Override
public HL7InQueue processHL7InQueue(HL7InQueue hl7InQueue) throws HL7Exception {
    if (hl7InQueue == null) {
        throw new HL7Exception("hl7InQueue argument cannot be null");
    }
    // mark this queue object as processing so that it isn't processed twice
    if (OpenmrsUtil.nullSafeEquals(HL7Constants.HL7_STATUS_PROCESSING, hl7InQueue.getMessageState())) {
        throw new HL7Exception("The hl7InQueue message with id: " + hl7InQueue.getHL7InQueueId() + " is already processing. " + ",key=" + hl7InQueue.getHL7SourceKey() + ")");
    } else {
        hl7InQueue.setMessageState(HL7Constants.HL7_STATUS_PROCESSING);
    }
    if (log.isDebugEnabled()) {
        log.debug("Processing HL7 inbound queue (id=" + hl7InQueue.getHL7InQueueId() + ",key=" + hl7InQueue.getHL7SourceKey() + ")");
    }
    // Parse the HL7 into an HL7Message or abort with failure
    String hl7Message = hl7InQueue.getHL7Data();
    try {
        // Parse the inbound HL7 message using the parser
        // NOT making a direct call here so that AOP can happen around this
        // method
        Message parsedMessage = Context.getHL7Service().parseHL7String(hl7Message);
        // Send the parsed message to our receiver routine for processing
        // into db
        // NOT making a direct call here so that AOP can happen around this
        // method
        Context.getHL7Service().processHL7Message(parsedMessage);
        // Move HL7 inbound queue entry into the archive before exiting
        log.debug("Archiving HL7 inbound queue entry");
        Context.getHL7Service().saveHL7InArchive(new HL7InArchive(hl7InQueue));
        log.debug("Removing HL7 message from inbound queue");
        Context.getHL7Service().purgeHL7InQueue(hl7InQueue);
    } catch (HL7Exception e) {
        boolean skipError = false;
        log.debug("Unable to process hl7inqueue: " + hl7InQueue.getHL7InQueueId(), e);
        log.debug("Hl7inqueue source: " + hl7InQueue.getHL7Source());
        log.debug("hl7_processor.ignore_missing_patient_non_local? " + Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_IGNORE_MISSING_NONLOCAL_PATIENTS, "false"));
        if (e.getCause() != null && "Could not resolve patient".equals(e.getCause().getMessage()) && !"local".equals(hl7InQueue.getHL7Source().getName()) && "true".equals(Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_IGNORE_MISSING_NONLOCAL_PATIENTS, "false"))) {
            skipError = true;
        }
        if (!skipError) {
            setFatalError(hl7InQueue, "Trouble parsing HL7 message (" + hl7InQueue.getHL7SourceKey() + ")", e);
        }
    } catch (Exception e) {
        setFatalError(hl7InQueue, "Exception while attempting to process HL7 In Queue (" + hl7InQueue.getHL7SourceKey() + ")", e);
    }
    return hl7InQueue;
}
Also used : HL7InArchive(org.openmrs.hl7.HL7InArchive) Message(ca.uhn.hl7v2.model.Message) HL7Exception(ca.uhn.hl7v2.HL7Exception) URISyntaxException(java.net.URISyntaxException) DAOException(org.openmrs.api.db.DAOException) FileNotFoundException(java.io.FileNotFoundException) APIException(org.openmrs.api.APIException) HL7Exception(ca.uhn.hl7v2.HL7Exception) EncodingNotSupportedException(ca.uhn.hl7v2.parser.EncodingNotSupportedException) IOException(java.io.IOException) PatientIdentifierException(org.openmrs.api.PatientIdentifierException) ApplicationException(ca.uhn.hl7v2.app.ApplicationException)

Example 58 with HL7Exception

use of ca.uhn.hl7v2.HL7Exception 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)

Example 59 with HL7Exception

use of ca.uhn.hl7v2.HL7Exception in project openmrs-core by openmrs.

the class HL7ServiceTest method getPersonFromNK1_shouldReturnAPatientIfValidPatientIdentifiersExist.

/**
 * @throws HL7Exception
 * @see HL7Service#createPersonFromNK1(NK1)
 */
@Test
public void getPersonFromNK1_shouldReturnAPatientIfValidPatientIdentifiersExist() 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|||||||||||||||||2178037d-f86b-4f12-8d8b-be3ebc220029^^^UUID^v4~9-1^^^Test Identifier Type^PT||||\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\r" + "OBX|2|DT|5096^RETURN VISIT DATE^99DCT||20080229|||||||||20080212");
    ORU_R01 oru = (ORU_R01) message;
    List<NK1> nk1List = new ORUR01Handler().getNK1List(oru);
    Person result = hl7service.createPersonFromNK1(nk1List.get(0));
    Assert.assertNotNull("should have returned something", result);
    Assert.assertTrue("should have returned a Patient", result instanceof Patient);
}
Also used : Message(ca.uhn.hl7v2.model.Message) ORUR01Handler(org.openmrs.hl7.handler.ORUR01Handler) ORU_R01(ca.uhn.hl7v2.model.v25.message.ORU_R01) NK1(ca.uhn.hl7v2.model.v25.segment.NK1) Patient(org.openmrs.Patient) Person(org.openmrs.Person) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 60 with HL7Exception

use of ca.uhn.hl7v2.HL7Exception in project openmrs-core by openmrs.

the class HL7ServiceTest method resolvePersonFromIdentifiers_shouldFindAPersonBasedOnTheInternalPersonID.

/**
 * @throws HL7Exception
 * @see HL7Service#resolvePersonFromIdentifiers(null)
 */
@Test
public void resolvePersonFromIdentifiers_shouldFindAPersonBasedOnTheInternalPersonID() 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|||||||||||||||||2^^^L^PN||||\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\r" + "OBX|2|DT|5096^RETURN VISIT DATE^99DCT||20080229|||||||||20080212");
    ORU_R01 oru = (ORU_R01) message;
    List<NK1> nk1List = new ORUR01Handler().getNK1List(oru);
    Assert.assertEquals("too many NK1s parsed out", 1, nk1List.size());
    Person result = hl7service.resolvePersonFromIdentifiers(nk1List.get(0).getNextOfKinAssociatedPartySIdentifiers());
    Assert.assertNotNull("should have found a person", result);
    Assert.assertEquals("found the wrong person", 2, result.getId().intValue());
}
Also used : Message(ca.uhn.hl7v2.model.Message) ORUR01Handler(org.openmrs.hl7.handler.ORUR01Handler) ORU_R01(ca.uhn.hl7v2.model.v25.message.ORU_R01) NK1(ca.uhn.hl7v2.model.v25.segment.NK1) Person(org.openmrs.Person) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

Message (ca.uhn.hl7v2.model.Message)36 HL7Exception (ca.uhn.hl7v2.HL7Exception)34 Test (org.junit.Test)24 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)24 ORU_R01 (ca.uhn.hl7v2.model.v25.message.ORU_R01)22 NK1 (ca.uhn.hl7v2.model.v25.segment.NK1)16 ORUR01Handler (org.openmrs.hl7.handler.ORUR01Handler)15 Person (org.openmrs.Person)11 ApplicationException (ca.uhn.hl7v2.app.ApplicationException)10 CX (ca.uhn.hl7v2.model.v25.datatype.CX)10 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 Patient (org.openmrs.Patient)7 APIException (org.openmrs.api.APIException)7 PatientIdentifierException (org.openmrs.api.PatientIdentifierException)7 Segment (ca.uhn.hl7v2.model.Segment)6 Structure (ca.uhn.hl7v2.model.Structure)6 Type (ca.uhn.hl7v2.model.Type)6 PID (ca.uhn.hl7v2.model.v25.segment.PID)6 EncodingNotSupportedException (ca.uhn.hl7v2.parser.EncodingNotSupportedException)6