001/* 002 * (C) Copyright 2006-2010 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 */ 021package org.nuxeo.ecm.platform.relations.core.listener; 022 023import java.util.List; 024 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.event.Event; 027import org.nuxeo.ecm.core.event.EventContext; 028import org.nuxeo.ecm.core.event.EventListener; 029import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 030import org.nuxeo.ecm.platform.relations.api.Graph; 031import org.nuxeo.ecm.platform.relations.api.RelationManager; 032import org.nuxeo.ecm.platform.relations.api.Resource; 033import org.nuxeo.ecm.platform.relations.api.Statement; 034import org.nuxeo.ecm.platform.relations.api.util.RelationConstants; 035import org.nuxeo.runtime.api.Framework; 036 037/** 038 * Core Event listener that cleans relation on deleted documents; it should be executed after PublishRelationsListener 039 * so as to be able to copy relations from the deleted proxies. 040 * 041 * @author mcedica 042 */ 043public class DeleteRelationsListener implements EventListener { 044 045 private RelationManager relationManager; 046 047 @Override 048 public void handleEvent(Event event) { 049 EventContext ctx = event.getContext(); 050 if (ctx instanceof DocumentEventContext) { 051 DocumentEventContext docEventContext = (DocumentEventContext) ctx; 052 DocumentModel doc = docEventContext.getSourceDocument(); 053 relationManager = getRelationManager(); 054 055 // create resource from the document being deleted 056 Resource sourceResource = relationManager.getResource(RelationConstants.DOCUMENT_NAMESPACE, doc, null); 057 058 // remove all the relations from the default graf in which this 059 // document is an object in the statement 060 Graph graph = relationManager.getGraphByName(RelationConstants.GRAPH_NAME); 061 List<Statement> statementList = graph.getStatements(null, null, sourceResource); 062 graph.remove(statementList); 063 064 // remove all the relations in which this document is a subject in 065 // the statement 066 statementList = graph.getStatements(sourceResource, null, null); 067 graph.remove(statementList); 068 } 069 } 070 071 public RelationManager getRelationManager() { 072 if (relationManager == null) { 073 relationManager = Framework.getService(RelationManager.class); 074 } 075 return relationManager; 076 } 077 078}