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.util.ExclusiveWrite;
41  import info.magnolia.context.Context;
42  import info.magnolia.jcr.iterator.FilteringNodeIterator;
43  import info.magnolia.jcr.iterator.FilteringPropertyIterator;
44  import info.magnolia.jcr.predicate.JCRMgnlPropertyHidingPredicate;
45  import info.magnolia.jcr.predicate.NodeTypePredicate;
46  import info.magnolia.jcr.util.NodeTypes;
47  import info.magnolia.objectfactory.Components;
48  
49  import java.util.Iterator;
50  
51  import javax.jcr.Node;
52  import javax.jcr.Property;
53  import javax.jcr.RepositoryException;
54  
55  /**
56   * Command to mark node as deleted and remove all the non-system content.<br>
57   * Context parameter:
58   * <ul>
59   * <li>DELETED_NODE_PROP_NAME : Node name to delete.</li>
60   * <li>Context.ATTRIBUTE_UUID : Identifier of the parent node.</li>
61   * <li>Context.ATTRIBUTE_PATH : Path of the parent node</li>
62   * </ul>
63   */
64  public class MarkNodeAsDeletedCommand extends BaseRepositoryCommand {
65  
66      public static final String DELETED_NODE_TEMPLATE = "ui-admincentral:deleted";
67  
68      public static final String DELETED_NODE_PROP_NAME = "deleteNode";
69  
70      private boolean versionManually = true;
71  
72      private boolean forcePreDelete;
73  
74      private VersionManager versionManager;
75  
76      @Override
77      public boolean execute(Context context) throws Exception {
78          versionManager = Components.getComponent(VersionManager.class);
79  
80          final Node parentNode = getJCRNode(context);
81          final Node node = parentNode.getNode((String) context.get(DELETED_NODE_PROP_NAME));
82          boolean hasActiveSubscriber = false;
83          for (Subscriber subscriber : ActivationManagerFactory.getActivationManager().getSubscribers()) {
84              if (subscriber.isActive()) {
85                  hasActiveSubscriber = true;
86                  break;
87              }
88          }
89          if (hasActiveSubscriber || isForcePreDelete()) {
90              preDeleteNode(node, context);
91          } else {
92              node.remove();
93              parentNode.getSession().save();
94          }
95  
96          return true;
97      }
98  
99      protected void preDeleteNode(Node node, Context context) throws RepositoryException {
100         // TODO: MAGNOLIA-4793 - versioning might be "unsupported" do we still
101         // purge in such case?
102         version(node, context);
103         synchronized (ExclusiveWrite.getInstance()) {
104             markAsDeleted(node);
105             purgeContent(node);
106             storeDeletionInfo(node, context);
107             // save changes before progressing on sub node - means we can't roll back, but session doesn't grow out of limits
108             node.getSession().save();
109         }
110 
111         // MAGNOLIA-5337 - should find all of child nodes, but not only NodeTypes.Content node
112         // for (Iterator<Node> iter = node.getNodes(); iter.hasNext();) {
113         for (Iterator<Node> iter = new FilteringNodeIterator(node.getNodes(), new NodeTypePredicate(NodeTypes.Content.NAME, true)); iter.hasNext();) {
114             preDeleteNode(iter.next(), context);
115         }
116     }
117 
118     protected void storeDeletionInfo(Node node, Context context) throws RepositoryException {
119         String comment = (String) context.get("comment");
120         if (comment == null) {
121             comment = MessagesManager.get("versions.comment.restore");
122         }
123         NodeTypes.Deleted.set(node, comment);
124     }
125 
126     protected void version(Node node, Context context) throws RepositoryException {
127         if (isVersionManually()) {
128             synchronized (ExclusiveWrite.getInstance()) {
129                 String comment = (String) context.get("comment");
130                 if (comment == null) {
131                     comment = MessagesManager.get("versions.comment.deleted");
132                 }
133                 node.setProperty(NodeTypes.Deleted.COMMENT, comment);
134                 node.getSession().save();
135             }
136             versionManager.addVersion(node);
137         }
138     }
139 
140     protected void markAsDeleted(Node node) throws RepositoryException {
141         // add mixin
142         node.addMixin(NodeTypes.Deleted.NAME);
143         // change template
144         NodeTypes.Renderable.set(node, DELETED_NODE_TEMPLATE);
145     }
146 
147     protected void purgeContent(Node node) throws RepositoryException {
148         // delete paragraphs & collections
149         for (Iterator<Node> iter = new FilteringNodeIterator(node.getNodes(), new NodeTypePredicate(NodeTypes.ContentNode.NAME)); iter.hasNext();) {
150             iter.next().remove();
151         }
152         // delete properties (incl title ??)
153         for (Iterator<Property> iter = new FilteringPropertyIterator(node.getProperties(), new JCRMgnlPropertyHidingPredicate()); iter.hasNext();) {
154             Property property = iter.next();
155             property.remove();
156         }
157     }
158 
159     public boolean isVersionManually() {
160         return versionManually;
161     }
162 
163     public void setVersionManually(boolean versionManually) {
164         this.versionManually = versionManually;
165     }
166 
167     public boolean isForcePreDelete() {
168         return forcePreDelete;
169     }
170 
171     public void setForcePreDelete(boolean forcePreDelete) {
172         this.forcePreDelete = forcePreDelete;
173     }
174 }