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.api.DocumentNotFoundException; 033import org.nuxeo.ecm.core.event.PostCommitEventListener; 034import org.nuxeo.ecm.platform.comment.api.CommentManager; 035import org.nuxeo.ecm.platform.comment.service.CommentServiceConfig; 036import org.nuxeo.ecm.platform.relations.api.Graph; 037import org.nuxeo.ecm.platform.relations.api.QNameResource; 038import org.nuxeo.ecm.platform.relations.api.RelationManager; 039import org.nuxeo.ecm.platform.relations.api.Resource; 040import org.nuxeo.ecm.platform.relations.api.Statement; 041import org.nuxeo.runtime.api.Framework; 042 043public class DocumentRemovedCommentEventListener extends AbstractCommentListener implements PostCommitEventListener { 044 045 private static final Log log = LogFactory.getLog(DocumentRemovedCommentEventListener.class); 046 047 @Override 048 protected void doProcess(CoreSession coreSession, RelationManager relationManager, CommentServiceConfig config, 049 DocumentModel docMessage) { 050 log.debug("Processing relations cleanup on Document removal"); 051 CommentManager commentManager = Framework.getService(CommentManager.class); 052 if (commentManager.hasFeature(COMMENTS_LINKED_WITH_PROPERTY)) { 053 deleteCommentChildren(coreSession, commentManager, docMessage); 054 coreSession.save(); 055 } else { 056 onDocumentRemoved(coreSession, relationManager, config, docMessage); 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}