use of org.openmrs.PersonName in project openmrs-core by openmrs.
the class PersonDAOTest method getPersonName_shouldNotGetPersonNameGivenInvalidId.
@Test
public void getPersonName_shouldNotGetPersonNameGivenInvalidId() {
PersonName personName = dao.getPersonName(-1);
assertNull(personName);
}
use of org.openmrs.PersonName in project openmrs-core by openmrs.
the class DropMillisecondsHibernateInterceptorTest method shouldClearMillisecondsWhenSavingANewObject.
@Test
public void shouldClearMillisecondsWhenSavingANewObject() {
Date dateWithMillisecond = new Date(567L);
Date dateWithoutMillisecond = new Date(0L);
Person person = new Person();
person.addName(new PersonName("Alice", null, "Paul"));
person.setGender("F");
person.setBirthdate(dateWithMillisecond);
personService.savePerson(person);
Context.flushSession();
assertThat(person.getBirthdate(), is(dateWithoutMillisecond));
}
use of org.openmrs.PersonName in project openmrs-core by openmrs.
the class RequiredDataAdviceTest method setUp.
@Before
public void setUp() {
Context.setUserContext(userContext);
context.setServiceContext(serviceContext);
Context.setContext(serviceContext);
serviceContext.setApplicationContext(applicationContext);
User user = new User();
user.setUuid("1010d442-e134-11de-babe-001e378eb67e");
user.setUserId(1);
user.setUsername("admin");
user.addRole(new Role(RoleConstants.SUPERUSER));
Person person = new Person();
person.setUuid("6adb7c42-cfd2-4301-b53b-ff17c5654ff7");
person.setId(1);
person.addName(new PersonName("Bob", "", "Smith"));
Calendar calendar = Calendar.getInstance();
calendar.set(1980, 01, 01);
person.setBirthdate(calendar.getTime());
person.setGender("M");
user.setPerson(person);
when(userContext.getAuthenticatedUser()).thenReturn(user);
when(userContext.isAuthenticated()).thenReturn(true);
Map<String, SaveHandler> saveHandlers = new HashMap<>();
saveHandlers.put("saveHandler", saveHandler);
when(applicationContext.getBeansOfType(SaveHandler.class)).thenReturn(saveHandlers);
Map<String, VoidHandler> voidHandlers = new HashMap<>();
voidHandlers.put("voidHandler", voidHandler);
when(applicationContext.getBeansOfType(VoidHandler.class)).thenReturn(voidHandlers);
// Clear cache since handlers are updated
HandlerUtil.clearCachedHandlers();
}
use of org.openmrs.PersonName in project openmrs-core by openmrs.
the class PersonByNameComparator method comparePersonsByName.
/**
* Compares two person objects by name
*
* @should return negative if personName for person1 comes before that of person2
* @should return positive if personName for person1 comes after that of person2
* @should return zero if the givenName middleName and familyName match
* @should be case insensitive
* @since 1.8
*/
public static int comparePersonsByName(Person person1, Person person2) {
// test for null cases (sorting them to be last in a list)
if (person1 == null || person1.getPersonName() == null) {
return 1;
} else if (person2 == null || person2.getPersonName() == null) {
return -1;
}
// if neither are null, do the actual comparison
PersonName name1 = person1.getPersonName();
PersonName name2 = person2.getPersonName();
int ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyName() != null ? name1.getFamilyName().toLowerCase() : null, name2.getFamilyName() != null ? name2.getFamilyName().toLowerCase() : null);
if (ret == 0) {
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyName2() != null ? name1.getFamilyName().toLowerCase() : null, name2.getFamilyName2() != null ? name2.getFamilyName2().toLowerCase() : null);
}
if (ret == 0) {
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getGivenName() != null ? name1.getGivenName().toLowerCase() : null, name2.getGivenName() != null ? name2.getGivenName().toLowerCase() : null);
}
if (ret == 0) {
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getMiddleName() != null ? name1.getMiddleName().toLowerCase() : null, name2.getMiddleName() != null ? name2.getMiddleName().toLowerCase() : null);
}
if (ret == 0) {
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyNamePrefix() != null ? name1.getFamilyNamePrefix().toLowerCase() : null, name2.getFamilyNamePrefix() != null ? name2.getFamilyNamePrefix().toLowerCase() : null);
}
if (ret == 0) {
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyNameSuffix() != null ? name1.getFamilyNameSuffix().toLowerCase() : null, name2.getFamilyNameSuffix() != null ? name2.getFamilyNameSuffix().toLowerCase() : null);
}
return ret;
}
use of org.openmrs.PersonName 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