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);
}
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();
}));
}
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;
}
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;
}
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);
}
Aggregations