Skip to content

Commit

Permalink
Old address will be voided and not overriden when address is edited f…
Browse files Browse the repository at this point in the history
…rom web form
  • Loading branch information
subhrajitroy committed Jul 12, 2011
1 parent 0d2f245 commit ea53c46
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.motechproject.server.model;

import org.motechproject.server.model.ghana.Community;
import org.motechproject.server.model.ghana.Facility;
import org.openmrs.Location;
import org.openmrs.PersonAddress;

public class PatientAddress {
private Location location;
private Community community;
private String addressLine1;

public PatientAddress near(Facility facility) {
this.location = facility.getLocation();
return this;
}

public PatientAddress in(Location location) {
this.location = location;
return this;
}

public PatientAddress in(Community community) {
this.community = community;
return this;
}

public PatientAddress at(String addressLine1) {
this.addressLine1 = addressLine1;
return this;
}

public PersonAddress build() {
PersonAddress address = new PersonAddress();
address.setAddress1(addressLine1);
if (community != null) {
address.setAddress2(community.getName());
}
address.setRegion(location.getRegion());
address.setCountyDistrict(location.getCountyDistrict());
address.setStateProvince(location.getStateProvince());
address.setNeighborhoodCell(location.getName());
return address;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.motechproject.server.model.ghana.Community;
import org.motechproject.server.model.ghana.Facility;
import org.openmrs.Patient;
import org.openmrs.PersonAddress;
import org.openmrs.PersonName;

import java.util.Set;
Expand Down Expand Up @@ -85,4 +86,20 @@ private void updateName(PersonName fromName, PersonName toName) {
toName.setMiddleName(fromName.getMiddleName());
toName.setFamilyName(fromName.getFamilyName());
}

public PatientEditor editAddress(PersonAddress newAddress) {
if (patient.getPersonAddress() == null) {
patient.addAddress(newAddress);
return this;
}

PersonAddress oldAddress = patient.getPersonAddress();
if(!oldAddress.equalsContent(newAddress)){
oldAddress.setVoided(true);
oldAddress.setVoidReason("Address edited");
patient.addAddress(newAddress);
}

return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ public void editPatient(Patient patient, String firstName,
String middleName, String lastName, String preferredName,
Date dateOfBirth, Boolean estimatedBirthDate, Gender sex,
Boolean insured, String nhis, Date nhisExpires, Patient mother,
Community community, String address, String phoneNumber,
Community community, String addressLine1, String phoneNumber,
Date expDeliveryDate, Boolean enroll, Boolean consent,
ContactNumberType ownership, MediaType format, String language,
DayOfWeek dayOfWeek, Date timeOfDay, Facility facility) {
Expand All @@ -520,16 +520,6 @@ public void editPatient(Patient patient, String firstName,
patient.setBirthdateEstimated(estimatedBirthDate);
patient.setGender(GenderTypeConverter.toOpenMRSString(sex));


PersonAddress patientAddress = patient.getPersonAddress();
if (patientAddress == null) {
patientAddress = new PersonAddress();
patientAddress.setAddress1(address);
patient.addAddress(patientAddress);
} else {
patientAddress.setAddress1(address);
}

relationshipService.saveOrUpdateMotherRelationship(mother, patient, true);

PatientEditor editor = new PatientEditor(patient);
Expand All @@ -544,7 +534,7 @@ public void editPatient(Patient patient, String firstName,
boolean bothCommunitiesExistAndAreSame = community != null && currentCommunity != null && currentCommunity.equals(community);

if (!bothCommunitiesExistAndAreSame) {
patient = editor.removeFrom(currentCommunity).addTo(community).done();
editor.removeFrom(currentCommunity).addTo(community);
}

setPatientAttributes(patient, phoneNumber, ownership, format, language,
Expand All @@ -553,6 +543,8 @@ public void editPatient(Patient patient, String firstName,
editor.editName(new PersonName(firstName,middleName,lastName));
editor.editPreferredName(new PersonName(preferredName,middleName,lastName));

editor.editAddress(new PatientAddress().near(facility).in(community).at(addressLine1).build());

patientService.savePatient(patient);

Integer dueDateObsId = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.motechproject.server.svc.impl;

import org.junit.Test;
import org.motechproject.server.model.PatientEditor;
import org.openmrs.Patient;
import org.openmrs.PersonAddress;

import static org.junit.Assert.assertTrue;

public class PatientAddressEditorTest {

@Test
public void shouldAddNewAddressIfNoneExists() {
Patient patient = new Patient();
PatientEditor editor = new PatientEditor(patient);
PersonAddress address = new PersonAddress();
address.setAddress1("1st Cross");
address.setRegion("Upper East");
address.setCountyDistrict("Kassenna Nankana West");
address.setStateProvince("Navio");
address.setNeighborhoodCell("Navio CHPS");

editor.editAddress(address).done();

assertTrue(patient.getAddresses().size() == 1);
for (PersonAddress personAddress : patient.getAddresses()) {
assertTrue(personAddress.equalsContent(address));
}
}

@Test
public void shouldVoidExistingAddressAndAddNewOne() {
Patient patient = new Patient();
PatientEditor editor = new PatientEditor(patient);

PersonAddress oldAddress = new PersonAddress();
oldAddress.setAddress1("1st Cross");
oldAddress.setRegion("Upper East");
oldAddress.setCountyDistrict("Kassenna Nankana West");
oldAddress.setStateProvince("Navio");
oldAddress.setNeighborhoodCell("Navio CHPS");

patient.addAddress(oldAddress);

PersonAddress newAddress = new PersonAddress();
newAddress.setAddress1("2nd Cross");
newAddress.setRegion("Upper East");
newAddress.setCountyDistrict("Kassenna Nankana West");
newAddress.setStateProvince("Navio");
newAddress.setNeighborhoodCell("Navio CHPS");

editor.editAddress(newAddress).done();

assertTrue(patient.getAddresses().size() == 2);
for (PersonAddress personAddress : patient.getAddresses()) {
if(personAddress.isVoided()){
assertTrue(personAddress.equalsContent(oldAddress));
}else{
assertTrue(personAddress.equalsContent(newAddress));
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,7 @@ public void editPatientAll() throws ParseException {

Facility facility = new Facility();
facility.setFacilityId(11117);
facility.setLocation(new Location());
expect(motechService.facilityFor(patient)).andReturn(facility);

replay(contextService, patientService, motechService, personService,
Expand Down

0 comments on commit ea53c46

Please sign in to comment.