Skip to content

Commit

Permalink
TRUNK-3900: Add ProviderByPersonNameComparator
Browse files Browse the repository at this point in the history
  • Loading branch information
mogoodrich committed Feb 21, 2013
1 parent 2906c95 commit 131314b
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 58 deletions.
65 changes: 32 additions & 33 deletions api/src/main/java/org/openmrs/hl7/handler/ORUR01Handler.java
Expand Up @@ -22,36 +22,6 @@
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Concept;
import org.openmrs.ConceptAnswer;
import org.openmrs.ConceptName;
import org.openmrs.ConceptProposal;
import org.openmrs.Drug;
import org.openmrs.Encounter;
import org.openmrs.EncounterRole;
import org.openmrs.EncounterType;
import org.openmrs.Form;
import org.openmrs.Location;
import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.Person;
import org.openmrs.PersonAttribute;
import org.openmrs.PersonAttributeType;
import org.openmrs.Provider;
import org.openmrs.Relationship;
import org.openmrs.RelationshipType;
import org.openmrs.User;
import org.openmrs.api.context.Context;
import org.openmrs.hl7.HL7Constants;
import org.openmrs.hl7.HL7InQueueProcessor;
import org.openmrs.hl7.HL7Service;
import org.openmrs.obs.ComplexData;
import org.openmrs.util.OpenmrsConstants;
import org.openmrs.util.OpenmrsUtil;
import org.springframework.util.StringUtils;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.app.Application;
import ca.uhn.hl7v2.app.ApplicationException;
Expand Down Expand Up @@ -88,6 +58,35 @@
import ca.uhn.hl7v2.model.v25.segment.PV1;
import ca.uhn.hl7v2.parser.EncodingCharacters;
import ca.uhn.hl7v2.parser.PipeParser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Concept;
import org.openmrs.ConceptAnswer;
import org.openmrs.ConceptName;
import org.openmrs.ConceptProposal;
import org.openmrs.Drug;
import org.openmrs.Encounter;
import org.openmrs.EncounterRole;
import org.openmrs.EncounterType;
import org.openmrs.Form;
import org.openmrs.Location;
import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.Person;
import org.openmrs.PersonAttribute;
import org.openmrs.PersonAttributeType;
import org.openmrs.Provider;
import org.openmrs.Relationship;
import org.openmrs.RelationshipType;
import org.openmrs.User;
import org.openmrs.api.context.Context;
import org.openmrs.hl7.HL7Constants;
import org.openmrs.hl7.HL7InQueueProcessor;
import org.openmrs.hl7.HL7Service;
import org.openmrs.obs.ComplexData;
import org.openmrs.util.OpenmrsConstants;
import org.openmrs.util.OpenmrsUtil;
import org.springframework.util.StringUtils;

/**
* Parses ORUR01 messages into openmrs Encounter objects Usage: GenericParser parser = new
Expand Down Expand Up @@ -138,7 +137,7 @@ public boolean canProcess(Message message) {
* @should create an encounter and find the provider by uuid
* @should create an encounter and find the provider by providerId
* @should fail if the provider name type code is not specified and is not a personId
* @should set complex data for obs with complex concepts
* @should set complex data for obs with complex concepts
*/
public Message processMessage(Message message) throws ApplicationException {

Expand Down Expand Up @@ -1081,14 +1080,14 @@ private Location getLocation(PV1 pv1) throws HL7Exception {
location.setLocationId(locationId);
return location;
}


/**
* needs to find a Form based on information in MSH-21. example: 16^AMRS.ELD.FORMID
*
* @param msh
* @return
* @throws HL7Exception
*/
*/
private Form getForm(MSH msh) throws HL7Exception {
Integer formId = null;
try {
Expand Down
@@ -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.

Copy link
@djazayeri

djazayeri Feb 21, 2013

Member

I would avoid explicitly writing this here, in case it ever changes...

This comment has been minimized.

Copy link
@mogoodrich

mogoodrich Feb 21, 2013

Author Member

fair enough

*/
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());
}
}
45 changes: 22 additions & 23 deletions api/src/test/java/org/openmrs/hl7/handler/ORUR01HandlerTest.java
Expand Up @@ -13,16 +13,21 @@
*/
package org.openmrs.hl7.handler;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Vector;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.app.ApplicationException;
import ca.uhn.hl7v2.app.MessageTypeRouter;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v25.message.ORU_R01;
import ca.uhn.hl7v2.model.v25.segment.NK1;
import ca.uhn.hl7v2.model.v25.segment.OBR;
import ca.uhn.hl7v2.model.v25.segment.OBX;
import ca.uhn.hl7v2.parser.GenericParser;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -51,15 +56,9 @@
import org.openmrs.test.Verifies;
import org.openmrs.util.OpenmrsConstants;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.app.ApplicationException;
import ca.uhn.hl7v2.app.MessageTypeRouter;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v25.message.ORU_R01;
import ca.uhn.hl7v2.model.v25.segment.NK1;
import ca.uhn.hl7v2.model.v25.segment.OBR;
import ca.uhn.hl7v2.model.v25.segment.OBX;
import ca.uhn.hl7v2.parser.GenericParser;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
* TODO finish testing all methods ORUR01Handler
Expand Down Expand Up @@ -1099,15 +1098,15 @@ public void processMessage_shouldFailIfTheProviderNameTypeCodeIsNotSpecifiedAndI
Message hl7message = parser.parse(hl7string);
router.processMessage(hl7message);
}

/**
* @see {@link ORUR01Handler#processMessage(Message)}
*
*/
@Test
@Verifies(value = "should set complex data for obs with complex concepts", method = "processMessage(Message)")
public void processMessage_shouldSetComplexDataForObsWithComplexConcepts() throws Exception {
ObsHandler handler = new ObsHandler();
* @see {@link ORUR01Handler#processMessage(Message)}
*
*/
@Test
@Verifies(value = "should set complex data for obs with complex concepts", method = "processMessage(Message)")
public void processMessage_shouldSetComplexDataForObsWithComplexConcepts() throws Exception {
ObsHandler handler = new ObsHandler();
final String handlerName = "NeigborHandler";
final String data = "{\"firstname\":\"Horatio\"}";
Context.getObsService().registerHandler(handlerName, handler);
Expand All @@ -1125,7 +1124,7 @@ public void processMessage_shouldSetComplexDataForObsWithComplexConcepts() throw
Context.getObsService().removeHandler(handlerName);
}
Assert.assertEquals(data, handler.getCreatedObs().getComplexData().getData());
}
}

private class ObsHandler implements ComplexObsHandler {

Expand Down Expand Up @@ -1154,5 +1153,5 @@ public boolean purgeComplexData(Obs obs) {
return false;
}

}
}
}
@@ -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);
}
}
Expand Up @@ -42,7 +42,6 @@
import javax.servlet.http.HttpServletResponse;

import liquibase.changelog.ChangeSet;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -1328,7 +1327,7 @@ public void run() {
}

// connect via jdbc with root user and create an openmrs user
String host="'%'";
String host = "'%'";
if (wizardModel.databaseConnection.contains("localhost")) {
host = "'localhost'";
}
Expand Down

3 comments on commit 131314b

@mogoodrich
Copy link
Member Author

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!

@djazayeri
Copy link
Member

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.

@mogoodrich
Copy link
Member Author

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...

Please sign in to comment.