View Javadoc
1   /**
2    * This file Copyright (c) 2014-2015 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.Rule;
38  import info.magnolia.context.Context;
39  import info.magnolia.jcr.predicate.NodeTypePredicate;
40  import info.magnolia.jcr.predicate.RuleBasedNodePredicate;
41  import info.magnolia.jcr.util.NodeUtil;
42  
43  import javax.inject.Inject;
44  import javax.jcr.Node;
45  import javax.jcr.RepositoryException;
46  import javax.jcr.version.Version;
47  import javax.jcr.version.VersionIterator;
48  
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  /**
53   * Command to restore a node and all its descendants to their previous version.
54   */
55  public class RestorePreviousVersionCommand extends RuleBasedCommand {
56      private static final Logger log = LoggerFactory.getLogger(RestorePreviousVersionCommand.class);
57  
58      private VersionManager versionManager;
59  
60      private boolean parentNodeTypeOnly = false;
61  
62      @Inject
63      public RestorePreviousVersionCommand(VersionManager versionManager) {
64          this.versionManager = versionManager;
65      }
66  
67      @Override
68      public boolean execute(Context context) throws Exception {
69  
70          final Node node = getJCRNode(context);
71  
72          parentNodeTypeOnly = (Boolean) (context.containsKey("parentNodeTypeOnly") ? context.get("parentNodeTypeOnly") : false);
73  
74          restore(node);
75  
76          return true;
77      }
78  
79      @Override
80      protected Rule getDefaultRule() {
81          // disable default rule, we use a different strategy here, see #restoreAllChildren
82          return null;
83      }
84  
85      private void restore(Node node) throws RepositoryException {
86          String nodeType = node.getPrimaryNodeType().getName();
87  
88          log.debug("Restoring previous version of node at {} of type {}", node.getPath(), nodeType);
89  
90          // Get last version of parent or single node.
91          Version version = getPreviousVersion(node);
92          // Check the version.
93          if (version == null) {
94              throw new RepositoryException("No previous version found for node at " + node.getPath());
95          }
96  
97          // Restore parent previous version
98          versionManager.restore(node, version, true);
99  
100         restoreAllChildren(node, nodeType);
101     }
102 
103     /**
104      * Restores all descendant nodes according to the following strategy:
105      * <ul>
106      * <li>Restore descendants of the same type as the parent, if {@link #isParentNodeTypeOnly()} is set to <code>true</code>
107      * <li>Restore descendants according to the configured {@link Rule} for allowed node-types
108      * <li>Restore descendants whose type is in the <code>mgnl:</code> namespace (fallback behavior).
109      * </ul>
110      *
111      * @see #isParentNodeTypeOnly()
112      * @see #getRule()
113      */
114     protected void restoreAllChildren(Node node, String parentNodeType) throws RepositoryException {
115 
116         Iterable<Node> children;
117 
118         if (isParentNodeTypeOnly()) {
119             children = NodeUtil.collectAllChildren(node, new NodeTypePredicate(parentNodeType));
120         } else if (this.getRule() != null) {
121             children = NodeUtil.collectAllChildren(node, new RuleBasedNodePredicate(this.getRule()));
122         } else {
123             children = NodeUtil.collectAllChildren(node);
124         }
125 
126         for (Node child : children) {
127             Version childVersion = getPreviousVersion(child);
128             if (childVersion == null) {
129                 log.debug("No previous version found for subnode {}. Skipping restore...", child.getPath());
130                 continue;
131             }
132             versionManager.restore(child, childVersion, true);
133         }
134     }
135 
136     /**
137      * @return previous version or null if not found.
138      */
139     private Version getPreviousVersion(Node node) throws RepositoryException {
140         Version previousVersion = null;
141         VersionIterator versionIterator = versionManager.getAllVersions(node);
142 
143         while (versionIterator.hasNext()) {
144             previousVersion = versionIterator.nextVersion();
145         }
146 
147         return previousVersion;
148     }
149 
150     /**
151      * @return true if the command context contains an <code>parentNodeTypeOnly</code> parameter whose value is true, false (default setting) otherwise.
152      */
153     public boolean isParentNodeTypeOnly() {
154         return parentNodeTypeOnly;
155     }
156 
157 }