KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > core > version > VersionManager


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

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 /**
28  * Singleton class which should be used for any operation related to versioning VersionManager synchronizes all
29  * operations like add version, restore version and remove version but it does not synchronize between operations
30  * @author Sameer Charles
31  * $Id:VersionManager.java 6430 2006-09-20 11:25:35Z scharles $
32  */

33 public final class VersionManager extends BaseVersionManager {
34
35     /**
36      * Logger.
37      */

38     private static Logger log = LoggerFactory.getLogger(VersionManager.class);
39
40     /**
41      * do not instanciate
42      */

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     /**
54      * get instance
55      */

56     public static VersionManager getInstance() {
57         return (VersionManager) FactoryUtil.getSingleton(VersionManager.class);
58     }
59
60     /**
61      * since version is set "only revert" always return true
62      * */

63     public boolean isInvalidMaxVersions() {
64         return VersionConfig.getInstance().getMaxVersionAllowed() < 1;
65     }
66
67     /**
68      * set version history to max version possible
69      * @param node
70      * @throws RepositoryException if failed to get VersionHistory or fail to remove
71      */

72     public void setMaxVersionHistory(Content node) throws RepositoryException {
73         VersionHistory history = node.getJCRNode().getVersionHistory();
74         VersionIterator versions = history.getAllVersions();
75         // size - 2 to skip root version
76
long indexToRemove = (versions.getSize() - 2) - VersionConfig.getInstance().getMaxVersionAllowed();
77         if (indexToRemove > 0) {
78             // skip root version
79
versions.nextVersion();
80             // remove the version after rootVersion
81
for (; indexToRemove > 0; indexToRemove--) {
82                 history.removeVersion(versions.nextVersion().getName());
83             }
84         }
85     }
86
87 }
88
Popular Tags