View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.content2bean.impl;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.util.ContentUtil;
38  import info.magnolia.cms.util.SystemContentWrapper;
39  import info.magnolia.content2bean.Content2BeanException;
40  import info.magnolia.content2bean.Content2BeanTransformer;
41  import info.magnolia.content2bean.PropertyTypeDescriptor;
42  import info.magnolia.content2bean.TransformationState;
43  import info.magnolia.content2bean.TypeDescriptor;
44  import info.magnolia.content2bean.TypeMapping;
45  import info.magnolia.objectfactory.Classes;
46  import info.magnolia.objectfactory.Components;
47  import org.apache.commons.beanutils.BeanUtilsBean;
48  import org.apache.commons.beanutils.MethodUtils;
49  import org.apache.commons.beanutils.PropertyUtils;
50  import org.apache.commons.beanutils.PropertyUtilsBean;
51  import org.apache.commons.lang.LocaleUtils;
52  import org.apache.commons.lang.StringUtils;
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  import javax.jcr.RepositoryException;
57  import java.lang.reflect.Method;
58  import java.util.Collection;
59  import java.util.Iterator;
60  import java.util.Locale;
61  import java.util.Map;
62  
63  /**
64   * Concrete implementation using reflection and adder methods.
65   *
66   * @author philipp
67   * @version $Id: Content2BeanTransformerImpl.java 32667 2010-03-13 00:37:06Z gjoseph $
68   */
69  public class Content2BeanTransformerImpl implements Content2BeanTransformer, Content.ContentFilter {
70  
71      private static final Logger log = LoggerFactory.getLogger(Content2BeanTransformerImpl.class);
72  
73      private final BeanUtilsBean beanUtilsBean;
74  
75      public Content2BeanTransformerImpl() {
76          super();
77  
78          // We use non-static BeanUtils conversion, so we can
79          // * use our custom ConvertUtilsBean
80          // * control converters (convertUtilsBean.register()) - we can register them here, locally, as opposed to a global ConvertUtils.register()
81          final EnumAwareConvertUtilsBean convertUtilsBean = new EnumAwareConvertUtilsBean();
82  
83          // de-register the converter for Class, we do our own conversion in convertPropertyValue()
84          convertUtilsBean.deregister(Class.class);
85  
86          this.beanUtilsBean = new BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean());
87      }
88  
89      /**
90       * Resolves in this order
91       * <ul>
92       * <li> checks the class property of the current node
93       * <li> calls onResolve subclasses should override
94       * <li> reflection on the parent bean
95       * <li> in case of a collection/map type call getClassForCollectionProperty
96       * <li> otherwise use a Map
97       * </ul>
98       */
99      public TypeDescriptor resolveType(TransformationState state) throws ClassNotFoundException {
100         TypeDescriptor typeDscr = null;
101         Content node = state.getCurrentContent();
102 
103         try {
104             if (node.hasNodeData("class")) {
105                 String className = node.getNodeData("class").getString();
106                 if (StringUtils.isBlank(className)) {
107                     throw new ClassNotFoundException("(no value for class property)");
108                 }
109                 Class<?> clazz = Classes.getClassFactory().forName(className);
110                 typeDscr = getTypeMapping().getTypeDescriptor(clazz);
111             }
112         }
113         catch (RepositoryException e) {
114             // ignore
115             log.warn("can't read class property", e);
116         }
117 
118         if (typeDscr == null && state.getLevel() > 1) {
119             TypeDescriptor parentTypeDscr = state.getCurrentType();
120             PropertyTypeDescriptor propDscr;
121 
122             if (parentTypeDscr.isMap() || parentTypeDscr.isCollection()) {
123                 if (state.getLevel() > 2) {
124                     // this is not necesserely the parent node of the current
125                     String mapProperyName = state.peekContent(1).getName();
126                     propDscr = state.peekType(1).getPropertyTypeDescriptor(mapProperyName);
127                     if (propDscr != null) {
128                         typeDscr = propDscr.getCollectionEntryType();
129                     }
130                 }
131             } else {
132                 propDscr = state.getCurrentType().getPropertyTypeDescriptor(node.getName());
133                 if (propDscr != null) {
134                     typeDscr = propDscr.getType();
135                 }
136             }
137         }
138 
139         typeDscr = onResolveType(state, typeDscr);
140 
141         if (typeDscr != null) {
142             // might be that the factory util defines a default implementation for interfaces
143             final Class<?> type = typeDscr.getType();
144             typeDscr = getTypeMapping().getTypeDescriptor(Components.getComponentProvider().getImplementation(type));
145 
146             // now that we know the property type we should delegate to the custom transformer if any defined
147             Content2BeanTransformer customTransformer = typeDscr.getTransformer();
148             if (customTransformer != null && customTransformer != this) {
149                 TypeDescriptor typeFoundByCustomTransformer = customTransformer.resolveType(state);
150                 // if no specific type has been provided by the
151                 if (typeFoundByCustomTransformer != TypeMapping.MAP_TYPE) {
152                     // might be that the factory util defines a default implementation for interfaces
153                     Class<?> implementation = Components.getComponentProvider().getImplementation(typeFoundByCustomTransformer.getType());
154                     typeDscr = getTypeMapping().getTypeDescriptor(implementation);
155                 }
156             }
157         }
158 
159         if (typeDscr == null || typeDscr.needsDefaultMapping()) {
160             if (typeDscr == null) {
161                 log.debug("was not able to resolve type for node [{}] will use a map", node);
162             }
163             typeDscr = TypeMapping.MAP_TYPE;
164         }
165 
166         log.debug("{} --> {}", node.getHandle(), typeDscr.getType());
167 
168         return typeDscr;
169     }
170 
171 
172     /**
173      * Called once the type should have been resolved. The resolvedType might be
174      * null if no type has been resolved. After the call the FactoryUtil and
175      * custom transformers are used to get the final type.
176      * TODO - check javadoc
177      */
178     protected TypeDescriptor onResolveType(TransformationState state, TypeDescriptor resolvedType) {
179         return resolvedType;
180     }
181 
182     public Collection<Content> getChildren(Content node) {
183         return node.getChildren(this);
184     }
185 
186     /**
187      * Process all nodes except metadata
188      */
189     public boolean accept(Content content) {
190         return ContentUtil.EXCLUDE_META_DATA_CONTENT_FILTER.accept(content);
191     }
192 
193     /**
194      * Do not set class property. In case of a map/collection try to use adder method.
195      */
196     public void setProperty(TransformationState state, PropertyTypeDescriptor descriptor, Map<String, Object> values) {
197         TypeMapping mapping = getTypeMapping();
198 
199         String propertyName = descriptor.getName();
200         if (propertyName.equals("class")) {
201             return;
202         }
203         Object value = values.get(propertyName);
204         Object bean = state.getCurrentBean();
205 
206         if (propertyName.equals("content") && value == null) {
207             value = new SystemContentWrapper(state.getCurrentContent());
208         } else if (propertyName.equals("name") && value == null) {
209             value = state.getCurrentContent().getName();
210         } else if (propertyName.equals("className") && value == null) {
211             value = values.get("class");
212         }
213 
214         // do no try to set a bean-property that has no corresponding node-property
215         //else if (!values.containsKey(propertyName)) {
216         if (value == null) {
217             return;
218         }
219 
220         log.debug("try to set {}.{} with value {}", new Object[]{bean, propertyName, value});
221 
222         // if the parent bean is a map, we can't guess the types.
223         if (!(bean instanceof Map)) {
224             try {
225                 PropertyTypeDescriptor dscr = mapping.getPropertyTypeDescriptor(bean.getClass(), propertyName);
226                 if (dscr.getType() != null) {
227 
228                     // try to use an adder method for a Collection property of the bean
229                     if (dscr.isCollection() || dscr.isMap()) {
230                         log.debug("{} is of type collection, map or /array", propertyName);
231                         Method method = dscr.getAddMethod();
232 
233                         if (method != null) {
234                             log.debug("clearing the current content of the collection/map");
235                             try {
236                                 Object col = PropertyUtils.getProperty(bean, propertyName);
237                                 if (col != null) {
238                                     MethodUtils.invokeExactMethod(col, "clear", new Object[]{});
239                                 }
240                             }
241                             catch (Exception e) {
242                                 log.debug("no clear method found on collection {}", propertyName);
243                             }
244 
245                             Class<?> entryClass = dscr.getCollectionEntryType().getType();
246 
247                             log.debug("will add values by using adder method {}", method.getName());
248                             for (Iterator<Object> iter = ((Map<Object, Object>) value).keySet().iterator(); iter.hasNext();) {
249                                 Object key = iter.next();
250                                 Object entryValue = ((Map<Object, Object>) value).get(key);
251                                 entryValue = convertPropertyValue(entryClass, entryValue);
252                                 if (dscr.isCollection()) {
253                                     log.debug("will add value {}", entryValue);
254                                     method.invoke(bean, new Object[]{entryValue});
255                                 }
256                                 // is a map
257                                 else {
258                                     log.debug("will add key {} with value {}", key, entryValue);
259                                     method.invoke(bean, new Object[]{key, entryValue});
260                                 }
261                             }
262 
263                             return;
264                         } else {
265                             log.debug("no add method found for property {}", propertyName);
266                             if (dscr.isCollection()) {
267                                 log.debug("transform the valus to a collection", propertyName);
268                                 value = ((Map<Object, Object>) value).values();
269                             }
270                         }
271                     } else {
272                         value = convertPropertyValue(dscr.getType().getType(), value);
273                     }
274                 }
275             }
276             catch (Exception e) {
277                 // do it better
278                 log.error("Can't set property [" + propertyName + "] to value [" + value + "] in bean [" + bean.getClass().getName() + "] for node " + state.getCurrentContent().getHandle() + " due to {}", e.toString());
279                 log.debug("stacktrace", e);
280             }
281         }
282 
283         try {
284             // This uses the converters registered in beanUtilsBean.convertUtilsBean (see constructor of this class)
285             // If a converter is registered, beanutils will convert value.toString(), not the value object as-is.
286             // If no converter is registered, then the value Object is set as-is.
287             // If convertPropertyValue() already converted this value, you'll probably want to unregister the beanutils converter.
288             // some conversions like string to class. Performance of PropertyUtils.setProperty() would be better
289             beanUtilsBean.setProperty(bean, propertyName, value);
290 
291             // TODO this also does things we probably don't want/need, i.e nested and indexed properties
292 
293         } catch (Exception e) {
294             // do it better
295             log.error("Can't set property [" + propertyName + "] to value [" + value + "] in bean [" + bean.getClass().getName() + "] for node " + state.getCurrentContent().getHandle() + " due to {}", e.toString());
296             log.debug("stacktrace", e);
297         }
298 
299     }
300 
301     /**
302      * Most of the conversion is done by the BeanUtils.
303      * TODO don't use bean utils conversion since it can't be used for the adder methods
304      */
305     public Object convertPropertyValue(Class<?> propertyType, Object value) throws Content2BeanException {
306         if (Class.class.equals(propertyType)) {
307             try {
308                 return Classes.getClassFactory().forName(value.toString());
309             } catch (ClassNotFoundException e) {
310                 log.error(e.getMessage());
311                 throw new Content2BeanException(e);
312             }
313         }
314 
315         if (Locale.class.equals(propertyType)) {
316             if (value instanceof String) {
317                 String localeStr = (String) value;
318                 if (StringUtils.isNotEmpty(localeStr)) {
319                     return LocaleUtils.toLocale(localeStr);
320                 }
321             }
322         }
323 
324         if ((Collection.class.equals(propertyType)) && (value instanceof Map)) {
325             return ((Map) value).values();
326         }
327 
328         // this is mainly the case when we are flattening node hierarchies
329         if ((String.class.equals(propertyType)) && (value instanceof Map) && (((Map) value).size() == 1)) {
330             return ((Map) value).values().iterator().next();
331         }
332 
333         return value;
334     }
335 
336     /**
337      * Use the factory util to instantiate. This is useful to get default implementation of interfaces
338      */
339     public Object newBeanInstance(TransformationState state, Map properties) throws Content2BeanException {
340         // we try first to use conversion (Map --> primitive type)
341         // this is the case when we flattening the hierarchy?
342         Object bean = convertPropertyValue(state.getCurrentType().getType(), properties);
343         // were the properties transformed?
344         if (bean == properties) {
345             try {
346                 bean = Components.getComponentProvider().newInstance(state.getCurrentType().getType());
347             }
348             catch (Throwable e) {
349                 throw new Content2BeanException(e);
350             }
351         }
352         return bean;
353     }
354 
355     /**
356      * Call init method if present
357      */
358     public void initBean(TransformationState state, Map properties) throws Content2BeanException {
359         Object bean = state.getCurrentBean();
360 
361         Method init;
362         try {
363             init = bean.getClass().getMethod("init", new Class[]{});
364             try {
365                 init.invoke(bean); // no parameters
366             }
367             catch (Exception e) {
368                 throw new Content2BeanException("can't call init method", e);
369             }
370         }
371         catch (SecurityException e) {
372             return;
373         }
374         catch (NoSuchMethodException e) {
375             return;
376         }
377         log.debug("{} is initialized", bean);
378     }
379 
380     public TransformationState newState() {
381         return Components.getComponentProvider().newInstance(TransformationState.class);
382     }
383 
384     /**
385      * Returns the default mapping.
386      */
387     public TypeMapping getTypeMapping() {
388         return TypeMapping.Factory.getDefaultMapping();
389     }
390 
391 }