View Javadoc
1   /**
2    * This file Copyright (c) 2011-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.cms.core.version;
35  
36  import info.magnolia.cms.core.HierarchyManager;
37  import info.magnolia.cms.core.Path;
38  import info.magnolia.cms.security.PermissionUtil;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.context.MgnlContext.Op;
41  import info.magnolia.jcr.decoration.ContentDecoratorNodeWrapper;
42  import info.magnolia.objectfactory.Components;
43  import info.magnolia.repository.RepositoryConstants;
44  import info.magnolia.repository.RepositoryManager;
45  
46  import javax.jcr.InvalidItemStateException;
47  import javax.jcr.ItemExistsException;
48  import javax.jcr.ItemNotFoundException;
49  import javax.jcr.Node;
50  import javax.jcr.PathNotFoundException;
51  import javax.jcr.RepositoryException;
52  import javax.jcr.Session;
53  import javax.jcr.UnsupportedRepositoryOperationException;
54  import javax.jcr.lock.LockException;
55  import javax.jcr.nodetype.ConstraintViolationException;
56  import javax.jcr.version.Version;
57  import javax.jcr.version.VersionException;
58  import javax.jcr.version.VersionHistory;
59  import javax.jcr.version.VersionIterator;
60  
61  import org.slf4j.Logger;
62  import org.slf4j.LoggerFactory;
63  
64  /**
65   * Wrapper providing support for Magnolia specific versioning ops (by copy).
66   */
67  public class MgnlVersioningNodeWrapper extends ContentDecoratorNodeWrapper<MgnlVersioningContentDecorator> {
68  
69      private static final Logger log = LoggerFactory.getLogger(MgnlVersioningNodeWrapper.class);
70  
71      private final RepositoryManager repositoryManager;
72  
73      /**
74       * @deprecated since 5.4, use {@link #MgnlVersioningNodeWrapper(Node, MgnlVersioningContentDecorator, RepositoryManager)}.
75       */
76      @Deprecated
77      public MgnlVersioningNodeWrapper(Node node) {
78          this(node, new MgnlVersioningContentDecorator());
79      }
80  
81      /**
82       * @deprecated since 5.4, use {@link #MgnlVersioningNodeWrapper(Node, MgnlVersioningContentDecorator, RepositoryManager)}.
83       */
84      @Deprecated
85      public MgnlVersioningNodeWrapper(Node node, MgnlVersioningContentDecorator mgnlVersioningContentDecorator) {
86          this(node, mgnlVersioningContentDecorator, Components.getComponent(RepositoryManager.class));
87      }
88  
89      public MgnlVersioningNodeWrapper(Node node, MgnlVersioningContentDecorator mgnlVersioningContentDecorator, RepositoryManager repositoryManager) {
90          super(node, mgnlVersioningContentDecorator);
91          this.repositoryManager = repositoryManager;
92      }
93  
94      @Override
95      public void restore(String versionName, boolean removeExisting) throws VersionException, ItemExistsException, UnsupportedRepositoryOperationException,
96              LockException, InvalidItemStateException, RepositoryException {
97          VersionManager versionMan = VersionManager.getInstance();
98          Node raw = deepUnwrap(getClass());
99          Version version = versionMan.getVersion(raw, versionName);
100         versionMan.restore(raw, version, removeExisting);
101     }
102 
103     @Override
104     public void restore(Version version, boolean removeExisting) throws VersionException, ItemExistsException, InvalidItemStateException,
105             UnsupportedRepositoryOperationException, LockException, RepositoryException {
106         VersionManager versionMan = VersionManager.getInstance();
107         versionMan.restore(deepUnwrap(getClass()), version, removeExisting);
108     }
109 
110     @Override
111     public void restore(Version version, String relPath, boolean removeExisting) throws PathNotFoundException, ItemExistsException, VersionException,
112             ConstraintViolationException, UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {
113         throw new UnsupportedOperationException("Magnolia doesn't support restore into specified path at the moment");
114     }
115 
116     @Override
117     public void restoreByLabel(String versionLabel, boolean removeExisting) throws VersionException, ItemExistsException,
118             UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {
119         throw new UnsupportedOperationException("Magnolia doesn't support locating versions by label at the moment");
120     }
121 
122     /**
123      * Remove version history while removing the content.
124      */
125     @Override
126     public void remove() throws RepositoryException {
127         wrapped = getWrappedNode();
128         final String nodePath = Path.getAbsolutePath(wrapped.getPath());
129         log.debug("removing {} from {}", wrapped.getPath(), getSession().getWorkspace().getName());
130         PermissionUtil.verifyIsGrantedOrThrowException(getSession(), Path.getAbsolutePath(getPath()), Session.ACTION_REMOVE);
131         final String workspaceName = getSession().getWorkspace().getName();
132         if (!(workspaceName.endsWith("-" + RepositoryConstants.VERSION_STORE) || workspaceName.endsWith("-" + RepositoryConstants.SYSTEM))) {
133             MgnlContext.doInSystemContext(new Op<Void, RepositoryException>() {
134                 @Override
135                 public Void exec() throws RepositoryException {
136                     try {
137                         final String uuid = wrapped.getUUID();
138                         HierarchyManager hm = MgnlContext.getHierarchyManager(repositoryManager.getRepositoryNameForWorkspace(workspaceName) + "-" + RepositoryConstants.VERSION_STORE);
139                         Node versionedNode = hm.getContentByUUID(uuid).getJCRNode();
140                         log.debug("Located versioned node {}({})", uuid, nodePath);
141 
142                         VersionHistory history = versionedNode.getVersionHistory();
143 
144                         log.debug("Removing versioned node {}({})", uuid, nodePath);
145                         versionedNode.remove();
146                         hm.save();
147 
148                         VersionIterator iter = history.getAllVersions();
149                         // skip root version. It can't be deleted manually, but will be cleaned up automatically after removing all other versions (see JCR-134)
150                         iter.nextVersion();
151                         while (iter.hasNext()) {
152                             Version version = iter.nextVersion();
153                             log.debug("removing version {}", version.getName());
154                             history.removeVersion(version.getName());
155                         }
156                         // at this point history should be deleted automatically (at least on JR)
157                     } catch (ItemNotFoundException e) {
158                         // doesn't exist in version store, ignore
159                     } catch (UnsupportedRepositoryOperationException e) {
160                         // not versionable or not referenceable ... either way ignore
161                     }
162                     return null;
163                 }
164             });
165         }
166         wrapped.remove();
167     }
168 }