/** * @param child The child location to remove. * @since 1.5 */ public void removeChildLocation(Location child) { if (getChildLocations() != null) { childLocations.remove(child); } }
/** * Returns all childLocations where child.locationId = this.locationId. * * @param includeRetired specifies whether or not to include voided childLocations * @return Returns a Set<Location> of all the childLocations. * @since 1.5 * @should return a set of locations */ public Set<Location> getChildLocations(boolean includeRetired) { Set<Location> ret = new HashSet<>(); if (includeRetired) { ret = getChildLocations(); } else if (getChildLocations() != null) { for (Location l : getChildLocations()) { if (!l.getRetired()) { ret.add(l); } } } 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<>(); for (Location childLocation : getChildLocations()) { if (!childLocation.getRetired() || includeRetired) { result.add(childLocation); result.addAll(childLocation.getDescendantLocations(includeRetired)); } } return result; }
/** * @see org.openmrs.api.db.LocationDAO#saveLocation(org.openmrs.Location) */ @Override 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; }
if (getChildLocations() == null) { childLocations = new HashSet<>();
/** * You should be able to remove a child location from a location. * * @see LocationService#saveLocation(Location) */ @Test public void saveLocation_shouldRemoveChildLocationFromLocation() { LocationService ls = Context.getLocationService(); // Retrieving a location with initially 2 child locations Location location = ls.getLocation(1); // Removing a child location.removeChildLocation(location.getChildLocations().iterator().next()); ls.saveLocation(location); Location newSavedLocation = ls.getLocation(location.getLocationId()); // Saved location should have 1 child locations now assertEquals(1, newSavedLocation.getChildLocations().size()); }
assertNotNull("newSavedLocation.childLocations must be not null", newSavedLocation.getChildLocations()); assertEquals(1, newSavedLocation.getChildLocations().size()); Location newChildA = newSavedLocation.getChildLocations().iterator().next(); assertNotNull("newSavedLocation.childLocations must be not null", newChildA.getChildLocations()); assertEquals(1, newSavedLocation.getChildLocations().size()); assertTrue("Level B child location must be the previously created childB", newChildA.getChildLocations().iterator() .next().equals(childB));
@Test public void shouldIncludeTheListOfChildLocations() throws Exception { Location location = service.getLocationByUuid(getUuid()); Assert.assertEquals(0, location.getChildLocations().size()); location.addChildLocation(service.getLocation(2)); service.saveLocation(location); MockHttpServletRequest httpReq = request(RequestMethod.GET, getURI() + "/" + getUuid()); httpReq.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL); SimpleObject result = deserialize(handle(httpReq)); Assert.assertEquals(1, ((Collection) PropertyUtils.getProperty(result, "childLocations")).size()); }
@Test public void shouldOverwriteAListOfChildLocations() throws Exception { Location location = service.getLocationByUuid(getUuid()); location.addChildLocation(service.getLocation(2)); service.saveLocation(location); String json = "{ \"childLocations\": [] }"; MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid()); req.setContent(json.getBytes()); handle(req); Location updatedLocation = service.getLocationByUuid(getUuid()); Assert.assertNotNull(updatedLocation); Assert.assertTrue(updatedLocation.getChildLocations().isEmpty()); }