001/* 002 * (C) Copyright 2007-2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 */ 019package org.nuxeo.ecm.platform.comment.impl; 020 021import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_PARENT_ID; 022 023import java.util.ArrayList; 024import java.util.HashSet; 025import java.util.List; 026import java.util.Set; 027import java.util.stream.Collectors; 028 029import org.nuxeo.ecm.core.api.CoreInstance; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.IdRef; 033import org.nuxeo.ecm.platform.comment.api.Comment; 034import org.nuxeo.ecm.platform.comment.api.CommentManager; 035import org.nuxeo.ecm.platform.comment.api.CommentableDocument; 036import org.nuxeo.runtime.api.Framework; 037 038/** 039 * @author <a href="mailto:[email protected]">George Lefter</a> 040 */ 041public class CommentableDocumentAdapter implements CommentableDocument { 042 043 private static final long serialVersionUID = 2996381735762615450L; 044 045 final DocumentModel docModel; 046 047 final CommentManager commentManager; 048 049 public CommentableDocumentAdapter(DocumentModel docModel) { 050 this.commentManager = Framework.getService(CommentManager.class); 051 this.docModel = docModel; 052 } 053 054 public DocumentModel addComment(DocumentModel comment) { 055 return addComment(docModel, comment); 056 } 057 058 @SuppressWarnings("unchecked") 059 public DocumentModel addComment(DocumentModel comment, String path) { 060 comment.setPropertyValue(COMMENT_PARENT_ID, docModel.getId()); 061 return commentManager.createLocatedComment(docModel, comment, path); 062 } 063 064 @SuppressWarnings("unchecked") 065 public DocumentModel addComment(DocumentModel parent, DocumentModel comment) { 066 comment.setPropertyValue(COMMENT_PARENT_ID, parent.getId()); 067 return commentManager.createComment(parent, comment); 068 } 069 070 public void removeComment(DocumentModel comment) { 071 commentManager.deleteComment(docModel.getCoreSession(), comment.getId()); 072 } 073 074 public List<DocumentModel> getComments() { 075 return getComments(docModel); 076 } 077 078 public List<DocumentModel> getComments(DocumentModel parent) { 079 CoreSession session = docModel.getCoreSession(); 080 List<Comment> comments = commentManager.getComments(session, parent.getId()); 081 return CoreInstance.doPrivileged(session, s -> { 082 return comments.stream().map(comment -> { 083 DocumentModel commentModel = s.getDocument(new IdRef(comment.getId())); 084 commentModel.detach(true); 085 return commentModel; 086 }).collect(Collectors.toList()); 087 }); 088 } 089 090}