001/* 002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 */ 019package org.nuxeo.ecm.platform.relations.core.listener; 020 021import java.util.List; 022 023import org.nuxeo.ecm.core.api.DocumentModel; 024import org.nuxeo.ecm.core.event.Event; 025import org.nuxeo.ecm.core.event.EventContext; 026import org.nuxeo.ecm.core.event.EventListener; 027import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 028import org.nuxeo.ecm.platform.relations.api.Graph; 029import org.nuxeo.ecm.platform.relations.api.RelationManager; 030import org.nuxeo.ecm.platform.relations.api.Resource; 031import org.nuxeo.ecm.platform.relations.api.Statement; 032import org.nuxeo.ecm.platform.relations.api.util.RelationConstants; 033import org.nuxeo.runtime.api.Framework; 034 035/** 036 * Core Event listener that cleans relation on deleted documents; it should be executed after PublishRelationsListener 037 * so as to be able to copy relations from the deleted proxies. 038 * 039 * @author mcedica 040 */ 041public class DeleteRelationsListener implements EventListener { 042 043 private RelationManager relationManager; 044 045 public void handleEvent(Event event) { 046 EventContext ctx = event.getContext(); 047 if (ctx instanceof DocumentEventContext) { 048 DocumentEventContext docEventContext = (DocumentEventContext) ctx; 049 DocumentModel doc = docEventContext.getSourceDocument(); 050 relationManager = getRelationManager(); 051 052 // create resource from the document being deleted 053 Resource sourceResource = relationManager.getResource(RelationConstants.DOCUMENT_NAMESPACE, doc, null); 054 055 // remove all the relations from the default graf in which this 056 // document is an object in the statement 057 Graph graph = relationManager.getGraphByName(RelationConstants.GRAPH_NAME); 058 List<Statement> statementList = graph.getStatements(null, null, sourceResource); 059 graph.remove(statementList); 060 061 // remove all the relations in which this document is a subject in 062 // the statement 063 statementList = graph.getStatements(sourceResource, null, null); 064 graph.remove(statementList); 065 } 066 } 067 068 public RelationManager getRelationManager() { 069 if (relationManager == null) { 070 relationManager = Framework.getService(RelationManager.class); 071 } 072 return relationManager; 073 } 074 075}