Search in sources :

Example 21 with ID

use of ca.uhn.hl7v2.model.v25.datatype.ID 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 22 with ID

use of ca.uhn.hl7v2.model.v25.datatype.ID 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 23 with ID

use of ca.uhn.hl7v2.model.v25.datatype.ID in project openmrs-core by openmrs.

the class ORUR01HandlerTest method processMessage_shouldPreferFormIdIfUuidIsNotFound.

/**
 * @see ORUR01Handler#processMessage(Message)
 */
@Test
public void processMessage_shouldPreferFormIdIfUuidIsNotFound() throws Exception {
    // save original encounter count
    List<Encounter> encounters = Context.getEncounterService().getEncountersByPatient(new Patient(3));
    Integer originalEncounters = encounters.size();
    // process message
    String hl7String = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20090728170332||ORU^R01|gu99yBh4loLX2mh9cHaV|P|2.5|1||||||||1^AMRS.ELD.FORMID~oicu812^AMRS.ELD.FORMUUID^^\r" + "PID|||3^^^^||Beren^John^Bondo||\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";
    Message hl7message = parser.parse(hl7String);
    router.processMessage(hl7message);
    // make sure an encounter was added
    encounters = Context.getEncounterService().getEncountersByPatient(new Patient(3));
    Assert.assertEquals(originalEncounters + 1, encounters.size());
    // get last encounter
    Encounter enc = encounters.get(encounters.size() - 1);
    // check the form id
    Form form = enc.getForm();
    Assert.assertEquals(1, form.getId().intValue());
}
Also used : Message(ca.uhn.hl7v2.model.Message) Form(org.openmrs.Form) Encounter(org.openmrs.Encounter) Patient(org.openmrs.Patient) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 24 with ID

use of ca.uhn.hl7v2.model.v25.datatype.ID in project openmrs-core by openmrs.

the class ORUR01HandlerTest method processMessage_shouldFailIfQuestionDatatypeIsCodedAndABooleanIsNotAValidAnswer.

/**
 * @see ORUR01Handler#processMessage(Message)
 */
@Test(expected = ApplicationException.class)
public void processMessage_shouldFailIfQuestionDatatypeIsCodedAndABooleanIsNotAValidAnswer() throws Exception {
    GlobalProperty trueConceptGlobalProperty = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_TRUE_CONCEPT, "7", "Concept id of the concept defining the TRUE boolean concept");
    GlobalProperty falseConceptGlobalProperty = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_FALSE_CONCEPT, "8", "Concept id of the concept defining the TRUE boolean concept");
    Context.getAdministrationService().saveGlobalProperty(trueConceptGlobalProperty);
    Context.getAdministrationService().saveGlobalProperty(falseConceptGlobalProperty);
    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|2|NM|4^CIVIL STATUS^99DCT||1|||||||||20080206";
    Assert.assertEquals("Coded", Context.getConceptService().getConcept(4).getDatatype().getName());
    Message hl7message = parser.parse(hl7string);
    router.processMessage(hl7message);
}
Also used : Message(ca.uhn.hl7v2.model.Message) GlobalProperty(org.openmrs.GlobalProperty) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 25 with ID

use of ca.uhn.hl7v2.model.v25.datatype.ID in project openmrs-core by openmrs.

the class ADTA28Handler method createPatient.

// Create a new patient when this patient doesn't exist in the database
private Patient createPatient(PID pid, String creatorName) throws HL7Exception {
    Patient patient = new Patient();
    // Try to use the specified username as the creator
    User creator = Context.getUserService().getUserByUsername(creatorName);
    if (creator != null) {
        patient.setCreator(creator);
    }
    // Create all patient identifiers specified in the message
    // Copied code from resolvePatientId() in HL7ServiceImpl.java
    CX[] idList = pid.getPatientIdentifierList();
    if (idList == null || idList.length < 1) {
        throw new HL7Exception("Missing patient identifier in PID segment");
    }
    List<PatientIdentifier> goodIdentifiers = new ArrayList<>();
    for (CX id : idList) {
        String assigningAuthority = id.getAssigningAuthority().getNamespaceID().getValue();
        String hl7PatientId = id.getIDNumber().getValue();
        log.debug("identifier has id=" + hl7PatientId + " assigningAuthority=" + assigningAuthority);
        if (assigningAuthority != null && assigningAuthority.length() > 0) {
            try {
                PatientIdentifierType pit = Context.getPatientService().getPatientIdentifierTypeByName(assigningAuthority);
                if (pit == null) {
                    log.warn("Can't find PatientIdentifierType named '" + assigningAuthority + "'");
                    // skip identifiers with unknown type
                    continue;
                }
                PatientIdentifier pi = new PatientIdentifier();
                if (creator != null) {
                    pi.setCreator(creator);
                }
                pi.setIdentifierType(pit);
                pi.setIdentifier(hl7PatientId);
                // Get default location
                Location location = Context.getLocationService().getDefaultLocation();
                if (location == null) {
                    throw new HL7Exception("Cannot find default location");
                }
                pi.setLocation(location);
                try {
                    PatientIdentifierValidator.validateIdentifier(pi);
                    goodIdentifiers.add(pi);
                } catch (PatientIdentifierException ex) {
                    log.warn("Patient identifier in PID is invalid: " + pi, ex);
                }
            } catch (Exception e) {
                log.error("Uncaught error parsing/creating patient identifier '" + hl7PatientId + "' for assigning authority '" + assigningAuthority + "'", e);
            }
        } else {
            log.error("PID contains identifier with no assigning authority");
            continue;
        }
    }
    if (goodIdentifiers.isEmpty()) {
        throw new HL7Exception("PID segment has no recognizable patient identifiers.");
    }
    patient.addIdentifiers(goodIdentifiers);
    // Extract patient name from the message
    XPN patientNameX = pid.getPatientName(0);
    if (patientNameX == null) {
        throw new HL7Exception("Missing patient name in the PID segment");
    }
    // Patient name
    PersonName name = new PersonName();
    name.setFamilyName(patientNameX.getFamilyName().getSurname().getValue());
    name.setGivenName(patientNameX.getGivenName().getValue());
    name.setMiddleName(patientNameX.getSecondAndFurtherGivenNamesOrInitialsThereof().getValue());
    if (creator != null) {
        name.setCreator(creator);
    }
    patient.addName(name);
    // Gender (checks for null, but not for 'M' or 'F')
    String gender = pid.getAdministrativeSex().getValue();
    if (gender == null) {
        throw new HL7Exception("Missing gender in the PID segment");
    }
    gender = gender.toUpperCase();
    if (!OpenmrsConstants.GENDER().containsKey(gender)) {
        throw new HL7Exception("Unrecognized gender: " + gender);
    }
    patient.setGender(gender);
    // Date of Birth
    TS dateOfBirth = pid.getDateTimeOfBirth();
    if (dateOfBirth == null || dateOfBirth.getTime() == null || dateOfBirth.getTime().getValue() == null) {
        throw new HL7Exception("Missing birth date in the PID segment");
    }
    patient.setBirthdate(tsToDate(dateOfBirth));
    // Estimated birthdate?
    ID precisionTemp = dateOfBirth.getDegreeOfPrecision();
    if (precisionTemp != null && precisionTemp.getValue() != null) {
        String precision = precisionTemp.getValue().toUpperCase();
        log.debug("The birthdate is estimated: " + precision);
        if ("Y".equals(precision) || "L".equals(precision)) {
            patient.setBirthdateEstimated(true);
        }
    }
    return patient;
}
Also used : PersonName(org.openmrs.PersonName) User(org.openmrs.User) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) PatientIdentifier(org.openmrs.PatientIdentifier) HL7Exception(ca.uhn.hl7v2.HL7Exception) PatientIdentifierException(org.openmrs.api.PatientIdentifierException) ApplicationException(ca.uhn.hl7v2.app.ApplicationException) CX(ca.uhn.hl7v2.model.v25.datatype.CX) XPN(ca.uhn.hl7v2.model.v25.datatype.XPN) HL7Exception(ca.uhn.hl7v2.HL7Exception) PID(ca.uhn.hl7v2.model.v25.segment.PID) ID(ca.uhn.hl7v2.model.v25.datatype.ID) PatientIdentifierType(org.openmrs.PatientIdentifierType) PatientIdentifierException(org.openmrs.api.PatientIdentifierException) Location(org.openmrs.Location) TS(ca.uhn.hl7v2.model.v25.datatype.TS)

Aggregations

HL7Exception (ca.uhn.hl7v2.HL7Exception)14 Message (ca.uhn.hl7v2.model.Message)10 ApplicationException (ca.uhn.hl7v2.app.ApplicationException)8 Test (org.junit.Test)7 Patient (org.openmrs.Patient)7 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)7 PID (ca.uhn.hl7v2.model.v25.segment.PID)6 Person (org.openmrs.Person)6 PatientIdentifierException (org.openmrs.api.PatientIdentifierException)6 EncodingNotSupportedException (ca.uhn.hl7v2.parser.EncodingNotSupportedException)5 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 URISyntaxException (java.net.URISyntaxException)5 ArrayList (java.util.ArrayList)5 APIException (org.openmrs.api.APIException)5 DAOException (org.openmrs.api.db.DAOException)5 CX (ca.uhn.hl7v2.model.v25.datatype.CX)4 XCN (ca.uhn.hl7v2.model.v25.datatype.XCN)4 ORU_R01 (ca.uhn.hl7v2.model.v25.message.ORU_R01)4 ORC (ca.uhn.hl7v2.model.v25.segment.ORC)4