View Javadoc

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