Search in sources :

Example 16 with Article

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

the class CommentCrudServiceImpl method createComment.

@Override
public ServiceResponse<CommentOutputView> createComment(Optional<ArticleIdentifier> articleId, CommentInputView input) {
    final Optional<String> parentCommentUri = Optional.ofNullable(input.getParentCommentId());
    final Article article;
    final Comment parentComment;
    if (parentCommentUri.isPresent()) {
        parentComment = readComment(CommentIdentifier.create(parentCommentUri.get()));
        if (parentComment == null) {
            throw new RestClientException("Parent comment not found: " + parentCommentUri, HttpStatus.BAD_REQUEST);
        }
        article = parentComment.getArticle();
        ArticleIdentifier articleDoiFromDb = ArticleIdentifier.create(parentComment.getArticle().getDoi());
        if (!articleId.isPresent()) {
            articleId = Optional.of(articleDoiFromDb);
        } else if (!articleId.get().equals(articleDoiFromDb)) {
            String message = String.format("Parent comment (%s) not from declared article (%s).", parentCommentUri.get(), articleId.get());
            throw new RestClientException(message, HttpStatus.BAD_REQUEST);
        }
    } else {
        // The comment is a root-level reply to an article (no parent comment).
        if (!articleId.isPresent()) {
            throw new RestClientException("Must provide articleId or parentCommentUri", HttpStatus.BAD_REQUEST);
        }
        article = articleCrudService.readArticle(articleId.get());
        parentComment = null;
    }
    // comment receives same DOI prefix as article
    String doiPrefix = extractDoiPrefix(articleId.get());
    // generate a new DOI out of a random UUID
    UUID uuid = UUID.randomUUID();
    Doi createdCommentUri = Doi.create(doiPrefix + "annotation/" + uuid);
    Comment created = new Comment();
    created.setArticle(article);
    created.setParent(parentComment);
    created.setCommentUri(createdCommentUri.getName());
    created.setUserProfileID(Long.valueOf(Strings.nullToEmpty(input.getCreatorUserId())));
    created.setTitle(Strings.nullToEmpty(input.getTitle()));
    created.setBody(Strings.nullToEmpty(input.getBody()));
    created.setHighlightedText(Strings.nullToEmpty(input.getHighlightedText()));
    created.setCompetingInterestBody(Strings.nullToEmpty(input.getCompetingInterestStatement()));
    created.setIsRemoved(Boolean.valueOf(Strings.nullToEmpty(input.getIsRemoved())));
    hibernateTemplate.save(created);
    // the new comment can't have any children yet
    List<Comment> childComments = ImmutableList.of();
    CompetingInterestPolicy competingInterestPolicy = new CompetingInterestPolicy(runtimeConfiguration);
    CommentOutputView.Factory viewFactory = new CommentOutputView.Factory(competingInterestPolicy, childComments, article);
    CommentOutputView view = viewFactory.buildView(created);
    return ServiceResponse.reportCreated(view);
}
Also used : Comment(org.ambraproject.rhino.model.Comment) Article(org.ambraproject.rhino.model.Article) ArticleIdentifier(org.ambraproject.rhino.identity.ArticleIdentifier) RestClientException(org.ambraproject.rhino.rest.RestClientException) CommentOutputView(org.ambraproject.rhino.view.comment.CommentOutputView) UUID(java.util.UUID) Doi(org.ambraproject.rhino.identity.Doi) CompetingInterestPolicy(org.ambraproject.rhino.view.comment.CompetingInterestPolicy)

Example 17 with Article

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

the class ArticleRevisionWriteServiceImpl method writeRevision.

@Override
public ArticleRevision writeRevision(ArticleRevisionIdentifier revisionId, ArticleIngestionIdentifier ingestionId) {
    Preconditions.checkArgument(revisionId.getArticleIdentifier().equals(ingestionId.getArticleIdentifier()));
    ArticleIngestion ingestion = articleCrudService.readIngestion(ingestionId);
    Article article = ingestion.getArticle();
    Optional<ArticleRevision> previousLatest = articleCrudService.getLatestRevision(article);
    ArticleRevision newRevision = articleCrudService.getRevision(revisionId).orElseGet(() -> {
        ArticleRevision revision = new ArticleRevision();
        revision.setRevisionNumber(revisionId.getRevision());
        return revision;
    });
    newRevision.setIngestion(ingestion);
    hibernateTemplate.saveOrUpdate(newRevision);
    if (!previousLatest.isPresent() || previousLatest.get().getRevisionNumber() <= newRevision.getRevisionNumber()) {
        refreshForLatestRevision(newRevision);
    }
    return newRevision;
}
Also used : ArticleIngestion(org.ambraproject.rhino.model.ArticleIngestion) ArticleRevision(org.ambraproject.rhino.model.ArticleRevision) Article(org.ambraproject.rhino.model.Article)

Example 18 with Article

use of org.ambraproject.rhino.model.Article 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 19 with Article

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

the class ArticleCrudServiceImpl method serveCategories.

/**
   * {@inheritDoc}
   *
   * @param articleId
   */
@Override
public ServiceResponse<Collection<CategoryAssignmentView>> serveCategories(final ArticleIdentifier articleId) throws IOException {
    Article article = readArticle(articleId);
    Collection<ArticleCategoryAssignment> categoryAssignments = taxonomyService.getAssignmentsForArticle(article);
    Collection<CategoryAssignmentView> views = categoryAssignments.stream().map(CategoryAssignmentView::new).collect(Collectors.toList());
    return ServiceResponse.serveView(views);
}
Also used : ArticleCategoryAssignment(org.ambraproject.rhino.model.ArticleCategoryAssignment) CategoryAssignmentView(org.ambraproject.rhino.view.article.CategoryAssignmentView) Article(org.ambraproject.rhino.model.Article)

Example 20 with Article

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

the class ArticleListCrudServiceImpl method fetchArticles.

/**
   * Fetch all articles with the given IDs, in the same iteration error.
   *
   * @param articleIds a set of article IDs
   * @return the articles in the same order, if all exist
   * @throws RestClientException if not every article ID belongs to an existing article
   */
private List<Article> fetchArticles(Set<ArticleIdentifier> articleIds) {
    if (articleIds.isEmpty())
        return ImmutableList.of();
    final Map<String, Integer> articleKeys = new HashMap<>();
    int i = 0;
    for (ArticleIdentifier articleId : articleIds) {
        articleKeys.put(articleId.getDoiName(), i++);
    }
    List<Article> articles = (List<Article>) hibernateTemplate.findByNamedParam("from Article where doi in :articleKeys", "articleKeys", articleKeys.keySet());
    if (articles.size() < articleKeys.size()) {
        throw new RestClientException(buildMissingArticleMessage(articles, articleKeys.keySet()), HttpStatus.NOT_FOUND);
    }
    Collections.sort(articles, (a1, a2) -> {
        // We expect the error check above to guarantee that both values will be found in the map
        int i1 = articleKeys.get(a1.getDoi());
        int i2 = articleKeys.get(a2.getDoi());
        return i1 - i2;
    });
    return articles;
}
Also used : ArticleIdentifier(org.ambraproject.rhino.identity.ArticleIdentifier) HashMap(java.util.HashMap) Article(org.ambraproject.rhino.model.Article) RestClientException(org.ambraproject.rhino.rest.RestClientException) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ArticleList(org.ambraproject.rhino.model.ArticleList) List(java.util.List)

Aggregations

Article (org.ambraproject.rhino.model.Article)31 ArticleRevision (org.ambraproject.rhino.model.ArticleRevision)10 ArticleIdentifier (org.ambraproject.rhino.identity.ArticleIdentifier)9 List (java.util.List)8 ArticleIngestion (org.ambraproject.rhino.model.ArticleIngestion)8 RestClientException (org.ambraproject.rhino.rest.RestClientException)7 Document (org.w3c.dom.Document)7 IOException (java.io.IOException)6 Optional (java.util.Optional)6 Query (org.hibernate.Query)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 Collection (java.util.Collection)5 Collectors (java.util.stream.Collectors)5 ArticleCrudService (org.ambraproject.rhino.service.ArticleCrudService)5 LocalDate (java.time.LocalDate)4 ArrayList (java.util.ArrayList)4 Doi (org.ambraproject.rhino.identity.Doi)4 ArticleCategoryAssignment (org.ambraproject.rhino.model.ArticleCategoryAssignment)4 CacheableResponse (org.ambraproject.rhino.rest.response.CacheableResponse)4 HttpStatus (org.springframework.http.HttpStatus)4