View Javadoc

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