Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: openmrs/openmrs-core
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0b227ca8e6a3
Choose a base ref
...
head repository: openmrs/openmrs-core
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f1c8e2a0191d
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Jun 18, 2013

  1. Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    c6779ba View commit details

Commits on Jun 19, 2013

  1. Merge pull request #345 from akolodziejski/TRUNK-3859

    TRUNK-3859 LocationService should provide a method to get all child locations
    rkorytkowski committed Jun 19, 2013

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    f1c8e2a View commit details
Showing with 89 additions and 0 deletions.
  1. +19 −0 api/src/main/java/org/openmrs/Location.java
  2. +70 −0 api/src/test/java/org/openmrs/LocationTest.java
19 changes: 19 additions & 0 deletions api/src/main/java/org/openmrs/Location.java
Original file line number Diff line number Diff line change
@@ -402,6 +402,25 @@ else if (getChildLocations() != null) {
return ret;
}

/**
* Returns the descendant locations.
*
* @param includeRetired specifies whether or not to include voided childLocations
* @return Returns a Set<Location> of the descendant location.
* @since 1.10
*/
public Set<Location> getDescendantLocations(boolean includeRetired) {
Set<Location> result = new HashSet<Location>();

for (Location childLocation : getChildLocations()) {
if (!childLocation.isRetired() || includeRetired) {
result.add(childLocation);
result.addAll(childLocation.getDescendantLocations(includeRetired));
}
}
return result;
}

/**
* @param childLocations The childLocations to set.
* @since 1.5
70 changes: 70 additions & 0 deletions api/src/test/java/org/openmrs/LocationTest.java
Original file line number Diff line number Diff line change
@@ -15,9 +15,17 @@

import static org.junit.Assert.assertTrue;

import java.util.HashSet;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;
import org.openmrs.test.Verifies;

import com.google.common.collect.Sets;

import static org.hamcrest.Matchers.*;

public class LocationTest {

/**
@@ -39,4 +47,66 @@ public void isInHierarchy_shouldShouldFindLocationInHierarchy() throws Exception
assertTrue(Location.isInHierarchy(locationChild, locationGrandParent));
}

@Test
public void getDescendantLocations_shouldReturnAllDescendantLocationsIfIncludeRetiredIsTrue() {

Location rootLocation = new Location();
//first level
Location locationOne = new Location();
Location locationTwo = new Location();
//second level
Location childOflocationOne = new Location();
Location childOnfLocationTwo = new Location();

//make child-parent relations
rootLocation.setChildLocations(Sets.newHashSet(locationOne, locationTwo));

locationOne.setChildLocations(Sets.newHashSet(childOflocationOne));
locationTwo.setChildLocations(Sets.newHashSet(childOnfLocationTwo));

childOflocationOne.setChildLocations(new HashSet<Location>());
childOnfLocationTwo.setChildLocations(new HashSet<Location>());

Set<Location> descendantLocations = rootLocation.getDescendantLocations(true);

Set<Location> expectedLocations = Sets.newHashSet(locationOne, locationTwo, childOflocationOne, childOnfLocationTwo);
Assert.assertThat(descendantLocations, equalTo(expectedLocations));

}

@Test
public void getDescendantLocations_shouldReturnNonRetiredDescendantLocationsIfIncludeRetiredIsFalse() {

Location rootLocation = new Location();
//first level
Location nonRetiredLocation = new Location();
Location retiredLocation = new Location();
retiredLocation.setRetired(true);
//second level
Location firstChildOfNonRetiredLocation = new Location();
Location secondChildOfNonRetiredLocation = new Location();

Location firstChildOfRetiredLocation = new Location();

//make child-parent relations
rootLocation.setChildLocations(Sets.newHashSet(nonRetiredLocation, retiredLocation));

nonRetiredLocation.setChildLocations(Sets
.newHashSet(firstChildOfNonRetiredLocation, secondChildOfNonRetiredLocation));
retiredLocation.setChildLocations(Sets.newHashSet(firstChildOfRetiredLocation));

firstChildOfNonRetiredLocation.setChildLocations(new HashSet<Location>());
secondChildOfNonRetiredLocation.setChildLocations(new HashSet<Location>());

firstChildOfRetiredLocation.setChildLocations(new HashSet<Location>());

//action
Set<Location> descendantLocations = rootLocation.getDescendantLocations(false);

Set<Location> expectedLocations = Sets.newHashSet(nonRetiredLocation, firstChildOfNonRetiredLocation,
secondChildOfNonRetiredLocation);

Assert.assertThat(descendantLocations, equalTo(expectedLocations));
}

}