View Javadoc

1   /**
2    * This file Copyright (c) 2010-2011 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.module.admininterface.commands;
35  
36  import java.util.Calendar;
37  import javax.jcr.PathNotFoundException;
38  import javax.jcr.RepositoryException;
39  import javax.jcr.UnsupportedRepositoryOperationException;
40  
41  import info.magnolia.cms.core.Content;
42  import info.magnolia.cms.core.ItemType;
43  import info.magnolia.cms.core.MetaData;
44  import info.magnolia.cms.core.NodeData;
45  import info.magnolia.cms.exchange.ActivationManagerFactory;
46  import info.magnolia.cms.exchange.Subscriber;
47  import info.magnolia.cms.i18n.MessagesManager;
48  import info.magnolia.cms.security.AccessDeniedException;
49  import info.magnolia.cms.util.ExclusiveWrite;
50  import info.magnolia.context.Context;
51  import info.magnolia.context.MgnlContext;
52  import info.magnolia.module.admininterface.commands.BaseRepositoryCommand;
53  
54  
55  public class MarkNodeAsDeletedCommand extends BaseRepositoryCommand {
56  
57      public static final String DELETED_NODE_TEMPLATE = "mgnlDeleted";
58  
59      public static final String DELETED_NODE_DELETED_BY = "mgnl:deletedBy";
60      public static final String DELETED_NODE_DELETED_ON = "mgnl:deletedOn";
61  
62      private static final String DELETED_NODE_PROP_NAME = "deleteNode";
63  
64      private boolean versionManually = true;
65  
66      @Override
67      public boolean execute(Context context) throws Exception {
68  
69          final Content parentNode = getNode(context);
70          final Content node = parentNode.getContent((String) context.get(DELETED_NODE_PROP_NAME));
71          boolean hasActiveSubscriber = false;
72          for (Subscriber subscriber : ActivationManagerFactory.getActivationManager().getSubscribers()) {
73              if (subscriber.isActive()) {
74                  hasActiveSubscriber = true;
75                  break;
76              }
77          }
78          if (hasActiveSubscriber) {
79              preDeleteNode(node, context);
80          } else {
81              node.delete();
82              parentNode.save();
83          }
84  
85          return true;
86      }
87  
88      private void preDeleteNode(Content node, Context context) throws RepositoryException, AccessDeniedException {
89          // TODO: versioning might be "unsupported" do we still purge in such case?
90          version(node, context);
91          synchronized (ExclusiveWrite.getInstance()) {
92              markAsDeleted(node);
93              purgeContent(node);
94              storeDeletionInfo(node, context);
95              // save changes before progressing on sub node - means we can't roll back, but session doesn't grow out of limits
96              node.save();
97          }
98          for(Content childPage : node.getChildren(ItemType.CONTENT)) {
99              preDeleteNode(childPage, context);
100         }
101     }
102 
103     private void storeDeletionInfo(Content node, Context context) throws AccessDeniedException, PathNotFoundException, RepositoryException {
104         node.setNodeData(DELETED_NODE_DELETED_BY, MgnlContext.getUser().getName());
105         node.setNodeData(DELETED_NODE_DELETED_ON, Calendar.getInstance());
106         String comment = (String) context.get("comment");
107         if (comment == null) {
108             comment = MessagesManager.get("versions.comment.restore");
109         }
110         node.getMetaData().setProperty(Context.ATTRIBUTE_COMMENT, comment);
111     }
112 
113     private void version(Content node, Context context) throws UnsupportedRepositoryOperationException, RepositoryException {
114         if (versionManually) {
115             synchronized (ExclusiveWrite.getInstance()) {
116                 String comment = (String) context.get("comment");
117                 if (comment == null) {
118                     comment = MessagesManager.get("versions.comment.deleted");
119                 }
120                 node.getMetaData().setProperty(Context.ATTRIBUTE_COMMENT, comment);
121                 node.save();
122             }
123             node.addVersion();
124         }
125     }
126 
127     protected void markAsDeleted(Content node) throws RepositoryException, AccessDeniedException {
128         // add mixin
129         node.addMixin(ItemType.DELETED_NODE_MIXIN);
130         // change template
131         MetaData metadata = node.getMetaData();
132         metadata.setTemplate(DELETED_NODE_TEMPLATE);
133         if (metadata.getActivationStatus() != MetaData.ACTIVATION_STATUS_NOT_ACTIVATED) {
134             metadata.setModificationDate();
135         }
136     }
137 
138     protected void purgeContent(Content node) throws RepositoryException {
139         // delete paragraphs & collections
140         for (Content child : node.getChildren(ItemType.CONTENTNODE.getSystemName())) {
141             child.delete();
142         }
143         // delete properties (incl title ??)
144         for (NodeData prop : node.getNodeDataCollection()) {
145             prop.delete();
146         }
147     }
148 
149     public boolean isVersionManually() {
150         return versionManually;
151     }
152 
153     public void setVersionManually(boolean versionManually) {
154         this.versionManually = versionManually;
155     }
156 
157 }