View Javadoc

1   /**
2    * This file Copyright (c) 2010-2013 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.exchange.ActivationManagerFactory;
38  import info.magnolia.cms.exchange.Subscriber;
39  import info.magnolia.cms.i18n.MessagesManager;
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.ExclusiveWrite;
45  import info.magnolia.cms.util.Rule;
46  import info.magnolia.context.Context;
47  import info.magnolia.context.MgnlContext;
48  import info.magnolia.jcr.iterator.FilteringNodeIterator;
49  import info.magnolia.jcr.iterator.FilteringPropertyIterator;
50  import info.magnolia.jcr.predicate.JCRMgnlPropertyHidingPredicate;
51  import info.magnolia.jcr.predicate.RuleBasedNodePredicate;
52  import info.magnolia.jcr.util.NodeTypes;
53  import info.magnolia.objectfactory.Components;
54  
55  import java.util.Iterator;
56  
57  import javax.jcr.Node;
58  import javax.jcr.Property;
59  import javax.jcr.RepositoryException;
60  
61  import org.apache.commons.lang.StringUtils;
62  
63  /**
64   * Command to mark node as deleted and remove all the non-system content.<br>
65   * Context parameter:
66   * <ul>
67   * <li>DELETED_NODE_PROP_NAME : Node name to delete.</li>
68   * <li>Context.ATTRIBUTE_UUID : Identifier of the parent node.</li>
69   * <li>Context.ATTRIBUTE_PATH : Path of the parent node</li>
70   * </ul>
71   */
72  public class MarkNodeAsDeletedCommand extends RuleBasedCommand {
73  
74      public static final String DELETED_NODE_TEMPLATE = "ui-admincentral:deleted";
75  
76      public static final String DELETED_NODE_PROP_NAME = "deleteNode";
77  
78      private boolean versionManually = true;
79  
80      private boolean forcePreDelete;
81  
82      private VersionManager versionManager;
83  
84      @Override
85      public boolean execute(Context context) throws Exception {
86          versionManager = Components.getComponent(VersionManager.class);
87  
88          final Node parentNode = getJCRNode(context);
89          final Node node = parentNode.getNode((String) context.get(DELETED_NODE_PROP_NAME));
90  
91          // make sure currentUser has permission to mark node as deleted, since this is performed in system context
92          User currentUser = MgnlContext.getUser();
93          if (currentUser == null) {
94              throw new AccessDeniedException(String.format("Current user in context %s was null, denying execution of command %s", context, this));
95          } else if (!PermissionUtil.isGranted(node, Permission.WRITE)) {
96              throw new AccessDeniedException(String.format("%s: not allowed to delete item", node.getPath()));
97          }
98  
99          boolean hasActiveSubscriber = false;
100         for (Subscriber subscriber : ActivationManagerFactory.getActivationManager().getSubscribers()) {
101             if (subscriber.isActive()) {
102                 hasActiveSubscriber = true;
103                 break;
104             }
105         }
106         if (hasActiveSubscriber || isForcePreDelete()) {
107             preDeleteNode(node, context);
108         } else {
109             node.remove();
110             parentNode.getSession().save();
111         }
112 
113         return true;
114     }
115 
116     protected void preDeleteNode(Node node, Context context) throws RepositoryException {
117         // TODO: MAGNOLIA-4793 - versioning might be "unsupported" do we still purge in such case?
118         version(node, context);
119         synchronized (ExclusiveWrite.getInstance()) {
120             markAsDeleted(node);
121             purgeContent(node);
122             storeDeletionInfo(node, context);
123             // save changes before progressing on sub node - means we can't roll back, but session doesn't grow out of limits
124             node.getSession().save();
125         }
126 
127         Rule rule = new Rule(StringUtils.split(getItemTypes() + "," + NodeTypes.MetaData.NAME + "," + NodeTypes.Resource.NAME, ", "));
128         rule.reverse();
129         for (Iterator<Node> iter = new FilteringNodeIterator(node.getNodes(), new RuleBasedNodePredicate(rule)); iter.hasNext();) {
130             preDeleteNode(iter.next(), context);
131         }
132     }
133 
134     protected void storeDeletionInfo(Node node, Context context) throws RepositoryException {
135         String comment = (String) context.get("comment");
136         if (comment == null) {
137             comment = MessagesManager.get("versions.comment.restore");
138         }
139         NodeTypes.Deleted.set(node, comment);
140     }
141 
142     protected void version(Node node, Context context) throws RepositoryException {
143         if (isVersionManually()) {
144             synchronized (ExclusiveWrite.getInstance()) {
145                 String comment = (String) context.get("comment");
146                 if (comment == null) {
147                     comment = MessagesManager.get("versions.comment.deleted");
148                 }
149                 node.setProperty(NodeTypes.Deleted.COMMENT, comment);
150                 node.getSession().save();
151             }
152             versionManager.addVersion(node, getRule());
153         }
154     }
155 
156     protected void markAsDeleted(Node node) throws RepositoryException {
157         // add mixin
158         node.addMixin(NodeTypes.Deleted.NAME);
159         // change template
160         if (node.isNodeType(NodeTypes.Renderable.NAME)) {
161             NodeTypes.Renderable.set(node, DELETED_NODE_TEMPLATE);
162         }
163     }
164 
165     protected void purgeContent(Node node) throws RepositoryException {
166         // delete paragraphs & collections
167         for (Iterator<Node> iter = new FilteringNodeIterator(node.getNodes(), new RuleBasedNodePredicate(getRule())); iter.hasNext();) {
168             iter.next().remove();
169         }
170         // delete properties (incl title ??)
171         for (Iterator<Property> iter = new FilteringPropertyIterator(node.getProperties(), new JCRMgnlPropertyHidingPredicate()); iter.hasNext();) {
172             Property property = iter.next();
173             property.remove();
174         }
175     }
176 
177     public boolean isVersionManually() {
178         return versionManually;
179     }
180 
181     public void setVersionManually(boolean versionManually) {
182         this.versionManually = versionManually;
183     }
184 
185     public boolean isForcePreDelete() {
186         return forcePreDelete;
187     }
188 
189     public void setForcePreDelete(boolean forcePreDelete) {
190         this.forcePreDelete = forcePreDelete;
191     }
192 }