@Test public void testGetAncestorsOfEdOrgWithCycle() { Entity sea = buildEntityType(SEA); Entity lea = buildEntityType(LEA); Entity school = buildEntityType(SCHOOL); school.getBody().put(PARENT_ED_ORG_REF, Arrays.asList(lea.getEntityId())); lea.getBody().put(PARENT_ED_ORG_REF, Arrays.asList(sea.getEntityId(), school.getEntityId())); sea.getBody().put(PARENT_ED_ORG_REF, Arrays.asList()); when(repo.findById(EntityNames.EDUCATION_ORGANIZATION, sea.getEntityId())).thenReturn(sea); when(repo.findById(EntityNames.EDUCATION_ORGANIZATION, lea.getEntityId())).thenReturn(lea); when(repo.findById(EntityNames.EDUCATION_ORGANIZATION, school.getEntityId())).thenReturn(school); Set<Entity> ancestors = underTest.getAncestorsOfEdOrg(school); assertNotNull(ancestors); assertTrue("LEA must be an ancestor", ancestors.contains(lea)); assertTrue("school should be contained in the results as well, per required behavior", ancestors.contains(school)); assertEquals("unexpected ancestors", 2, ancestors.size()); }
public Set<Entity> getAncestorsOfEdOrg(Entity entity) { if (isSEA(entity)) { return null; } Set<String> visitedIds = new HashSet<String>(); Set<Entity> ancestors = new HashSet<Entity>(); List<Entity> stack = new ArrayList<Entity>(10); ancestors.add(entity); stack.add(entity); while (!stack.isEmpty()) { Entity cur = stack.remove(stack.size() - 1); if (visitedIds.contains(cur.getEntityId())) { continue; } else { visitedIds.add(cur.getEntityId()); } if (cur.getBody().containsKey("parentEducationAgencyReference")) { List<Entity> parentEdOrgs = getParentEdOrg(cur); if (parentEdOrgs != null) { for (Entity parent:parentEdOrgs) { stack.add(parent); if (!isSEA(parent)) { ancestors.add(parent); } } } } } return ancestors; }
EdOrgHierarchyHelper { public Set<Entity> getAncestorsOfEdOrg(Entity entity) { if (isSEA(entity)) { return null; } Set<String> visitedIds = new HashSet<String>(); Set<Entity> ancestors = new HashSet<Entity>(); List<Entity> stack = new ArrayList<Entity>(10); ancestors.add(entity); stack.add(entity); while (!stack.isEmpty()) { Entity cur = stack.remove(stack.size() - 1); if (visitedIds.contains(cur.getEntityId())) { continue; } else { visitedIds.add(cur.getEntityId()); } if (cur.getBody().containsKey("parentEducationAgencyReference")) { List<Entity> parentEdOrgs = getParentEdOrg(cur); if (parentEdOrgs != null) { for (Entity parent:parentEdOrgs) { stack.add(parent); if (!isSEA(parent)) { ancestors.add(parent); } } } } } return ancestors; } }
EdOrgHierarchyHelper { public Set<Entity> getAncestorsOfEdOrg(Entity entity) { if (isSEA(entity)) { return null; } Set<String> visitedIds = new HashSet<String>(); Set<Entity> ancestors = new HashSet<Entity>(); List<Entity> stack = new ArrayList<Entity>(10); ancestors.add(entity); stack.add(entity); while (!stack.isEmpty()) { Entity cur = stack.remove(stack.size() - 1); if (visitedIds.contains(cur.getEntityId())) { continue; } else { visitedIds.add(cur.getEntityId()); } if (cur.getBody().containsKey("parentEducationAgencyReference")) { List<Entity> parentEdOrgs = getParentEdOrg(cur); if (parentEdOrgs != null) { for (Entity parent:parentEdOrgs) { stack.add(parent); if (!isSEA(parent)) { ancestors.add(parent); } } } } } return ancestors; } EdOrgHierarchyHelper(Repository<Entity> repo); }
EdOrgHierarchyHelper { public Set<Entity> getAncestorsOfEdOrg(Entity entity) { if (isSEA(entity)) { return null; } Set<String> visitedIds = new HashSet<String>(); Set<Entity> ancestors = new HashSet<Entity>(); List<Entity> stack = new ArrayList<Entity>(10); ancestors.add(entity); stack.add(entity); while (!stack.isEmpty()) { Entity cur = stack.remove(stack.size() - 1); if (visitedIds.contains(cur.getEntityId())) { continue; } else { visitedIds.add(cur.getEntityId()); } if (cur.getBody().containsKey("parentEducationAgencyReference")) { List<Entity> parentEdOrgs = getParentEdOrg(cur); if (parentEdOrgs != null) { for (Entity parent:parentEdOrgs) { stack.add(parent); if (!isSEA(parent)) { ancestors.add(parent); } } } } } return ancestors; } EdOrgHierarchyHelper(Repository<Entity> repo); boolean isSEA(Entity entity); boolean isLEA(Entity entity); boolean isSchool(Entity entity); List<Entity> getTopLEAOfEdOrg(Entity entity); Set<Entity> getAncestorsOfEdOrg(Entity entity); String getSEAOfEdOrg(Entity entity); Entity getSEA(); }
EdOrgHierarchyHelper { public Set<Entity> getAncestorsOfEdOrg(Entity entity) { if (isSEA(entity)) { return null; } Set<String> visitedIds = new HashSet<String>(); Set<Entity> ancestors = new HashSet<Entity>(); List<Entity> stack = new ArrayList<Entity>(10); ancestors.add(entity); stack.add(entity); while (!stack.isEmpty()) { Entity cur = stack.remove(stack.size() - 1); if (visitedIds.contains(cur.getEntityId())) { continue; } else { visitedIds.add(cur.getEntityId()); } if (cur.getBody().containsKey("parentEducationAgencyReference")) { List<Entity> parentEdOrgs = getParentEdOrg(cur); if (parentEdOrgs != null) { for (Entity parent:parentEdOrgs) { stack.add(parent); if (!isSEA(parent)) { ancestors.add(parent); } } } } } return ancestors; } EdOrgHierarchyHelper(Repository<Entity> repo); boolean isSEA(Entity entity); boolean isLEA(Entity entity); boolean isSchool(Entity entity); List<Entity> getTopLEAOfEdOrg(Entity entity); Set<Entity> getAncestorsOfEdOrg(Entity entity); String getSEAOfEdOrg(Entity entity); Entity getSEA();  }