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.util.NodeUtil;
37 import info.magnolia.jcr.util.PropertyUtil;
38
39 import javax.jcr.ItemExistsException;
40 import javax.jcr.ItemNotFoundException;
41 import javax.jcr.Node;
42 import javax.jcr.RepositoryException;
43 import javax.jcr.Value;
44
45
46
47
48
49
50 public abstract class Ops {
51 public static NodeOperation addNode(final String name) {
52 return new AbstractNodeOperation() {
53 @Override
54 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
55
56 return context.addNode(name);
57 }
58 };
59 }
60
61 public static NodeOperation addNode(final String name, final String type) {
62 return new AbstractNodeOperation() {
63 @Override
64 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
65 return context.addNode(name, type);
66 }
67 };
68 }
69
70 public static NodeOperation getNode(final String name) {
71 return new AbstractNodeOperation() {
72 @Override
73 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
74 return context.getNode(name);
75 }
76 };
77 }
78
79
80
81
82 public static NodeOperation addProperty(final String name, final String newValue) {
83 return new AbstractNodeOperation() {
84 @Override
85 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
86 if (context.hasProperty(name)) {
87 throw new ItemExistsException("Property " + name + " already exists at " + context.getPath());
88 }
89 final Value value = PropertyUtil.createValue(newValue, context.getSession().getValueFactory());
90 context.setProperty(name, value);
91 return context;
92 }
93 };
94 }
95
96
97
98
99
100 public static NodeOperation setProperty(final String name, final Object newValue) {
101 return new AbstractNodeOperation() {
102 @Override
103 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
104 if (!context.hasProperty(name)) {
105 throw new ItemNotFoundException(name);
106 }
107 final Value value = PropertyUtil.createValue(newValue, context.getSession().getValueFactory());
108 context.setProperty(name, value);
109 return context;
110 }
111 };
112 }
113
114
115
116
117 public static NodeOperation renameNode(final String currentName, final String newName) {
118 return new AbstractNodeOperation() {
119 @Override
120 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
121 NodeUtil.renameNode(context.getNode(currentName), newName);
122 return context;
123 }
124 };
125 }
126
127
128
129
130 public static NodeOperation renameProperty(final String name, final String newName) {
131 return new AbstractNodeOperation() {
132 @Override
133 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
134 if (!context.hasProperty(name)) {
135 throw new ItemNotFoundException(name);
136 }
137 if (context.hasProperty(newName)) {
138
139 throw new ItemExistsException(newName);
140 }
141 final Value value = context.getProperty(name).getValue();
142 context.setProperty(newName, value);
143 context.getProperty(name).remove();
144 return context;
145 }
146 };
147 }
148
149
150
151
152 public static NodeOperation moveNode(final String nodeName, final String dest) {
153 return new AbstractNodeOperation() {
154 @Override
155 protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
156 NodeUtil.moveNode(context.getNode(nodeName), context.getNode(dest));
157 return context;
158 }
159 };
160 }
161
162
163
164
165 public static NodeOperation noop() {
166 return new NodeOperation() {
167 @Override
168 public NodeOperation then(NodeOperation... childrenOps) {
169 return null;
170 }
171
172 @Override
173 public void exec(Node context, ErrorHandler errorHandler) {
174 }
175 };
176 }
177 }