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
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
77 version(node, context);
78 synchronized (ExclusiveWrite.getInstance()) {
79 markAsDeleted(node);
80 purgeContent(node);
81 storeDeletionInfo(node, context);
82
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
116 node.addMixin(ItemType.DELETED_NODE_MIXIN);
117
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
127 for (Content child : node.getChildren(ItemType.CONTENTNODE.getSystemName())) {
128 child.delete();
129 }
130
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 }