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