0
我有以下從aem/cq5中的jcr查詢返回的無序列表。 從JCR查詢搜索結果構建頁面樹
正如你所看到的,有些頁面是其他頁面的父頁面。 我試圖通過比較childnode.parent.pagepath和Node.pagePath來構建Node-> childNodes關係。
我無法建立關係和完整的模型。
以下是我試過的代碼:
public class SiteMapFooterServiceImpl implements SiteMapFooterService{
private static final Logger LOG = LoggerFactory.getLogger(SiteMapFooterService.class);
@Override
public Collection<SiteMapNavigation> getSiteMapPages(Page currentPage, ResourceResolver resourceResolver) {
Map<String, SiteMapNavigation> siteMapNavigationItems = new HashMap<String, SiteMapNavigation>();
String rootPath = currentPage.getAbsoluteParent(3).getPath();
SearchResult result = getQueryResult(resourceResolver, rootPath, "includeInSiteMap", "true", "cq:PageContent");
Map<String, Page> siteMapPages = new HashMap<String, Page>();
Map<String, Page> pageMap = new HashMap<String, Page>();
if(result != null) {
for (Hit hit : result.getHits()) {
Resource sitePageContent;
Page sitePage;
try {
sitePageContent = hit.getResource();
sitePage = PageUtility.getPageForResouce(sitePageContent);
siteMapPages.put(sitePage.getPath(), sitePage);
} catch (RepositoryException e) {
LOG.error("Error retrieving paths in getSiteMapPages - SiteMapFooterServiceImpl ", e);
}
}
for(Entry<String, Page> sitePageEntry : siteMapPages.entrySet()) {
buildPageTree(siteMapPages.get(sitePageEntry.getKey()), siteMapPages, siteMapNavigationItems, pageMap);
}
}
return siteMapNavigationItems.values();
}
private void buildPageTree(Page sitePage, Map<String, Page> siteMapPages,
Map<String, SiteMapNavigation> siteMapNavigationItems, Map<String, Page> parentPagesMap) {
Page parentPage = sitePage.getParent();
//if parent exists in the result set - siteMapPages
if(siteMapPages.get(parentPage.getPath()) != null) {
Page sitePageParent = siteMapPages.get(parentPage.getPath());
if(parentPagesMap.get(parentPage.getPath()) != null) {
//get the parent navigation item
SiteMapNavigation parentNavigationItem = siteMapNavigationItems.get(parentPage.getPath());
if(parentPagesMap.get(sitePage.getPath()) != null){
//make this page as child of existing parent
SiteMapNavigation sitePageNavigationItem = siteMapNavigationItems.get(sitePage.getPath());
parentNavigationItem.getChildPages().add(sitePageNavigationItem);
//remove the existent sitepage node from sitemapnavigationitems
siteMapNavigationItems.remove(sitePage.getPath());
parentPagesMap.remove(sitePage.getPath());
//add it to parents list
parentPagesMap.put(sitePageParent.getPath(), sitePageParent);
} else {
//create child
SiteMapNavigation childNavigation = createSiteMapNavigationItem(sitePage);
parentNavigationItem.getChildPages().add(childNavigation);
}
//add to parent navigation list
// parentNavigationItem.getChildPages().add(createSiteMapNavigationItem(sitePage));
} else {
//create parent
SiteMapNavigation parentNavigationItem = createSiteMapNavigationItem(sitePageParent);
//check if sitepage exists as a parent in the pageMap
if(parentPagesMap.get(sitePage.getPath()) != null){
//make this page as child of new parent
SiteMapNavigation sitePageNavigationItem = siteMapNavigationItems.get(sitePage.getPath());
parentNavigationItem.getChildPages().add(sitePageNavigationItem);
//remove the existent sitepage node from sitemapnavigationitems
siteMapNavigationItems.remove(sitePage.getPath());
parentPagesMap.remove(sitePage.getPath());
siteMapNavigationItems.put(parentNavigationItem.getRawPath(), parentNavigationItem);
//add it to parents list
parentPagesMap.put(sitePageParent.getPath(), sitePageParent);
} else {
//create child
SiteMapNavigation childNavigation = createSiteMapNavigationItem(sitePage);
parentNavigationItem.getChildPages().add(childNavigation);
siteMapNavigationItems.put(parentNavigationItem.getRawPath(), parentNavigationItem);
}
//add it to parents list
parentPagesMap.put(sitePageParent.getPath(), sitePageParent);
}
} else {
//if parent NOT exists in the result set - siteMapPages, then add the
page to siteMapNavigationItems as the first level page
SiteMapNavigation siteMapNavigation =
createSiteMapNavigationItem(sitePage);
siteMapNavigationItems.put(siteMapNavigation.getRawPath(),
siteMapNavigation);
parentPagesMap.put(sitePage.getPath(), sitePage);
}
}
private SiteMapNavigation createSiteMapNavigationItem(Page
pageToProcess) {
SiteMapNavigation siteMapNavigation = new SiteMapNavigation();
siteMapNavigation.setTitle(PageUtility.getTitle(pageToProcess));
siteMapNavigation.setPagePath(pageToProcess.getPath() + ".html");
siteMapNavigation.setRawPath(pageToProcess.getPath());
return siteMapNavigation;
}
private SearchResult getQueryResult(ResourceResolver
resourceResolver, String contentPath, String property, String
propertyValue, String nodeType) {
Map<String, String> queryMap = new HashMap<String, String>();
queryMap.put("path", contentPath);
queryMap.put("type", nodeType);
queryMap.put("property", property);
queryMap.put("property.1_value", propertyValue);
queryMap.put("p.limit", "-1");
Session session = resourceResolver.adaptTo(Session.class);
QueryBuilder builder = resourceResolver.adaptTo(QueryBuilder.class);
Query query = builder.createQuery(PredicateGroup.create(queryMap),
session);
return query.getResult();
}
}
任何幫助表示讚賞。
你只想返回共同父母?我不明白你想解決的問題... –
@florian salihovic我需要顯式縮進這些路徑基於路徑子,父母關係的結構/樹結構。 – Sri