View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.commands.impl;
35  
36  import info.magnolia.cms.core.version.VersionManager;
37  import info.magnolia.cms.util.ExclusiveWrite;
38  import info.magnolia.context.Context;
39  import info.magnolia.jcr.util.NodeTypes;
40  import info.magnolia.jcr.util.PropertyUtil;
41  
42  import java.util.ArrayList;
43  import java.util.HashMap;
44  import java.util.List;
45  import java.util.Map;
46  
47  import javax.jcr.Node;
48  import javax.jcr.NodeIterator;
49  import javax.jcr.RepositoryException;
50  import javax.jcr.version.Version;
51  
52  import org.apache.commons.lang.StringUtils;
53  
54  /**
55   * Creates a version for the passed path in the website repository.
56   */
57  public class VersionCommand extends RuleBasedCommand {
58  
59      private boolean recursive;
60  
61      private String comment;
62  
63      /**
64       * @see info.magnolia.commands.MgnlCommand#execute(info.magnolia.context.Context)
65       */
66      @Override
67      public boolean execute(Context ctx) throws Exception {
68          final Node node = getJCRNode(ctx);
69          if (isRecursive()) {
70              // set versionMap and version name for this node
71              List versionMap = new ArrayList();
72              versionRecursively(node, ctx, versionMap);
73              ctx.setAttribute(Context.ATTRIBUTE_VERSION_MAP, versionMap, Context.LOCAL_SCOPE);
74          } else {
75              addComment(node);
76              Version version = VersionManager.getInstance().addVersion(node, getRule());
77              ctx.setAttribute(Context.ATTRIBUTE_VERSION, version.getName(), Context.LOCAL_SCOPE);
78              cleanComment(node);
79          }
80  
81          return true;
82      }
83  
84      protected void addComment(final Node node) throws RepositoryException {
85          synchronized (ExclusiveWrite.getInstance()) {
86              PropertyUtil.setProperty(node, NodeTypes.Versionable.COMMENT, getComment() != null ? getComment() : "");
87              node.getSession().save();
88          }
89      }
90  
91      protected void cleanComment(final Node node) throws RepositoryException {
92          synchronized (ExclusiveWrite.getInstance()) {
93              if (node.hasProperty(NodeTypes.Versionable.COMMENT)
94                      && !StringUtils.EMPTY.equals(node.getProperty(NodeTypes.Versionable.COMMENT).getString())) {
95                  node.getProperty(NodeTypes.Versionable.COMMENT).remove();
96                  node.getSession().save();
97              }
98          }
99      }
100 
101     /**
102      * Recursive version the descendant nodes in the tree. Uses the reversed {@link #rule} to filter the nodes meant to be versioned.
103      * {@link info.magnolia.cms.util.Rule#isAllowed} is used to composite the child nodes into one version - node types defined in the
104      * {@link info.magnolia.cms.util.Rule#allowedTypes} will not be versioned.
105      *
106      * @param versionMap keeps tack of the versioned nodes and gets passed to the context.
107      */
108     private void versionRecursively(Node node, Context ctx, List versionMap) throws RepositoryException {
109         addComment(node);
110         Version version = VersionManager.getInstance().addVersion(node, getRule());
111         Map entry = new HashMap();
112         entry.put("version", version.getName());
113         entry.put("uuid", node.getIdentifier());
114         versionMap.add(entry);
115         if (StringUtils.isEmpty((String) ctx.getAttribute(Context.ATTRIBUTE_VERSION))) {
116             ctx.setAttribute(Context.ATTRIBUTE_VERSION, version.getName(), Context.LOCAL_SCOPE);
117         }
118 
119         cleanComment(node);
120 
121         NodeIterator children = node.getNodes();
122         while (children.hasNext()) {
123             Node child = children.nextNode();
124             if (!getRule().isAllowed(child)) {
125                 versionRecursively(child, ctx, versionMap);
126             }
127         }
128     }
129 
130     /**
131      * @return is recursive versioning
132      */
133     public boolean isRecursive() {
134         return recursive;
135     }
136 
137     /**
138      * @param recursive
139      */
140     public void setRecursive(boolean recursive) {
141         this.recursive = recursive;
142     }
143 
144     public String getComment() {
145         return comment;
146     }
147 
148     public void setComment(String comment) {
149         this.comment = comment;
150     }
151 
152     @Override
153     public void release() {
154         super.release();
155         this.recursive = false;
156     }
157 }