@POST
@Path("addComment")
public Object addComment() throws ClientException {
try {
HttpServletRequest request = ctx.getRequest();
CoreSession session = ctx.getCoreSession();
// Create pending comment
DocumentModel myComment = session.createDocumentModel("Comment");
// Set comment properties
myComment.setProperty("comment", "author",
ctx.getPrincipal().getName());
myComment.setProperty("comment", "text",
request.getParameter("commentContent"));
myComment.setProperty("comment", "creationDate",
Calendar.getInstance());
// Retrieve document to comment
String docToCommentRef = request.getParameter("docToCommentRef");
DocumentModel docToComment = session.getDocument(new IdRef(
docToCommentRef));
String commentParentRef = request.getParameter("commentParentRef");
// Create comment
CommentableDocument commentableDoc = null;
if (docToComment != null) {
commentableDoc = docToComment.getAdapter(CommentableDocument.class);
}
DocumentModel newComment;
if (commentParentRef != null) {
// if exists retrieve comment parent
DocumentModel commentParent = session.getDocument(new IdRef(
commentParentRef));
newComment = commentableDoc.addComment(commentParent, myComment);
} else {
newComment = commentableDoc.addComment(myComment);
}