Search in sources :

Example 1 with ArticleType

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

the class BrowseController method buildArticleGroups.

private List<TypedArticleGroup> buildArticleGroups(Site site, String issueId, List<Map<String, ?>> articles) throws IOException {
    // Articles grouped by their type. Order within the value lists is significant.
    ArticleType.Dictionary typeDictionary = ArticleType.getDictionary(site.getTheme());
    ListMultimap<ArticleType, Map<String, Object>> groupedArticles = LinkedListMultimap.create();
    for (Map<String, ?> article : articles) {
        // Omit unpublished articles
        if (!article.containsKey("revisionNumber"))
            continue;
        String doi = (String) article.get("doi");
        Map<String, Object> populatedArticle = new HashMap<>(article);
        Map<String, ?> ingestion = (Map<String, ?>) article.get("ingestion");
        ArticleType articleType = typeDictionary.lookUp((String) ingestion.get("articleType"));
        populatedArticle.put("relatedArticles", articleMetadataFactory.fetchRelatedArticles(doi));
        populateAuthors(populatedArticle, site);
        groupedArticles.put(articleType, populatedArticle);
    }
    // The article types supported by this site, in the order in which they are supposed to appear.
    ImmutableList<ArticleType> articleTypes = typeDictionary.getSequence();
    // Produce a correctly ordered list of TypedArticleGroup, populated with the article groups.
    List<TypedArticleGroup> articleGroups = new ArrayList<>(articleTypes.size());
    for (ArticleType articleType : articleTypes) {
        List<Map<String, Object>> articlesOfType = groupedArticles.removeAll(articleType);
        if (!articlesOfType.isEmpty()) {
            articleGroups.add(new TypedArticleGroup(articleType, articlesOfType));
        }
    }
    // If any article groups were not matched, append them to the end.
    for (Map.Entry<ArticleType, List<Map<String, Object>>> entry : Multimaps.asMap(groupedArticles).entrySet()) {
        ArticleType type = entry.getKey();
        TypedArticleGroup group = new TypedArticleGroup(type, entry.getValue());
        articleGroups.add(group);
        log.warn(String.format("Issue %s has articles of type \"%s\", which is not configured for %s: %s", issueId, type.getName(), site.getKey(), Lists.transform(group.articles, article -> article.get("doi"))));
    }
    return articleGroups;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ArticleType(org.ambraproject.wombat.model.ArticleType)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ArticleType (org.ambraproject.wombat.model.ArticleType)1