001/* 002 * (C) Copyright 2013 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 * Arnaud Kervern <akervern@nuxeo.com> 016 */ 017 018package org.nuxeo.ecm.user.registration; 019 020import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_REMOVED; 021import static org.nuxeo.ecm.user.registration.DocumentRegistrationInfo.DOCUMENT_ID_FIELD; 022 023import org.nuxeo.ecm.core.api.DocumentModel; 024import org.nuxeo.ecm.core.api.DocumentModelList; 025import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner; 026import org.nuxeo.ecm.core.event.Event; 027import org.nuxeo.ecm.core.event.EventListener; 028import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 029 030public class RegistrationCleanerListener implements EventListener { 031 032 @Override 033 public void handleEvent(Event event) { 034 if (!event.getName().equals(DOCUMENT_REMOVED) || !(event.getContext() instanceof DocumentEventContext)) { 035 return; 036 } 037 038 DocumentEventContext context = (DocumentEventContext) event.getContext(); 039 final DocumentModel sourceDocument = context.getSourceDocument(); 040 041 if (sourceDocument.getType().equals("UserRegistration") || sourceDocument.isVersion()) { 042 return; 043 } 044 045 new UnrestrictedSessionRunner(context.getCoreSession()) { 046 @Override 047 public void run() { 048 DocumentModelList docs = session.query(String.format( 049 "Select * from Document where ecm:mixinType = 'UserRegistration' and %s = '%s'", 050 DOCUMENT_ID_FIELD, sourceDocument.getId())); 051 for (DocumentModel doc : docs) { 052 session.removeDocument(doc.getRef()); 053 } 054 } 055 }.runUnrestricted(); 056 } 057}