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.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
57
58
59
60
61
62
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
101
102 version(node, context);
103 synchronized (ExclusiveWrite.getInstance()) {
104 markAsDeleted(node);
105 purgeContent(node);
106 storeDeletionInfo(node, context);
107
108 node.getSession().save();
109 }
110
111
112
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
142 node.addMixin(NodeTypes.Deleted.NAME);
143
144 NodeTypes.Renderable.set(node, DELETED_NODE_TEMPLATE);
145 }
146
147 protected void purgeContent(Node node) throws RepositoryException {
148
149 for (Iterator<Node> iter = new FilteringNodeIterator(node.getNodes(), new NodeTypePredicate(NodeTypes.ContentNode.NAME)); iter.hasNext();) {
150 iter.next().remove();
151 }
152
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 }