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;
}
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;
}
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;
}
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;
}
}
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;
}
Aggregations