Search in sources :

Example 6 with Taxonomy

use of org.onehippo.taxonomy.api.Taxonomy in project hippo by NHS-digital-website.

the class TaxonomyFacetWrapper method createTaxonomyFacets.

private List<TaxonomyFacet> createTaxonomyFacets() {
    // get the taxonomy facets
    List<HippoFolderBean> taxonomyFacets = facetBean.getFolders().stream().filter((facet) -> facet.getName().equals(TAXONOMY_FACET_NAME)).findAny().map(HippoFolderBean::getFolders).orElseGet(() -> {
        log.error("Unable to find taxonomy facet.");
        return null;
    });
    if (isEmpty(taxonomyFacets)) {
        return emptyList();
    }
    // link the taxonomy facets to the taxonomy category
    HashMap<String, TaxonomyFacet> keyToTaxonomyFacet = new HashMap<>(taxonomyFacets.size());
    for (HippoFolderBean facet : taxonomyFacets) {
        String key = facet.getName();
        keyToTaxonomyFacet.put(key, new TaxonomyFacet(taxonomy, key, facet));
    }
    // build the tree structure
    List<TaxonomyFacet> rootTaxonomyFacets = new ArrayList<>();
    for (TaxonomyFacet taxonomyFacet : keyToTaxonomyFacet.values()) {
        Category taxonomyCategory = taxonomyFacet.getTaxonomyCategory();
        Category parentTaxonomy = taxonomyCategory == null ? null : taxonomyCategory.getParent();
        if (parentTaxonomy == null) {
            rootTaxonomyFacets.add(taxonomyFacet);
        } else {
            String parentKey = parentTaxonomy.getKey();
            TaxonomyFacet parent = keyToTaxonomyFacet.get(parentKey);
            if (parent == null) {
                log.error("No parent facet found for taxonomy key: " + taxonomyCategory.getName());
            } else {
                parent.addChild(taxonomyFacet);
            }
        }
    }
    return rootTaxonomyFacets;
}
Also used : CollectionUtils.isEmpty(org.springframework.util.CollectionUtils.isEmpty) java.util(java.util) Logger(org.slf4j.Logger) Collections.emptyList(java.util.Collections.emptyList) Taxonomy(org.onehippo.taxonomy.api.Taxonomy) HippoFacetNavigationBean(org.hippoecm.hst.content.beans.standard.HippoFacetNavigationBean) LoggerFactory(org.slf4j.LoggerFactory) Category(org.onehippo.taxonomy.api.Category) HippoFolderBean(org.hippoecm.hst.content.beans.standard.HippoFolderBean) Category(org.onehippo.taxonomy.api.Category) HippoFolderBean(org.hippoecm.hst.content.beans.standard.HippoFolderBean)

Aggregations

Taxonomy (org.onehippo.taxonomy.api.Taxonomy)6 Category (org.onehippo.taxonomy.api.Category)5 TaxonomyManager (org.onehippo.taxonomy.api.TaxonomyManager)5 java.util (java.util)3 Collections.emptyList (java.util.Collections.emptyList)3 Collectors (java.util.stream.Collectors)3 RequestContextProvider (org.hippoecm.hst.container.RequestContextProvider)3 HippoBean (org.hippoecm.hst.content.beans.standard.HippoBean)3 HstServices (org.hippoecm.hst.site.HstServices)3 ArrayUtils.isEmpty (org.apache.commons.lang3.ArrayUtils.isEmpty)2 HippoFacetNavigationBean (org.hippoecm.hst.content.beans.standard.HippoFacetNavigationBean)2 HippoFolder (org.hippoecm.hst.content.beans.standard.HippoFolder)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 RepositoryException (javax.jcr.RepositoryException)1 Constraint (org.hippoecm.hst.content.beans.query.builder.Constraint)1 HippoFolderBean (org.hippoecm.hst.content.beans.standard.HippoFolderBean)1