Skip to content

Commit

Permalink
TRUNK-3992: PersonByNameComparator should be case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
mogoodrich committed May 23, 2013
1 parent 3d2266e commit c5c4876
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
25 changes: 17 additions & 8 deletions api/src/main/java/org/openmrs/util/PersonByNameComparator.java
Expand Up @@ -13,11 +13,11 @@
*/
package org.openmrs.util;

import java.util.Comparator;

import org.openmrs.Person;
import org.openmrs.PersonName;

import java.util.Comparator;

/**
* A simple person comparator for sorting persons by name. Sorts names based on the following
* precedence: FamilyName, FamilyName2, GivenName, MiddleName, FamilyNamePrefix, FamilyNameSuffix
Expand All @@ -40,6 +40,7 @@ public int compare(Person person1, Person person2) {
* @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) {
Expand All @@ -55,26 +56,34 @@ public static int comparePersonsByName(Person person1, Person person2) {
PersonName name1 = person1.getPersonName();
PersonName name2 = person2.getPersonName();

int ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyName(), name2.getFamilyName());
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(), name2.getFamilyName2());
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(), name2.getGivenName());
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(), name2.getMiddleName());
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(), name2.getFamilyNamePrefix());
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(), name2.getFamilyNameSuffix());
ret = OpenmrsUtil.compareWithNullAsGreatest(name1.getFamilyNameSuffix() != null ? name1.getFamilyNameSuffix()
.toLowerCase() : null, name2.getFamilyNameSuffix() != null ? name2.getFamilyNameSuffix().toLowerCase()
: null);
}

return ret;
Expand Down
11 changes: 11 additions & 0 deletions api/src/test/java/org/openmrs/util/PersonByNameComparatorTest.java
Expand Up @@ -65,4 +65,15 @@ public void comparePersonsByName_shouldReturnZeroIfTheGivenNameMiddleNameAndFami
int actualValue = PersonByNameComparator.comparePersonsByName(person1, person2);
Assert.assertTrue("Expected zero but it was: " + actualValue, actualValue == 0);
}

@Test
@Verifies(value = "should not be case-sensitive", method = "comparePersonsByName(Person,Person)")
public void comparePersonsByName_shouldNotBeCaseSensitive() throws Exception {
Person person1 = new Person();
person1.addName(new PersonName("GIVENNAME", "MIDDLENAME", "FAMILYNAME"));
Person person2 = new Person();
person2.addName(new PersonName("givenName", "middleName", "familyName"));
int actualValue = PersonByNameComparator.comparePersonsByName(person1, person2);
Assert.assertTrue("Expected zero but it was: " + actualValue, actualValue == 0);
}
}

0 comments on commit c5c4876

Please sign in to comment.