use of org.openmrs.Location in project openmrs-core by openmrs.
the class ValidateUtilTest method validate_shouldPopulateErrorsIfObjectInvalid.
/**
* @see ValidateUtil#validate(Object,Errors)
*/
@Test
public void validate_shouldPopulateErrorsIfObjectInvalid() {
Location loc = new Location();
Errors errors = new BindException(loc, "");
ValidateUtil.validate(loc, errors);
assertTrue(errors.hasErrors());
}
use of org.openmrs.Location in project openmrs-core by openmrs.
the class ValidateUtilTest method validate_shouldThrowValidationExceptionIfErrorsOccurDuringValidation.
/**
* @see ValidateUtil#validate(Object)
*/
@Test(expected = ValidationException.class)
public void validate_shouldThrowValidationExceptionIfErrorsOccurDuringValidation() {
Location loc = new Location();
ValidateUtil.validate(loc);
}
use of org.openmrs.Location 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 org.openmrs.Location 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 org.openmrs.Location 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