use of org.ambraproject.wombat.model.TaxonomyGraph.CategoryView in project wombat by PLOS.
the class TaxonomyController method read.
@RequestMapping(name = "taxonomy", value = TAXONOMY_TEMPLATE, method = RequestMethod.GET)
@ResponseBody
public List<SubjectData> read(@SiteParam Site site, @RequestParam MultiValueMap<String, String> params) throws IOException {
Map<String, Object> taxonomyBrowserConfig = site.getTheme().getConfigMap("taxonomyBrowser");
boolean hasTaxonomyBrowser = (boolean) taxonomyBrowserConfig.get("hasTaxonomyBrowser");
if (!hasTaxonomyBrowser) {
throw new NotFoundException();
}
TaxonomyGraph taxonomyGraph = browseTaxonomyService.parseCategories(site.getJournalKey());
// parent will be null only for the ROOT taxonomy
String parent;
if (params.isEmpty()) {
parent = null;
} else {
List<String> categoryParams = params.get("c");
// todo: After cleaning up redirects and solving the 502 proxy error, this replace should be removed
categoryParams.replaceAll(s -> s.replace("_", " "));
parent = Joiner.on("/").join(categoryParams);
}
Map<String, Integer> articleCounts = browseTaxonomyService.getCounts(taxonomyGraph, site.getJournalKey());
final Collection<CategoryView> children;
if (parent != null) {
List<String> terms = TaxonomyGraph.parseTerms(parent);
String parentLeafNodeName = terms.get(terms.size() - 1);
CategoryView categoryView = taxonomyGraph.getView(parentLeafNodeName);
children = categoryView.getChildren().values();
} else {
children = taxonomyGraph.getRootCategoryViews();
}
Map<String, SortedSet<String>> tree = getShortTree(children);
List<SubjectData> results = new ArrayList<>(tree.size());
for (Map.Entry<String, SortedSet<String>> entry : tree.entrySet()) {
String key = entry.getKey();
String subjectName = Strings.nullToEmpty(parent) + '/' + key;
long childCount = entry.getValue().size();
long articleCount = articleCounts.get(key);
results.add(new SubjectData(subjectName, articleCount, childCount));
}
if (parent == null) {
long rootArticleCount = articleCounts.get("ROOT");
results.add(new SubjectData("ROOT", rootArticleCount, (long) results.size()));
}
Collections.sort(results, Comparator.comparing(SubjectData::getSubject));
return results;
}
use of org.ambraproject.wombat.model.TaxonomyGraph.CategoryView in project wombat by PLOS.
the class TaxonomyControllerTest method setup.
@Before
public void setup() throws IOException {
Map<String, Object> taxonomyBrowserConfig = new HashMap<String, Object>();
taxonomyBrowserConfig.put("hasTaxonomyBrowser", true);
when(theme.getConfigMap("taxonomyBrowser")).thenReturn(taxonomyBrowserConfig);
TaxonomyGraph taxonomyGraph = mock(TaxonomyGraph.class);
when(taxonomyGraph.getRootCategoryViews()).thenReturn(new ArrayList<CategoryView>());
Map<String, Integer> articleCounts = mock(Map.class);
when(articleCounts.get("ROOT")).thenReturn(0);
when(browseTaxonomyService.parseCategories(any())).thenReturn(taxonomyGraph);
when(browseTaxonomyService.getCounts(any(), any())).thenReturn(articleCounts);
}
use of org.ambraproject.wombat.model.TaxonomyGraph.CategoryView in project wombat by PLOS.
the class TaxonomyController method getShortTree.
/**
* For the top elements: return keys and the immediate children
*
* @param children
*
* @return a map of keys and the immediate children
*/
@SuppressWarnings("unchecked")
public static Map<String, SortedSet<String>> getShortTree(Collection<CategoryView> children) {
Map<String, SortedSet<String>> results = new ConcurrentSkipListMap<>();
for (CategoryView child : children) {
ConcurrentSkipListSet sortedSet = new ConcurrentSkipListSet();
sortedSet.addAll(child.getChildren().keySet());
results.put(child.getName(), sortedSet);
}
return results;
}
Aggregations