Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TRUNK-3900: Add ProviderByPersonNameComparator
- Loading branch information
1 parent
6456b6b
commit 42b28d3
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
api/src/main/java/org/openmrs/util/ProviderByPersonNameComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.openmrs.util; | ||
|
||
import org.openmrs.Provider; | ||
|
||
import java.util.Comparator; | ||
|
||
/** | ||
* Sorts providers by the primary person name associated with the underlying person | ||
* | ||
* Note that this ignores any values stored in the provider "name" property and sorts | ||
* solely on the underlying person name | ||
* | ||
* Utilizes the PersonByName comparator to do the underlying sort, which sorts names based on the following | ||
* precedence: FamilyName, FamilyName2, GivenName, MiddleName, FamilyNamePrefix, FamilyNameSuffix | ||
*/ | ||
public class ProviderByPersonNameComparator implements Comparator<Provider> { | ||
|
||
@Override | ||
public int compare(Provider provider1, Provider provider2) { | ||
|
||
// test for null cases (sorting them to be last in a list) | ||
boolean provider1IsNull = (provider1 == null || provider1.getPerson() == null); | ||
boolean provider2IsNull = (provider2 == null || provider2.getPerson() == null); | ||
|
||
if (provider1IsNull && provider2IsNull) { | ||
return 0; | ||
} else if (provider1IsNull) { | ||
return 1; | ||
} else if (provider2IsNull) { | ||
return -1; | ||
} | ||
|
||
// delegate to the person by name comparator | ||
return new PersonByNameComparator().compare(provider1.getPerson(), provider2.getPerson()); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
api/src/test/java/org/openmrs/util/ProviderByPersonNameComparatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.openmrs.util; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.openmrs.Person; | ||
import org.openmrs.PersonName; | ||
import org.openmrs.Provider; | ||
|
||
public class ProviderByPersonNameComparatorTest { | ||
|
||
/** | ||
* @see {@link PersonByNameComparator#comparePersonsByName(org.openmrs.Person, org.openmrs.Person)} | ||
*/ | ||
@Test | ||
public void compareProvidersByPersonsName_shouldReturnNegativeIfPersonNameForProvider1ComesBeforeThatOfProvider2() | ||
throws Exception { | ||
Person person1 = new Person(); | ||
person1.addName(new PersonName("givenName", "middleName", "familyName")); | ||
Provider provider1 = new Provider(); | ||
provider1.setPerson(person1); | ||
|
||
Person person2 = new Person(); | ||
person2.addName(new PersonName("givenName", "middleNamf", "familyName")); | ||
Provider provider2 = new Provider(); | ||
provider2.setPerson(person2); | ||
|
||
int actualValue = new ProviderByPersonNameComparator().compare(provider1, provider2); | ||
Assert.assertTrue("Expected a negative value but it was: " + actualValue, actualValue < 0); | ||
} | ||
|
||
/** | ||
* @see {@link PersonByNameComparator#comparePersonsByName(Person,Person)} | ||
*/ | ||
@Test | ||
public void compareProvidersByPersonName_shouldReturnPositiveIfPersonNameForProvider1ComesAfterThatOfProvider2() | ||
throws Exception { | ||
Person person1 = new Person(); | ||
person1.addName(new PersonName("givenNamf", "middleName", "familyName")); | ||
Provider provider1 = new Provider(); | ||
provider1.setPerson(person1); | ||
|
||
Person person2 = new Person(); | ||
person2.addName(new PersonName("givenName", "middleName", "familyName")); | ||
Provider provider2 = new Provider(); | ||
provider2.setPerson(person2); | ||
|
||
int actualValue = new ProviderByPersonNameComparator().compare(provider1, provider2); | ||
Assert.assertTrue("Expected a positive value but it was: " + actualValue, actualValue > 0); | ||
} | ||
|
||
/** | ||
* @see {@link PersonByNameComparator#comparePersonsByName(Person,Person)} | ||
*/ | ||
@Test | ||
public void compareProvidersByPersonName_shouldReturnZeroIfTheGivenNameMiddleNameAndFamilyNameMatch() throws Exception { | ||
Person person1 = new Person(); | ||
person1.addName(new PersonName("givenName", "middleName", "familyName")); | ||
Provider provider1 = new Provider(); | ||
provider1.setPerson(person1); | ||
|
||
Person person2 = new Person(); | ||
person2.addName(new PersonName("givenName", "middleName", "familyName")); | ||
Provider provider2 = new Provider(); | ||
provider2.setPerson(person2); | ||
|
||
int actualValue = new ProviderByPersonNameComparator().compare(provider1, provider2); | ||
Assert.assertTrue("Expected zero but it was: " + actualValue, actualValue == 0); | ||
} | ||
|
||
@Test | ||
public void compareProvidersByPersonName_shouldNotFailIfProvider1HasNoAssociatedPerson() throws Exception { | ||
Provider provider1 = new Provider(); | ||
|
||
Person person2 = new Person(); | ||
person2.addName(new PersonName("givenName", "middleName", "familyName")); | ||
Provider provider2 = new Provider(); | ||
provider2.setPerson(person2); | ||
|
||
int actualValue = new ProviderByPersonNameComparator().compare(provider1, provider2); | ||
Assert.assertTrue("Expected a positive value but it was: " + actualValue, actualValue > 0); | ||
; | ||
} | ||
|
||
@Test | ||
public void compareProvidersByPersonName_shouldNotFailIfProvider2HasNoAssociatedPerson() throws Exception { | ||
Person person1 = new Person(); | ||
person1.addName(new PersonName("givenName", "middleName", "familyName")); | ||
Provider provider1 = new Provider(); | ||
provider1.setPerson(person1); | ||
|
||
Provider provider2 = new Provider(); | ||
|
||
int actualValue = new ProviderByPersonNameComparator().compare(provider1, provider2); | ||
Assert.assertTrue("Expected a negative value but it was: " + actualValue, actualValue < 0); | ||
} | ||
|
||
@Test | ||
public void compareProvidersByPersonName_shouldNotFailIfNeitherProviderHasAnAssociatedPerson() throws Exception { | ||
|
||
Provider provider1 = new Provider(); | ||
Provider provider2 = new Provider(); | ||
|
||
int actualValue = new ProviderByPersonNameComparator().compare(provider1, provider2); | ||
Assert.assertTrue("Expected zero but it was: " + actualValue, actualValue == 0); | ||
} | ||
} |