use of org.openmrs.api.PatientIdentifierException 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;
}
use of org.openmrs.api.PatientIdentifierException in project openmrs-core by openmrs.
the class PatientIdentifierValidator method validateIdentifier.
/**
* Checks that the given {@link PatientIdentifier} is valid
*
* @param pi - the {@link PatientIdentifier} to validate
* @throws PatientIdentifierException if the {@link PatientIdentifier} is invalid
* @should fail validation if PatientIdentifier is null
* @should pass validation if PatientIdentifier is voided
* @should fail validation if another patient has a matching identifier of the same type
* @should pass if in use and id type uniqueness is set to non unique
* @see #validateIdentifier(String, PatientIdentifierType)
*/
public static void validateIdentifier(PatientIdentifier pi) throws PatientIdentifierException {
// Validate that the identifier is non-null
if (pi == null) {
throw new BlankIdentifierException("PatientIdentifier.error.null");
}
// Only validate if the PatientIdentifier is not voided
if (!pi.getVoided()) {
// Check that this is a valid identifier
validateIdentifier(pi.getIdentifier(), pi.getIdentifierType());
// Check that location is included if it is required (default behavior is to require it)
LocationBehavior lb = pi.getIdentifierType().getLocationBehavior();
if (pi.getLocation() == null && (lb == null || lb == LocationBehavior.REQUIRED)) {
String identifierString = (pi.getIdentifier() != null) ? pi.getIdentifier() : "";
throw new PatientIdentifierException(Context.getMessageSourceService().getMessage("PatientIdentifier.location.null", new Object[] { identifierString }, Context.getLocale()));
}
if (pi.getIdentifierType().getUniquenessBehavior() != UniquenessBehavior.NON_UNIQUE && Context.getPatientService().isIdentifierInUseByAnotherPatient(pi)) {
// Check is already in use by another patient
throw new IdentifierNotUniqueException(Context.getMessageSourceService().getMessage("PatientIdentifier.error.notUniqueWithParameter", new Object[] { pi.getIdentifier() }, Context.getLocale()), pi);
}
}
}
use of org.openmrs.api.PatientIdentifierException 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;
}
Aggregations