001/* 002 * (C) Copyright 2007-2020 Nuxeo (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.api.CommentConstants.COMMENT_PARENT_ID_PROPERTY; 022 023import java.util.List; 024import java.util.stream.Collectors; 025 026import org.nuxeo.ecm.core.api.CoreInstance; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.IdRef; 030import org.nuxeo.ecm.platform.comment.api.Comment; 031import org.nuxeo.ecm.platform.comment.api.CommentManager; 032import org.nuxeo.ecm.platform.comment.api.CommentableDocument; 033import org.nuxeo.runtime.api.Framework; 034 035/** 036 * @author <a href="mailto:glefter@nuxeo.com">George Lefter</a> 037 */ 038public class CommentableDocumentAdapter implements CommentableDocument { 039 040 private static final long serialVersionUID = 2996381735762615450L; 041 042 protected final DocumentModel docModel; 043 044 protected final CommentManager commentManager; 045 046 public CommentableDocumentAdapter(DocumentModel docModel) { 047 this.commentManager = Framework.getService(CommentManager.class); 048 this.docModel = docModel; 049 } 050 051 @Override 052 public DocumentModel addComment(DocumentModel comment) { 053 return addComment(docModel, comment); 054 } 055 056 @Override 057 public DocumentModel addComment(DocumentModel comment, String path) { 058 comment.setPropertyValue(COMMENT_PARENT_ID_PROPERTY, docModel.getId()); 059 return commentManager.createLocatedComment(docModel, comment, path); 060 } 061 062 @Override 063 public DocumentModel addComment(DocumentModel parent, DocumentModel comment) { 064 comment.setPropertyValue(COMMENT_PARENT_ID_PROPERTY, parent.getId()); 065 return commentManager.createComment(parent, comment); 066 } 067 068 @Override 069 public void removeComment(DocumentModel comment) { 070 commentManager.deleteComment(docModel.getCoreSession(), comment.getId()); 071 } 072 073 @Override 074 public List<DocumentModel> getComments() { 075 return getComments(docModel); 076 } 077 078 @Override 079 public List<DocumentModel> getComments(DocumentModel parent) { 080 CoreSession session = docModel.getCoreSession(); 081 List<Comment> comments = commentManager.getComments(session, parent.getId()); 082 return CoreInstance.doPrivileged(session, s -> { 083 return comments.stream().map(comment -> { 084 DocumentModel commentModel = s.getDocument(new IdRef(comment.getId())); 085 commentModel.detach(true); 086 return commentModel; 087 }).collect(Collectors.toList()); 088 }); 089 } 090 091}