1 13 package info.magnolia.cms.core.version; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.util.FactoryUtil; 17 import info.magnolia.cms.beans.config.VersionConfig; 18 19 import javax.jcr.RepositoryException; 20 import javax.jcr.version.VersionHistory; 21 import javax.jcr.version.VersionIterator; 22 23 import org.slf4j.Logger; 24 import org.slf4j.LoggerFactory; 25 26 27 33 public final class VersionManager extends BaseVersionManager { 34 35 38 private static Logger log = LoggerFactory.getLogger(VersionManager.class); 39 40 43 public VersionManager() { 44 try { 45 this.createInitialStructure(); 46 } 47 catch (RepositoryException re) { 48 log.error("Failed to initialize VersionManager"); 49 log.error(re.getMessage(), re); 50 } 51 } 52 53 56 public static VersionManager getInstance() { 57 return (VersionManager) FactoryUtil.getSingleton(VersionManager.class); 58 } 59 60 63 public boolean isInvalidMaxVersions() { 64 return VersionConfig.getInstance().getMaxVersionAllowed() < 1; 65 } 66 67 72 public void setMaxVersionHistory(Content node) throws RepositoryException { 73 VersionHistory history = node.getJCRNode().getVersionHistory(); 74 VersionIterator versions = history.getAllVersions(); 75 long indexToRemove = (versions.getSize() - 2) - VersionConfig.getInstance().getMaxVersionAllowed(); 77 if (indexToRemove > 0) { 78 versions.nextVersion(); 80 for (; indexToRemove > 0; indexToRemove--) { 82 history.removeVersion(versions.nextVersion().getName()); 83 } 84 } 85 } 86 87 } 88 | Popular Tags |