001/* 002 * (C) Copyright 2006-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 */ 019 020package org.nuxeo.ecm.platform.comment.listener; 021 022import static org.nuxeo.ecm.platform.comment.api.CommentManager.Feature.COMMENTS_LINKED_WITH_PROPERTY; 023 024import java.util.List; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.core.api.CoreSession; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.DocumentNotFoundException; 031import org.nuxeo.ecm.core.event.PostCommitEventListener; 032import org.nuxeo.ecm.platform.comment.api.CommentManager; 033import org.nuxeo.ecm.platform.comment.service.CommentServiceConfig; 034import org.nuxeo.ecm.platform.relations.api.Graph; 035import org.nuxeo.ecm.platform.relations.api.QNameResource; 036import org.nuxeo.ecm.platform.relations.api.RelationManager; 037import org.nuxeo.ecm.platform.relations.api.Resource; 038import org.nuxeo.ecm.platform.relations.api.Statement; 039import org.nuxeo.runtime.api.Framework; 040 041public class DocumentRemovedCommentEventListener extends AbstractCommentListener implements PostCommitEventListener { 042 043 private static final Log log = LogFactory.getLog(DocumentRemovedCommentEventListener.class); 044 045 @Override 046 protected void doProcess(CoreSession coreSession, RelationManager relationManager, CommentServiceConfig config, 047 DocumentModel docMessage) { 048 log.debug("Processing relations cleanup on Document removal"); 049 CommentManager commentManager = Framework.getService(CommentManager.class); 050 if (commentManager.hasFeature(COMMENTS_LINKED_WITH_PROPERTY)) { 051 deleteCommentChildren(coreSession, commentManager, docMessage); 052 coreSession.save(); 053 } else if (relationManager != null) { 054 onDocumentRemoved(coreSession, relationManager, config, docMessage); 055 } else { 056 log.info("Migration in progress or relation manager is missing"); 057 } 058 } 059 060 private static void onDocumentRemoved(CoreSession coreSession, RelationManager relationManager, 061 CommentServiceConfig config, DocumentModel docMessage) { 062 063 Resource documentRes = relationManager.getResource(config.documentNamespace, docMessage, null); 064 if (documentRes == null) { 065 log.error("Could not adapt document model to relation resource ; " 066 + "check the service relation adapters configuration"); 067 return; 068 } 069 Graph graph = relationManager.getGraph(config.graphName, coreSession); 070 List<Statement> statementList = graph.getStatements(null, null, documentRes); 071 072 // remove comments 073 for (Statement stmt : statementList) { 074 QNameResource resource = (QNameResource) stmt.getSubject(); 075 String commentId = resource.getLocalName(); 076 DocumentModel docModel = (DocumentModel) relationManager.getResourceRepresentation(config.commentNamespace, 077 resource, null); 078 079 if (docModel != null) { 080 try { 081 coreSession.removeDocument(docModel.getRef()); 082 log.debug("comment removal succeded for id: " + commentId); 083 } catch (DocumentNotFoundException e) { 084 log.error("comment removal failed", e); 085 } 086 } else { 087 log.warn("comment not found: id=" + commentId); 088 } 089 } 090 coreSession.save(); 091 // remove relations 092 graph.remove(statementList); 093 } 094 095}