use of ca.uhn.hl7v2.model.v25.datatype.IS in project camel by apache.
the class HL7DataFormatTest method testUnmarshalWithImplicitBig5Charset.
@Test
public void testUnmarshalWithImplicitBig5Charset() throws Exception {
String charset = "Big5";
MockEndpoint mock = getMockEndpoint("mock:unmarshalBig5");
mock.expectedMessageCount(1);
mock.message(0).body().isInstanceOf(Message.class);
mock.expectedHeaderReceived(HL7Constants.HL7_CHARSET, null);
mock.expectedHeaderReceived(Exchange.CHARSET_NAME, charset);
// Message without explicit encoding in MSH-18, but the unmarshaller "guesses"
// this time that it is Big5
byte[] body = createHL7AsString().getBytes(Charset.forName(charset));
template.sendBody("direct:unmarshalBig5", new ByteArrayInputStream(body));
assertMockEndpointsSatisfied();
Message msg = mock.getExchanges().get(0).getIn().getBody(Message.class);
assertEquals("2.4", msg.getVersion());
QRD qrd = (QRD) msg.get("QRD");
assertEquals("0101701234", qrd.getWhoSubjectFilter(0).getIDNumber().getValue());
}
use of ca.uhn.hl7v2.model.v25.datatype.IS in project openmrs-core by openmrs.
the class ORUR01Handler method createEncounter.
/**
* This method does not call the database to create the encounter row. The encounter is only
* created after all obs have been attached to it Creates an encounter pojo to be attached
* later. This method does not create an encounterId
*
* @param msh
* @param patient
* @param pv1
* @param orc
* @return
* @throws HL7Exception
*/
private Encounter createEncounter(MSH msh, Patient patient, PV1 pv1, ORC orc) throws HL7Exception {
// the encounter we will return
Encounter encounter;
// look for the encounter id in PV1-19
CX visitNumber = pv1.getVisitNumber();
Integer encounterId = null;
try {
encounterId = Integer.valueOf(visitNumber.getIDNumber().getValue());
} catch (NumberFormatException e) {
// pass
}
// the database
if (encounterId != null) {
encounter = Context.getEncounterService().getEncounter(encounterId);
} else {
// if no encounter_id was passed in, this is a new
// encounter, create the object
encounter = new Encounter();
Date encounterDate = getEncounterDate(pv1);
Provider provider = getProvider(pv1);
Location location = getLocation(pv1);
Form form = getForm(msh);
EncounterType encounterType = getEncounterType(msh, form);
User enterer = getEnterer(orc);
// Date dateEntered = getDateEntered(orc); // ignore this since we have no place in the data model to store it
encounter.setEncounterDatetime(encounterDate);
if (unknownRole == null) {
unknownRole = Context.getEncounterService().getEncounterRoleByUuid(EncounterRole.UNKNOWN_ENCOUNTER_ROLE_UUID);
}
encounter.setProvider(unknownRole, provider);
encounter.setPatient(patient);
encounter.setLocation(location);
encounter.setForm(form);
encounter.setEncounterType(encounterType);
encounter.setCreator(enterer);
encounter.setDateCreated(new Date());
}
return encounter;
}
use of ca.uhn.hl7v2.model.v25.datatype.IS in project openmrs-core by openmrs.
the class HL7ServiceImpl method resolveLocationId.
/**
* @param pl HL7 component of data type PL (person location) (see Ch 2.A.53)
* @return internal identifier of the specified location, or null if it is not found or
* ambiguous
*/
@Override
@Transactional(readOnly = true)
public Integer resolveLocationId(PL pl) throws HL7Exception {
// TODO: Get rid of hack that allows first component to be an integer
// location.location_id
String pointOfCare = pl.getPointOfCare().getValue();
String facility = pl.getFacility().getUniversalID().getValue();
// Care" as an internal openmrs location_id
try {
Integer locationId = Integer.valueOf(pointOfCare);
Location l = Context.getLocationService().getLocation(locationId);
if (l != null) {
return l.getLocationId();
}
} catch (Exception ex) {
if (facility == null) {
// throw an exception
throw new HL7Exception("Error trying to treat PL.pointOfCare '" + pointOfCare + "' as a location.location_id", ex);
}
}
// Treat the 4th component "Facility" as location.name
try {
Location l = Context.getLocationService().getLocation(facility);
if (l == null) {
log.debug("Couldn't find a location named '" + facility + "'");
}
return l == null ? null : l.getLocationId();
} catch (Exception ex) {
log.error("Error trying to treat PL.facility '" + facility + "' as a location.name", ex);
return null;
}
}
use of ca.uhn.hl7v2.model.v25.datatype.IS in project openmrs-core by openmrs.
the class HL7ServiceImpl method resolvePersonFromIdentifiers.
/**
* @param identifiers CX identifier list from an identifier (either PID or NK1)
* @return The internal id number of the Patient based on one of the given identifiers, or null
* if the patient is not found
* @throws HL7Exception
*/
@Override
@Transactional(readOnly = true)
public Person resolvePersonFromIdentifiers(CX[] identifiers) throws HL7Exception {
// give up if no identifiers exist
if (identifiers.length < 1) {
throw new HL7Exception("Missing patient identifier in PID segment");
}
// Take the first uniquely matching identifier
for (CX identifier : identifiers) {
String hl7PersonId = identifier.getIDNumber().getValue();
// TODO if 1st component is blank, check 2nd and 3rd of assigning
// authority
String assigningAuthority = identifier.getAssigningAuthority().getNamespaceID().getValue();
if (StringUtils.isNotBlank(assigningAuthority)) {
// Assigning authority defined
try {
PatientIdentifierType pit = Context.getPatientService().getPatientIdentifierTypeByName(assigningAuthority);
if (pit == null) {
// there is no matching PatientIdentifierType
if (assigningAuthority.equals(HL7Constants.HL7_AUTHORITY_UUID)) {
// the identifier is a UUID
Person p = Context.getPersonService().getPersonByUuid(hl7PersonId);
if (p != null) {
return p;
}
log.warn("Can't find person for UUID '" + hl7PersonId + "'");
// skip identifiers with unknown type
continue;
} else if (assigningAuthority.equals(HL7Constants.HL7_AUTHORITY_LOCAL)) {
// the ID is internal (local)
String idType = identifier.getIdentifierTypeCode().getValue();
try {
if (idType.equals(HL7Constants.HL7_ID_PERSON)) {
Integer pid = Integer.parseInt(hl7PersonId);
// patient_id == person_id, so just look for
// the person
Person p = Context.getPersonService().getPerson(pid);
if (p != null) {
return p;
}
} else if (idType.equals(HL7Constants.HL7_ID_PATIENT)) {
Integer pid = Integer.parseInt(hl7PersonId);
// patient_id == person_id, so just look for
// the person
Patient p = Context.getPatientService().getPatient(pid);
if (p != null) {
return p;
}
}
} catch (NumberFormatException e) {
}
log.warn("Can't find Local identifier of '" + hl7PersonId + "'");
// skip identifiers with unknown type
continue;
}
log.warn("Can't find PatientIdentifierType named '" + assigningAuthority + "'");
// skip identifiers with unknown type
continue;
}
List<PatientIdentifier> matchingIds = Context.getPatientService().getPatientIdentifiers(hl7PersonId, Collections.singletonList(pit), null, null, null);
if (matchingIds == null || matchingIds.isEmpty()) {
// no matches
log.warn("NO matches found for " + hl7PersonId);
// try next identifier
continue;
} else if (matchingIds.size() == 1) {
// unique match -- we're done
return matchingIds.get(0).getPatient();
} else {
// ambiguous identifier
log.debug("Ambiguous identifier in PID. " + matchingIds.size() + " matches for identifier '" + hl7PersonId + "' of type '" + pit + "'");
// try next identifier
continue;
}
} catch (Exception e) {
log.error("Error resolving patient identifier '" + hl7PersonId + "' for assigning authority '" + assigningAuthority + "'", e);
continue;
}
} else {
try {
log.debug("CX contains ID '" + hl7PersonId + "' without assigning authority -- assuming patient.patient_id");
return Context.getPatientService().getPatient(Integer.parseInt(hl7PersonId));
} catch (NumberFormatException e) {
log.warn("Invalid patient ID '" + hl7PersonId + "'");
}
}
}
return null;
}
use of ca.uhn.hl7v2.model.v25.datatype.IS 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;
}
Aggregations