Search in sources :

Example 1 with Category

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

the class HippoBeanHelper method getTaxonomyKeysAndNames.

/**
 * Return distinct collection of taxonomy in the format of [Key, Name]
 */
public static Map<String, String> getTaxonomyKeysAndNames(String[] keys) {
    Map<String, String> keyNamePairs = new HashMap<String, String>();
    // For each taxonomy tag key, get the name and also include hierarchy context (ancestors)
    if (keys != null) {
        // Lookup Taxonomy Tree
        TaxonomyManager taxonomyManager = HstServices.getComponentManager().getComponent(TaxonomyManager.class.getName());
        Taxonomy taxonomyTree = taxonomyManager.getTaxonomies().getTaxonomy(PUBLICATION_TAXONOMY);
        for (String key : keys) {
            List<Category> ancestors = (List<Category>) taxonomyTree.getCategoryByKey(key).getAncestors();
            // collect the ancestors
            Map<String, String> map = ancestors.stream().distinct().collect(Collectors.toMap(Category::getKey, category -> category.getInfo(Locale.UK).getName()));
            // add the current node
            map.putIfAbsent(key, taxonomyTree.getCategoryByKey(key).getInfo(Locale.UK).getName());
            // combine with master collection if haven't been collected already
            map.forEach(keyNamePairs::putIfAbsent);
        }
    }
    return keyNamePairs;
}
Also used : ArrayUtils.isEmpty(org.apache.commons.lang3.ArrayUtils.isEmpty) HstServices(org.hippoecm.hst.site.HstServices) java.util(java.util) Collections.emptyList(java.util.Collections.emptyList) TaxonomyManager(org.onehippo.taxonomy.api.TaxonomyManager) HippoFolder(org.hippoecm.hst.content.beans.standard.HippoFolder) Taxonomy(org.onehippo.taxonomy.api.Taxonomy) Category(org.onehippo.taxonomy.api.Category) RequestContextProvider(org.hippoecm.hst.container.RequestContextProvider) Collectors(java.util.stream.Collectors) HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) Category(org.onehippo.taxonomy.api.Category) TaxonomyManager(org.onehippo.taxonomy.api.TaxonomyManager) Taxonomy(org.onehippo.taxonomy.api.Taxonomy) Collections.emptyList(java.util.Collections.emptyList)

Example 2 with Category

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

the class TaxonomyFacetWrapperTest method mockCategory.

private Category mockCategory(String key) {
    Category category = mock(Category.class);
    given(category.getKey()).willReturn(key);
    CategoryInfo categoryInfo = mock(CategoryInfo.class);
    given(category.getInfo(Locale.UK)).willReturn(categoryInfo);
    given(categoryInfo.getName()).willReturn(capitalize(key).replace('_', ' '));
    // If the key is not a root one, set the parent
    if (!key.matches("taxonomy_\\d")) {
        Category parent = mockCategory(key.replaceFirst("_\\d$", ""));
        given(category.getParent()).willReturn(parent);
    }
    return category;
}
Also used : CategoryInfo(org.onehippo.taxonomy.api.CategoryInfo) Category(org.onehippo.taxonomy.api.Category)

Example 3 with Category

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

the class HippoBeanHelper method getTaxonomyList.

public static List<List<String>> getTaxonomyList(String[] keys) {
    List<List<String>> taxonomyList = new ArrayList<>();
    // For each taxonomy tag key, get the name and also include hierarchy context (ancestors)
    if (keys != null) {
        // Lookup Taxonomy Tree
        TaxonomyManager taxonomyManager = HstServices.getComponentManager().getComponent(TaxonomyManager.class.getName());
        Taxonomy taxonomyTree = taxonomyManager.getTaxonomies().getTaxonomy(getTaxonomyName());
        for (String key : keys) {
            List<Category> ancestors = (List<Category>) taxonomyTree.getCategoryByKey(key).getAncestors();
            List<String> list = ancestors.stream().map(category -> category.getInfo(Locale.UK).getName()).collect(Collectors.toList());
            list.add(taxonomyTree.getCategoryByKey(key).getInfo(Locale.UK).getName());
            taxonomyList.add(list);
        }
    }
    return taxonomyList;
}
Also used : HstServices(org.hippoecm.hst.site.HstServices) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) HstComponentException(org.hippoecm.hst.core.component.HstComponentException) Category(org.onehippo.taxonomy.api.Category) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) ArrayList(java.util.ArrayList) List(java.util.List) RepositoryException(javax.jcr.RepositoryException) TaxonomyManager(org.onehippo.taxonomy.api.TaxonomyManager) Locale(java.util.Locale) Taxonomy(org.onehippo.taxonomy.api.Taxonomy) Map(java.util.Map) RequestContextProvider(org.hippoecm.hst.container.RequestContextProvider) Category(org.onehippo.taxonomy.api.Category) TaxonomyManager(org.onehippo.taxonomy.api.TaxonomyManager) Taxonomy(org.onehippo.taxonomy.api.Taxonomy) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with Category

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

the class SearchComponent method calculateTaxonomyLabel.

private String calculateTaxonomyLabel(List<String> taxonomyKeys) {
    TaxonomyManager taxonomyManager = HstServices.getComponentManager().getComponent(TaxonomyManager.class.getName());
    Taxonomy taxonomy = taxonomyManager.getTaxonomies().getTaxonomy(HippoBeanHelper.PUBLICATION_TAXONOMY);
    List<String> pathLabels = new ArrayList<>();
    boolean isValid = true;
    for (int i = 1; i < taxonomyKeys.size(); i++) {
        final Category categoryByKey = taxonomy.getCategoryByKey(taxonomyKeys.get(i));
        if (categoryByKey != null && categoryByKey.getInfo(Locale.getDefault()) != null && categoryByKey.getInfo(Locale.getDefault()).getName() != null) {
            pathLabels.add(taxonomy.getCategoryByKey(taxonomyKeys.get(i)).getInfo(Locale.getDefault()).getName());
        } else {
            isValid = false;
        }
    }
    if (pathLabels.size() > 0 && isValid) {
        return pathLabels.get(pathLabels.size() - 1);
    } else {
        return null;
    }
}
Also used : Category(org.onehippo.taxonomy.api.Category) TaxonomyManager(org.onehippo.taxonomy.api.TaxonomyManager) Taxonomy(org.onehippo.taxonomy.api.Taxonomy) Constraint(org.hippoecm.hst.content.beans.query.builder.Constraint)

Example 5 with Category

use of org.onehippo.taxonomy.api.Category 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

Category (org.onehippo.taxonomy.api.Category)5 Taxonomy (org.onehippo.taxonomy.api.Taxonomy)4 TaxonomyManager (org.onehippo.taxonomy.api.TaxonomyManager)3 java.util (java.util)2 Collections.emptyList (java.util.Collections.emptyList)2 Collectors (java.util.stream.Collectors)2 RequestContextProvider (org.hippoecm.hst.container.RequestContextProvider)2 HippoBean (org.hippoecm.hst.content.beans.standard.HippoBean)2 HstServices (org.hippoecm.hst.site.HstServices)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 ArrayUtils.isEmpty (org.apache.commons.lang3.ArrayUtils.isEmpty)1 Constraint (org.hippoecm.hst.content.beans.query.builder.Constraint)1 HippoFacetNavigationBean (org.hippoecm.hst.content.beans.standard.HippoFacetNavigationBean)1 HippoFolder (org.hippoecm.hst.content.beans.standard.HippoFolder)1 HippoFolderBean (org.hippoecm.hst.content.beans.standard.HippoFolderBean)1