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