Search in sources :

Example 86 with PID

use of ca.uhn.hl7v2.model.v24.segment.PID in project openmrs-core by openmrs.

the class ORUR01HandlerTest method processMessage_shouldCreateConceptProposalAndWithObsAlongside.

/**
 * Should create a concept proposal because of the key string in the message
 *
 * @see ORUR01Handler#processMessage(Message)
 */
@Test
public void processMessage_shouldCreateConceptProposalAndWithObsAlongside() throws Exception {
    // remember initial occurrence of proposal's text in the model
    int initialOccurrences = Context.getConceptService().getConceptProposals("ASDFASDFASDF").size();
    String hl7string = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20081006115934||ORU^R01|a1NZBpKqu54QyrWBEUKf|P|2.5|1||||||||3^AMRS.ELD.FORMID\r" + "PID|||7^^^^~asdf^^^^||Joe^ ^Smith||\r" + "PV1||O|1^Bishop Muge||||1^asdf asdf (5-9)|||||||||||||||||||||||||||||||||||||20081003|||||||V\r" + "ORC|RE||||||||20081006115645|1^Super User\r" + "OBR|1|||1238^MEDICAL RECORD OBSERVATIONS^99DCT\r" + "OBX|2|DT|5096^RETURN VISIT DATE^99DCT||20081004|||||||||20081003\r" + "OBR|3|||1284^PROBLEM LIST^99DCT\r" + "OBX|2|CWE|6042^PROBLEM ADDED^99DCT||PROPOSED^ASDFASDFASDF^99DCT|||||||||20081003";
    Message hl7message = parser.parse(hl7string);
    router.processMessage(hl7message);
    // make sure that the proposal was added
    Assert.assertEquals("Processing of the HL7 message did not result in the new proposal being added to the model", initialOccurrences + 1, Context.getConceptService().getConceptProposals("ASDFASDFASDF").size());
}
Also used : Message(ca.uhn.hl7v2.model.Message) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 87 with PID

use of ca.uhn.hl7v2.model.v24.segment.PID 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

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 Patient (org.openmrs.Patient)26 NK1 (ca.uhn.hl7v2.model.v25.segment.NK1)22 Person (org.openmrs.Person)16 ORUR01Handler (org.openmrs.hl7.handler.ORUR01Handler)15 MSH (ca.uhn.hl7v2.model.v24.segment.MSH)14 Concept (org.openmrs.Concept)13 Obs (org.openmrs.Obs)13 ObsService (org.openmrs.api.ObsService)11 ADR_A19 (ca.uhn.hl7v2.model.v24.message.ADR_A19)10 MSA (ca.uhn.hl7v2.model.v24.segment.MSA)10 QRD (ca.uhn.hl7v2.model.v24.segment.QRD)10 Encounter (org.openmrs.Encounter)9 ADT_A01 (ca.uhn.hl7v2.model.v24.message.ADT_A01)8 PID (ca.uhn.hl7v2.model.v24.segment.PID)8 CX (ca.uhn.hl7v2.model.v25.datatype.CX)8 HL7Exception (ca.uhn.hl7v2.HL7Exception)7