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 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.service.CommentServiceConfig; 033import org.nuxeo.ecm.platform.relations.api.Graph; 034import org.nuxeo.ecm.platform.relations.api.QNameResource; 035import org.nuxeo.ecm.platform.relations.api.RelationManager; 036import org.nuxeo.ecm.platform.relations.api.Resource; 037import org.nuxeo.ecm.platform.relations.api.Statement; 038 039public class DocumentRemovedCommentEventListener extends AbstractCommentListener implements PostCommitEventListener { 040 041 private static final Log log = LogFactory.getLog(DocumentRemovedCommentEventListener.class); 042 043 @Override 044 protected void doProcess(CoreSession coreSession, RelationManager relationManager, CommentServiceConfig config, 045 DocumentModel docMessage) { 046 log.debug("Processing relations cleanup on Document removal"); 047 onDocumentRemoved(coreSession, relationManager, config, docMessage); 048 } 049 050 private static void onDocumentRemoved(CoreSession coreSession, RelationManager relationManager, 051 CommentServiceConfig config, DocumentModel docMessage) { 052 053 Resource documentRes = relationManager.getResource(config.documentNamespace, docMessage, null); 054 if (documentRes == null) { 055 log.error("Could not adapt document model to relation resource ; " 056 + "check the service relation adapters configuration"); 057 return; 058 } 059 Graph graph = relationManager.getGraph(config.graphName, coreSession); 060 List<Statement> statementList = graph.getStatements(null, null, documentRes); 061 062 // remove comments 063 for (Statement stmt : statementList) { 064 QNameResource resource = (QNameResource) stmt.getSubject(); 065 String commentId = resource.getLocalName(); 066 DocumentModel docModel = (DocumentModel) relationManager.getResourceRepresentation(config.commentNamespace, 067 resource, null); 068 069 if (docModel != null) { 070 try { 071 coreSession.removeDocument(docModel.getRef()); 072 log.debug("comment removal succeded for id: " + commentId); 073 } catch (DocumentNotFoundException e) { 074 log.error("comment removal failed", e); 075 } 076 } else { 077 log.warn("comment not found: id=" + commentId); 078 } 079 } 080 coreSession.save(); 081 // remove relations 082 graph.remove(statementList); 083 } 084 085}