@Transactional(readOnly = true) public List<Department> findRoots() { List<Department> roots = Lists.newArrayList(); Iterable<Department> items = departmentDao.findAllCached(); for (Department item : items) { if (item.getParent() == null) { roots.add(item); } } Collections.sort(roots); return roots; }
@Transactional(readOnly = true) public List<Department> findChildren(Department parent) { if (parent == null) { return findRoots(); } List<Department> children = Lists.newArrayList(); Iterable<Department> items = departmentDao.findAllCached(); for (Department item : items) { if (parent.equals(item.getParent())) { children.add(item); } } Collections.sort(children); return children; }
@Transactional(readOnly = true) public List<Department> findChildrenCascade(Department parent) { Assert.notNull(parent); List<Department> children = Lists.newArrayList(); Iterable<Department> items = departmentDao.findAllCached(); for (Department item : items) { Department loop = item.getParent(); while (loop != null) { if (parent.equals(loop)) { children.add(item); break; } loop = loop.getParent(); } } Collections.sort(children); return children; }