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.core.Content; |
41 |
|
import info.magnolia.cms.core.ItemType; |
42 |
|
import info.magnolia.cms.core.MgnlNodeType; |
43 |
|
import info.magnolia.cms.core.NodeData; |
44 |
|
import info.magnolia.cms.security.AccessDeniedException; |
45 |
|
import info.magnolia.cms.util.ContentWrapper; |
46 |
|
import info.magnolia.jcr.RuntimeRepositoryException; |
47 |
|
|
48 |
|
import java.math.BigDecimal; |
49 |
|
import java.util.Collection; |
50 |
|
import java.util.Iterator; |
51 |
|
|
52 |
|
import javax.jcr.PathNotFoundException; |
53 |
|
import javax.jcr.PropertyType; |
54 |
|
import javax.jcr.RepositoryException; |
55 |
|
import javax.jcr.Value; |
56 |
|
import javax.jcr.ValueFormatException; |
57 |
|
|
58 |
|
import org.apache.commons.collections.IteratorUtils; |
59 |
|
import org.apache.commons.lang.StringUtils; |
60 |
|
import org.slf4j.Logger; |
61 |
|
import org.slf4j.LoggerFactory; |
62 |
|
|
63 |
|
|
64 |
|
@link |
65 |
|
|
66 |
|
|
67 |
|
@link |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
@link |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
@link |
93 |
|
@link@link |
94 |
|
@link |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
@deprecated@link |
120 |
|
|
121 |
|
@Deprecated |
|
|
| 34.7% |
Uncovered Elements: 130 (199) |
Complexity: 62 |
Complexity Density: 0.48 |
|
122 |
|
public class MgnlGroovyNode extends ContentWrapper { |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
124 |
1 |
static {... |
125 |
|
|
126 |
1 |
setMetaClass(GroovySystem.getMetaClassRegistry().getMetaClass(MgnlGroovyNode.class), MgnlGroovyNode.class); |
127 |
|
} |
128 |
|
|
129 |
|
protected static final Logger log = LoggerFactory.getLogger(MgnlGroovyNode.class); |
130 |
|
|
131 |
|
private String name; |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
133 |
16 |
public MgnlGroovyNode(Content content) {... |
134 |
16 |
super(content); |
135 |
16 |
this.name = content.getName(); |
136 |
|
} |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
138 |
1 |
protected static void setMetaClass(final MetaClass metaClass, Class nodeClass) {... |
139 |
1 |
final MetaClass newMetaClass = new DelegatingMetaClass(metaClass) { |
140 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
141 |
0 |
@Override... |
142 |
|
public Object getAttribute(final Object object, final String attribute) { |
143 |
0 |
MgnlGroovyNode n = (MgnlGroovyNode) object; |
144 |
0 |
return n.get("@" + attribute); |
145 |
|
} |
146 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 4 |
Complexity Density: 0.67 |
|
147 |
0 |
@Override... |
148 |
|
public void setAttribute(final Object object, final String attribute, final Object newValue) { |
149 |
0 |
MgnlGroovyNode n = (MgnlGroovyNode) object; |
150 |
0 |
try { |
151 |
0 |
n.setNodeData(attribute, newValue); |
152 |
|
} |
153 |
|
catch (AccessDeniedException e) { |
154 |
0 |
log.error(e.getMessage()); |
155 |
|
} |
156 |
|
catch (PathNotFoundException e) { |
157 |
0 |
log.error(e.getMessage()); |
158 |
|
} |
159 |
|
catch (RepositoryException e) { |
160 |
0 |
log.error(e.getMessage()); |
161 |
|
} |
162 |
|
} |
163 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
164 |
0 |
@Override... |
165 |
|
public Object getProperty(Object object, String property) { |
166 |
0 |
if (object instanceof MgnlGroovyNode) { |
167 |
0 |
MgnlGroovyNode n = (MgnlGroovyNode) object; |
168 |
0 |
return n.get(property); |
169 |
|
} |
170 |
0 |
return super.getProperty(object, property); |
171 |
|
} |
172 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 6 |
Complexity Density: 0.5 |
|
173 |
0 |
@Override... |
174 |
|
public void setProperty(Object object, String property, Object newValue) { |
175 |
0 |
String attribute = null; |
176 |
0 |
if (property.startsWith("@")) { |
177 |
0 |
attribute = property.substring(1); |
178 |
|
} else { |
179 |
0 |
attribute = property; |
180 |
|
} |
181 |
0 |
MgnlGroovyNode n = (MgnlGroovyNode) object; |
182 |
0 |
try { |
183 |
|
|
184 |
0 |
if (newValue instanceof Integer) { |
185 |
0 |
newValue = ((Integer) newValue).longValue(); |
186 |
|
} |
187 |
0 |
n.setNodeData(attribute, newValue); |
188 |
|
} |
189 |
|
catch (AccessDeniedException e) { |
190 |
0 |
log.error(e.getMessage()); |
191 |
|
} |
192 |
|
catch (PathNotFoundException e) { |
193 |
0 |
log.error(e.getMessage()); |
194 |
|
} |
195 |
|
catch (RepositoryException e) { |
196 |
0 |
log.error(e.getMessage()); |
197 |
|
} |
198 |
|
} |
199 |
|
}; |
200 |
1 |
GroovySystem.getMetaClassRegistry().setMetaClass(nodeClass, newMetaClass); |
201 |
|
} |
202 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
203 |
2 |
public Iterator<Content> iterator() {... |
204 |
2 |
return getChainedChildrenIterator(); |
205 |
|
} |
206 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
207 |
0 |
public String name() {... |
208 |
0 |
return name; |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
@param |
215 |
|
@return |
216 |
|
|
|
|
| 68.4% |
Uncovered Elements: 12 (38) |
Complexity: 11 |
Complexity Density: 0.5 |
|
217 |
8 |
public Object get(String key) {... |
218 |
8 |
if (key != null && key.charAt(0) == '@') { |
219 |
3 |
String attributeName = key.substring(1); |
220 |
3 |
return getNodeDataValue(attributeName); |
221 |
|
} |
222 |
5 |
if ("parent".equals(key)) { |
223 |
0 |
try { |
224 |
0 |
return getParent(); |
225 |
|
} catch (RepositoryException e) { |
226 |
0 |
return null; |
227 |
|
} |
228 |
|
} |
229 |
5 |
if ("children".equals(key)) { |
230 |
2 |
Collection<Content> retVal = getChildren(); |
231 |
2 |
if (!isContentNodeType()) { |
232 |
0 |
retVal.addAll(getChildren(MgnlNodeType.NT_CONTENTNODE)); |
233 |
|
} |
234 |
2 |
return retVal; |
235 |
|
} |
236 |
3 |
if ("metaData".equals(key)) { |
237 |
0 |
return getMetaData(); |
238 |
|
} |
239 |
3 |
if ("nodeData".equals(key)) { |
240 |
0 |
return getNodeDataCollection(); |
241 |
|
} |
242 |
3 |
if ("name".equals(key)) { |
243 |
1 |
return name; |
244 |
|
} |
245 |
2 |
Content retVal = getByName(key); |
246 |
2 |
if (retVal != null) { |
247 |
0 |
return retVal; |
248 |
|
} |
249 |
|
|
250 |
2 |
return getNodeDataValue(key); |
251 |
|
} |
252 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
253 |
10 |
@Override... |
254 |
|
protected Content wrap(Content node) { |
255 |
10 |
return new MgnlGroovyNode(node); |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
@link |
261 |
|
|
262 |
|
@link@link |
263 |
|
@link@link |
264 |
|
@link@link |
265 |
|
@link@link |
266 |
|
@link@link |
267 |
|
@link |
268 |
|
|
269 |
|
|
|
|
| 51.4% |
Uncovered Elements: 17 (35) |
Complexity: 11 |
Complexity Density: 0.35 |
|
270 |
5 |
protected Object getNodeDataValue(String attributeName) {... |
271 |
5 |
Collection<NodeData> data = getNodeDataCollection(attributeName); |
272 |
5 |
Iterator<NodeData> it = data.iterator(); |
273 |
5 |
if (!it.hasNext()) { |
274 |
0 |
return null; |
275 |
|
} |
276 |
5 |
NodeData nodeData = it.next(); |
277 |
5 |
Value propertyValue = nodeData.getValue(); |
278 |
5 |
Object value = null; |
279 |
5 |
if (propertyValue != null) { |
280 |
5 |
try { |
281 |
5 |
switch (propertyValue.getType()) { |
282 |
3 |
case PropertyType.STRING: |
283 |
3 |
value = propertyValue.getString(); |
284 |
3 |
break; |
285 |
0 |
case PropertyType.BINARY: |
286 |
0 |
value = propertyValue; |
287 |
0 |
break; |
288 |
0 |
case PropertyType.DATE: |
289 |
0 |
value = propertyValue.getDate(); |
290 |
0 |
break; |
291 |
0 |
case PropertyType.DOUBLE: |
292 |
0 |
value = BigDecimal.valueOf(propertyValue.getDouble()); |
293 |
0 |
break; |
294 |
2 |
case PropertyType.LONG: |
295 |
2 |
value = BigDecimal.valueOf(propertyValue.getLong()); |
296 |
2 |
break; |
297 |
0 |
default: |
298 |
0 |
value = propertyValue.getString(); |
299 |
|
} |
300 |
|
} catch (ValueFormatException e) { |
301 |
0 |
log.warn(e.getMessage()); |
302 |
|
} catch (IllegalStateException e) { |
303 |
0 |
log.warn(e.getMessage()); |
304 |
|
} catch (RepositoryException e) { |
305 |
0 |
log.warn(e.getMessage()); |
306 |
|
} |
307 |
|
} |
308 |
5 |
return value; |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
@param |
315 |
|
@return |
316 |
|
|
|
|
| 30% |
Uncovered Elements: 7 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
317 |
2 |
protected Content getByName(String name) {... |
318 |
2 |
for (Iterator<Content> iter = getChainedChildrenIterator(); iter.hasNext();) { |
319 |
0 |
Content childNode = iter.next(); |
320 |
0 |
String childNodeName = childNode.getName(); |
321 |
0 |
if (name.equals(childNodeName)) { |
322 |
0 |
return childNode; |
323 |
|
} |
324 |
|
} |
325 |
2 |
return null; |
326 |
|
} |
327 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 4 |
Complexity Density: 0.31 |
|
328 |
0 |
@Override... |
329 |
|
public String toString() { |
330 |
0 |
StringBuilder retVal = new StringBuilder(); |
331 |
0 |
Iterator<Content> iterator = getChainedChildrenIterator(); |
332 |
0 |
retVal.append("(+) " + name + "\n"); |
333 |
0 |
while (iterator.hasNext()) { |
334 |
0 |
Content c = iterator.next(); |
335 |
0 |
if (hasMoreChildren(c)) { |
336 |
0 |
retVal.append("\t(+) "); |
337 |
|
} else { |
338 |
0 |
retVal.append("\t(-) "); |
339 |
|
} |
340 |
0 |
retVal.append(c.getName() + "\n"); |
341 |
|
} |
342 |
|
|
343 |
0 |
if (!getNodeDataCollection().isEmpty()) { |
344 |
0 |
for (NodeData nodeData : getNodeDataCollection()) { |
345 |
0 |
retVal.append("\t\t* " + nodeData.getName() + ": [" |
346 |
|
+ StringUtils.abbreviate(nodeData.getString(), 80) |
347 |
|
+ "]\n"); |
348 |
|
} |
349 |
|
|
350 |
|
} |
351 |
|
|
352 |
0 |
return retVal.toString(); |
353 |
|
} |
354 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
355 |
4 |
@SuppressWarnings("unchecked")... |
356 |
|
private Iterator<Content> getChainedChildrenIterator() { |
357 |
4 |
if (isContentNodeType()) { |
358 |
4 |
return getChildren().iterator(); |
359 |
|
} |
360 |
0 |
return IteratorUtils.chainedIterator(getChildren().iterator(), getChildren(ItemType.CONTENTNODE).iterator()); |
361 |
|
} |
362 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
363 |
0 |
private boolean hasMoreChildren(Content c) {... |
364 |
0 |
return c.hasChildren() || c.hasChildren(ItemType.CONTENTNODE.getSystemName()); |
365 |
|
} |
366 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
367 |
0 |
@Override... |
368 |
|
public int hashCode() { |
369 |
0 |
final int prime = 31; |
370 |
0 |
int result = 1; |
371 |
0 |
result = prime * result + ((name == null) ? 0 : name.hashCode()); |
372 |
0 |
return result; |
373 |
|
} |
374 |
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 7 |
Complexity Density: 0.54 |
|
375 |
0 |
@Override... |
376 |
|
public boolean equals(Object obj) { |
377 |
0 |
if (this == obj) |
378 |
0 |
return true; |
379 |
0 |
if (obj == null) |
380 |
0 |
return false; |
381 |
0 |
if (getClass() != obj.getClass()) |
382 |
0 |
return false; |
383 |
0 |
MgnlGroovyNode other = (MgnlGroovyNode) obj; |
384 |
0 |
if (name == null) { |
385 |
0 |
if (other.name != null) |
386 |
0 |
return false; |
387 |
0 |
} else if (!getHandle().equals(other.getHandle())) |
388 |
0 |
return false; |
389 |
0 |
return true; |
390 |
|
} |
391 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
392 |
6 |
private boolean isContentNodeType() {... |
393 |
6 |
try { |
394 |
6 |
return MgnlNodeType.NT_CONTENTNODE.equals(getItemType().getSystemName()); |
395 |
|
|
396 |
|
} catch (RepositoryException e) { |
397 |
0 |
throw new RuntimeRepositoryException(e); |
398 |
|
} |
399 |
|
} |
400 |
|
|
401 |
|
} |