Search in sources :

Example 1 with CompetingInterestPolicy

use of org.ambraproject.rhino.view.comment.CompetingInterestPolicy in project rhino by PLOS.

the class CommentCrudServiceImpl method createView.

private CommentOutputView createView(Comment comment) {
    Article article = comment.getArticle();
    Collection<Comment> articleComments = fetchAllComments(article);
    return new CommentOutputView.Factory(new CompetingInterestPolicy(runtimeConfiguration), articleComments, article).buildView(comment);
}
Also used : Comment(org.ambraproject.rhino.model.Comment) Article(org.ambraproject.rhino.model.Article) CommentOutputView(org.ambraproject.rhino.view.comment.CommentOutputView) CompetingInterestPolicy(org.ambraproject.rhino.view.comment.CompetingInterestPolicy)

Example 2 with CompetingInterestPolicy

use of org.ambraproject.rhino.view.comment.CompetingInterestPolicy 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 3 with CompetingInterestPolicy

use of org.ambraproject.rhino.view.comment.CompetingInterestPolicy in project rhino by PLOS.

the class CommentCrudServiceImpl method serveComments.

@Override
public ServiceResponse<List<CommentOutputView>> serveComments(ArticleIdentifier articleId) throws IOException {
    Article article = articleCrudService.readArticle(articleId);
    Collection<Comment> comments = fetchAllComments(article);
    CommentOutputView.Factory factory = new CommentOutputView.Factory(new CompetingInterestPolicy(runtimeConfiguration), comments, article);
    List<CommentOutputView> views = comments.stream().filter(comment -> comment.getParent() == null).sorted(CommentOutputView.BY_DATE).map(factory::buildView).collect(Collectors.toList());
    return ServiceResponse.serveView(views);
}
Also used : CompetingInterestPolicy(org.ambraproject.rhino.view.comment.CompetingInterestPolicy) Flag(org.ambraproject.rhino.model.Flag) ServiceResponse(org.ambraproject.rhino.rest.response.ServiceResponse) CommentCrudService(org.ambraproject.rhino.service.CommentCrudService) Article(org.ambraproject.rhino.model.Article) Journal(org.ambraproject.rhino.model.Journal) CacheableResponse(org.ambraproject.rhino.rest.response.CacheableResponse) CommentFlagInputView(org.ambraproject.rhino.view.comment.CommentFlagInputView) JournalCrudService(org.ambraproject.rhino.service.JournalCrudService) RestClientException(org.ambraproject.rhino.rest.RestClientException) Session(org.hibernate.Session) Autowired(org.springframework.beans.factory.annotation.Autowired) Strings(com.google.common.base.Strings) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) CommentNodeView(org.ambraproject.rhino.view.comment.CommentNodeView) CommentIdentifier(org.ambraproject.rhino.identity.CommentIdentifier) CommentFlagOutputView(org.ambraproject.rhino.view.comment.CommentFlagOutputView) CommentOutputView(org.ambraproject.rhino.view.comment.CommentOutputView) Query(org.hibernate.Query) Doi(org.ambraproject.rhino.identity.Doi) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) CommentCountView(org.ambraproject.rhino.view.comment.CommentCountView) Collection(java.util.Collection) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ArticleIdentifier(org.ambraproject.rhino.identity.ArticleIdentifier) HttpStatus(org.springframework.http.HttpStatus) ArticleCrudService(org.ambraproject.rhino.service.ArticleCrudService) List(java.util.List) CommentInputView(org.ambraproject.rhino.view.comment.CommentInputView) LocalDate(java.time.LocalDate) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) FlagReasonCode(org.ambraproject.rhino.model.FlagReasonCode) Pattern(java.util.regex.Pattern) Comment(org.ambraproject.rhino.model.Comment) Comment(org.ambraproject.rhino.model.Comment) Article(org.ambraproject.rhino.model.Article) CommentOutputView(org.ambraproject.rhino.view.comment.CommentOutputView) CompetingInterestPolicy(org.ambraproject.rhino.view.comment.CompetingInterestPolicy)

Aggregations

Article (org.ambraproject.rhino.model.Article)3 Comment (org.ambraproject.rhino.model.Comment)3 CommentOutputView (org.ambraproject.rhino.view.comment.CommentOutputView)3 CompetingInterestPolicy (org.ambraproject.rhino.view.comment.CompetingInterestPolicy)3 UUID (java.util.UUID)2 ArticleIdentifier (org.ambraproject.rhino.identity.ArticleIdentifier)2 Doi (org.ambraproject.rhino.identity.Doi)2 RestClientException (org.ambraproject.rhino.rest.RestClientException)2 Preconditions (com.google.common.base.Preconditions)1 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 LocalDate (java.time.LocalDate)1 Collection (java.util.Collection)1 List (java.util.List)1 Optional (java.util.Optional)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 CommentIdentifier (org.ambraproject.rhino.identity.CommentIdentifier)1