Search in sources :

Example 1 with ArticleList

use of org.ambraproject.rhino.model.ArticleList in project rhino by PLOS.

the class ArticleListCrudServiceImpl method create.

@Override
public ArticleListView create(ArticleListIdentity identity, String displayName, Set<ArticleIdentifier> articleIds) {
    if (listExists(identity)) {
        throw new RestClientException("List already exists: " + identity, HttpStatus.BAD_REQUEST);
    }
    ArticleList list = new ArticleList();
    list.setListType(identity.getType());
    list.setListKey(identity.getKey());
    list.setDisplayName(displayName);
    list.setArticles(fetchArticles(articleIds));
    Journal journal = (Journal) DataAccessUtils.uniqueResult(hibernateTemplate.findByCriteria(DetachedCriteria.forClass(Journal.class).add(Restrictions.eq("journalKey", identity.getJournalKey()))));
    if (journal == null) {
        throw new RestClientException("Journal not found: " + identity.getJournalKey(), HttpStatus.BAD_REQUEST);
    }
    Collection<ArticleList> journalLists = journal.getArticleLists();
    if (journalLists == null) {
        journal.setArticleLists(journalLists = new ArrayList<>(1));
    }
    journalLists.add(list);
    hibernateTemplate.update(journal);
    return articleListViewFactory.getView(list, journal.getJournalKey());
}
Also used : RestClientException(org.ambraproject.rhino.rest.RestClientException) ArrayList(java.util.ArrayList) Journal(org.ambraproject.rhino.model.Journal) ArticleList(org.ambraproject.rhino.model.ArticleList)

Example 2 with ArticleList

use of org.ambraproject.rhino.model.ArticleList in project rhino by PLOS.

the class ArticleListCrudServiceImpl method update.

@Override
public ArticleListView update(ArticleListIdentity identity, Optional<String> displayName, Optional<? extends Set<ArticleIdentifier>> articleIds) {
    ArticleListView listView = getArticleList(identity);
    ArticleList list = listView.getArticleList();
    if (displayName.isPresent()) {
        list.setDisplayName(displayName.get());
    }
    if (articleIds.isPresent()) {
        List<Article> newArticles = fetchArticles(articleIds.get());
        List<Article> oldArticles = list.getArticles();
        oldArticles.clear();
        oldArticles.addAll(newArticles);
    }
    hibernateTemplate.update(list);
    return listView;
}
Also used : Article(org.ambraproject.rhino.model.Article) ArticleListView(org.ambraproject.rhino.view.journal.ArticleListView) ArticleList(org.ambraproject.rhino.model.ArticleList)

Example 3 with ArticleList

use of org.ambraproject.rhino.model.ArticleList in project rhino by PLOS.

the class ArticleListCrudServiceImpl method asArticleListViews.

private Collection<ArticleListView> asArticleListViews(List<Object[]> results, boolean excludeArticleMetadata) {
    Collection<ArticleListView> views = new ArrayList<>(results.size());
    for (Object[] result : results) {
        String journalKey = (String) result[0];
        ArticleList articleList = (ArticleList) result[1];
        views.add(articleListViewFactory.getView(articleList, journalKey, excludeArticleMetadata));
    }
    return views;
}
Also used : ArrayList(java.util.ArrayList) ArticleListView(org.ambraproject.rhino.view.journal.ArticleListView) ArticleList(org.ambraproject.rhino.model.ArticleList)

Example 4 with ArticleList

use of org.ambraproject.rhino.model.ArticleList in project rhino by PLOS.

the class ArticleListCrudServiceImpl method getArticleList.

private ArticleListView getArticleList(final ArticleListIdentity identity) {
    Object[] result = DataAccessUtils.uniqueResult(hibernateTemplate.execute((HibernateCallback<List<Object[]>>) session -> {
        Query query = queryFor(session, "select j.journalKey, l", identity);
        return query.list();
    }));
    if (result == null) {
        throw nonexistentList(identity);
    }
    String journalKey = (String) result[0];
    ArticleList articleList = (ArticleList) result[1];
    return articleListViewFactory.getView(articleList, journalKey);
}
Also used : Query(org.hibernate.Query) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) ArticleList(org.ambraproject.rhino.model.ArticleList)

Aggregations

ArticleList (org.ambraproject.rhino.model.ArticleList)4 ArrayList (java.util.ArrayList)2 ArticleListView (org.ambraproject.rhino.view.journal.ArticleListView)2 Article (org.ambraproject.rhino.model.Article)1 Journal (org.ambraproject.rhino.model.Journal)1 RestClientException (org.ambraproject.rhino.rest.RestClientException)1 Query (org.hibernate.Query)1 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)1