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.util.ExclusiveWrite;
38 import info.magnolia.context.Context;
39 import info.magnolia.jcr.util.NodeTypes;
40 import info.magnolia.jcr.util.PropertyUtil;
41 import info.magnolia.objectfactory.Components;
42
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47
48 import javax.inject.Inject;
49 import javax.jcr.Node;
50 import javax.jcr.NodeIterator;
51 import javax.jcr.RepositoryException;
52 import javax.jcr.version.Version;
53
54 import org.apache.commons.lang3.StringUtils;
55
56
57
58
59 public class VersionCommand extends RuleBasedCommand {
60
61 private boolean recursive;
62 private String comment;
63 private String userName;
64
65 private final VersionManager versionManager;
66
67 @Inject
68 public VersionCommand(final VersionManager versionManager) {
69 this.versionManager = versionManager;
70 }
71
72
73
74
75 @Deprecated
76 public VersionCommand() {
77 this.versionManager = Components.getComponent(VersionManager.class);
78 }
79
80
81
82
83 @Override
84 public boolean execute(Context ctx) throws Exception {
85 final Node node = getJCRNode(ctx);
86 if (isRecursive()) {
87
88 List versionMap = new ArrayList();
89 versionRecursively(node, ctx, versionMap);
90 ctx.setAttribute(Context.ATTRIBUTE_VERSION_MAP, versionMap, Context.LOCAL_SCOPE);
91 } else {
92 addComment(node);
93 Version version;
94 if (StringUtils.isEmpty(getUserName())) {
95 version = versionManager.addVersion(node, getRule());
96 } else {
97 version = versionManager.addVersion(node, getRule(), getUserName());
98 }
99
100 if (version != null) {
101 ctx.setAttribute(Context.ATTRIBUTE_VERSION, version.getName(), Context.LOCAL_SCOPE);
102 }
103 cleanComment(node);
104 }
105
106 return true;
107 }
108
109 protected void addComment(final Node node) throws RepositoryException {
110 synchronized (ExclusiveWrite.getInstance()) {
111 PropertyUtil.setProperty(node, NodeTypes.Versionable.COMMENT, getComment() != null ? getComment() : "");
112 node.getSession().save();
113 }
114 }
115
116 protected void cleanComment(final Node node) throws RepositoryException {
117 synchronized (ExclusiveWrite.getInstance()) {
118 if (node.hasProperty(NodeTypes.Versionable.COMMENT)
119 && !StringUtils.EMPTY.equals(node.getProperty(NodeTypes.Versionable.COMMENT).getString())) {
120 node.getProperty(NodeTypes.Versionable.COMMENT).remove();
121 node.getSession().save();
122 }
123 }
124 }
125
126
127
128
129
130
131
132
133 private void versionRecursively(Node node, Context ctx, List versionMap) throws RepositoryException {
134 addComment(node);
135 Version version;
136 if (StringUtils.isEmpty(getUserName())) {
137 version = versionManager.addVersion(node, getRule());
138 } else {
139 version = versionManager.addVersion(node, getRule(), getUserName());
140 }
141
142 Map entry = new HashMap();
143 entry.put("uuid", node.getIdentifier());
144 if (version != null) {
145 entry.put("version", version.getName());
146 if (StringUtils.isEmpty((String) ctx.getAttribute(Context.ATTRIBUTE_VERSION))) {
147 ctx.setAttribute(Context.ATTRIBUTE_VERSION, version.getName(), Context.LOCAL_SCOPE);
148 }
149 }
150 versionMap.add(entry);
151
152 cleanComment(node);
153
154 NodeIterator children = node.getNodes();
155 while (children.hasNext()) {
156 Node child = children.nextNode();
157 if (!getRule().isAllowed(child)) {
158 versionRecursively(child, ctx, versionMap);
159 }
160 }
161 }
162
163
164
165
166 public boolean isRecursive() {
167 return recursive;
168 }
169
170 public void setRecursive(boolean recursive) {
171 this.recursive = recursive;
172 }
173
174 public String getComment() {
175 return comment;
176 }
177
178 public void setComment(String comment) {
179 this.comment = comment;
180 }
181
182 @Override
183 public void release() {
184 super.release();
185 this.recursive = false;
186 this.userName = null;
187 }
188
189 public String getUserName() {
190 return userName;
191 }
192
193 public void setUserName(final String userName) {
194 this.userName = userName;
195 }
196 }