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