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 String 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("Property " + name + " already exists at " + 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
108 public static NodeOperation setProperty(final String name, final Object newValue) {
109 return new AbstractNodeOperation() {
110 @Override
111 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
112 if (!context.hasProperty(name)) {
113 throw new ItemNotFoundException(name);
114 }
115 final Value value = PropertyUtil.createValue(newValue, context.getSession().getValueFactory());
116 context.setProperty(name, value);
117 return context;
118 }
119 };
120 }
121
122
123
124
125 public static NodeOperation renameNode(final String currentName, final String newName) {
126 return new AbstractNodeOperation() {
127 @Override
128 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
129 NodeUtil.renameNode(context.getNode(currentName), newName);
130 return context;
131 }
132 };
133 }
134
135
136
137
138 public static NodeOperation renameProperty(final String name, final String newName) {
139 return new AbstractNodeOperation() {
140 @Override
141 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
142 if (!context.hasProperty(name)) {
143 throw new ItemNotFoundException(name);
144 }
145 if (context.hasProperty(newName)) {
146
147 throw new ItemExistsException(newName);
148 }
149 final Value value = context.getProperty(name).getValue();
150 context.setProperty(newName, value);
151 context.getProperty(name).remove();
152 return context;
153 }
154 };
155 }
156
157
158
159
160 public static NodeOperation moveNode(final String nodeName, final String dest) {
161 return new AbstractNodeOperation() {
162 @Override
163 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
164 NodeUtil.moveNode(context.getNode(nodeName), context.getNode(dest));
165 return context;
166 }
167 };
168 }
169
170
171
172
173 public static NodeOperation noop() {
174 return new NodeOperation() {
175 @Override
176 public NodeOperation then(NodeOperation... childrenOps) {
177 return null;
178 }
179
180 @Override
181 public void exec(Node context, ErrorHandler errorHandler) {
182 }
183 };
184 }
185
186
187
188
189 public static final NodeOperation getOrAddNode(final String name, final String nodeType) {
190 return new AbstractNodeOperation() {
191
192 @Override
193 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
194 return (context.hasNode(name)) ? context.getNode(name) : context.addNode(name, nodeType);
195 }
196 };
197 }
198
199
200
201
202 public static final NodeOperation onChildNodes() {
203 return onChildNodes(new AbstractPredicate<Node>() {
204 @Override
205 public boolean evaluateTyped(Node t) {
206 return true;
207 }
208 });
209 }
210
211
212
213
214 public static final NodeOperation onChildNodes(final AbstractPredicate<Node> predicate) {
215 return new AbstractNodeOperation() {
216
217 private final Logger log = LoggerFactory.getLogger(AbstractNodeOperation.class);
218
219 private List<NodeOperation> childrenOps = new ArrayList<NodeOperation>();
220
221 @Override
222 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
223 return context;
224 }
225
226 @Override
227 public void exec(Node context, ErrorHandler errorHandler) {
228 try {
229 FilteringNodeIterator iter = new FilteringNodeIterator(context.getNodes(), predicate);
230 while (iter.hasNext()) {
231 for (NodeOperation childrenOp : childrenOps) {
232 childrenOp.exec(iter.nextNode(), errorHandler);
233 }
234 }
235 } catch (RepositoryException e) {
236 try {
237 errorHandler.handle(e, context);
238 } catch (RepositoryException e1) {
239 log.warn("Could not handle original exception {} because of: ", e.getMessage(), e1);
240 }
241 }
242
243 }
244
245 @Override
246 public NodeOperation then(NodeOperation... childrenOps) {
247
248 CollectionUtils.addAll(this.childrenOps, childrenOps);
249 return this;
250 }
251
252 };
253 }
254
255
256
257
258
259 public static final NodeOperation ifTrue(final boolean bool) {
260 return new AbstractNodeOperation() {
261
262 private List<NodeOperation> childrenOps = new ArrayList<NodeOperation>();
263
264 @Override
265 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
266 return context;
267 }
268
269 @Override
270 public void exec(Node context, ErrorHandler errorHandler) {
271 if (bool) {
272 for (NodeOperation childrenOp : childrenOps) {
273 childrenOp.exec(context, errorHandler);
274 }
275 }
276 }
277
278 @Override
279 public NodeOperation then(NodeOperation... childrenOps) {
280
281 CollectionUtils.addAll(this.childrenOps, childrenOps);
282 return this;
283 }
284
285 };
286 }
287 }