View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.beans.config.VersionConfig;
37  import info.magnolia.cms.core.version.VersionManager;
38  import info.magnolia.cms.exchange.ActivationManagerFactory;
39  import info.magnolia.cms.exchange.Subscriber;
40  import info.magnolia.cms.security.AccessDeniedException;
41  import info.magnolia.cms.security.Permission;
42  import info.magnolia.cms.security.PermissionUtil;
43  import info.magnolia.cms.security.User;
44  import info.magnolia.cms.util.Rule;
45  import info.magnolia.context.Context;
46  import info.magnolia.context.MgnlContext;
47  import info.magnolia.jcr.iterator.FilteringNodeIterator;
48  import info.magnolia.jcr.iterator.FilteringPropertyIterator;
49  import info.magnolia.jcr.predicate.JCRMgnlPropertyHidingPredicate;
50  import info.magnolia.jcr.predicate.RuleBasedNodePredicate;
51  import info.magnolia.jcr.util.NodeTypes;
52  import info.magnolia.objectfactory.Components;
53  
54  import java.util.Iterator;
55  
56  import javax.jcr.Node;
57  import javax.jcr.Property;
58  import javax.jcr.RepositoryException;
59  import javax.jcr.lock.Lock;
60  import javax.jcr.lock.LockManager;
61  
62  import org.apache.commons.lang3.StringUtils;
63  
64  /**
65   * Command to mark node as deleted and remove all the non-system content.<br>
66   * Context parameter:
67   * <ul>
68   * <li>DELETED_NODE_PROP_NAME : Node name to delete.</li>
69   * <li>Context.ATTRIBUTE_UUID : Identifier of the parent node.</li>
70   * <li>Context.ATTRIBUTE_PATH : Path of the parent node</li>
71   * </ul>
72   */
73  public class MarkNodeAsDeletedCommand extends RuleBasedCommand {
74  
75      public static final String DELETED_NODE_TEMPLATE = "ui-admincentral:deleted";
76      public static final String DELETED_NODE_PROP_NAME = "deleteNode";
77      public static final int HOLD_LOCK_IN_SECONDS = 30;
78  
79      private boolean versionManually = true;
80  
81      private boolean forcePreDelete;
82  
83      private VersionManager versionManager;
84  
85      @Override
86      public boolean execute(Context context) throws Exception {
87          versionManager = Components.getComponent(VersionManager.class);
88  
89          final Node parentNode = getJCRNode(context);
90          final Node node = parentNode.getNode((String) context.get(DELETED_NODE_PROP_NAME));
91          final LockManager manager = node.getSession().getWorkspace().getLockManager();
92  
93          // make sure currentUser has permission to mark node as deleted, since this is performed in system context
94          User currentUser = MgnlContext.getUser();
95          if (currentUser == null) {
96              throw new AccessDeniedException(String.format("Current user in context %s was null, denying execution of command %s", context, this));
97          } else if (!PermissionUtil.isGranted(node, Permission.WRITE)) {
98              throw new AccessDeniedException(String.format("%s: not allowed to delete item", node.getPath()));
99          }
100 
101         boolean hasActiveSubscriber = false;
102         for (Subscriber subscriber : ActivationManagerFactory.getActivationManager().getSubscribers()) {
103             if (subscriber.isActive()) {
104                 hasActiveSubscriber = true;
105                 break;
106             }
107         }
108 
109         if (hasActiveSubscriber || isForcePreDelete()) {
110             try {
111                 Lock lock = manager.lock(node.getPath(), true, false, HOLD_LOCK_IN_SECONDS, null);
112                 preDeleteNode(node, context, lock);
113             } finally {
114                 if (manager.isLocked(node.getPath())) {
115                     manager.unlock(node.getPath());
116                 }
117             }
118         } else {
119             this.deleteNode(node);
120             parentNode.getSession().save();
121         }
122 
123         return true;
124     }
125 
126     protected void deleteNode(Node node) throws RepositoryException {
127         node.remove();
128     }
129 
130     protected void preDeleteNode(Node node, Context context, Lock lock) throws RepositoryException {
131         // Due to deprecated #preDeleteNode(Node,Context) method, lock object can be null.
132         if (lock != null) {
133             lock.refresh();
134         }
135 
136         VersionConfig versionConfig = Components.getComponent(VersionConfig.class);
137         if (versionConfig.isActive()) {
138             version(node, context);
139         }
140 
141         markAsDeleted(node);
142         purgeContent(node);
143         storeDeletionInfo(node, context);
144         // save changes before progressing on sub node - means we can't roll back, but session doesn't grow out of limits
145         node.getSession().save();
146 
147 
148         Rule rule = new Rule(StringUtils.split(getItemTypes() + "," + NodeTypes.MetaData.NAME + "," + NodeTypes.Resource.NAME, ", "));
149         rule.reverse();
150         for (Iterator<Node> iter = new FilteringNodeIterator(node.getNodes(), new RuleBasedNodePredicate(rule)); iter.hasNext(); ) {
151             preDeleteNode(iter.next(), context, lock);
152         }
153     }
154 
155     /**
156      * @deprecated Since 5.5, Please use {@link #preDeleteNode(Node, Context, Lock)} instead
157      */
158     @Deprecated
159     protected void preDeleteNode(Node node, Context context) throws RepositoryException {
160         log.warn("Usage of deprecated method detected, Locked node at'{}' will be automatically unlocked after '{}' seconds.", node.getPath(), HOLD_LOCK_IN_SECONDS);
161         preDeleteNode(node, context, null);
162     }
163 
164     protected void storeDeletionInfo(Node node, Context context) throws RepositoryException {
165         String comment = (String) context.get("comment");
166 
167         NodeTypes.Deleted.set(node, comment);
168     }
169 
170     protected void version(Node node, Context context) throws RepositoryException {
171         if (isVersionManually()) {
172             String comment = (String) context.get("comment");
173             if (comment == null) {
174                 comment = "versions.comment.deleted";
175             }
176             node.setProperty(NodeTypes.Deleted.COMMENT, comment);
177             node.getSession().save();
178 
179             versionManager.addVersion(node, getRule());
180         }
181     }
182 
183     protected void markAsDeleted(Node node) throws RepositoryException {
184         // add mixin
185         node.addMixin(NodeTypes.Deleted.NAME);
186         // change template
187         if (node.isNodeType(NodeTypes.Renderable.NAME)) {
188             NodeTypes.Renderable.set(node, DELETED_NODE_TEMPLATE);
189         }
190     }
191 
192     protected void purgeContent(Node node) throws RepositoryException {
193         // delete paragraphs & collections
194         for (Iterator<Node> iter = new FilteringNodeIterator(node.getNodes(), new RuleBasedNodePredicate(getRule())); iter.hasNext(); ) {
195             iter.next().remove();
196         }
197         // delete properties (incl title ??)
198         for (Iterator<Property> iter = new FilteringPropertyIterator(node.getProperties(), new JCRMgnlPropertyHidingPredicate()); iter.hasNext(); ) {
199             Property property = iter.next();
200             property.remove();
201         }
202     }
203 
204     public boolean isVersionManually() {
205         return versionManually;
206     }
207 
208     public void setVersionManually(boolean versionManually) {
209         this.versionManually = versionManually;
210     }
211 
212     public boolean isForcePreDelete() {
213         return forcePreDelete;
214     }
215 
216     public void setForcePreDelete(boolean forcePreDelete) {
217         this.forcePreDelete = forcePreDelete;
218     }
219 }