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

Taken care of patient identifier and also updated savePatient - TRUNK-2200
  • Loading branch information
wluyima committed May 9, 2013
1 parent 8b70192 commit 94ef0df
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 14 deletions.
3 changes: 3 additions & 0 deletions api/src/main/java/org/openmrs/api/PatientService.java
Expand Up @@ -77,6 +77,9 @@ public interface PatientService extends OpenmrsService {
* @should update an existing patient
* @should fail when patient does not have required patient identifiers
* @should update the date changed and changed by on update of the person address
* @should set the preferred name address and identifier if none is specified
* @should not set the preferred name address and identifier if they already exist
* @should not set a voided name or address or identifier as preferred
*/
@Authorized( { PrivilegeConstants.ADD_PATIENTS, PrivilegeConstants.EDIT_PATIENTS })
public Patient savePatient(Patient patient) throws APIException;
Expand Down
52 changes: 52 additions & 0 deletions api/src/main/java/org/openmrs/api/impl/PatientServiceImpl.java
Expand Up @@ -143,6 +143,58 @@ public Patient savePatient(Patient patient) throws APIException {
checkPatientIdentifiers(patient);
}

PatientIdentifier preferredIdentifier = null;
PatientIdentifier possiblePreferredId = patient.getPatientIdentifier();
if (possiblePreferredId != null && possiblePreferredId.isPreferred() && !possiblePreferredId.isVoided()) {
preferredIdentifier = possiblePreferredId;
}

for (PatientIdentifier id : patient.getIdentifiers()) {
if (preferredIdentifier == null && !id.isVoided()) {
id.setPreferred(true);
preferredIdentifier = id;
continue;
}

if (!id.equals(preferredIdentifier))
id.setPreferred(false);
}

PersonName preferredName = null;
PersonName possiblePreferredName = patient.getPersonName();
if (possiblePreferredName != null && possiblePreferredName.isPreferred() && !possiblePreferredName.isVoided()) {
preferredName = possiblePreferredName;
}

for (PersonName name : patient.getNames()) {
if (preferredName == null && !name.isVoided()) {
name.setPreferred(true);
preferredName = name;
continue;
}

if (!name.equals(preferredName))
name.setPreferred(false);
}

PersonAddress preferredAddress = null;
PersonAddress possiblePreferredAddress = patient.getPersonAddress();
if (possiblePreferredAddress != null && possiblePreferredAddress.isPreferred()
&& !possiblePreferredAddress.isVoided()) {
preferredAddress = possiblePreferredAddress;
}

for (PersonAddress address : patient.getAddresses()) {
if (preferredAddress == null && !address.isVoided()) {
address.setPreferred(true);
preferredAddress = address;
continue;
}

if (!address.equals(preferredAddress))
address.setPreferred(false);
}

return dao.savePatient(patient);
}

Expand Down
31 changes: 23 additions & 8 deletions api/src/main/java/org/openmrs/api/impl/PersonServiceImpl.java
Expand Up @@ -420,24 +420,39 @@ 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;
PersonName preferredName = null;
PersonName possiblePreferredName = person.getPersonName();
if (possiblePreferredName != null && possiblePreferredName.isPreferred() && !possiblePreferredName.isVoided()) {
preferredName = possiblePreferredName;
}

for (PersonName name : person.getNames()) {
if (!hasPreferredName && !name.isVoided()) {
if (preferredName == null && !name.isVoided()) {
name.setPreferred(true);
hasPreferredName = true;
preferredName = name;
continue;
}
name.setPreferred(false);

if (!name.equals(preferredName))
name.setPreferred(false);
}

PersonAddress preferredAddress = null;
PersonAddress possiblePreferredAddress = person.getPersonAddress();
if (possiblePreferredAddress != null && possiblePreferredAddress.isPreferred()
&& !possiblePreferredAddress.isVoided()) {
preferredAddress = possiblePreferredAddress;
}

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

if (!address.equals(preferredAddress))
address.setPreferred(false);
}

return dao.savePerson(person);
Expand Down
104 changes: 104 additions & 0 deletions api/src/test/java/org/openmrs/api/PatientServiceTest.java
Expand Up @@ -3275,4 +3275,108 @@ public void mergePatients_shouldMergePatientNames() throws Exception {

}

/**
* @see PatientService#savePatient(Patient)
* @verifies set the preferred name address and identifier if none is specified
*/
@Test
public void savePatient_shouldSetThePreferredNameAddressAndIdentifierIfNoneIsSpecified() throws Exception {
Patient patient = new Patient();
patient.setGender("M");
PatientIdentifier identifier = new PatientIdentifier("QWERTY", patientService.getPatientIdentifierType(2),
locationService.getLocation(1));
patient.addIdentifier(identifier);
PersonName name = new PersonName("givenName", "middleName", "familyName");
patient.addName(name);
PersonAddress address = new PersonAddress();
address.setAddress1("some address");
patient.addAddress(address);

patientService.savePatient(patient);
Assert.assertTrue(identifier.isPreferred());
Assert.assertTrue(name.isPreferred());
Assert.assertTrue(address.isPreferred());
}

/**
* @see PatientService#savePatient(Patient)
* @verifies not set the preferred name address and identifier if they already exist
*/
@Test
public void savePatient_shouldNotSetThePreferredNameAddressAndIdentifierIfTheyAlreadyExist() throws Exception {
Patient patient = new Patient();
patient.setGender("M");
PatientIdentifier identifier = new PatientIdentifier("QWERTY", patientService.getPatientIdentifierType(2),
locationService.getLocation(1));
PatientIdentifier preferredIdentifier = new PatientIdentifier("QWERTY2", patientService.getPatientIdentifierType(2),
locationService.getLocation(1));
preferredIdentifier.setPreferred(true);
patient.addIdentifier(identifier);
patient.addIdentifier(preferredIdentifier);

PersonName name = new PersonName("givenName", "middleName", "familyName");
PersonName preferredName = new PersonName("givenName", "middleName", "familyName");
preferredName.setPreferred(true);
patient.addName(name);
patient.addName(preferredName);

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

patientService.savePatient(patient);
Assert.assertTrue(preferredIdentifier.isPreferred());
Assert.assertTrue(preferredName.isPreferred());
Assert.assertTrue(preferredAddress.isPreferred());
Assert.assertFalse(identifier.isPreferred());
Assert.assertFalse(name.isPreferred());
Assert.assertFalse(address.isPreferred());
}

/**
* @see PatientService#savePatient(Patient)
* @verifies not set a voided name or address or identifier as preferred
*/
@Test
public void savePatient_shouldNotSetAVoidedNameOrAddressOrIdentifierAsPreferred() throws Exception {
Patient patient = new Patient();
patient.setGender("M");
PatientIdentifier identifier = new PatientIdentifier("QWERTY", patientService.getPatientIdentifierType(2),
locationService.getLocation(1));
PatientIdentifier preferredIdentifier = new PatientIdentifier("QWERTY2", patientService.getPatientIdentifierType(2),
locationService.getLocation(1));
preferredIdentifier.setPreferred(true);
preferredIdentifier.setVoided(true);
patient.addIdentifier(identifier);
patient.addIdentifier(preferredIdentifier);

PersonName name = new PersonName("givenName", "middleName", "familyName");
PersonName preferredName = new PersonName("givenName", "middleName", "familyName");
preferredName.setPreferred(true);
preferredName.setVoided(true);
patient.addName(name);
patient.addName(preferredName);

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

patientService.savePatient(patient);
Assert.assertFalse(preferredIdentifier.isPreferred());
Assert.assertFalse(preferredName.isPreferred());
Assert.assertFalse(preferredAddress.isPreferred());
Assert.assertTrue(identifier.isPreferred());
Assert.assertTrue(name.isPreferred());
Assert.assertTrue(address.isPreferred());
}

}
9 changes: 3 additions & 6 deletions api/src/test/java/org/openmrs/api/PersonServiceTest.java
Expand Up @@ -2044,8 +2044,7 @@ public void saveRelationshipType_shouldFailIfTheDescriptionIsNotSpecified() thro
public void savePerson_shouldSetThePreferredNameAndAddressIfNoneIsSpecified() throws Exception {
Person person = new Person();
person.setGender("M");
Assert.assertNull(person.getId());
PersonName name = new PersonName("givenName", "middleName", "familyName");
PersonName name = new PersonName("givenName", "middleName", "familyName");
person.addName(name);
PersonAddress address = new PersonAddress();
address.setAddress1("some address");
Expand All @@ -2064,8 +2063,7 @@ public void savePerson_shouldSetThePreferredNameAndAddressIfNoneIsSpecified() th
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 name = new PersonName("givenName", "middleName", "familyName");
PersonName preferredName = new PersonName("givenName", "middleName", "familyName");
preferredName.setPreferred(true);
person.addName(name);
Expand Down Expand Up @@ -2094,8 +2092,7 @@ public void savePerson_shouldNotSetThePreferredNameAndAddressIfTheyAlreadyExist(
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 name = new PersonName("givenName", "middleName", "familyName");
PersonName preferredName = new PersonName("givenName", "middleName", "familyName");
preferredName.setPreferred(true);
preferredName.setVoided(true);
Expand Down

0 comments on commit 94ef0df

Please sign in to comment.