Skip to content

Commit de8c005

Browse files
committedMay 9, 2013
Manage preferred status for PersonName, PersonAddress and
PatientIdentifier in the API - TRUNK-2200
1 parent 94daadd commit de8c005

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
 

‎api/src/main/java/org/openmrs/api/PersonService.java

+3
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,9 @@ public List<Relationship> getRelationships(Person fromPerson, Person toPerson, R
715715
* @throws APIException
716716
* @should create new object when person id is null
717717
* @should update existing object when person id is not null
718+
* @should set the preferred name and address if none is specified
719+
* @should not set the preferred name and address if they already exist
720+
* @should not set a voided name or address as preferred
718721
*/
719722
@Authorized( { PrivilegeConstants.ADD_PERSONS, PrivilegeConstants.EDIT_PERSONS })
720723
public Person savePerson(Person person) throws APIException;

‎api/src/main/java/org/openmrs/api/impl/PersonServiceImpl.java

+20
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,26 @@ public void purgePerson(Person person) throws APIException {
420420
* @see org.openmrs.api.PersonService#savePerson(org.openmrs.Person)
421421
*/
422422
public Person savePerson(Person person) throws APIException {
423+
boolean hasPreferredName = false;
424+
for (PersonName name : person.getNames()) {
425+
if (!hasPreferredName && !name.isVoided()) {
426+
name.setPreferred(true);
427+
hasPreferredName = true;
428+
continue;
429+
}
430+
name.setPreferred(false);
431+
}
432+
433+
boolean hasPreferredAddress = false;
434+
for (PersonAddress address : person.getAddresses()) {
435+
if (!hasPreferredAddress && !address.isVoided()) {
436+
address.setPreferred(true);
437+
hasPreferredAddress = true;
438+
continue;
439+
}
440+
address.setPreferred(false);
441+
}
442+
423443
return dao.savePerson(person);
424444
}
425445

‎api/src/test/java/org/openmrs/api/PersonServiceTest.java

+82
Original file line numberDiff line numberDiff line change
@@ -2036,4 +2036,86 @@ public void saveRelationshipType_shouldFailIfTheDescriptionIsNotSpecified() thro
20362036
personService.saveRelationshipType(relationshipType);
20372037
}
20382038

2039+
/**
2040+
* @see {@link PersonService#savePerson(Person)}
2041+
*/
2042+
@Test
2043+
@Verifies(value = "should set the preferred name and address if none is specified", method = "savePerson(Person)")
2044+
public void savePerson_shouldSetThePreferredNameAndAddressIfNoneIsSpecified() throws Exception {
2045+
Person person = new Person();
2046+
person.setGender("M");
2047+
Assert.assertNull(person.getId());
2048+
PersonName name = new PersonName("givenName", "middleName", "familyName");
2049+
person.addName(name);
2050+
PersonAddress address = new PersonAddress();
2051+
address.setAddress1("some address");
2052+
person.addAddress(address);
2053+
2054+
personService.savePerson(person);
2055+
Assert.assertTrue(name.isPreferred());
2056+
Assert.assertTrue(address.isPreferred());
2057+
}
2058+
2059+
/**
2060+
* @see {@link PersonService#savePerson(Person)}
2061+
*/
2062+
@Test
2063+
@Verifies(value = "should not set the preferred name and address if they already exist", method = "savePerson(Person)")
2064+
public void savePerson_shouldNotSetThePreferredNameAndAddressIfTheyAlreadyExist() throws Exception {
2065+
Person person = new Person();
2066+
person.setGender("M");
2067+
Assert.assertNull(person.getId());
2068+
PersonName name = new PersonName("givenName", "middleName", "familyName");
2069+
PersonName preferredName = new PersonName("givenName", "middleName", "familyName");
2070+
preferredName.setPreferred(true);
2071+
person.addName(name);
2072+
person.addName(preferredName);
2073+
2074+
PersonAddress address = new PersonAddress();
2075+
address.setAddress1("some address");
2076+
PersonAddress preferredAddress = new PersonAddress();
2077+
preferredAddress.setAddress1("another address");
2078+
preferredAddress.setPreferred(true);
2079+
person.addAddress(address);
2080+
person.addAddress(preferredAddress);
2081+
2082+
personService.savePerson(person);
2083+
Assert.assertTrue(preferredName.isPreferred());
2084+
Assert.assertTrue(preferredAddress.isPreferred());
2085+
Assert.assertFalse(name.isPreferred());
2086+
Assert.assertFalse(address.isPreferred());
2087+
}
2088+
2089+
/**
2090+
* @see {@link PersonService#savePerson(Person)}
2091+
*/
2092+
@Test
2093+
@Verifies(value = "should not set a voided name or address as preferred", method = "savePerson(Person)")
2094+
public void savePerson_shouldNotSetAVoidedNameOrAddressAsPreferred() throws Exception {
2095+
Person person = new Person();
2096+
person.setGender("M");
2097+
Assert.assertNull(person.getId());
2098+
PersonName name = new PersonName("givenName", "middleName", "familyName");
2099+
PersonName preferredName = new PersonName("givenName", "middleName", "familyName");
2100+
preferredName.setPreferred(true);
2101+
preferredName.setVoided(true);
2102+
person.addName(name);
2103+
person.addName(preferredName);
2104+
2105+
PersonAddress address = new PersonAddress();
2106+
address.setAddress1("some address");
2107+
PersonAddress preferredAddress = new PersonAddress();
2108+
preferredAddress.setAddress1("another address");
2109+
preferredAddress.setPreferred(true);
2110+
preferredAddress.setVoided(true);
2111+
person.addAddress(address);
2112+
person.addAddress(preferredAddress);
2113+
2114+
personService.savePerson(person);
2115+
Assert.assertFalse(preferredName.isPreferred());
2116+
Assert.assertFalse(preferredAddress.isPreferred());
2117+
Assert.assertTrue(name.isPreferred());
2118+
Assert.assertTrue(address.isPreferred());
2119+
}
2120+
20392121
}

0 commit comments

Comments
 (0)
Please sign in to comment.