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.content2bean;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.util.NodeDataUtil;
38 import info.magnolia.content2bean.impl.Content2BeanTransformerImpl;
39 import info.magnolia.objectfactory.ComponentProvider;
40 import info.magnolia.objectfactory.Components;
41
42 import java.util.Iterator;
43 import java.util.LinkedHashMap;
44 import java.util.Map;
45
46 import javax.jcr.RepositoryException;
47
48 import org.apache.commons.beanutils.BeanUtils;
49
50
51
52
53
54
55
56
57
58
59
60
61
62 @Deprecated
63 public class Content2BeanUtil {
64
65
66
67
68
69
70 public static final Content2BeanTransformerImpl TO_MAP_TRANSFORMER = new ToMapTransformer();
71
72
73
74
75 public static final class DefaultClassTransformer extends Content2BeanTransformerImpl {
76
77
78 private final Class defaultClass;
79
80 public DefaultClassTransformer(Class defaultClass) {
81
82
83 this.defaultClass = defaultClass;
84 }
85
86 @Override
87 protected TypeDescriptorriptor">TypeDescriptor onResolveType(TypeMapping typeMapping, TransformationState state, TypeDescriptor resolvedType, ComponentProvider componentProvider) {
88
89 return resolvedType == null ? typeMapping.getTypeDescriptor(defaultClass) : resolvedType;
90 }
91 }
92
93
94
95
96 public static class ToMapTransformer extends Content2BeanTransformerImpl {
97 public ToMapTransformer() {
98 }
99
100 @Override
101 public TypeDescriptor resolveType(TypeMapping typeMapping, TransformationState state, ComponentProvider componentProvider) throws ClassNotFoundException {
102 return TypeMapping.MAP_TYPE;
103 }
104 }
105
106
107
108
109
110
111 public static Content2BeanProcessor getContent2BeanProcessor() {
112 return Components.getSingleton(Content2BeanProcessor.class);
113 }
114
115
116
117
118
119
120 public static Bean2ContentProcessor getBean2ContentProcessor() {
121 return Bean2ContentProcessor.Factory.getProcessor();
122 }
123
124
125
126
127
128
129 public static TypeMapping getTypeMapping() {
130 return Components.getSingleton(TypeMapping.class);
131 }
132
133
134
135
136
137
138 public static Content2BeanTransformer getContent2BeanTransformer() {
139 return Components.getSingleton(Content2BeanTransformer.class);
140 }
141
142
143
144
145
146 public static Object toBean(Content node) throws Content2BeanException {
147 return toBean(node, false);
148 }
149
150
151
152
153
154 public static Object toBean(Content node, final Class defaultClass) throws Content2BeanException {
155 return toBean(node, false, defaultClass);
156 }
157
158
159
160
161
162 public static Object toBean(Content node, boolean recursive, final Class defaultClass) throws Content2BeanException {
163 return toBean(node, recursive, new Content2BeanTransformerImpl() {
164 @Override
165 protected TypeDescriptorriptor">TypeDescriptor onResolveType(TypeMapping typeMapping, TransformationState state, TypeDescriptor resolvedType, ComponentProvider componentProvider) {
166 if (resolvedType == null && state.getLevel() == 1) {
167 return typeMapping.getTypeDescriptor(defaultClass);
168 }
169 return resolvedType;
170 }
171 });
172 }
173
174
175
176
177
178 public static Object toBean(Content node, boolean recursive) throws Content2BeanException {
179 return toBean(node, recursive, getContent2BeanTransformer());
180 }
181
182
183
184
185
186 public static Object toBean(Content node, boolean recursive, ComponentProvider componentProvider) throws Content2BeanException {
187 return toBean(node, recursive, componentProvider.getComponent(Content2BeanTransformer.class), componentProvider);
188 }
189
190
191
192
193
194
195 public static Object toBean(Content node, boolean recursive, Content2BeanTransformer transformer) throws Content2BeanException {
196 return toBean(node, recursive, transformer, Components.getComponentProvider());
197 }
198
199
200
201
202
203
204 public static Object toBean(Content node, boolean recursive, Content2BeanTransformer transformer, ComponentProvider componentProvider) throws Content2BeanException {
205 return componentProvider.getComponent(Content2BeanProcessor.class).toBean(node, recursive, transformer, componentProvider);
206 }
207
208
209
210
211
212
213
214
215 public static Map toMap(Content node) throws Content2BeanException {
216 return toMap(node, false);
217 }
218
219
220
221
222
223
224
225
226 public static Map toMap(Content node, boolean recursive) throws Content2BeanException {
227 return (Map) setProperties(new LinkedHashMap(), node, recursive);
228 }
229
230
231
232
233 public static <K, V> Map<K, V> toMap(Content node, boolean recursive, Class defaultClass) throws Content2BeanException {
234 return (Map<K, V>) setProperties(new LinkedHashMap(), node, recursive, defaultClass);
235 }
236
237
238
239
240
241
242
243 public static Map toPureMaps(Content node, boolean recursive) throws Content2BeanException {
244 return (Map) toBean(node, recursive, new ToMapTransformer());
245 }
246
247
248
249
250
251 public static Object setProperties(Object bean, Content node) throws Content2BeanException {
252 return setProperties(bean, node, false);
253 }
254
255
256
257
258
259 public static Object setProperties(Object bean, Content node, boolean recursive) throws Content2BeanException {
260 return setProperties(bean, node, recursive, getContent2BeanTransformer());
261 }
262
263
264
265
266 public static Object setProperties(Object bean, Content node, boolean recursive, final Class defaultClass) throws Content2BeanException {
267 return setProperties(bean, node, recursive, new Content2BeanTransformerImpl() {
268 @Override
269 protected TypeDescriptorriptor">TypeDescriptor onResolveType(TypeMapping typeMapping, TransformationState state, TypeDescriptor resolvedType, ComponentProvider componentProvider) {
270 if (resolvedType == null && state.getLevel() == 2) {
271 return typeMapping.getTypeDescriptor(defaultClass);
272 }
273 return resolvedType;
274 }
275 });
276 }
277
278
279
280
281
282 public static Object setProperties(Object bean, Content node, boolean recursive, Content2BeanTransformer transformer) throws Content2BeanException {
283 return setProperties(bean, node, recursive, transformer, Components.getComponentProvider());
284 }
285
286
287
288
289
290 public static Object setProperties(Object bean, Content node, boolean recursive, Content2BeanTransformer transformer, ComponentProvider componentProvider) throws Content2BeanException {
291 return componentProvider.getComponent(Content2BeanProcessor.class).setProperties(bean, node, recursive, transformer, componentProvider);
292 }
293
294
295
296
297
298
299 public static void addCollectionPropertyMapping(Class type, String name, Class mappedType) {
300 TypeMapping mapping = getTypeMapping();
301 mapping.getPropertyTypeDescriptor(type, name).setCollectionEntryType(mapping.getTypeDescriptor(mappedType));
302 }
303
304
305
306
307
308
309 public static void setNodeDatas(Content node, Object bean, String[] excludes) throws Content2BeanException {
310 Map properties = toMap(bean);
311
312 for (int i = 0; i < excludes.length; i++) {
313 String exclude = excludes[i];
314 properties.remove(exclude);
315 }
316
317 setNodeDatas(node, properties);
318 }
319
320
321
322
323
324
325 public static void setNodeDatas(Content node, Object obj) throws Content2BeanException {
326 setNodeDatas(node, toMap(obj));
327 }
328
329
330
331
332
333
334 public static void setNodeDatas(Content node, Map map) throws Content2BeanException {
335 for (Iterator iter = map.keySet().iterator(); iter.hasNext(); ) {
336 String name = (String) iter.next();
337 try {
338 NodeDataUtil.getOrCreate(node, name).setValue(map.get(name).toString());
339 } catch (RepositoryException e) {
340 throw new Content2BeanException("can't set/create nodedata [" + name + "] on node " + node.getHandle(), e);
341 }
342 }
343 }
344
345
346
347
348
349
350 static private Map toMap(Object bean) throws Content2BeanException {
351 try {
352 return BeanUtils.describe(bean);
353 } catch (Exception e) {
354 throw new Content2BeanException("can't read properties from bean", e);
355 }
356 }
357
358
359 }
360