001/* 002 * (C) Copyright 2018 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 * Funsho David 018 * Nuno Cunha <[email protected]> 019 */ 020 021package org.nuxeo.ecm.platform.comment.impl; 022 023import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_PARENT_ID; 024 025import java.util.List; 026import java.util.stream.Collectors; 027import java.util.stream.Stream; 028 029import org.nuxeo.ecm.core.api.CoreSession; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.api.DocumentRef; 032import org.nuxeo.ecm.core.api.IdRef; 033import org.nuxeo.ecm.core.api.PartialList; 034import org.nuxeo.ecm.platform.comment.api.Comment; 035import org.nuxeo.ecm.platform.comment.api.CommentManager; 036import org.nuxeo.ecm.platform.comment.api.exceptions.CommentNotFoundException; 037import org.nuxeo.ecm.platform.comment.api.exceptions.CommentSecurityException; 038 039/** 040 * @since 10.3 041 */ 042public class BridgeCommentManager extends AbstractCommentManager { 043 044 protected final CommentManager first; 045 046 protected final CommentManager second; 047 048 public BridgeCommentManager(CommentManager first, CommentManager second) { 049 this.first = first; 050 this.second = second; 051 } 052 053 @Override 054 public List<DocumentModel> getComments(DocumentModel docModel) { 055 return Stream.concat(first.getComments(docModel).stream(), second.getComments(docModel).stream()) 056 .distinct() 057 .collect(Collectors.toList()); 058 } 059 060 @Override 061 public List<DocumentModel> getComments(CoreSession session, DocumentModel docModel) 062 throws CommentSecurityException { 063 return Stream.concat(first.getComments(session, docModel).stream(), 064 second.getComments(session, docModel).stream()).distinct().collect(Collectors.toList()); 065 } 066 067 @Override 068 public DocumentModel createComment(DocumentModel docModel, String comment) { 069 return second.createComment(docModel, comment); 070 } 071 072 @Override 073 public DocumentModel createComment(DocumentModel docModel, String comment, String author) { 074 return second.createComment(docModel, comment, author); 075 } 076 077 @Override 078 public DocumentModel createComment(DocumentModel docModel, DocumentModel comment) throws CommentSecurityException { 079 return second.createComment(docModel, comment); 080 } 081 082 @Override 083 public DocumentModel createComment(DocumentModel docModel, DocumentModel parent, DocumentModel child) { 084 return second.createComment(docModel, parent, child); 085 } 086 087 @Override 088 public void deleteComment(DocumentModel docModel, DocumentModel comment) { 089 if (comment.getPropertyValue(COMMENT_PARENT_ID) != null) { 090 second.deleteComment(docModel, comment); 091 } else { 092 first.deleteComment(docModel, comment); 093 } 094 } 095 096 @Override 097 public List<DocumentModel> getDocumentsForComment(DocumentModel comment) { 098 return Stream.concat(first.getDocumentsForComment(comment).stream(), 099 second.getDocumentsForComment(comment).stream()).distinct().collect(Collectors.toList()); 100 } 101 102 @Override 103 public DocumentModel getThreadForComment(DocumentModel comment) throws CommentSecurityException { 104 if (comment.getPropertyValue(COMMENT_PARENT_ID) != null) { 105 return second.getThreadForComment(comment); 106 } 107 return first.getThreadForComment(comment); 108 } 109 110 @Override 111 public DocumentModel createLocatedComment(DocumentModel docModel, DocumentModel comment, String path) 112 throws CommentSecurityException { 113 return second.createLocatedComment(docModel, comment, path); 114 } 115 116 @Override 117 public Comment createComment(CoreSession session, Comment comment) 118 throws CommentNotFoundException, CommentSecurityException { 119 return second.createComment(session, comment); 120 } 121 122 @Override 123 public Comment getComment(CoreSession session, String commentId) 124 throws CommentNotFoundException, CommentSecurityException { 125 return second.getComment(session, commentId); 126 } 127 128 @Override 129 public List<Comment> getComments(CoreSession session, String documentId) { 130 return Stream.concat(first.getComments(session, documentId).stream(), 131 second.getComments(session, documentId).stream()).distinct().collect(Collectors.toList()); 132 } 133 134 @Override 135 public PartialList<Comment> getComments(CoreSession session, String documentId, Long pageSize, 136 Long currentPageIndex, boolean sortAscending) throws CommentSecurityException { 137 List<Comment> firstComments = first.getComments(session, documentId, pageSize, currentPageIndex, sortAscending); 138 List<Comment> secondComments = second.getComments(session, documentId, pageSize, currentPageIndex, 139 sortAscending); 140 List<Comment> allComments = Stream.concat(firstComments.stream(), secondComments.stream()) 141 .distinct() 142 .collect(Collectors.toList()); 143 return new PartialList<>(allComments, allComments.size()); 144 } 145 146 @Override 147 public Comment updateComment(CoreSession session, String commentId, Comment comment) 148 throws CommentNotFoundException, CommentSecurityException { 149 DocumentRef commentRef = new IdRef(commentId); 150 if (!session.exists(commentRef)) { 151 throw new CommentNotFoundException("The comment " + commentId + " does not exist"); 152 } 153 if (session.getDocument(commentRef).getPropertyValue(COMMENT_PARENT_ID) != null) { 154 return second.updateComment(session, commentId, comment); 155 } else { 156 return first.updateComment(session, commentId, comment); 157 } 158 } 159 160 @Override 161 public void deleteComment(CoreSession session, String commentId) 162 throws CommentNotFoundException, CommentSecurityException { 163 DocumentRef commentRef = new IdRef(commentId); 164 if (!session.exists(commentRef)) { 165 throw new CommentNotFoundException("The comment " + commentId + " does not exist"); 166 } 167 if (session.getDocument(commentRef).getPropertyValue(COMMENT_PARENT_ID) != null) { 168 second.deleteComment(session, commentId); 169 } else { 170 first.deleteComment(session, commentId); 171 } 172 } 173 174 @Override 175 public Comment getExternalComment(CoreSession session, String entityId) 176 throws CommentNotFoundException, CommentSecurityException { 177 return second.getExternalComment(session, entityId); 178 } 179 180 @Override 181 public Comment updateExternalComment(CoreSession session, String entityId, Comment comment) 182 throws CommentNotFoundException, CommentSecurityException { 183 return second.updateExternalComment(session, entityId, comment); 184 } 185 186 @Override 187 public void deleteExternalComment(CoreSession session, String entityId) 188 throws CommentNotFoundException, CommentSecurityException { 189 second.deleteExternalComment(session, entityId); 190 } 191 192 @Override 193 public boolean hasFeature(Feature feature) { 194 switch (feature) { 195 case COMMENTS_LINKED_WITH_PROPERTY: 196 return false; 197 default: 198 throw new UnsupportedOperationException(feature.name()); 199 } 200 } 201}