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 StringBuilder nxql = new StringBuilder().append("select ecm:uuid from Document where ") 038 .append(SnapshotableAdapter.CHILDREN_PROP) 039 .append("/* IN ("); 040 for (int i = 0; i < uuids.size(); i++) { 041 if (i > 0) { 042 nxql.append(","); 043 } 044 nxql.append("'").append(uuids.get(i)).append("'"); 045 } 046 nxql.append(")"); 047 result = session.queryAndFetch(nxql.toString(), NXQL.NXQL, QueryFilter.EMPTY); 048 if (result.iterator().hasNext()) { 049 return false; 050 } 051 return true; 052 } finally { 053 if (result != null) { 054 result.close(); 055 } 056 } 057 058 } 059 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}