Search in sources :

Example 11 with RestClientException

use of org.ambraproject.rhino.rest.RestClientException 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 12 with RestClientException

use of org.ambraproject.rhino.rest.RestClientException in project rhino by PLOS.

the class CommentCrudServiceImpl method getFlag.

private Flag getFlag(Long commentFlagId) {
    Flag flag = hibernateTemplate.execute(session -> {
        Query query = session.createQuery("FROM Flag WHERE commentFlagId = :commentFlagId");
        query.setParameter("commentFlagId", commentFlagId);
        return (Flag) query.uniqueResult();
    });
    if (flag == null) {
        String message = "Comment flag not found at the provided ID: " + commentFlagId;
        throw new RestClientException(message, HttpStatus.NOT_FOUND);
    }
    return flag;
}
Also used : Query(org.hibernate.Query) RestClientException(org.ambraproject.rhino.rest.RestClientException) Flag(org.ambraproject.rhino.model.Flag)

Example 13 with RestClientException

use of org.ambraproject.rhino.rest.RestClientException in project rhino by PLOS.

the class VolumeCrudServiceImpl method create.

@Override
public Volume create(String journalKey, VolumeInputView input) {
    Preconditions.checkNotNull(journalKey);
    VolumeIdentifier volumeId = VolumeIdentifier.create(input.getDoi());
    if (getVolume(volumeId).isPresent()) {
        throw new RestClientException("Volume already exists with DOI: " + volumeId.getDoi(), HttpStatus.BAD_REQUEST);
    }
    Volume volume = new Volume();
    volume.setIssues(new ArrayList<>(0));
    volume = applyInput(volume, input);
    Journal journal = journalCrudService.readJournal(journalKey);
    journal.getVolumes().add(volume);
    hibernateTemplate.save(journal);
    return volume;
}
Also used : Volume(org.ambraproject.rhino.model.Volume) RestClientException(org.ambraproject.rhino.rest.RestClientException) Journal(org.ambraproject.rhino.model.Journal) VolumeIdentifier(org.ambraproject.rhino.identity.VolumeIdentifier)

Example 14 with RestClientException

use of org.ambraproject.rhino.rest.RestClientException in project rhino by PLOS.

the class IssueCrudController method create.

@Transactional(rollbackFor = { Throwable.class })
@RequestMapping(value = "/journals/{journalKey}/volumes/{volumeDoi}/issues", method = RequestMethod.POST)
@ApiImplicitParam(name = "body", paramType = "body", dataType = "IssueInputView", value = "example: {\"doi\": \"10.1371/issue.pbio.v02.i07\", " + "\"displayName\": \"July\", " + "\"imageArticleDoi\": \"10.1371/image.pbio.v02.i07\", " + "\"articleOrder\": [\"10.1371/journal.pbio.0020213\", \"10.1371/journal.pbio.0020214\", " + "\"10.1371/journal.pbio.0020228\"]}")
public ResponseEntity<?> create(HttpServletRequest request, @PathVariable("journalKey") String journalKey, @PathVariable("volumeDoi") String volumeDoi) throws IOException {
    // TODO: Validate journalKey
    VolumeIdentifier volumeId = VolumeIdentifier.create(DoiEscaping.unescape(volumeDoi));
    IssueInputView input = readJsonFromRequest(request, IssueInputView.class);
    if (StringUtils.isBlank(input.getDoi())) {
        throw new RestClientException("issueUri required", HttpStatus.BAD_REQUEST);
    }
    Issue issue = issueCrudService.create(volumeId, input);
    return ServiceResponse.reportCreated(issueOutputViewFactory.getView(issue)).asJsonResponse(entityGson);
}
Also used : Issue(org.ambraproject.rhino.model.Issue) RestClientException(org.ambraproject.rhino.rest.RestClientException) VolumeIdentifier(org.ambraproject.rhino.identity.VolumeIdentifier) IssueInputView(org.ambraproject.rhino.view.journal.IssueInputView) ApiImplicitParam(com.wordnik.swagger.annotations.ApiImplicitParam) Transactional(org.springframework.transaction.annotation.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with RestClientException

use of org.ambraproject.rhino.rest.RestClientException 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

RestClientException (org.ambraproject.rhino.rest.RestClientException)21 Transactional (org.springframework.transaction.annotation.Transactional)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 Volume (org.ambraproject.rhino.model.Volume)5 Issue (org.ambraproject.rhino.model.Issue)4 Journal (org.ambraproject.rhino.model.Journal)4 ApiImplicitParam (com.wordnik.swagger.annotations.ApiImplicitParam)3 ManifestXml (org.ambraproject.rhino.content.xml.ManifestXml)3 ArticleIdentifier (org.ambraproject.rhino.identity.ArticleIdentifier)3 Article (org.ambraproject.rhino.model.Article)3 Comment (org.ambraproject.rhino.model.Comment)3 Query (org.hibernate.Query)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Doi (org.ambraproject.rhino.identity.Doi)2 VolumeIdentifier (org.ambraproject.rhino.identity.VolumeIdentifier)2 ArticleList (org.ambraproject.rhino.model.ArticleList)2 Session (org.hibernate.Session)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1