001/* 002 * (C) Copyright 2006-2011 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 * Florent Guillaume 018 */ 019 020package org.nuxeo.ecm.core.versioning; 021 022import java.util.Collection; 023import java.util.List; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.DocumentNotFoundException; 030import org.nuxeo.ecm.core.api.IdRef; 031import org.nuxeo.ecm.core.event.Event; 032import org.nuxeo.ecm.core.event.EventContext; 033import org.nuxeo.ecm.core.event.EventService; 034import org.nuxeo.ecm.core.event.impl.EventContextImpl; 035import org.nuxeo.ecm.core.event.impl.ShallowDocumentModel; 036import org.nuxeo.ecm.core.model.Document; 037import org.nuxeo.ecm.core.model.Session; 038import org.nuxeo.runtime.api.Framework; 039 040/** 041 * Removes the version history if no proxies exist, otherwise do nothing. 042 * 043 * @author Florent Guillaume 044 */ 045public class DefaultVersionRemovalPolicy implements VersionRemovalPolicy { 046 047 private static final Log log = LogFactory.getLog(DefaultVersionRemovalPolicy.class); 048 049 public static final String ORPHAN_VERSION_REMOVE = "orphan_versions_to_remove"; 050 051 @Override 052 public void removeVersions(Session session, Document doc, CoreSession coreSession) { 053 Collection<Document> proxies = session.getProxies(doc, null); 054 if (doc.isProxy()) { 055 // if doc is itself a proxy it should not be considered 056 // in the list of remaining proxies 057 proxies.remove(doc); 058 if (proxies.isEmpty()) { 059 // removal of last proxy 060 Document source = doc.getSourceDocument(); 061 if (source.isVersion()) { 062 // get live doc from version 063 try { 064 source = source.getSourceDocument(); 065 } catch (DocumentNotFoundException e) { 066 // live already removed 067 source = null; 068 } 069 } // else was a live proxy 070 // if a live doc remains, still don't remove versions 071 if (source != null) { 072 return; 073 } 074 } 075 } 076 if (proxies.isEmpty()) { 077 List<String> versionsIds = doc.getVersionsIds(); 078 if (log.isDebugEnabled()) { 079 log.debug(String.format("Removing %s versions for: %s", versionsIds.size(), doc.getUUID())); 080 } 081 082 if (versionsIds.size() > 0) { 083 DocumentModel docModel = coreSession.getDocument(new IdRef(doc.getUUID())); 084 EventContext evtctx = new EventContextImpl(coreSession, coreSession.getPrincipal(), 085 new ShallowDocumentModel(docModel), versionsIds); 086 Event evt = evtctx.newEvent(ORPHAN_VERSION_REMOVE); 087 Framework.getService(EventService.class).fireEvent(evt); 088 } 089 } 090 } 091}