Search in sources :

Example 6 with Doi

use of org.ambraproject.rhino.identity.Doi 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 7 with Doi

use of org.ambraproject.rhino.identity.Doi in project rhino by PLOS.

the class ArticleCrudServiceImpl method getLatestRevision.

@Override
public Optional<ArticleRevision> getLatestRevision(Article article) {
    Integer maxRevisionNumber = hibernateTemplate.execute(session -> {
        Query query = session.createQuery("" + "SELECT MAX(rev.revisionNumber) " + "FROM ArticleRevision rev, ArticleItem item " + "WHERE item.doi = :doi " + "  AND rev.ingestion = item.ingestion");
        query.setParameter("doi", Doi.create(article.getDoi()).getName());
        return (Integer) query.uniqueResult();
    });
    if (maxRevisionNumber == null)
        return Optional.empty();
    return Optional.ofNullable(hibernateTemplate.execute(session -> {
        Query query = session.createQuery("" + "FROM ArticleRevision as av " + "WHERE av.ingestion.article = :article " + "AND av.revisionNumber = :latestRevision");
        query.setParameter("article", article);
        query.setParameter("latestRevision", maxRevisionNumber);
        return (ArticleRevision) query.uniqueResult();
    }));
}
Also used : ArticleIngestionIdentifier(org.ambraproject.rhino.identity.ArticleIngestionIdentifier) ServiceResponse(org.ambraproject.rhino.rest.response.ServiceResponse) ArticleFileIdentifier(org.ambraproject.rhino.identity.ArticleFileIdentifier) RelatedArticleLink(org.ambraproject.rhino.model.article.RelatedArticleLink) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Collections2(com.google.common.collect.Collections2) Archive(org.ambraproject.rhino.util.Archive) ArticleXml(org.ambraproject.rhino.content.xml.ArticleXml) ArticleIngestion(org.ambraproject.rhino.model.ArticleIngestion) Matcher(java.util.regex.Matcher) Document(org.w3c.dom.Document) Map(java.util.Map) Query(org.hibernate.Query) StringEscapeUtils(org.apache.commons.lang3.StringEscapeUtils) ArticleAllAuthorsView(org.ambraproject.rhino.view.article.author.ArticleAllAuthorsView) ArticleItem(org.ambraproject.rhino.model.ArticleItem) RepoObjectMetadata(org.plos.crepo.model.metadata.RepoObjectMetadata) Collection(java.util.Collection) Set(java.util.Set) ResolvedDoiView(org.ambraproject.rhino.view.ResolvedDoiView) AssetCrudService(org.ambraproject.rhino.service.AssetCrudService) Collectors(java.util.stream.Collectors) ArticleIdentifier(org.ambraproject.rhino.identity.ArticleIdentifier) Objects(java.util.Objects) List(java.util.List) ArticleRevisionIdentifier(org.ambraproject.rhino.identity.ArticleRevisionIdentifier) ArticleRevision(org.ambraproject.rhino.model.ArticleRevision) LocalDate(java.time.LocalDate) Optional(java.util.Optional) ArticleFile(org.ambraproject.rhino.model.ArticleFile) Pattern(java.util.regex.Pattern) ArticleRevisionView(org.ambraproject.rhino.view.article.ArticleRevisionView) ArticleRelationship(org.ambraproject.rhino.model.ArticleRelationship) Iterables(com.google.common.collect.Iterables) Article(org.ambraproject.rhino.model.Article) CacheableResponse(org.ambraproject.rhino.rest.response.CacheableResponse) RestClientException(org.ambraproject.rhino.rest.RestClientException) ArticleOverview(org.ambraproject.rhino.view.article.ArticleOverview) ArticleItemIdentifier(org.ambraproject.rhino.identity.ArticleItemIdentifier) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) TaxonomyService(org.ambraproject.rhino.service.taxonomy.TaxonomyService) XpathReader(org.ambraproject.rhino.content.xml.XpathReader) Doi(org.ambraproject.rhino.identity.Doi) ByteSource(com.google.common.io.ByteSource) XPathException(javax.xml.xpath.XPathException) ArticleIngestionView(org.ambraproject.rhino.view.article.ArticleIngestionView) Logger(org.slf4j.Logger) XmlContentException(org.ambraproject.rhino.content.xml.XmlContentException) IOException(java.io.IOException) ArticleCategoryAssignment(org.ambraproject.rhino.model.ArticleCategoryAssignment) HttpStatus(org.springframework.http.HttpStatus) ArticleCrudService(org.ambraproject.rhino.service.ArticleCrudService) AuthorView(org.ambraproject.rhino.view.article.author.AuthorView) CategoryAssignmentView(org.ambraproject.rhino.view.article.CategoryAssignmentView) ItemSetView(org.ambraproject.rhino.view.article.ItemSetView) InputStream(java.io.InputStream) Query(org.hibernate.Query)

Example 8 with Doi

use of org.ambraproject.rhino.identity.Doi in project rhino by PLOS.

the class ArticleXml method disambiguateAssetNodes.

/**
   * @param assetNodesMetadata metadata for at least two asset nodes with the same DOI and unequal content
   * @return the metadata with the most non-empty fields
   * @throws XmlContentException if two or more asset nodes have non-empty, inconsistent title or description
   */
@VisibleForTesting
static AssetMetadata disambiguateAssetNodes(Collection<AssetMetadata> assetNodesMetadata) {
    // Find the node with the most substantial content
    AssetMetadata bestNode = assetNodesMetadata.stream().min(ASSET_NODE_PREFERENCE).orElseThrow(() -> new IllegalArgumentException("Argument list must not be empty"));
    // If any other nodes have non-empty fields that are inconsistent with bestNode, complain about ambiguity
    Collection<AssetMetadata> ambiguous = assetNodesMetadata.stream().filter(node -> {
        String title = node.getTitle();
        boolean ambiguousTitle = !title.isEmpty() && !title.equals(bestNode.getTitle());
        String description = node.getDescription();
        boolean ambiguousDescription = !description.isEmpty() && !description.equals(bestNode.getDescription());
        return ambiguousTitle || ambiguousDescription;
    }).collect(Collectors.toList());
    if (!ambiguous.isEmpty()) {
        throw new XmlContentException(String.format("Ambiguous asset nodes: %s, %s", bestNode, ambiguous));
    }
    return bestNode;
}
Also used : LinkedListMultimap(com.google.common.collect.LinkedListMultimap) Logger(org.slf4j.Logger) ListMultimap(com.google.common.collect.ListMultimap) Collection(java.util.Collection) RelatedArticleLink(org.ambraproject.rhino.model.article.RelatedArticleLink) LoggerFactory(org.slf4j.LoggerFactory) Multimap(com.google.common.collect.Multimap) Collectors(java.util.stream.Collectors) ArticleIdentifier(org.ambraproject.rhino.identity.ArticleIdentifier) Strings(com.google.common.base.Strings) List(java.util.List) Lists(com.google.common.collect.Lists) AssetMetadata(org.ambraproject.rhino.model.article.AssetMetadata) ImmutableList(com.google.common.collect.ImmutableList) Document(org.w3c.dom.Document) Node(org.w3c.dom.Node) LocalDate(java.time.LocalDate) Map(java.util.Map) Preconditions(com.google.common.base.Preconditions) Doi(org.ambraproject.rhino.identity.Doi) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ArticleMetadata(org.ambraproject.rhino.model.article.ArticleMetadata) Comparator(java.util.Comparator) AssetMetadata(org.ambraproject.rhino.model.article.AssetMetadata) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with Doi

use of org.ambraproject.rhino.identity.Doi in project rhino by PLOS.

the class ArticlePackageBuilder method findAssetType.

private static AssetType findAssetType(AssetNodesByDoi assetNodeMap, ManifestXml.Asset asset) {
    if (asset.getAssetTagName().equals(ManifestXml.AssetTagName.ARTICLE)) {
        return AssetType.ARTICLE;
    }
    Doi assetIdentity = Doi.create(asset.getUri());
    if (!assetNodeMap.getDois().contains(assetIdentity)) {
        if (asset.isStrikingImage()) {
            return AssetType.STANDALONE_STRIKING_IMAGE;
        } else {
            throw new RestClientException("Asset not mentioned in manuscript: " + asset.getUri(), HttpStatus.BAD_REQUEST);
        }
    }
    List<Node> nodes = assetNodeMap.getNodes(assetIdentity);
    AssetType identifiedType = null;
    for (Node node : nodes) {
        String nodeName = node.getNodeName();
        AssetType assetType = getByXmlNodeName(nodeName);
        if (assetType != null) {
            if (identifiedType == null) {
                identifiedType = assetType;
            } else if (!identifiedType.equals(assetType)) {
                String message = String.format("Ambiguous nodes: %s, %s", identifiedType, assetType);
                throw new RestClientException(message, HttpStatus.BAD_REQUEST);
            }
        }
    }
    if (identifiedType == null) {
        throw new RestClientException("Type not recognized", HttpStatus.BAD_REQUEST);
    }
    return identifiedType;
}
Also used : Node(org.w3c.dom.Node) RestClientException(org.ambraproject.rhino.rest.RestClientException) AssetNodesByDoi(org.ambraproject.rhino.content.xml.AssetNodesByDoi) Doi(org.ambraproject.rhino.identity.Doi)

Example 10 with Doi

use of org.ambraproject.rhino.identity.Doi in project rhino by PLOS.

the class ArticleXml method findAllAssetNodes.

/**
   * Find each node within this object's XML whose name is expected to be associated with an asset entity.
   *
   * @return the list of asset nodes
   */
public AssetNodesByDoi findAllAssetNodes() {
    // Find all nodes of an asset type and map them by DOI
    List<Node> rawNodes = readNodeList(ASSET_EXPRESSION);
    ListMultimap<Doi, Node> nodeMap = LinkedListMultimap.create(rawNodes.size());
    for (Node node : rawNodes) {
        Doi assetDoi = getAssetDoi(node);
        if (assetDoi != null) {
            nodeMap.put(assetDoi, node);
        } else {
            findNestedDoi(node, nodeMap);
        }
    }
    // Replace <graphic> nodes without changing keys or iteration order
    for (Map.Entry<Doi, Node> entry : nodeMap.entries()) {
        Node node = entry.getValue();
        if (node.getNodeName().equals(GRAPHIC)) {
            entry.setValue(replaceGraphicNode(node));
        }
    }
    return new AssetNodesByDoi(nodeMap);
}
Also used : Node(org.w3c.dom.Node) Map(java.util.Map) Doi(org.ambraproject.rhino.identity.Doi)

Aggregations

Doi (org.ambraproject.rhino.identity.Doi)12 RestClientException (org.ambraproject.rhino.rest.RestClientException)4 Node (org.w3c.dom.Node)4 Collection (java.util.Collection)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 ArticleIdentifier (org.ambraproject.rhino.identity.ArticleIdentifier)3 Article (org.ambraproject.rhino.model.Article)3 Strings (com.google.common.base.Strings)2 Lists (com.google.common.collect.Lists)2 LocalDate (java.time.LocalDate)2 Optional (java.util.Optional)2 ArticleFile (org.ambraproject.rhino.model.ArticleFile)2 ArticleIngestion (org.ambraproject.rhino.model.ArticleIngestion)2 ArticleItem (org.ambraproject.rhino.model.ArticleItem)2 ArticleMetadata (org.ambraproject.rhino.model.article.ArticleMetadata)2 RelatedArticleLink (org.ambraproject.rhino.model.article.RelatedArticleLink)2 Query (org.hibernate.Query)2 Logger (org.slf4j.Logger)2