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