001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Florent Guillaume 011 */ 012 013package org.nuxeo.ecm.core.versioning; 014 015import java.util.Collection; 016import java.util.List; 017 018import org.apache.commons.logging.Log; 019import org.apache.commons.logging.LogFactory; 020import org.nuxeo.ecm.core.api.CoreSession; 021import org.nuxeo.ecm.core.api.DocumentModel; 022import org.nuxeo.ecm.core.api.DocumentNotFoundException; 023import org.nuxeo.ecm.core.api.IdRef; 024import org.nuxeo.ecm.core.event.Event; 025import org.nuxeo.ecm.core.event.EventContext; 026import org.nuxeo.ecm.core.event.EventService; 027import org.nuxeo.ecm.core.event.impl.EventContextImpl; 028import org.nuxeo.ecm.core.event.impl.ShallowDocumentModel; 029import org.nuxeo.ecm.core.model.Document; 030import org.nuxeo.ecm.core.model.Session; 031import org.nuxeo.runtime.api.Framework; 032 033/** 034 * Removes the version history if no proxies exist, otherwise do nothing. 035 * 036 * @author Florent Guillaume 037 */ 038public class DefaultVersionRemovalPolicy implements VersionRemovalPolicy { 039 040 private static final Log log = LogFactory.getLog(DefaultVersionRemovalPolicy.class); 041 042 public static final String ORPHAN_VERSION_REMOVE = "orphan_versions_to_remove"; 043 044 @Override 045 public void removeVersions(Session session, Document doc, CoreSession coreSession) { 046 Collection<Document> proxies = session.getProxies(doc, null); 047 if (doc.isProxy()) { 048 // if doc is itself a proxy it should not be considered 049 // in the list of remaining proxies 050 proxies.remove(doc); 051 if (proxies.isEmpty()) { 052 // removal of last proxy 053 Document source = doc.getSourceDocument(); 054 if (source.isVersion()) { 055 // get live doc from version 056 try { 057 source = source.getSourceDocument(); 058 } catch (DocumentNotFoundException e) { 059 // live already removed 060 source = null; 061 } 062 } // else was a live proxy 063 // if a live doc remains, still don't remove versions 064 if (source != null) { 065 return; 066 } 067 } 068 } 069 if (proxies.isEmpty()) { 070 List<String> versionsIds = doc.getVersionsIds(); 071 if (log.isDebugEnabled()) { 072 log.debug(String.format("Removing %s versions for: %s", versionsIds.size(), doc.getUUID())); 073 } 074 075 if (versionsIds.size() > 0) { 076 DocumentModel docModel = coreSession.getDocument(new IdRef(doc.getUUID())); 077 EventContext evtctx = new EventContextImpl(coreSession, coreSession.getPrincipal(), 078 new ShallowDocumentModel(docModel), versionsIds); 079 Event evt = evtctx.newEvent(ORPHAN_VERSION_REMOVE); 080 Framework.getLocalService(EventService.class).fireEvent(evt); 081 } 082 } 083 } 084}