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.jcr.nodebuilder;
35
36 import info.magnolia.jcr.iterator.FilteringNodeIterator;
37 import info.magnolia.jcr.predicate.AbstractPredicate;
38 import info.magnolia.jcr.util.NodeUtil;
39 import info.magnolia.jcr.util.PropertyUtil;
40
41 import java.util.ArrayList;
42 import java.util.List;
43
44 import javax.jcr.ItemExistsException;
45 import javax.jcr.ItemNotFoundException;
46 import javax.jcr.Node;
47 import javax.jcr.RepositoryException;
48 import javax.jcr.Value;
49
50 import org.apache.commons.collections4.CollectionUtils;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54
55
56
57 public abstract class Ops {
58 public static NodeOperation addNode(final String name) {
59 return new AbstractNodeOperation() {
60 @Override
61 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
62
63 return context.addNode(name);
64 }
65 };
66 }
67
68 public static NodeOperation addNode(final String name, final String type) {
69 return new AbstractNodeOperation() {
70 @Override
71 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
72 return context.addNode(name, type);
73 }
74 };
75 }
76
77 public static NodeOperation getNode(final String name) {
78 return new AbstractNodeOperation() {
79 @Override
80 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
81 return context.getNode(name);
82 }
83 };
84 }
85
86
87
88
89 public static NodeOperation addProperty(final String name, final Object newValue) {
90 return new AbstractNodeOperation() {
91 @Override
92 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
93 if (context.hasProperty(name)) {
94 throw new ItemExistsException(String.format("Property %s already exists at %s", name, context.getPath()));
95 }
96 final Value value = PropertyUtil.createValue(newValue, context.getSession().getValueFactory());
97 context.setProperty(name, value);
98 return context;
99 }
100 };
101 }
102
103
104
105
106
107 @Deprecated
108 public static NodeOperation addProperty(final String name, final String newValue) {
109 return addProperty(name, (Object) newValue);
110 }
111
112
113
114
115
116
117 public static NodeOperation setProperty(final String name, final Object newValue) {
118 return new AbstractNodeOperation() {
119 @Override
120 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
121 if (!context.hasProperty(name)) {
122 throw new ItemNotFoundException(String.format("Property %s does not exist at %s", name, context.getPath()));
123 }
124 final Value value = PropertyUtil.createValue(newValue, context.getSession().getValueFactory());
125 context.setProperty(name, value);
126 return context;
127 }
128 };
129 }
130
131
132
133
134 public static NodeOperation renameNode(final String currentName, final String newName) {
135 return new AbstractNodeOperation() {
136 @Override
137 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
138 NodeUtil.renameNode(context.getNode(currentName), newName);
139 return context;
140 }
141 };
142 }
143
144
145
146
147 public static NodeOperation renameProperty(final String name, final String newName) {
148 return new AbstractNodeOperation() {
149 @Override
150 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
151 if (!context.hasProperty(name)) {
152 throw new ItemNotFoundException(String.format("Property %s does not exist at %s", name, context.getPath()));
153 }
154 if (context.hasProperty(newName)) {
155 throw new ItemExistsException(String.format("Property %s already exists at %s", newName, context.getPath()));
156 }
157 final Value value = context.getProperty(name).getValue();
158 context.setProperty(newName, value);
159 context.getProperty(name).remove();
160 return context;
161 }
162 };
163 }
164
165
166
167
168 public static NodeOperation moveNode(final String nodeName, final String dest) {
169 return new AbstractNodeOperation() {
170 @Override
171 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
172 NodeUtil.moveNode(context.getNode(nodeName), context.getNode(dest));
173 return context;
174 }
175 };
176 }
177
178
179
180
181 public static NodeOperation noop() {
182 return new NodeOperation() {
183 @Override
184 public NodeOperation then(NodeOperation... childrenOps) {
185 return null;
186 }
187
188 @Override
189 public void exec(Node context, ErrorHandler errorHandler) {
190 }
191 };
192 }
193
194
195
196
197 public static final NodeOperation getOrAddNode(final String name, final String nodeType) {
198 return new AbstractNodeOperation() {
199
200 @Override
201 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
202 return (context.hasNode(name)) ? context.getNode(name) : context.addNode(name, nodeType);
203 }
204 };
205 }
206
207
208
209
210 public static final NodeOperation onChildNodes() {
211 return onChildNodes(new AbstractPredicate<Node>() {
212 @Override
213 public boolean evaluateTyped(Node t) {
214 return true;
215 }
216 });
217 }
218
219
220
221
222 public static final NodeOperation onChildNodes(final AbstractPredicate<Node> predicate) {
223 return new AbstractNodeOperation() {
224
225 private final Logger log = LoggerFactory.getLogger(AbstractNodeOperation.class);
226
227 private List<NodeOperation> childrenOps = new ArrayList<NodeOperation>();
228
229 @Override
230 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
231 return context;
232 }
233
234 @Override
235 public void exec(Node context, ErrorHandler errorHandler) {
236 try {
237 FilteringNodeIteratorterator.html#FilteringNodeIterator">FilteringNodeIterator iter = new FilteringNodeIterator(context.getNodes(), predicate);
238 while (iter.hasNext()) {
239 for (NodeOperation childrenOp : childrenOps) {
240 childrenOp.exec(iter.nextNode(), errorHandler);
241 }
242 }
243 } catch (RepositoryException e) {
244 try {
245 errorHandler.handle(e, context);
246 } catch (RepositoryException e1) {
247 log.warn("Could not handle original exception {} because of: ", e.getMessage(), e1);
248 }
249 }
250
251 }
252
253 @Override
254 public NodeOperation then(NodeOperation... childrenOps) {
255
256 CollectionUtils.addAll(this.childrenOps, childrenOps);
257 return this;
258 }
259
260 };
261 }
262
263
264
265
266
267 public static final NodeOperation ifTrue(final boolean bool) {
268 return new AbstractNodeOperation() {
269
270 private List<NodeOperation> childrenOps = new ArrayList<NodeOperation>();
271
272 @Override
273 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
274 return context;
275 }
276
277 @Override
278 public void exec(Node context, ErrorHandler errorHandler) {
279 if (bool) {
280 for (NodeOperation childrenOp : childrenOps) {
281 childrenOp.exec(context, errorHandler);
282 }
283 }
284 }
285
286 @Override
287 public NodeOperation then(NodeOperation... childrenOps) {
288
289 CollectionUtils.addAll(this.childrenOps, childrenOps);
290 return this;
291 }
292
293 };
294 }
295 }