Skip to content

Commit

Permalink
TRUNK-3014: Applying patch to propagate attrs to obs.groupMembers whe…
Browse files Browse the repository at this point in the history
…n invokin Encounter.addObs
  • Loading branch information
acbarbosa authored and djazayeri committed Dec 11, 2012
1 parent 8b9b742 commit 41f320b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 17 deletions.
47 changes: 39 additions & 8 deletions api/src/main/java/org/openmrs/Encounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
*/
package org.openmrs;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -269,20 +271,49 @@ public void setObs(Set<Obs> obs) {
* @should set encounter attribute on obs
* @should add obs to non null initial obs set
* @should add encounter attrs to obs if attributes are null
* @should add encounter attrs to obs groupMembers if attributes are null
*/
public void addObs(Obs observation) {
if (obs == null)
obs = new HashSet<Obs>();

if (observation != null) {
observation.setEncounter(this);

if (observation.getObsDatetime() == null)
observation.setObsDatetime(getEncounterDatetime());
if (observation.getPerson() == null)
observation.setPerson(getPatient());
if (observation.getLocation() == null)
observation.setLocation(getLocation());
obs.add(observation);

//Propagate some attributes to the obs and any groupMembers

// a Deque is a two-ended queue, that lets us add to the end, and fetch from the beginning
Deque<Obs> obsToUpdate = new ArrayDeque<Obs>();
obsToUpdate.add(observation);

//prevent infinite recursion if an obs is its own group member
Set<Obs> seenIt = new HashSet<Obs>();

while (!obsToUpdate.isEmpty()) {
Obs o = obsToUpdate.removeFirst();

//has this obs already been processed?
if (o == null || seenIt.contains(o))
continue;
seenIt.add(o);

o.setEncounter(this);

//if the attribute was already set, preserve it
//if not, inherit the values sfrom the encounter
if (o.getObsDatetime() == null)
o.setObsDatetime(getEncounterDatetime());
if (o.getPerson() == null)
o.setPerson(getPatient());
if (o.getLocation() == null)
o.setLocation(getLocation());

//propagate attributes to all group members as well
if (o.getGroupMembers(true) != null) {
obsToUpdate.addAll(o.getGroupMembers());
}
}

}
}

Expand Down
42 changes: 42 additions & 0 deletions api/src/test/java/org/openmrs/EncounterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,48 @@ public void addObs_shouldAddEncounterAttrsToObsIfAttributesAreNull() throws Exce
assertTrue(obs.getPerson().equals(patient));
}

/**
* @see {@link Encounter#addObs(Obs)}
*/
@Test
@Verifies(value = "should add encounter attrs to obs if attributes are null", method = "addObs(Obs)")
public void addObs_shouldAddEncounterAttrsToObsGroupMembersIfAttributesAreNull() throws Exception {
/// an encounter that will hav the date/location/patient on it
Encounter encounter = new Encounter();

Date date = new Date();
encounter.setEncounterDatetime(date);

Location location = new Location(1);
encounter.setLocation(location);

Patient patient = new Patient(1);
encounter.setPatient(patient);

// add an obs that doesn't have date/location/patient set on it.
Obs obs = new Obs(123);
Obs childObs = new Obs(456);
obs.addGroupMember(childObs);

//check for infinite recursion
// childObs-->childObs2 and childObs2-->childObs
Obs childObs2 = new Obs(456);
childObs.addGroupMember(childObs2);
childObs2.addGroupMember(childObs);

assertTrue(obs.getGroupMembers() != null && obs.getGroupMembers().size() == 1);

encounter.addObs(obs);

// check the values of the obs attrs to see if they were added
assertTrue(childObs.getObsDatetime().equals(date));
assertTrue(childObs.getLocation().equals(location));
assertTrue(childObs.getPerson().equals(patient));
assertTrue(childObs2.getObsDatetime().equals(date));
assertTrue(childObs2.getLocation().equals(location));
assertTrue(childObs2.getPerson().equals(patient));
}

/**
* @see {@link Encounter#addOrder(Order)}
*/
Expand Down
32 changes: 23 additions & 9 deletions api/src/test/java/org/openmrs/api/EncounterServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,31 @@ public void saveEncounter_shouldCascadeSaveToContainedObs() throws Exception {

// First, create a new Encounter
Encounter enc = buildEncounter();
es.saveEncounter(enc);

// Now add an obs to it
Obs newObs = new Obs();
newObs.setConcept(new Concept(1));
newObs.setValueNumeric(50d);

enc.addObs(newObs);
es.saveEncounter(enc);
//add an obs to the encounter
Obs groupObs = new Obs();
groupObs.setConcept(new Concept(1));
groupObs.setValueNumeric(50d);

// add an obs to the group
Obs childObs = new Obs();
childObs.setConcept(new Concept(1));
childObs.setValueNumeric(50d);
groupObs.addGroupMember(childObs);
enc.addObs(groupObs);

//confirm that save and new enc id are cascaded to obs groupMembers
//even though childObs aren't directly associated to enc
assertNotNull("save succeeds without error", es.saveEncounter(enc));
assertTrue("enc save succeeds", enc.getId() > 0);

assertNotNull("obs save succeeds", groupObs.getObsId());
assertEquals("encounter id propogated", groupObs.getEncounter().getId(), enc.getId());
assertEquals("encounter time propogated", groupObs.getObsDatetime(), enc.getEncounterDatetime());
assertNotNull("obs save succeeds", childObs.getObsId());
assertEquals("encounter id propogated", childObs.getEncounter().getId(), enc.getId());
assertEquals("encounter time propogated", childObs.getObsDatetime(), enc.getEncounterDatetime());

assertNotNull(newObs.getObsId());
}

/**
Expand Down

0 comments on commit 41f320b

Please sign in to comment.