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 public void handleEvent(Event event) { 048 EventContext ctx = event.getContext(); 049 if (ctx instanceof DocumentEventContext) { 050 DocumentEventContext docEventContext = (DocumentEventContext) ctx; 051 DocumentModel doc = docEventContext.getSourceDocument(); 052 relationManager = getRelationManager(); 053 054 // create resource from the document being deleted 055 Resource sourceResource = relationManager.getResource(RelationConstants.DOCUMENT_NAMESPACE, doc, null); 056 057 // remove all the relations from the default graf in which this 058 // document is an object in the statement 059 Graph graph = relationManager.getGraphByName(RelationConstants.GRAPH_NAME); 060 List<Statement> statementList = graph.getStatements(null, null, sourceResource); 061 graph.remove(statementList); 062 063 // remove all the relations in which this document is a subject in 064 // the statement 065 statementList = graph.getStatements(sourceResource, null, null); 066 graph.remove(statementList); 067 } 068 } 069 070 public RelationManager getRelationManager() { 071 if (relationManager == null) { 072 relationManager = Framework.getService(RelationManager.class); 073 } 074 return relationManager; 075 } 076 077}