Search in sources :

Example 1 with TS

use of ca.uhn.hl7v2.model.v25.datatype.TS in project streamsx.health by IBMStreams.

the class ObxToSplMapper method messageToModel.

@SuppressWarnings("unchecked")
public <T> Iterable<T> messageToModel(Message message) {
    ArrayList<Observation> observations = new ArrayList<Observation>();
    if (message instanceof ORU_R01) {
        ORU_R01 oruMsg = (ORU_R01) message;
        String obxTs = "";
        String obxLocation = "";
        String sendingApp = "";
        String sendingFacility = "";
        try {
            MSH msh = oruMsg.getMSH();
            sendingApp = msh.getSendingApplication().encode();
            sendingFacility = msh.getSendingFacility().encode();
            List<ORU_R01_PATIENT_RESULT> patient_RESULTAll = ((ORU_R01) message).getPATIENT_RESULTAll();
            for (ORU_R01_PATIENT_RESULT result : patient_RESULTAll) {
                ORU_R01_ORDER_OBSERVATION order_OBSERVATION = result.getORDER_OBSERVATION();
                OBR obr = order_OBSERVATION.getOBR();
                DTM ts = obr.getObservationDateTime();
                obxTs = ts.getValue();
                ORU_R01_PATIENT patient = result.getPATIENT();
                ORU_R01_VISIT visit = patient.getVISIT();
                PL location = visit.getPV1().getAssignedPatientLocation();
                obxLocation = location.encode();
                List<ORU_R01_OBSERVATION> observationAll = order_OBSERVATION.getOBSERVATIONAll();
                for (ORU_R01_OBSERVATION oru_R01_OBSERVATION : observationAll) {
                    parseOBX(observations, obxTs, obxLocation, oru_R01_OBSERVATION.getOBX(), sendingApp, sendingFacility);
                }
            }
        } catch (HL7Exception e) {
            if (TRACE.isDebugEnabled())
                TRACE.log(TraceLevel.WARN, e);
        }
        try {
            OBR obr = (OBR) oruMsg.get("OBR");
            DTM ts = obr.getObservationDateTime();
            obxTs = ts.getValue();
            Structure tmp = oruMsg.get("PV1");
            if (tmp != null) {
                PV1 pv1 = (PV1) tmp;
                PL location = pv1.getAssignedPatientLocation();
                obxLocation = location.encode();
            }
            Structure[] structures = oruMsg.getAll("OBX");
            for (Structure structure : structures) {
                parseOBX(observations, obxTs, obxLocation, (OBX) structure, sendingApp, sendingFacility);
            }
        } catch (HL7Exception e) {
            if (TRACE.isDebugEnabled())
                TRACE.log(TraceLevel.WARN, e);
        }
    }
    return (Iterable<T>) observations;
}
Also used : ORU_R01_OBSERVATION(ca.uhn.hl7v2.model.v26.group.ORU_R01_OBSERVATION) MSH(ca.uhn.hl7v2.model.v26.segment.MSH) ORU_R01_PATIENT_RESULT(ca.uhn.hl7v2.model.v26.group.ORU_R01_PATIENT_RESULT) ORU_R01_VISIT(ca.uhn.hl7v2.model.v26.group.ORU_R01_VISIT) ArrayList(java.util.ArrayList) PV1(ca.uhn.hl7v2.model.v26.segment.PV1) ORU_R01_ORDER_OBSERVATION(ca.uhn.hl7v2.model.v26.group.ORU_R01_ORDER_OBSERVATION) ORU_R01_PATIENT(ca.uhn.hl7v2.model.v26.group.ORU_R01_PATIENT) ORU_R01(ca.uhn.hl7v2.model.v26.message.ORU_R01) Observation(com.ibm.streamsx.health.hapi.model.Observation) HL7Exception(ca.uhn.hl7v2.HL7Exception) DTM(ca.uhn.hl7v2.model.v26.datatype.DTM) PL(ca.uhn.hl7v2.model.v26.datatype.PL) Structure(ca.uhn.hl7v2.model.Structure) OBR(ca.uhn.hl7v2.model.v26.segment.OBR)

Example 2 with TS

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

the class ORUR01Handler method getDatetime.

/**
 * Return a java date object for the given TS
 *
 * @param ts TS to parse
 * @return date object or null
 * @throws HL7Exception
 */
private Date getDatetime(TS ts) throws HL7Exception {
    Date datetime = null;
    DTM value = ts.getTime();
    if (value.getYear() == 0 || value.getValue() == null) {
        return null;
    }
    try {
        datetime = getDate(value.getYear(), value.getMonth(), value.getDay(), value.getHour(), value.getMinute(), value.getSecond());
    } catch (DataTypeException e) {
    }
    return datetime;
}
Also used : DataTypeException(ca.uhn.hl7v2.model.DataTypeException) DTM(ca.uhn.hl7v2.model.v25.datatype.DTM) Date(java.util.Date)

Example 3 with TS

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

the class HL7ServiceImpl method createPersonFromNK1.

/**
 * @see org.openmrs.hl7.HL7Service#createPersonFromNK1(ca.uhn.hl7v2.model.v25.segment.NK1)
 */
@Override
public Person createPersonFromNK1(NK1 nk1) throws HL7Exception {
    // NOTE: following block (with minor modifications) stolen from
    // ADTA28Handler
    // TODO: generalize this for use with both PID and NK1 segments
    Person person = new Person();
    // UUID
    CX[] identifiers = nk1.getNextOfKinAssociatedPartySIdentifiers();
    String uuid = getUuidFromIdentifiers(identifiers);
    if (Context.getPersonService().getPersonByUuid(uuid) != null) {
        throw new HL7Exception("Non-unique UUID '" + uuid + "' for new person");
    }
    person.setUuid(uuid);
    // Patient Identifiers
    List<PatientIdentifier> goodIdentifiers = new ArrayList<>();
    for (CX id : identifiers) {
        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) {
                    if (!"UUID".equals(assigningAuthority)) {
                        log.warn("Can't find PatientIdentifierType named '" + assigningAuthority + "'");
                    }
                    // skip identifiers with unknown type
                    continue;
                }
                PatientIdentifier pi = new PatientIdentifier();
                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 NK1 is invalid: " + pi, ex);
                }
            } catch (Exception e) {
                log.error("Uncaught error parsing/creating patient identifier '" + hl7PatientId + "' for assigning authority '" + assigningAuthority + "'", e);
            }
        } else {
            log.debug("NK1 contains identifier with no assigning authority");
            continue;
        }
    }
    if (!goodIdentifiers.isEmpty()) {
        // If we have one identifier, set it as the preferred to make the validator happy.
        if (goodIdentifiers.size() == 1) {
            goodIdentifiers.get(0).setPreferred(true);
        }
        // cast the person as a Patient and add identifiers
        person = new Patient(person);
        ((Patient) person).addIdentifiers(goodIdentifiers);
    }
    // Person names
    for (XPN patientNameX : nk1.getNKName()) {
        PersonName name = new PersonName();
        name.setFamilyName(patientNameX.getFamilyName().getSurname().getValue());
        name.setGivenName(patientNameX.getGivenName().getValue());
        name.setMiddleName(patientNameX.getSecondAndFurtherGivenNamesOrInitialsThereof().getValue());
        person.addName(name);
    }
    // Gender (checks for null, but not for 'M' or 'F')
    String gender = nk1.getAdministrativeSex().getValue();
    if (gender == null) {
        throw new HL7Exception("Missing gender in an NK1 segment");
    }
    gender = gender.toUpperCase();
    if (!OpenmrsConstants.GENDER().containsKey(gender)) {
        throw new HL7Exception("Unrecognized gender: " + gender);
    }
    person.setGender(gender);
    // Date of Birth
    TS dateOfBirth = nk1.getDateTimeOfBirth();
    if (dateOfBirth == null || dateOfBirth.getTime() == null || dateOfBirth.getTime().getValue() == null) {
        throw new HL7Exception("Missing birth date in an NK1 segment");
    }
    person.setBirthdate(HL7Util.parseHL7Timestamp(dateOfBirth.getTime().getValue()));
    // 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)) {
            person.setBirthdateEstimated(true);
        }
    }
    // save the new person or patient
    if (person instanceof Patient) {
        Context.getPatientService().savePatient((Patient) person);
    } else {
        Context.getPersonService().savePerson(person);
    }
    return person;
}
Also used : PersonName(org.openmrs.PersonName) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) PatientIdentifier(org.openmrs.PatientIdentifier) 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) CX(ca.uhn.hl7v2.model.v25.datatype.CX) XPN(ca.uhn.hl7v2.model.v25.datatype.XPN) HL7Exception(ca.uhn.hl7v2.HL7Exception) ID(ca.uhn.hl7v2.model.v25.datatype.ID) PID(ca.uhn.hl7v2.model.v25.segment.PID) Person(org.openmrs.Person) PatientIdentifierType(org.openmrs.PatientIdentifierType) PatientIdentifierException(org.openmrs.api.PatientIdentifierException) Location(org.openmrs.Location) TS(ca.uhn.hl7v2.model.v25.datatype.TS)

Example 4 with TS

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

the class ORUR01Handler method parseObs.

/**
 * Creates the Obs pojo from the OBX message
 *
 * @param encounter The Encounter object this Obs is a member of
 * @param obx The hl7 obx message
 * @param obr The parent hl7 or message
 * @param uid unique string for this message for any error reporting purposes
 * @return Obs pojo with all values filled in
 * @throws HL7Exception if there is a parsing exception
 * @throws ProposingConceptException if the answer to this obs is a proposed concept
 * @should add comments to an observation from NTE segments
 * @should add multiple comments for an observation as one comment
 * @should add comments to an observation group
 */
private Obs parseObs(Encounter encounter, OBX obx, OBR obr, String uid) throws HL7Exception, ProposingConceptException {
    if (log.isDebugEnabled()) {
        log.debug("parsing observation: " + obx);
    }
    Varies[] values = obx.getObservationValue();
    // bail out if no values were found
    if (values == null || values.length < 1) {
        return null;
    }
    String hl7Datatype = values[0].getName();
    if (log.isDebugEnabled()) {
        log.debug("  datatype = " + hl7Datatype);
    }
    Concept concept = getConcept(obx.getObservationIdentifier(), uid);
    if (log.isDebugEnabled()) {
        log.debug("  concept = " + concept.getConceptId());
    }
    ConceptName conceptName = getConceptName(obx.getObservationIdentifier());
    if (log.isDebugEnabled()) {
        log.debug("  concept-name = " + conceptName);
    }
    Date datetime = getDatetime(obx);
    if (log.isDebugEnabled()) {
        log.debug("  timestamp = " + datetime);
    }
    if (datetime == null) {
        datetime = encounter.getEncounterDatetime();
    }
    Obs obs = new Obs();
    obs.setPerson(encounter.getPatient());
    obs.setConcept(concept);
    obs.setEncounter(encounter);
    obs.setObsDatetime(datetime);
    obs.setLocation(encounter.getLocation());
    obs.setCreator(encounter.getCreator());
    obs.setDateCreated(encounter.getDateCreated());
    // set comments if there are any
    StringBuilder comments = new StringBuilder();
    ORU_R01_OBSERVATION parent = (ORU_R01_OBSERVATION) obx.getParent();
    // iterate over all OBX NTEs
    for (int i = 0; i < parent.getNTEReps(); i++) {
        for (FT obxComment : parent.getNTE(i).getComment()) {
            if (comments.length() > 0) {
                comments.append(" ");
            }
            comments = comments.append(obxComment.getValue());
        }
    }
    // only set comments if there are any
    if (StringUtils.hasText(comments.toString())) {
        obs.setComment(comments.toString());
    }
    Type obx5 = values[0].getData();
    if ("NM".equals(hl7Datatype)) {
        String value = ((NM) obx5).getValue();
        if (value == null || value.length() == 0) {
            log.warn("Not creating null valued obs for concept " + concept);
            return null;
        } else if ("0".equals(value) || "1".equals(value)) {
            concept = concept.hydrate(concept.getConceptId().toString());
            obs.setConcept(concept);
            if (concept.getDatatype().isBoolean()) {
                obs.setValueBoolean("1".equals(value));
            } else if (concept.getDatatype().isNumeric()) {
                try {
                    obs.setValueNumeric(Double.valueOf(value));
                } catch (NumberFormatException e) {
                    throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.notnumericConcept", new Object[] { value, concept.getConceptId(), conceptName.getName(), uid }, null), e);
                }
            } else if (concept.getDatatype().isCoded()) {
                Concept answer = "1".equals(value) ? Context.getConceptService().getTrueConcept() : Context.getConceptService().getFalseConcept();
                boolean isValidAnswer = false;
                Collection<ConceptAnswer> conceptAnswers = concept.getAnswers();
                if (conceptAnswers != null && !conceptAnswers.isEmpty()) {
                    for (ConceptAnswer conceptAnswer : conceptAnswers) {
                        if (conceptAnswer.getAnswerConcept().getId().equals(answer.getId())) {
                            obs.setValueCoded(answer);
                            isValidAnswer = true;
                            break;
                        }
                    }
                }
                // answer the boolean answer concept was't found
                if (!isValidAnswer) {
                    throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.invalidAnswer", new Object[] { answer.toString(), uid }, null));
                }
            } else {
                // throw this exception to make sure that the handler doesn't silently ignore bad hl7 message
                throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.CannotSetBoolean", new Object[] { obs.getConcept().getConceptId() }, null));
            }
        } else {
            try {
                obs.setValueNumeric(Double.valueOf(value));
            } catch (NumberFormatException e) {
                throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.notnumericConcept", new Object[] { value, concept.getConceptId(), conceptName.getName(), uid }, null), e);
            }
        }
    } else if ("CWE".equals(hl7Datatype)) {
        log.debug("  CWE observation");
        CWE value = (CWE) obx5;
        String valueIdentifier = value.getIdentifier().getValue();
        log.debug("    value id = " + valueIdentifier);
        String valueName = value.getText().getValue();
        log.debug("    value name = " + valueName);
        if (isConceptProposal(valueIdentifier)) {
            if (log.isDebugEnabled()) {
                log.debug("Proposing concept");
            }
            throw new ProposingConceptException(concept, valueName);
        } else {
            log.debug("    not proposal");
            try {
                Concept valueConcept = getConcept(value, uid);
                obs.setValueCoded(valueConcept);
                if (HL7Constants.HL7_LOCAL_DRUG.equals(value.getNameOfAlternateCodingSystem().getValue())) {
                    Drug valueDrug = new Drug();
                    valueDrug.setDrugId(Integer.valueOf(value.getAlternateIdentifier().getValue()));
                    obs.setValueDrug(valueDrug);
                } else {
                    ConceptName valueConceptName = getConceptName(value);
                    if (valueConceptName != null) {
                        if (log.isDebugEnabled()) {
                            log.debug("    value concept-name-id = " + valueConceptName.getConceptNameId());
                            log.debug("    value concept-name = " + valueConceptName.getName());
                        }
                        obs.setValueCodedName(valueConceptName);
                    }
                }
            } catch (NumberFormatException e) {
                throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.InvalidConceptId", new Object[] { valueIdentifier, valueName }, null));
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("  Done with CWE");
        }
    } else if ("CE".equals(hl7Datatype)) {
        CE value = (CE) obx5;
        String valueIdentifier = value.getIdentifier().getValue();
        String valueName = value.getText().getValue();
        if (isConceptProposal(valueIdentifier)) {
            throw new ProposingConceptException(concept, valueName);
        } else {
            try {
                obs.setValueCoded(getConcept(value, uid));
                obs.setValueCodedName(getConceptName(value));
            } catch (NumberFormatException e) {
                throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.InvalidConceptId", new Object[] { valueIdentifier, valueName }, null));
            }
        }
    } else if ("DT".equals(hl7Datatype)) {
        DT value = (DT) obx5;
        if (value != null) {
            Date valueDate = getDate(value.getYear(), value.getMonth(), value.getDay(), 0, 0, 0);
            obs.setValueDatetime(valueDate);
        } else {
            log.warn("Not creating null valued obs for concept " + concept);
            return null;
        }
    } else if ("TS".equals(hl7Datatype)) {
        DTM value = ((TS) obx5).getTime();
        if (value != null) {
            Date valueDate = getDate(value.getYear(), value.getMonth(), value.getDay(), value.getHour(), value.getMinute(), value.getSecond());
            obs.setValueDatetime(valueDate);
        } else {
            log.warn("Not creating null valued obs for concept " + concept);
            return null;
        }
    } else if ("TM".equals(hl7Datatype)) {
        TM value = (TM) obx5;
        if (value != null) {
            Date valueTime = getDate(0, 0, 0, value.getHour(), value.getMinute(), value.getSecond());
            obs.setValueDatetime(valueTime);
        } else {
            log.warn("Not creating null valued obs for concept " + concept);
            return null;
        }
    } else if ("ST".equals(hl7Datatype)) {
        ST value = (ST) obx5;
        if (value == null || value.getValue() == null || value.getValue().trim().length() == 0) {
            log.warn("Not creating null valued obs for concept " + concept);
            return null;
        }
        obs.setValueText(value.getValue());
    } else if ("ED".equals(hl7Datatype)) {
        ED value = (ED) obx5;
        if (value == null || value.getData() == null || !StringUtils.hasText(value.getData().getValue())) {
            log.warn("Not creating null valued obs for concept " + concept);
            return null;
        }
        // we need to hydrate the concept so that the EncounterSaveHandler
        // doesn't fail since it needs to check if it is a concept numeric
        Concept c = Context.getConceptService().getConcept(obs.getConcept().getConceptId());
        obs.setConcept(c);
        String title = null;
        if (obs.getValueCodedName() != null) {
            title = obs.getValueCodedName().getName();
        }
        if (!StringUtils.hasText(title)) {
            title = c.getName().getName();
        }
        obs.setComplexData(new ComplexData(title, value.getData().getValue()));
    } else {
        // do we need to support BIT just in case it slips thru?
        throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.UpsupportedObsType", new Object[] { hl7Datatype }, null));
    }
    return obs;
}
Also used : FT(ca.uhn.hl7v2.model.v25.datatype.FT) CWE(ca.uhn.hl7v2.model.v25.datatype.CWE) DT(ca.uhn.hl7v2.model.v25.datatype.DT) ComplexData(org.openmrs.obs.ComplexData) ConceptName(org.openmrs.ConceptName) ED(ca.uhn.hl7v2.model.v25.datatype.ED) Concept(org.openmrs.Concept) ORU_R01_OBSERVATION(ca.uhn.hl7v2.model.v25.group.ORU_R01_OBSERVATION) Drug(org.openmrs.Drug) Obs(org.openmrs.Obs) ST(ca.uhn.hl7v2.model.v25.datatype.ST) CE(ca.uhn.hl7v2.model.v25.datatype.CE) ConceptAnswer(org.openmrs.ConceptAnswer) Date(java.util.Date) RelationshipType(org.openmrs.RelationshipType) EncounterType(org.openmrs.EncounterType) Type(ca.uhn.hl7v2.model.Type) PersonAttributeType(org.openmrs.PersonAttributeType) HL7Exception(ca.uhn.hl7v2.HL7Exception) Collection(java.util.Collection) TM(ca.uhn.hl7v2.model.v25.datatype.TM) DTM(ca.uhn.hl7v2.model.v25.datatype.DTM) DTM(ca.uhn.hl7v2.model.v25.datatype.DTM) Varies(ca.uhn.hl7v2.model.Varies) NM(ca.uhn.hl7v2.model.v25.datatype.NM) TS(ca.uhn.hl7v2.model.v25.datatype.TS)

Example 5 with TS

use of ca.uhn.hl7v2.model.v25.datatype.TS 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)4 TS (ca.uhn.hl7v2.model.v25.datatype.TS)3 ArrayList (java.util.ArrayList)3 ApplicationException (ca.uhn.hl7v2.app.ApplicationException)2 CX (ca.uhn.hl7v2.model.v25.datatype.CX)2 DTM (ca.uhn.hl7v2.model.v25.datatype.DTM)2 ID (ca.uhn.hl7v2.model.v25.datatype.ID)2 XPN (ca.uhn.hl7v2.model.v25.datatype.XPN)2 PID (ca.uhn.hl7v2.model.v25.segment.PID)2 Date (java.util.Date)2 DataTypeException (ca.uhn.hl7v2.model.DataTypeException)1 Structure (ca.uhn.hl7v2.model.Structure)1 Type (ca.uhn.hl7v2.model.Type)1 Varies (ca.uhn.hl7v2.model.Varies)1 CE (ca.uhn.hl7v2.model.v25.datatype.CE)1 CWE (ca.uhn.hl7v2.model.v25.datatype.CWE)1 DT (ca.uhn.hl7v2.model.v25.datatype.DT)1 ED (ca.uhn.hl7v2.model.v25.datatype.ED)1 FT (ca.uhn.hl7v2.model.v25.datatype.FT)1 NM (ca.uhn.hl7v2.model.v25.datatype.NM)1