001/* 002 * (C) Copyright 2012 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 * Thierry Delprat 018 */ 019package org.nuxeo.snapshot; 020 021import java.util.Collections; 022import java.util.List; 023 024import org.nuxeo.ecm.core.api.CoreSession; 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.IterableQueryResult; 027import org.nuxeo.ecm.core.event.impl.ShallowDocumentModel; 028import org.nuxeo.ecm.core.query.QueryFilter; 029import org.nuxeo.ecm.core.query.sql.NXQL; 030import org.nuxeo.ecm.core.versioning.OrphanVersionRemovalFilter; 031 032public class SnapshotRemovalPolicy implements OrphanVersionRemovalFilter { 033 034 protected boolean canRemoveVersions(CoreSession session, DocumentModel doc, List<String> uuids) { 035 IterableQueryResult result = null; 036 try { 037 StringBuffer nxql = new StringBuffer("select ecm:uuid from Document where "); 038 nxql.append(SnapshotableAdapter.CHILDREN_PROP + "/* IN ("); 039 for (int i = 0; i < uuids.size(); i++) { 040 if (i > 0) { 041 nxql.append(","); 042 } 043 nxql.append("'" + uuids.get(i) + "'"); 044 } 045 nxql.append(")"); 046 result = session.queryAndFetch(nxql.toString(), NXQL.NXQL, QueryFilter.EMPTY); 047 if (result.iterator().hasNext()) { 048 return false; 049 } 050 return true; 051 } finally { 052 if (result != null) { 053 result.close(); 054 } 055 } 056 057 } 058 059 @SuppressWarnings("unchecked") 060 @Override 061 public List<String> getRemovableVersionIds(CoreSession session, ShallowDocumentModel deletedLiveDoc, 062 List<String> versionUUIDs) { 063 064 if (!canRemoveVersions(session, deletedLiveDoc, versionUUIDs)) { 065 return Collections.emptyList(); 066 } 067 return versionUUIDs; 068 } 069}