@Override
public DocumentModel createComment(DocumentModel docModel, DocumentModel commentModel)
throws CommentSecurityException {
NuxeoPrincipal principal = commentModel.getCoreSession().getPrincipal();
try (CloseableCoreSession session = CoreInstance.openCoreSessionSystem(docModel.getRepositoryName())) {
DocumentRef docRef = getAncestorRef(session, docModel);
if (!session.hasPermission(principal, docRef, SecurityConstants.READ)) {
throw new CommentSecurityException(
"The user " + principal.getName() + " can not create comments on document " + docModel.getId());
}
String path = getCommentContainerPath(session, docModel.getId());
DocumentModel commentModelToCreate = session.createDocumentModel(path, COMMENT_NAME,
commentModel.getType());
commentModelToCreate.copyContent(commentModel);
commentModelToCreate.setPropertyValue(COMMENT_ANCESTOR_IDS,
(Serializable) computeAncestorIds(session, docModel.getId()));
DocumentModel comment = session.createDocument(commentModelToCreate);
comment.detach(true);
notifyEvent(session, CommentEvents.COMMENT_ADDED, docModel, comment);
return comment;
}
}