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
2906c95
commit 131314b
Showing
5 changed files
with
223 additions
and
58 deletions.
There are no files selected for viewing
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
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
/** | ||
* The contents of this file are subject to the OpenMRS Public License | ||
* Version 1.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* http://license.openmrs.org | ||
* | ||
* Software distributed under the License is distributed on an "AS IS" | ||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing rights and limitations | ||
* under the License. | ||
* | ||
* Copyright (C) OpenMRS, LLC. All Rights Reserved. | ||
*/ | ||
package org.openmrs.util; | ||
|
||
import java.util.Comparator; | ||
|
||
import org.openmrs.Provider; | ||
|
||
/** | ||
* 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 | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
*/ | ||
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()); | ||
} | ||
} |
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
119 changes: 119 additions & 0 deletions
119
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,119 @@ | ||
/** | ||
* The contents of this file are subject to the OpenMRS Public License | ||
* Version 1.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* http://license.openmrs.org | ||
* | ||
* Software distributed under the License is distributed on an "AS IS" | ||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing rights and limitations | ||
* under the License. | ||
* | ||
* Copyright (C) OpenMRS, LLC. All Rights Reserved. | ||
*/ | ||
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); | ||
} | ||
} |
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
3 comments
on commit 131314b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about not pushing up the random maven auto-formatting in another commit!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me Mark.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, since i backported it this morning... :)
I'll go ahead and change the comment...
I would avoid explicitly writing this here, in case it ever changes...