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.nodebuilder;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.ItemType;
38 import info.magnolia.cms.core.NodeData;
39 import info.magnolia.cms.util.ContentUtil;
40 import info.magnolia.cms.util.NodeDataUtil;
41 import info.magnolia.cms.util.NodeTypeFilter;
42
43 import javax.jcr.ItemExistsException;
44 import javax.jcr.ItemNotFoundException;
45 import javax.jcr.RepositoryException;
46 import javax.jcr.Value;
47
48
49
50
51
52
53 @Deprecated
54 public abstract class Ops {
55 public static NodeOperation addNode(final String name) {
56 return new AbstractNodeOperation() {
57 @Override
58 protected Content="../../../info/magnolia/cms/core/Content.html#Content">Content doExec(Content context, ErrorHandler errorHandler) throws RepositoryException {
59 return context.createContent(name);
60 }
61 };
62 }
63
64 public static NodeOperation addNode(final String name, final String type) {
65 return new AbstractNodeOperation() {
66 @Override
67 protected Content="../../../info/magnolia/cms/core/Content.html#Content">Content doExec(Content context, ErrorHandler errorHandler) throws RepositoryException {
68 return context.createContent(name, type);
69 }
70 };
71 }
72
73 public static NodeOperation addNode(final String name, final ItemType type) {
74 return new AbstractNodeOperation() {
75 @Override
76 protected Content="../../../info/magnolia/cms/core/Content.html#Content">Content doExec(Content context, ErrorHandler errorHandler) throws RepositoryException {
77 return context.createContent(name, type);
78 }
79 };
80 }
81
82 public static NodeOperation getNode(final String name) {
83 return new AbstractNodeOperation() {
84 @Override
85 protected Content="../../../info/magnolia/cms/core/Content.html#Content">Content doExec(Content context, ErrorHandler errorHandler) throws RepositoryException {
86 return context.getContent(name);
87 }
88 };
89 }
90
91
92
93
94 public static NodeOperation remove(final String name) {
95 return new AbstractNodeOperation() {
96 @Override
97 protected Content="../../../info/magnolia/cms/core/Content.html#Content">Content doExec(Content context, ErrorHandler errorHandler) throws RepositoryException {
98 context.delete(name);
99 return context;
100 }
101 };
102 }
103
104
105
106
107 public static NodeOperation addProperty(final String name, final Object value) {
108 return new AbstractNodeOperation() {
109 @Override
110 protected Content="../../../info/magnolia/cms/core/Content.html#Content">Content doExec(Content context, ErrorHandler errorHandler) throws RepositoryException {
111 if (context.hasNodeData(name)) {
112
113 throw new ItemExistsException(name);
114 }
115 context.createNodeData(name, value);
116 return context;
117 }
118 };
119 }
120
121
122
123
124
125
126 public static NodeOperation setProperty(final String name, final Object newValue) {
127 return new AbstractNodeOperation() {
128 @Override
129 protected Content="../../../info/magnolia/cms/core/Content.html#Content">Content doExec(Content context, ErrorHandler errorHandler) throws RepositoryException {
130 if (!context.hasNodeData(name)) {
131 throw new ItemNotFoundException(name);
132 }
133 final Value value = NodeDataUtil.createValue(newValue, context.getJCRNode().getSession().getValueFactory());
134 context.setNodeData(name, value);
135 return context;
136 }
137 };
138 }
139
140
141
142
143
144
145
146 public static NodeOperation setProperty(final String name, final Object expectedCurrentValue, final Object newValue) {
147 return new AbstractNodeOperation() {
148 @Override
149 protected Content="../../../info/magnolia/cms/core/Content.html#Content">Content doExec(Content context, ErrorHandler errorHandler) throws RepositoryException {
150 if (!context.hasNodeData(name)) {
151 throw new ItemNotFoundException(name);
152 }
153 final NodeData current = context.getNodeData(name);
154 if (!expectedCurrentValue.equals(NodeDataUtil.getValueObject(current))) {
155 errorHandler.report("Expected " + expectedCurrentValue + " at " + current.getHandle() + " but found " + current.getString() + " instead; can't set value to " + newValue + ".");
156 return context;
157 }
158
159 final Value value = NodeDataUtil.createValue(newValue, context.getJCRNode().getSession().getValueFactory());
160 current.setValue(value);
161 return context;
162 }
163 };
164 }
165
166
167
168
169 public static NodeOperation renameNode(final String currentName, final String newName) {
170 return new AbstractNodeOperation() {
171 @Override
172 protected Content="../../../info/magnolia/cms/core/Content.html#Content">Content doExec(Content context, ErrorHandler errorHandler) throws RepositoryException {
173 ContentUtil.rename(context.getContent(currentName), newName);
174 return context;
175 }
176 };
177 }
178
179
180
181
182 public static NodeOperation renameProperty(final String name, final String newName) {
183 return new AbstractNodeOperation() {
184 @Override
185 protected Content="../../../info/magnolia/cms/core/Content.html#Content">Content doExec(Content context, ErrorHandler errorHandler) throws RepositoryException {
186 if (!context.hasNodeData(name)) {
187 throw new ItemNotFoundException(name);
188 }
189 if (context.hasNodeData(newName)) {
190
191 throw new ItemExistsException(newName);
192 }
193 final Value value = context.getNodeData(name).getValue();
194 context.setNodeData(newName, value);
195 context.deleteNodeData(name);
196 return context;
197 }
198 };
199 }
200
201
202
203
204 public static NodeOperation moveNode(final String nodeName, final String dest) {
205 return new AbstractNodeOperation() {
206 @Override
207 protected Content="../../../info/magnolia/cms/core/Content.html#Content">Content doExec(Content context, ErrorHandler errorHandler) throws RepositoryException {
208 ContentUtil.moveInSession(context.getContent(nodeName), dest);
209 return context;
210 }
211 };
212 }
213
214
215
216
217 public static NodeOperation copyNode(final String nodeName, final String dest) {
218 return new AbstractNodeOperation() {
219 @Override
220 protected Content="../../../info/magnolia/cms/core/Content.html#Content">Content doExec(Content context, ErrorHandler errorHandler) throws RepositoryException {
221 ContentUtil.copyInSession(context.getContent(nodeName), dest);
222 return context;
223 }
224 };
225 }
226
227
228
229
230 public static NodeOperation onChildNodes(final NodeOperation... childrenOps) {
231 return onChildNodes(ContentUtil.EXCLUDE_META_DATA_CONTENT_FILTER, childrenOps);
232 }
233
234
235
236
237 public static NodeOperation onChildNodes(final String type, final NodeOperation... childrenOps) {
238 return onChildNodes(new NodeTypeFilter(type), childrenOps);
239 }
240
241
242
243
244 public static NodeOperation onChildNodes(final ItemType type, final NodeOperation... childrenOps) {
245 return onChildNodes(new NodeTypeFilter(type), childrenOps);
246 }
247
248
249
250
251 public static NodeOperation onChildNodes(final Content.ContentFilter filter, final NodeOperation... childrenOps) {
252 return new AbstractNodeOperation() {
253
254 @Override
255 protected Content="../../../info/magnolia/cms/core/Content.html#Content">Content doExec(Content context, ErrorHandler errorHandler) throws RepositoryException {
256 for (Content subNode : context.getChildren(filter)) {
257 for (NodeOperation nodeOperation : childrenOps) {
258 nodeOperation.exec(subNode, errorHandler);
259 }
260 }
261 return context;
262 }
263 };
264 }
265
266
267
268
269 public static NodeOperation noop() {
270 return new NodeOperation() {
271 @Override
272 public NodeOperation then(NodeOperation... childrenOps) {
273 return null;
274 }
275
276 @Override
277 public void exec(Content context, ErrorHandler errorHandler) {
278 }
279 };
280 }
281
282
283
284
285
286
287 abstract static class AbstractOp extends AbstractNodeOperation {
288 }
289
290 }