private Page findRootPage() { Page page = currentPage; while (true) { Page parent = page.getParent(); if (parent == null) { return page; } else { page = parent; } } } }
public class Pages { public static List<Page> getPath(Page from, Page to) { if (from == null || to == null) throw new IllegalArgumentException(); List<Page> path = new ArrayList<Page>(); Page page=to.getParent(); Page last=from.getParent(); // I'm assuming getPath() can be null occassionaly and is a String String lastPath; if(last!=null && (lastPath=last.getPath())!=null){ // The assignment above is an acceptable one, as it saves a nested if statement // traverse your path while(page!=null && page.getPath()!=null && !(page.getPath().equals(lastPath))) { path.add(page); page=page.getParent(); } } Collections.reverse(path); return path.contains(from) ? path : null; }
public class Pages { public static List<Page> getPath(Page from, Page to) { if (from == null || to == null) return new ArrayList<Page>(); List<Page> path = new ArrayList<Page>(); Page page=to.getParent(); Page last=from.getParent(); // I'm assuming getPath() can be null occassionaly and is a String String lastPath; if(last!=null && (lastPath=last.getPath())!=null){ // The assignment above is an acceptable one, as it saves a nested if statement // traverse your path while(page!=null && page.getPath()!=null && !(page.getPath().equals(lastPath))){ path.add(page); page=page.getParent(); } } Collections.reverse(path); return path;//return path or empty list }
/** * Build html title from page titles up to site root page. * @param page Page * @return Html title */ private String getRecursivePageTitle(Page page) { if (siteRoot.isRootPage(page)) { return StringUtils.defaultString(page.getPageTitle(), page.getTitle()); } else if (Template.is(page, AppTemplate.ADMIN_STRUCTURE_ELEMENT)) { return getRecursivePageTitle(page.getParent()); } else { return StringUtils.defaultString(page.getPageTitle(), page.getTitle()) + " - " + getRecursivePageTitle(page.getParent()); } }