Search in sources :

Example 1 with TaxonomyGraph

use of org.ambraproject.wombat.model.TaxonomyGraph in project wombat by PLOS.

the class SearchController method modelSubjectHierarchy.

private TaxonomyGraph modelSubjectHierarchy(Model model, Site site, String subject) throws IOException {
    TaxonomyGraph fullTaxonomyView = browseTaxonomyService.parseCategories(site.getJournalKey());
    Collection<String> subjectParents;
    Collection<String> subjectChildren;
    if (subject != null && subject.length() > 0) {
        // Recreate the category name as stored in the DB
        subject = subject.replace("_", " ");
        TaxonomyGraph.CategoryView categoryView = fullTaxonomyView.getView(subject);
        if (categoryView == null) {
            throw new NotFoundException(String.format("category %s does not exist.", subject));
        } else {
            if (categoryView.getParents().isEmpty()) {
                subjectParents = new HashSet<>();
            } else {
                subjectParents = categoryView.getParents().keySet();
            }
            subjectChildren = categoryView.getChildren().keySet();
        }
    } else {
        subjectParents = new HashSet<>();
        subjectChildren = fullTaxonomyView.getRootCategoryNames();
    }
    model.addAttribute("subjectParents", subjectParents);
    model.addAttribute("subjectChildren", subjectChildren);
    return fullTaxonomyView;
}
Also used : TaxonomyGraph(org.ambraproject.wombat.model.TaxonomyGraph)

Example 2 with TaxonomyGraph

use of org.ambraproject.wombat.model.TaxonomyGraph 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 3 with TaxonomyGraph

use of org.ambraproject.wombat.model.TaxonomyGraph 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 4 with TaxonomyGraph

use of org.ambraproject.wombat.model.TaxonomyGraph in project wombat by PLOS.

the class SearchController method subjectAreaSearch.

/**
 * Set defaults and performs search for subject area landing page
 *
 * @param request HTTP request for browsing subject areas
 * @param model   model that will be passed to the template
 * @param site    site the request originates from
 * @param params  HTTP request params
 * @param subject the subject area to be search; return all articles if no subject area is provided
 * @throws IOException
 */
private void subjectAreaSearch(HttpServletRequest request, Model model, Site site, MultiValueMap<String, String> params, String subject) throws IOException {
    TaxonomyGraph taxonomyGraph = modelSubjectHierarchy(model, site, subject);
    String subjectName;
    if (Strings.isNullOrEmpty(subject)) {
        params.add("subject", "");
        subjectName = "All Subject Areas";
    } else {
        subject = subject.replace("_", " ");
        params.add("subject", subject);
        subjectName = taxonomyGraph.getName(subject);
    }
    model.addAttribute("subjectName", subjectName);
    // set defaults for subject area landing page
    if (isNullOrEmpty(params.get("resultsPerPage"))) {
        params.add("resultsPerPage", BROWSE_RESULTS_PER_PAGE);
    }
    if (isNullOrEmpty(params.get("sortOrder"))) {
        params.add("sortOrder", "DATE_NEWEST_FIRST");
    }
    if (isNullOrEmpty(params.get("filterJournals"))) {
        params.add("filterJournals", site.getJournalKey());
    }
    CommonParams commonParams = modelCommonParams(request, model, site, params);
    ArticleSearchQuery query = commonParams.makeArticleSearchQueryBuilder().setSimple(false).build();
    SolrSearchApi.Result searchResults = solrSearchApi.search(query);
    model.addAttribute("articles", SolrArticleAdapter.unpackSolrQuery(searchResults));
    model.addAttribute("searchResults", addArticleLinks(searchResults, request, site, siteSet));
    model.addAttribute("page", commonParams.getSingleParam(params, "page", "1"));
    model.addAttribute("journalKey", site.getKey());
    model.addAttribute("isBrowse", true);
    String authId = request.getRemoteUser();
    boolean subscribed = false;
    if (authId != null) {
        String subjectParam = Strings.isNullOrEmpty(subject) ? "" : subjectName;
        subscribed = alertService.isUserSubscribed(authId, site.getJournalKey(), subjectParam);
    }
    model.addAttribute("subscribed", subscribed);
}
Also used : SolrSearchApi(org.ambraproject.wombat.service.remote.SolrSearchApi) ArticleSearchQuery(org.ambraproject.wombat.service.remote.ArticleSearchQuery) TaxonomyGraph(org.ambraproject.wombat.model.TaxonomyGraph)

Aggregations

TaxonomyGraph (org.ambraproject.wombat.model.TaxonomyGraph)4 CategoryView (org.ambraproject.wombat.model.TaxonomyGraph.CategoryView)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SortedSet (java.util.SortedSet)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1 ArticleSearchQuery (org.ambraproject.wombat.service.remote.ArticleSearchQuery)1 SolrSearchApi (org.ambraproject.wombat.service.remote.SolrSearchApi)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