001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.cms.impl.jcr.util;
023:
024: import org.jboss.portal.cms.CMSException;
025:
026: import javax.jcr.Node;
027: import javax.jcr.NodeIterator;
028: import javax.jcr.Property;
029: import javax.jcr.PropertyIterator;
030: import javax.jcr.RepositoryException;
031: import javax.jcr.version.Version;
032: import javax.jcr.version.VersionHistory;
033: import javax.jcr.version.VersionIterator;
034: import java.util.Iterator;
035:
036: /**
037: * Utility class for Versioning
038: *
039: * @author <a href="mailto:roy@jboss.org">Roy Russo</a>
040: * @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
041: */
042: public class VersionUtil {
043: /**
044: * Creates a new node, with a new node label and checks it in.
045: *
046: * @param node
047: * @throws CMSException
048: */
049: public static void createVersion(Node node, boolean isLive)
050: throws CMSException {
051: if (NodeTypeUtil.isFolder(node)) {
052: createFolderVersion(node, isLive);
053: } else if (NodeTypeUtil.isFile(node)) {
054: createFileVersion(node, isLive);
055: } else if (NodeTypeUtil.isContent(node)) {
056: createContentVersion(node, isLive);
057: }
058:
059: }
060:
061: public static void createContentVersion(Node node, boolean isLive)
062: throws CMSException {
063: // Check that the node is of type: content
064: if (!NodeTypeUtil.isContent(node)) {
065: throw new CMSException("The node is not a \"content\"");
066: }
067:
068: // Mark the content as "Live"
069: try {
070: if (isLive) {
071: VersionHistory versionHistory = node
072: .getVersionHistory();
073: versionHistory.addVersionLabel(
074: node.checkin().getName(), "LIVE", true);
075: } else {
076: node.checkin();
077: }
078: } catch (RepositoryException e) {
079: throw new CMSException(
080: "Cannot mark the content as \"Live\"");
081: }
082: }
083:
084: public static void createFileVersion(Node node, boolean isLive)
085: throws CMSException {
086: // Check that the node is of type: file
087: if (!NodeTypeUtil.isFile(node)) {
088: throw new CMSException("The node is not a \"file\"");
089: }
090:
091: try {
092: Iterator it = node.getNodes();
093: while (it.hasNext()) {
094: Node tmpNode = (Node) it.next();
095: if (NodeTypeUtil.isContent(tmpNode)) {
096: createContentVersion(tmpNode, isLive);
097: }
098: }
099: } catch (RepositoryException e) {
100: throw new CMSException("Cannot mark the file as \"Live\"");
101: }
102:
103: }
104:
105: public static void createFolderVersion(Node node, boolean isLive)
106: throws CMSException {
107: // Check that the node is of type: folder
108: if (!NodeTypeUtil.isFolder(node)) {
109: throw new CMSException("The node is not a \"folder\"");
110: }
111:
112: try {
113: Iterator it = node.getNodes();
114: while (it.hasNext()) {
115: Node tmpNode = (Node) it.next();
116: if (NodeTypeUtil.isFolder(tmpNode)) {
117: createFolderVersion(tmpNode, isLive);
118: } else {
119: createFileVersion(tmpNode, isLive);
120: }
121: }
122: } catch (RepositoryException e) {
123: throw new CMSException(
124: "Cannot mark the directory as \"Live\"");
125: }
126: }
127:
128: /**
129: * Prints to std out, version node and properties information.
130: *
131: * @param node
132: */
133: public static void print(Node node) {
134: try {
135: VersionHistory vh = node.getVersionHistory();
136: VersionIterator vi = vh.getAllVersions();
137: while (vi.hasNext()) {
138: Version v = vi.nextVersion();
139: NodeIterator ni = v.getNodes();
140: while (ni.hasNext()) {
141: Node n = ni.nextNode();
142: System.out.println("################### VERSION: "
143: + v.getName() + " ###################");
144: System.out.println("Version Path: " + v.getPath());
145: System.out.println("Node Path: " + n.getPath());
146:
147: PropertyIterator references = v.getReferences();
148: while (references.hasNext()) {
149: Property ref = references.nextProperty();
150: System.out.println("REF: " + ref.getName());
151: }
152:
153: PropertyIterator pi2 = n.getProperties();
154: while (pi2.hasNext()) {
155: Property p2 = pi2.nextProperty();
156: if (!p2.getName()
157: .equals("jcr:frozenMixinTypes")
158: && !p2.getName().equals(
159: "jcr:predecessors")) {
160: System.out.println(p2.getName() + " = "
161: + p2.getString());
162: }
163: }
164: NodeIterator ni2 = n.getNodes(); // content
165: while (ni2.hasNext()) {
166: Node n2 = ni2.nextNode();
167: System.out.println("$$$$$$$$$$ " + n2.getName()
168: + "$$$$$$$$$$");
169: System.out.println("$$$$$$$$$$" + n2.getPath()
170: + "$$$$$$$$$$");
171: PropertyIterator references2 = n2
172: .getReferences();
173: while (references2.hasNext()) {
174: Property ref2 = references2.nextProperty();
175: System.out
176: .println("REF: " + ref2.getName());
177: }
178:
179: PropertyIterator pi3 = n2.getProperties();
180: while (pi3.hasNext()) {
181: Property p3 = pi3.nextProperty();
182: System.out.println(p3.getName() + " = "
183: + p3.getString());
184: }
185: }
186: }
187: System.out.println("\n\n");
188: }
189: } catch (Exception e) {
190: e.printStackTrace();
191: }
192: }
193:
194: /**
195: * @param history
196: * @return
197: */
198: public static Version getLiveVersion(VersionHistory history) {
199: Version liveVersion = null;
200:
201: try {
202: liveVersion = history.getVersionByLabel("LIVE");
203: } catch (Exception e) {
204: liveVersion = null;
205: }
206:
207: return liveVersion;
208: }
209: }
|