/**
  * @param locationAndDepths
  * @param locations
  * @param i counter
  */
 private void populateLocationAndDepthList(
     List<LocationAndDepth> locationAndDepths, Collection<Location> locations, int depth) {
   for (Location location : locations) {
     locationAndDepths.add(new LocationAndDepth(depth, location));
     if (location.getChildLocations() != null && location.getChildLocations().size() > 0) {
       populateLocationAndDepthList(locationAndDepths, location.getChildLocations(), depth + 1);
     }
   }
 }
  /** @see org.openmrs.api.db.LocationDAO#saveLocation(org.openmrs.Location) */
  public Location saveLocation(Location location) {
    if (location.getChildLocations() != null && location.getLocationId() != null) {
      // hibernate has a problem updating child collections
      // if the parent object was already saved so we do it
      // explicitly here
      for (Location child : location.getChildLocations())
        if (child.getLocationId() == null) saveLocation(child);
    }

    sessionFactory.getCurrentSession().saveOrUpdate(location);
    return location;
  }
  /**
   * Utility method that returns all child locations and children of its child locations recursively
   *
   * @param location
   * @param foundLocations
   * @return
   */
  private Set<Location> getChildLocationsRecursively(
      Location location, Set<Location> foundLocations) {
    if (foundLocations == null) foundLocations = new LinkedHashSet<Location>();

    foundLocations.add(location);

    if (location.getChildLocations() != null) {
      for (Location l : location.getChildLocations()) {
        foundLocations.add(l);
        getChildLocationsRecursively(l, foundLocations);
      }
    }

    return foundLocations;
  }
 public LocationMock(Location loc) {
   setAddress1(loc.getAddress1());
   setAddress2(loc.getAddress2());
   setChangedBy(loc.getChangedBy());
   setChildLocations(loc.getChildLocations(true));
   setCityVillage(loc.getCityVillage());
   setCountry(loc.getCountry());
   setCountyDistrict(loc.getCountyDistrict());
   setCreator(loc.getCreator());
   setDateChanged(loc.getDateChanged());
   setDateCreated(loc.getDateCreated());
   setDateRetired(loc.getDateRetired());
   setDescription(loc.getDescription());
   setId(loc.getId());
   setLatitude(loc.getLatitude());
   setLongitude(loc.getLongitude());
   setName(loc.getName());
   setNeighborhoodCell(loc.getNeighborhoodCell());
   setParentLocation(loc.getParentLocation());
   setPostalCode(loc.getPostalCode());
   setRegion(loc.getRegion());
   setRetired(loc.getRetired());
   setRetiredBy(loc.getRetiredBy());
   setRetireReason(loc.getRetireReason());
   setStateProvince(loc.getStateProvince());
   setSubregion(loc.getSubregion());
   setTags(loc.getTags());
   setTownshipDivision(loc.getTownshipDivision());
   setUuid(loc.getUuid());
 }