Search in sources :

Example 1 with CategoryView

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;
}
Also used : ArrayList(java.util.ArrayList) SortedSet(java.util.SortedSet) CategoryView(org.ambraproject.wombat.model.TaxonomyGraph.CategoryView) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) TaxonomyGraph(org.ambraproject.wombat.model.TaxonomyGraph) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with CategoryView

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);
}
Also used : HashMap(java.util.HashMap) CategoryView(org.ambraproject.wombat.model.TaxonomyGraph.CategoryView) TaxonomyGraph(org.ambraproject.wombat.model.TaxonomyGraph) Before(org.junit.Before)

Example 3 with CategoryView

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;
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) CategoryView(org.ambraproject.wombat.model.TaxonomyGraph.CategoryView) SortedSet(java.util.SortedSet)

Aggregations

CategoryView (org.ambraproject.wombat.model.TaxonomyGraph.CategoryView)3 SortedSet (java.util.SortedSet)2 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)2 TaxonomyGraph (org.ambraproject.wombat.model.TaxonomyGraph)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentSkipListSet (java.util.concurrent.ConcurrentSkipListSet)1 Before (org.junit.Before)1 MultiValueMap (org.springframework.util.MultiValueMap)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1