1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
90 version(node, context);
91 synchronized (ExclusiveWrite.getInstance()) {
92 markAsDeleted(node);
93 purgeContent(node);
94 storeDeletionInfo(node, context);
95
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
129 node.addMixin(ItemType.DELETED_NODE_MIXIN);
130
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
140 for (Content child : node.getChildren(ItemType.CONTENTNODE.getSystemName())) {
141 child.delete();
142 }
143
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 }