Search in sources :

Example 1 with ApiAddress

use of org.ambraproject.wombat.service.remote.ApiAddress in project wombat by PLOS.

the class ArticleController method receiveCommentFlag.

@RequestMapping(name = "postCommentFlag", method = RequestMethod.POST, value = "/article/comments/flag")
@ResponseBody
public Object receiveCommentFlag(HttpServletRequest request, @RequestParam("reasonCode") String reasonCode, @RequestParam("comment") String flagCommentBody, @RequestParam("target") String targetCommentDoi) throws IOException {
    checkCommentsAreEnabled();
    Map<String, Object> validationErrors = commentValidationService.validateFlag(flagCommentBody);
    if (!validationErrors.isEmpty()) {
        return ImmutableMap.of("validationErrors", validationErrors);
    }
    String authId = request.getRemoteUser();
    final String creatorUserId = authId == null ? null : userApi.getUserIdFromAuthId(authId);
    ArticleCommentFlag flag = new ArticleCommentFlag(creatorUserId, flagCommentBody, reasonCode);
    Map<String, Object> comment = getComment(targetCommentDoi);
    String parentArticleDoi = getParentArticleDoiFromComment(comment);
    ApiAddress address = ApiAddress.builder("articles").embedDoi(parentArticleDoi).addToken("comments").embedDoi(targetCommentDoi).addToken("flags").build();
    articleApi.postObject(address, flag);
    // the "201 CREATED" status is all the AJAX client needs
    return ImmutableMap.of();
}
Also used : ArticleCommentFlag(org.ambraproject.wombat.model.ArticleCommentFlag) ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with ApiAddress

use of org.ambraproject.wombat.service.remote.ApiAddress in project wombat by PLOS.

the class CommentServiceImpl method getRecentJournalComments.

@Override
public List<Map<String, Object>> getRecentJournalComments(String journalKey, int count) throws IOException {
    Preconditions.checkArgument(count >= 0);
    ApiAddress requestAddress = ApiAddress.builder("comments").addParameter("journal", journalKey).addParameter("limit", count).build();
    List<Map<String, Object>> comments = articleApi.requestObject(requestAddress, List.class);
    comments.forEach(comment -> modifyCommentTree(comment, CommentFormatting::addFormattingFields));
    addArticleTitle(comments);
    addCreatorData(comments);
    return comments;
}
Also used : ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 3 with ApiAddress

use of org.ambraproject.wombat.service.remote.ApiAddress in project wombat by PLOS.

the class CommentServiceImpl method getComment.

@Override
public Map<String, Object> getComment(String commentDoi, Site site) throws IOException {
    Map<String, Object> comment;
    try {
        final ApiAddress commentUrl = ApiAddress.builder("comments").embedDoi(commentDoi).build();
        comment = articleApi.requestObject(commentUrl, Map.class);
    } catch (EntityNotFoundException enfe) {
        throw new CommentNotFoundException(commentDoi, enfe);
    }
    modifyCommentTree(comment, CommentFormatting::addFormattingFields);
    if (site.getType() == null || !site.getType().equals("preprints")) {
        addCreatorData(ImmutableList.of(comment));
    }
    return comment;
}
Also used : ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 4 with ApiAddress

use of org.ambraproject.wombat.service.remote.ApiAddress in project wombat by PLOS.

the class CommentControllerTest method setup.

@Before
public void setup() throws IOException {
    when(articleMetadata.validateVisibility(anyString())).thenReturn(articleMetadata);
    when(articleMetadata.populate(any(), any())).thenReturn(articleMetadata);
    when(articleMetadata.fillAmendments(any())).thenReturn(articleMetadata);
    doReturn(articleMetadata).when(articleMetadataFactory).newInstance(any(), any(), any(), any(), any(), any());
    expectedRequestedDoi = RequestedDoiVersion.of(EXPECTED_DOI);
    expectedArticlePointer = new ArticlePointer(expectedRequestedDoi, EXPECTED_DOI, EXPECTED_INGESTION_NUMBER, expectedRequestedDoi.getRevisionNumber());
    when(articleResolutionService.toIngestion(expectedRequestedDoi)).thenReturn(expectedArticlePointer);
    Map<String, Object> itemResponse = ImmutableMap.of("items", ImmutableMap.of());
    doAnswer(invocation -> {
        return itemResponse;
    }).when(articleService).getItemTable(expectedArticlePointer);
    ImmutableMap<String, String> journal = ImmutableMap.of("journalKey", DESKTOP_PLOS_ONE);
    ImmutableMap<String, Object> ingestionMetadata = ImmutableMap.of("journal", journal);
    ApiAddress address = ApiAddress.builder("articles").embedDoi(EXPECTED_DOI).addToken("relationships").build();
    when(articleApi.requestObject(address, ArticleMetadata.Factory.RELATED_ARTICLE_GSON_TYPE)).thenReturn(ImmutableList.of());
    when(articleApi.requestObject(any(), eq(Map.class))).thenReturn(ingestionMetadata);
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ArticlePointer(org.ambraproject.wombat.identity.ArticlePointer) Before(org.junit.Before)

Example 5 with ApiAddress

use of org.ambraproject.wombat.service.remote.ApiAddress in project wombat by PLOS.

the class ArticleMetadataTest method testFetchRelatedArticles.

@Test
public void testFetchRelatedArticles() throws Exception {
    String doi = "10.9999/journal.xxxx.0";
    List<RelatedArticle> map = new Gson().fromJson(read("articleMeta/ppat.1005446.related.json"), ArticleMetadata.Factory.RELATED_ARTICLE_GSON_TYPE);
    ApiAddress address = ApiAddress.builder("articles").embedDoi(doi).addToken("relationships").build();
    when(articleApi.requestObject(address, ArticleMetadata.Factory.RELATED_ARTICLE_GSON_TYPE)).thenReturn(map);
    List<RelatedArticle> raList = articleMetadataFactory.fetchRelatedArticles(doi);
    assertEquals(1, raList.size());
    RelatedArticle ra = raList.get(0);
    assertEquals("10.1371/journal.ppat.1006021", ra.getDoi());
    assertEquals(null, ra.getType().getSpecificUse());
}
Also used : RelatedArticle(org.ambraproject.wombat.model.RelatedArticle) Gson(com.google.gson.Gson) ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress) Test(org.junit.Test)

Aggregations

ApiAddress (org.ambraproject.wombat.service.remote.ApiAddress)15 Map (java.util.Map)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ArticlePointer (org.ambraproject.wombat.identity.ArticlePointer)3 Test (org.junit.Test)3 Gson (com.google.gson.Gson)2 ArticleComment (org.ambraproject.wombat.model.ArticleComment)2 ArticleCommentFlag (org.ambraproject.wombat.model.ArticleCommentFlag)2 RelatedArticle (org.ambraproject.wombat.model.RelatedArticle)2 HttpResponse (org.apache.http.HttpResponse)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Type (java.lang.reflect.Type)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 NotFoundException (org.ambraproject.wombat.controller.NotFoundException)1