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