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.module.groovy.support.nodes; |
35 |
|
|
36 |
|
import groovy.lang.DelegatingMetaClass; |
37 |
|
import groovy.lang.GroovySystem; |
38 |
|
import groovy.lang.MetaClass; |
39 |
|
|
40 |
|
import info.magnolia.cms.security.AccessDeniedException; |
41 |
|
import info.magnolia.jcr.RuntimeRepositoryException; |
42 |
|
import info.magnolia.jcr.iterator.DelegatingNodeIterator; |
43 |
|
import info.magnolia.jcr.util.MetaDataUtil; |
44 |
|
import info.magnolia.jcr.util.NodeTypes; |
45 |
|
import info.magnolia.jcr.util.NodeUtil; |
46 |
|
import info.magnolia.jcr.util.PropertyUtil; |
47 |
|
import info.magnolia.jcr.wrapper.DelegateNodeWrapper; |
48 |
|
|
49 |
|
import java.math.BigDecimal; |
50 |
|
import java.util.ArrayList; |
51 |
|
import java.util.List; |
52 |
|
|
53 |
|
import javax.jcr.Node; |
54 |
|
import javax.jcr.NodeIterator; |
55 |
|
import javax.jcr.PathNotFoundException; |
56 |
|
import javax.jcr.Property; |
57 |
|
import javax.jcr.PropertyIterator; |
58 |
|
import javax.jcr.PropertyType; |
59 |
|
import javax.jcr.RepositoryException; |
60 |
|
import javax.jcr.Value; |
61 |
|
import javax.jcr.ValueFormatException; |
62 |
|
|
63 |
|
import org.apache.commons.lang.StringUtils; |
64 |
|
import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter; |
65 |
|
import org.slf4j.Logger; |
66 |
|
import org.slf4j.LoggerFactory; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@link |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
@link |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
|
|
| 30.6% |
Uncovered Elements: 143 (206) |
Complexity: 70 |
Complexity Density: 0.51 |
|
119 |
|
public class MgnlGroovyJCRNode extends DelegateNodeWrapper { |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
121 |
1 |
static {... |
122 |
|
|
123 |
1 |
setMetaClass(GroovySystem.getMetaClassRegistry().getMetaClass(MgnlGroovyJCRNode.class), MgnlGroovyJCRNode.class); |
124 |
|
} |
125 |
|
|
126 |
|
protected static final Logger log = LoggerFactory.getLogger(MgnlGroovyJCRNode.class); |
127 |
|
|
128 |
|
private String name; |
129 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
130 |
16 |
public MgnlGroovyJCRNode(Node node) {... |
131 |
16 |
super(node); |
132 |
16 |
try { |
133 |
16 |
this.name = "/".equals(node.getPath()) ? "/root" : getName(); |
134 |
|
} catch (RepositoryException e) { |
135 |
0 |
throw new RuntimeRepositoryException(e); |
136 |
|
} |
137 |
|
} |
138 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
139 |
1 |
protected static void setMetaClass(final MetaClass metaClass, Class nodeClass) {... |
140 |
1 |
final MetaClass newMetaClass = new DelegatingMetaClass(metaClass) { |
141 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 3 |
Complexity Density: 0.5 |
|
142 |
0 |
@Override... |
143 |
|
public Object getAttribute(final Object object, final String attribute) { |
144 |
0 |
MgnlGroovyJCRNode n = (MgnlGroovyJCRNode) object; |
145 |
0 |
try { |
146 |
0 |
return n.get("@" + attribute); |
147 |
|
} catch (ValueFormatException e) { |
148 |
0 |
log.error(e.getMessage()); |
149 |
|
} catch (RepositoryException e) { |
150 |
0 |
log.error(e.getMessage()); |
151 |
|
} |
152 |
0 |
return null; |
153 |
|
} |
154 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 4 |
Complexity Density: 0.67 |
|
155 |
0 |
@Override... |
156 |
|
public void setAttribute(final Object object, final String attribute, final Object newValue) { |
157 |
0 |
MgnlGroovyJCRNode n = (MgnlGroovyJCRNode) object; |
158 |
0 |
try { |
159 |
0 |
PropertyUtil.setProperty(n, attribute, newValue); |
160 |
|
} |
161 |
|
catch (AccessDeniedException e) { |
162 |
0 |
log.error(e.getMessage()); |
163 |
|
} |
164 |
|
catch (PathNotFoundException e) { |
165 |
0 |
log.error(e.getMessage()); |
166 |
|
} |
167 |
|
catch (RepositoryException e) { |
168 |
0 |
log.error(e.getMessage()); |
169 |
|
} |
170 |
|
} |
171 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 4 |
Complexity Density: 0.5 |
|
172 |
0 |
@Override... |
173 |
|
public Object getProperty(Object object, String property) { |
174 |
0 |
if (object instanceof MgnlGroovyJCRNode) { |
175 |
0 |
MgnlGroovyJCRNode n = (MgnlGroovyJCRNode) object; |
176 |
0 |
try { |
177 |
0 |
return n.get(property); |
178 |
|
} catch (ValueFormatException e) { |
179 |
0 |
log.error(e.getMessage()); |
180 |
|
} catch (RepositoryException e) { |
181 |
0 |
log.error(e.getMessage()); |
182 |
|
} |
183 |
0 |
return null; |
184 |
|
} |
185 |
0 |
return super.getProperty(object, property); |
186 |
|
} |
187 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 6 |
Complexity Density: 0.5 |
|
188 |
0 |
@Override... |
189 |
|
public void setProperty(Object object, String property, Object newValue) { |
190 |
0 |
String attribute = null; |
191 |
0 |
if (property.startsWith("@")) { |
192 |
0 |
attribute = property.substring(1); |
193 |
|
} else { |
194 |
0 |
attribute = property; |
195 |
|
} |
196 |
0 |
MgnlGroovyJCRNode n = (MgnlGroovyJCRNode) object; |
197 |
0 |
try { |
198 |
|
|
199 |
0 |
if (newValue instanceof Integer) { |
200 |
0 |
newValue = ((Integer) newValue).longValue(); |
201 |
|
} |
202 |
0 |
PropertyUtil.setProperty(n, attribute, newValue); |
203 |
|
} |
204 |
|
catch (AccessDeniedException e) { |
205 |
0 |
log.error(e.getMessage()); |
206 |
|
} |
207 |
|
catch (PathNotFoundException e) { |
208 |
0 |
log.error(e.getMessage()); |
209 |
|
} |
210 |
|
catch (RepositoryException e) { |
211 |
0 |
log.error(e.getMessage()); |
212 |
|
} |
213 |
|
} |
214 |
|
}; |
215 |
|
|
216 |
1 |
GroovySystem.getMetaClassRegistry().setMetaClass(nodeClass, newMetaClass); |
217 |
|
} |
218 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
219 |
0 |
public String name() {... |
220 |
0 |
return name; |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
@param |
227 |
|
@return |
228 |
|
@throws |
229 |
|
@throws |
230 |
|
|
|
|
| 81.2% |
Uncovered Elements: 6 (32) |
Complexity: 10 |
Complexity Density: 0.5 |
|
231 |
9 |
public Object get(String key) throws ValueFormatException, RepositoryException {... |
232 |
9 |
if (key != null && key.charAt(0) == '@') { |
233 |
3 |
String attributeName = key.substring(1); |
234 |
3 |
Property property = PropertyUtil.getPropertyOrNull(getWrappedNode(), attributeName); |
235 |
3 |
return getPropertyValue(property); |
236 |
|
} |
237 |
6 |
try { |
238 |
6 |
if ("parent".equals(key)) { |
239 |
0 |
return getParent(); |
240 |
|
} |
241 |
6 |
if ("children".equals(key) || "nodes".equals(key)) { |
242 |
3 |
Iterable<Node> iterable = NodeUtil.getNodes(getWrappedNode()); |
243 |
3 |
return new MgnlGroovyWrappingNodeIterator(new NodeIteratorAdapter(iterable.iterator())); |
244 |
|
} |
245 |
3 |
if ("metaData".equals(key)) { |
246 |
0 |
return MetaDataUtil.getMetaData(getWrappedNode()); |
247 |
|
} |
248 |
3 |
if ("properties".equals(key)) { |
249 |
0 |
return getProperties(); |
250 |
|
} |
251 |
3 |
if ("name".equals(key)) { |
252 |
1 |
return name; |
253 |
|
} |
254 |
2 |
return new MgnlGroovyJCRNode(getNode(key)); |
255 |
|
|
256 |
|
} catch (RepositoryException e) { |
257 |
2 |
log.info(e.getMessage()); |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
2 |
Property property = PropertyUtil.getPropertyOrNull(getWrappedNode(), key); |
262 |
2 |
return getPropertyValue(property); |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
@link |
268 |
|
|
269 |
|
@link@link |
270 |
|
@link@link |
271 |
|
@link@link |
272 |
|
@link@link |
273 |
|
@link@link |
274 |
|
@link |
275 |
|
|
276 |
|
|
|
|
| 41.7% |
Uncovered Elements: 7 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
277 |
5 |
protected Object getPropertyValue(Property property) throws RepositoryException, ValueFormatException {... |
278 |
5 |
if (property == null) { |
279 |
0 |
return "unknown property"; |
280 |
|
} |
281 |
5 |
if (property.isMultiple()) { |
282 |
0 |
List<Object> values = new ArrayList<Object>(); |
283 |
0 |
for (Value value : property.getValues()) { |
284 |
0 |
values.add(getPropertyValueInternal(value)); |
285 |
|
} |
286 |
0 |
return values; |
287 |
|
} else { |
288 |
5 |
return getPropertyValueInternal(property.getValue()); |
289 |
|
} |
290 |
|
|
291 |
|
} |
292 |
|
|
|
|
| 0% |
Uncovered Elements: 32 (32) |
Complexity: 8 |
Complexity Density: 0.36 |
|
293 |
0 |
public String print() {... |
294 |
0 |
StringBuilder retVal = new StringBuilder(); |
295 |
0 |
try { |
296 |
0 |
Iterable<Node> nodes = NodeUtil.getNodes(getWrappedNode()); |
297 |
0 |
retVal.append("(+) " + name + "\n"); |
298 |
0 |
for (Node node : nodes) { |
299 |
0 |
if (!showNode(node)) { |
300 |
0 |
continue; |
301 |
|
} |
302 |
0 |
if ((NodeUtil.getNodes(node).iterator().hasNext())) { |
303 |
0 |
retVal.append("\t(+) "); |
304 |
|
} else { |
305 |
0 |
retVal.append("\t(-) "); |
306 |
|
} |
307 |
0 |
retVal.append(node.getName() + "\n"); |
308 |
|
} |
309 |
|
|
310 |
0 |
if (getProperties().getSize() > 0) { |
311 |
0 |
PropertyIterator it = super.getProperties(); |
312 |
0 |
while (it.hasNext()) { |
313 |
0 |
Property property = it.nextProperty(); |
314 |
0 |
if (!showProperty(property)) { |
315 |
0 |
continue; |
316 |
|
} |
317 |
0 |
String name = property.getName(); |
318 |
|
|
319 |
0 |
retVal.append("\t\t* " + name + ": [" |
320 |
|
+ StringUtils.abbreviate(getPropertyValue(property).toString(), 80) |
321 |
|
+ "]\n"); |
322 |
|
|
323 |
|
} |
324 |
|
|
325 |
|
} |
326 |
|
} catch (ValueFormatException e) { |
327 |
0 |
e.printStackTrace(); |
328 |
|
} catch (RepositoryException e) { |
329 |
0 |
e.printStackTrace(); |
330 |
|
} |
331 |
|
|
332 |
0 |
return retVal.toString(); |
333 |
|
} |
334 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
335 |
0 |
@Override... |
336 |
|
public int hashCode() { |
337 |
0 |
final int prime = 31; |
338 |
0 |
int result = 1; |
339 |
0 |
result = prime * result + ((name == null) ? 0 : name.hashCode()); |
340 |
0 |
return result; |
341 |
|
} |
342 |
|
|
|
|
| 0% |
Uncovered Elements: 27 (27) |
Complexity: 8 |
Complexity Density: 0.53 |
|
343 |
0 |
@Override... |
344 |
|
public boolean equals(Object obj) { |
345 |
0 |
if (this == obj) |
346 |
0 |
return true; |
347 |
0 |
if (obj == null) |
348 |
0 |
return false; |
349 |
0 |
if (getClass() != obj.getClass()) |
350 |
0 |
return false; |
351 |
0 |
MgnlGroovyJCRNode other = (MgnlGroovyJCRNode) obj; |
352 |
0 |
if (name == null) { |
353 |
0 |
if (other.name != null) |
354 |
0 |
return false; |
355 |
|
} else |
356 |
0 |
try { |
357 |
0 |
if (!getIdentifier().equals(other.getIdentifier())) |
358 |
0 |
return false; |
359 |
|
} catch (RepositoryException e) { |
360 |
0 |
return false; |
361 |
|
} |
362 |
0 |
return true; |
363 |
|
} |
364 |
|
|
365 |
|
|
366 |
|
|
367 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 1 |
|
368 |
|
protected class MgnlGroovyWrappingNodeIterator extends DelegatingNodeIterator { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
369 |
3 |
public MgnlGroovyWrappingNodeIterator(NodeIterator iterator) {... |
370 |
3 |
super(iterator); |
371 |
|
} |
372 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
373 |
0 |
@Override... |
374 |
|
public Node nextNode() { |
375 |
0 |
return new MgnlGroovyJCRNode(super.nextNode()); |
376 |
|
} |
377 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
378 |
8 |
@Override... |
379 |
|
public Object next() { |
380 |
8 |
return new MgnlGroovyJCRNode((Node) super.next()); |
381 |
|
} |
382 |
|
} |
383 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 5 |
Complexity Density: 0.83 |
|
384 |
4 |
protected boolean showNode(Node node) {... |
385 |
4 |
try { |
386 |
4 |
if (NodeTypes.MetaData.NAME.equals(node.getPrimaryNodeType().getName()) |
387 |
|
|| node.getName().startsWith("jcr:") |
388 |
|
|| node.getName().startsWith("rep:")) { |
389 |
3 |
return false; |
390 |
|
} |
391 |
|
} catch (RepositoryException e) { |
392 |
0 |
log.error(e.getMessage()); |
393 |
0 |
return false; |
394 |
|
} |
395 |
1 |
return true; |
396 |
|
} |
397 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
398 |
2 |
protected boolean showProperty(Property property) {... |
399 |
2 |
try { |
400 |
2 |
if (property.getName().startsWith("jcr:")) { |
401 |
1 |
return false; |
402 |
|
} |
403 |
|
} catch (RepositoryException e) { |
404 |
0 |
log.error(e.getMessage()); |
405 |
0 |
return false; |
406 |
|
} |
407 |
1 |
return true; |
408 |
|
} |
409 |
|
|
|
|
| 29.4% |
Uncovered Elements: 12 (17) |
Complexity: 8 |
Complexity Density: 0.47 |
|
410 |
5 |
private Object getPropertyValueInternal(Value property) throws RepositoryException {... |
411 |
5 |
switch (property.getType()) { |
412 |
3 |
case PropertyType.STRING: |
413 |
3 |
return property.getString(); |
414 |
0 |
case PropertyType.BOOLEAN: |
415 |
0 |
return property.getBoolean(); |
416 |
0 |
case PropertyType.BINARY: |
417 |
0 |
return property.getBinary().getStream(); |
418 |
0 |
case PropertyType.DATE: |
419 |
0 |
return property.getDate(); |
420 |
0 |
case PropertyType.DECIMAL: |
421 |
0 |
return property.getDecimal(); |
422 |
0 |
case PropertyType.DOUBLE: |
423 |
0 |
return BigDecimal.valueOf(property.getDouble()); |
424 |
2 |
case PropertyType.LONG: |
425 |
2 |
return BigDecimal.valueOf(property.getLong()); |
426 |
0 |
default: |
427 |
0 |
return property.getString(); |
428 |
|
} |
429 |
|
} |
430 |
|
|
431 |
|
} |