Skip to content

Commit

Permalink
TRUNK-5408 Patient Constructor that takes in Patient (Revision) (open…
Browse files Browse the repository at this point in the history
…mrs#2697)

* Added since annotations, made patient identifier clonable, ensured cloned patients identifiers refer to correct patient

* Removed redundant code
  • Loading branch information
shaoyuancc authored and dkayiwa committed Aug 6, 2018
1 parent 20504f5 commit b44b9a7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
10 changes: 8 additions & 2 deletions api/src/main/java/org/openmrs/Patient.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,19 @@ public Patient(Integer patientId) {
* <br>
*
* @param patient the person object to copy onto a new Patient
* @since 2.2.0
*/
public Patient(Patient patient){
super(patient);
this.patientId = patient.getPatientId();
this.allergyStatus = patient.getAllergyStatus();
this.identifiers = patient.getIdentifiers();
Set<PatientIdentifier> newIdentifiers = new TreeSet<>();
for (PatientIdentifier pid : patient.getIdentifiers()) {
PatientIdentifier identifierClone = (PatientIdentifier) pid.clone();
identifierClone.setPatient(this);
newIdentifiers.add(identifierClone);
}
this.identifiers = newIdentifiers;
}

// Property accessors
Expand Down Expand Up @@ -381,7 +388,6 @@ public Integer getId() {
@Override
public void setId(Integer id) {
setPatientId(id);

}

/**
Expand Down
20 changes: 19 additions & 1 deletion api/src/main/java/org/openmrs/PatientIdentifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @see org.openmrs.PatientIdentifierType
*/
@Indexed
public class PatientIdentifier extends BaseChangeableOpenmrsData implements java.io.Serializable, Comparable<PatientIdentifier> {
public class PatientIdentifier extends BaseChangeableOpenmrsData implements java.io.Serializable, Cloneable, Comparable<PatientIdentifier> {

public static final long serialVersionUID = 1123121L;

Expand Down Expand Up @@ -262,6 +262,24 @@ public Integer getPatientIdentifierId() {
public void setPatientIdentifierId(Integer patientIdentifierId) {
this.patientIdentifierId = patientIdentifierId;
}

/**
* bitwise copy of the PatientIdentifier object. NOTICE: THIS WILL NOT COPY THE PATIENT OBJECT. The
* PatientIdentifier.patient object in this object AND the cloned object will point at the same
* patient
*
* @return New PatientIdentifier object
* @since 2.2.0
*/
@Override
public Object clone() {
try {
return super.clone();
}
catch (CloneNotSupportedException e) {
throw new InternalError("PatientIdentifier should be cloneable");
}
}

/**
Provides a default comparator.
Expand Down
31 changes: 25 additions & 6 deletions api/src/test/java/org/openmrs/PatientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertSame;

import java.util.Date;
import java.util.HashSet;
Expand Down Expand Up @@ -44,17 +45,27 @@ public void patient_shouldConstructCloneWhenPassedPatient() {
pn.setMiddleName("middleName");
pnSet.add(pn);

PatientIdentifier pa1 = new PatientIdentifier();
PatientIdentifier pi1 = new PatientIdentifier();
PatientIdentifierType identifierType = new PatientIdentifierType(1);
Location location = new Location(1);

pa1.setIdentifier("theid");
pa1.setIdentifierType(identifierType);
pa1.setLocation(location);
pa1.setVoided(true);
pi1.setIdentifier("theid");
pi1.setIdentifierType(identifierType);
pi1.setLocation(location);
pi1.setVoided(true);

PatientIdentifier pi2 = new PatientIdentifier();
PatientIdentifierType identifierType2 = new PatientIdentifierType(2);
Location location2 = new Location(2);

pi2.setIdentifier("theid2");
pi2.setIdentifierType(identifierType2);
pi2.setLocation(location2);
pi2.setVoided(false);

p.setNames(pnSet);
p.addIdentifier(pa1);
p.addIdentifier(pi1);
p.addIdentifier(pi2);
p.setAllergyStatus("TestAllergy");
p.setId(1);

Expand All @@ -65,6 +76,14 @@ public void patient_shouldConstructCloneWhenPassedPatient() {
assertEquals(p.getNames(), p2.getNames());
assertEquals(p.getGivenName(), p2.getGivenName());
assertEquals(p.getIdentifiers(),p2.getIdentifiers());
// Check that each identifier refers to the correct patient object
for (PatientIdentifier pid : p2.getIdentifiers()) {
assertSame(p2, pid.getPatient());
}
// Make sure that the original patient hasn't been dirtied
for (PatientIdentifier pid : p.getIdentifiers()) {
assertSame(p, pid.getPatient());
}
assertEquals(p.getPatientId(), p2.getPatientId());
assertEquals(p.getId(), p2.getId());
assertEquals(p.getPerson(), p2.getPerson());
Expand Down

0 comments on commit b44b9a7

Please sign in to comment.