Skip to content

Commit

Permalink
Manage preferred status for PersonName, PersonAddress and
Browse files Browse the repository at this point in the history
PatientIdentifier in the API - TRUNK-2200
  • Loading branch information
wluyima committed May 9, 2013
1 parent 94daadd commit de8c005
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/src/main/java/org/openmrs/api/PersonService.java
Expand Up @@ -715,6 +715,9 @@ public List<Relationship> getRelationships(Person fromPerson, Person toPerson, R
* @throws APIException
* @should create new object when person id is null
* @should update existing object when person id is not null
* @should set the preferred name and address if none is specified
* @should not set the preferred name and address if they already exist
* @should not set a voided name or address as preferred
*/
@Authorized( { PrivilegeConstants.ADD_PERSONS, PrivilegeConstants.EDIT_PERSONS })
public Person savePerson(Person person) throws APIException;
Expand Down
20 changes: 20 additions & 0 deletions api/src/main/java/org/openmrs/api/impl/PersonServiceImpl.java
Expand Up @@ -420,6 +420,26 @@ public void purgePerson(Person person) throws APIException {
* @see org.openmrs.api.PersonService#savePerson(org.openmrs.Person)
*/
public Person savePerson(Person person) throws APIException {
boolean hasPreferredName = false;
for (PersonName name : person.getNames()) {
if (!hasPreferredName && !name.isVoided()) {
name.setPreferred(true);
hasPreferredName = true;
continue;
}
name.setPreferred(false);
}

boolean hasPreferredAddress = false;
for (PersonAddress address : person.getAddresses()) {
if (!hasPreferredAddress && !address.isVoided()) {
address.setPreferred(true);
hasPreferredAddress = true;
continue;
}
address.setPreferred(false);
}

return dao.savePerson(person);
}

Expand Down
82 changes: 82 additions & 0 deletions api/src/test/java/org/openmrs/api/PersonServiceTest.java
Expand Up @@ -2036,4 +2036,86 @@ public void saveRelationshipType_shouldFailIfTheDescriptionIsNotSpecified() thro
personService.saveRelationshipType(relationshipType);
}

/**
* @see {@link PersonService#savePerson(Person)}
*/
@Test
@Verifies(value = "should set the preferred name and address if none is specified", method = "savePerson(Person)")
public void savePerson_shouldSetThePreferredNameAndAddressIfNoneIsSpecified() throws Exception {
Person person = new Person();
person.setGender("M");
Assert.assertNull(person.getId());
PersonName name = new PersonName("givenName", "middleName", "familyName");
person.addName(name);
PersonAddress address = new PersonAddress();
address.setAddress1("some address");
person.addAddress(address);

personService.savePerson(person);
Assert.assertTrue(name.isPreferred());
Assert.assertTrue(address.isPreferred());
}

/**
* @see {@link PersonService#savePerson(Person)}
*/
@Test
@Verifies(value = "should not set the preferred name and address if they already exist", method = "savePerson(Person)")
public void savePerson_shouldNotSetThePreferredNameAndAddressIfTheyAlreadyExist() throws Exception {
Person person = new Person();
person.setGender("M");
Assert.assertNull(person.getId());
PersonName name = new PersonName("givenName", "middleName", "familyName");
PersonName preferredName = new PersonName("givenName", "middleName", "familyName");
preferredName.setPreferred(true);
person.addName(name);
person.addName(preferredName);

PersonAddress address = new PersonAddress();
address.setAddress1("some address");
PersonAddress preferredAddress = new PersonAddress();
preferredAddress.setAddress1("another address");
preferredAddress.setPreferred(true);
person.addAddress(address);
person.addAddress(preferredAddress);

personService.savePerson(person);
Assert.assertTrue(preferredName.isPreferred());
Assert.assertTrue(preferredAddress.isPreferred());
Assert.assertFalse(name.isPreferred());
Assert.assertFalse(address.isPreferred());
}

/**
* @see {@link PersonService#savePerson(Person)}
*/
@Test
@Verifies(value = "should not set a voided name or address as preferred", method = "savePerson(Person)")
public void savePerson_shouldNotSetAVoidedNameOrAddressAsPreferred() throws Exception {
Person person = new Person();
person.setGender("M");
Assert.assertNull(person.getId());
PersonName name = new PersonName("givenName", "middleName", "familyName");
PersonName preferredName = new PersonName("givenName", "middleName", "familyName");
preferredName.setPreferred(true);
preferredName.setVoided(true);
person.addName(name);
person.addName(preferredName);

PersonAddress address = new PersonAddress();
address.setAddress1("some address");
PersonAddress preferredAddress = new PersonAddress();
preferredAddress.setAddress1("another address");
preferredAddress.setPreferred(true);
preferredAddress.setVoided(true);
person.addAddress(address);
person.addAddress(preferredAddress);

personService.savePerson(person);
Assert.assertFalse(preferredName.isPreferred());
Assert.assertFalse(preferredAddress.isPreferred());
Assert.assertTrue(name.isPreferred());
Assert.assertTrue(address.isPreferred());
}

}

0 comments on commit de8c005

Please sign in to comment.