001/* 002 * (C) Copyright 2006-2009 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 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.comment.listener; 023 024import static org.nuxeo.ecm.platform.comment.api.CommentManager.Feature.COMMENTS_LINKED_WITH_PROPERTY; 025 026import java.util.List; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.event.EventListener; 033import org.nuxeo.ecm.platform.comment.api.CommentManager; 034import org.nuxeo.ecm.platform.comment.service.CommentServiceConfig; 035import org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants; 036import org.nuxeo.ecm.platform.relations.api.Graph; 037import org.nuxeo.ecm.platform.relations.api.RelationManager; 038import org.nuxeo.ecm.platform.relations.api.Resource; 039import org.nuxeo.ecm.platform.relations.api.Statement; 040import org.nuxeo.runtime.api.Framework; 041 042public class CommentRemovedEventListener extends AbstractCommentListener implements EventListener { 043 044 private static final Log log = LogFactory.getLog(CommentRemovedEventListener.class); 045 046 @Override 047 protected void doProcess(CoreSession coreSession, RelationManager relationManager, CommentServiceConfig config, 048 DocumentModel docMessage) { 049 log.debug("Processing relations cleanup on Comment removal"); 050 String typeName = docMessage.getType(); 051 if (CommentsConstants.COMMENT_DOC_TYPE.equals(typeName) || "Post".equals(typeName)) { 052 CommentManager commentManager = Framework.getService(CommentManager.class); 053 if (commentManager.hasFeature(COMMENTS_LINKED_WITH_PROPERTY)) { 054 deleteCommentChildren(coreSession, commentManager, docMessage); 055 coreSession.save(); 056 } else { 057 onCommentRemoved(relationManager, config, docMessage); 058 } 059 } 060 } 061 062 private static void onCommentRemoved(RelationManager relationManager, CommentServiceConfig config, 063 DocumentModel docModel) { 064 Resource commentRes = relationManager.getResource(config.commentNamespace, docModel, null); 065 if (commentRes == null) { 066 log.warn("Could not adapt document model to relation resource; " 067 + "check the service relation adapters configuration"); 068 return; 069 } 070 Graph graph = relationManager.getGraph(config.graphName, docModel.getCoreSession()); 071 List<Statement> statementList = graph.getStatements(commentRes, null, null); 072 graph.remove(statementList); 073 } 074}