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.core.NodeData;
38  import info.magnolia.cms.util.ExtendingContentWrapper;
39  import info.magnolia.cms.util.NodeDataUtil;
40  import info.magnolia.content2bean.Content2BeanException;
41  import info.magnolia.content2bean.Content2BeanProcessor;
42  import info.magnolia.content2bean.Content2BeanTransformer;
43  import info.magnolia.content2bean.PropertyTypeDescriptor;
44  import info.magnolia.content2bean.TransformationState;
45  import info.magnolia.content2bean.TypeDescriptor;
46  
47  import java.util.Collection;
48  import java.util.LinkedHashMap;
49  import java.util.Map;
50  
51  import javax.jcr.RepositoryException;
52  
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  /**
57   * Contains the logic for traversing the hierarchy and do the calls to the transformer.
58   * @author philipp
59   * @version $Id: Content2BeanProcessorImpl.java 36506 2010-08-18 12:05:34Z had $
60   *
61   */
62  public class Content2BeanProcessorImpl implements Content2BeanProcessor {
63      private static final Logger log = LoggerFactory.getLogger(Content2BeanProcessorImpl.class);
64  
65      private boolean forceCreation = true;
66  
67      public Object toBean(Content node, boolean recursive, final Content2BeanTransformer transformer) throws Content2BeanException{
68         return toBean(new ExtendingContentWrapper(node), recursive, transformer, transformer.newState());
69      }
70  
71      protected Object toBean(Content node, boolean recursive, Content2BeanTransformer transformer, TransformationState state) throws Content2BeanException{
72  
73          state.pushContent(node);
74  
75          TypeDescriptor type = null;
76          try {
77              type = transformer.resolveType(state);
78          }
79          catch (Throwable e) {
80              if(isForceCreation()){
81                  log.warn("can't resolve class for node " +  node.getHandle(), e);
82              }
83              else{
84                  throw new Content2BeanException("can't resolve class for node " +  node.getHandle(), e);
85              }
86          }
87  
88          Object bean = null;
89          if(type != null){
90              state.pushType(type);
91  
92              transformer = resolveTransformer(type, transformer);
93  
94              Map<String, Object> values = toMap(node, recursive, transformer, state);
95  
96              try {
97                  bean = transformer.newBeanInstance(state, values);
98              }
99              catch (Throwable e) {
100                 if(isForceCreation()){
101                     log.warn("Can't instantiate bean for " +  node.getHandle(), e);
102                 }
103                 else{
104                     throw new Content2BeanException("Can't instantiate bean for " +  node.getHandle(), e);
105                 }
106             }
107 
108             if(bean != null){
109                 state.pushBean(bean);
110 
111                 setProperties(values, transformer, state);
112 
113                 transformer.initBean(state, values);
114 
115                 state.popBean();
116             }
117             else{
118                 if(forceCreation){
119                     log.warn("can't instantiate bean of type " + type.getType().getName() + " for node " + node.getHandle());
120                 }
121                 else{
122                     throw new Content2BeanException("can't instantiate bean of type " + type.getType().getName());
123                 }
124             }
125 
126             state.popType();
127         }
128         state.popContent();
129 
130         return bean;
131     }
132 
133     public Object setProperties(final Object bean, Content node, boolean recursive, Content2BeanTransformer transformer) throws Content2BeanException {
134         // enable extending feature
135         node = new ExtendingContentWrapper(node);
136 
137         TransformationState state = transformer.newState();
138         state.pushBean(bean);
139         state.pushContent(node);
140 
141         TypeDescriptor type = transformer.getTypeMapping().getTypeDescriptor(bean.getClass());
142 
143         state.pushType(type);
144 
145         transformer = resolveTransformer(type, transformer);
146 
147         Map<String, Object> values = toMap(node, recursive, transformer, state);
148 
149         setProperties(values, transformer, state);
150 
151         transformer.initBean(state, values);
152 
153         state.popBean();
154         state.popType();
155         state.popContent();
156 
157         return bean;
158     }
159 
160     /**
161      * Transforms the children of provided content into a map.
162      */
163     protected Map<String, Object> toMap(Content node, boolean recursive, Content2BeanTransformer transformer, TransformationState state) throws Content2BeanException {
164         Map<String, Object> map = new LinkedHashMap<String, Object>();
165         for (NodeData nd : node.getNodeDataCollection()) {
166             Object val = NodeDataUtil.getValueObject(nd);
167             if (val != null) {
168                 map.put(nd.getName(), val);
169             }
170         }
171 
172         if(recursive){
173             final Collection<Content> children = transformer.getChildren(node);
174             for (Content childNode : children) {
175                 // in case the the class can not get resolved we can use now
176                 // the parent bean to resolve the class
177 
178                 Object childBean = toBean(childNode, true, transformer, state);
179                 // can be null if forceCreation is true
180                 if(childBean != null){
181                     String name = childNode.getName();
182                     try {
183                         if(childNode.getIndex() > 1){
184                             name += childNode.getIndex();
185                         }
186                     }
187                     catch (RepositoryException e) {
188                         log.error("can't read index of the node [" + childNode + "]", e);
189                     }
190                     map.put(name, childBean);
191                 }
192             }
193         }
194 
195         return map;
196     }
197 
198     /**
199      * Populates the properties of the bean with values from the map.
200      * TODO in case the bean is a map / collection the transfomer.setProperty() method should be called too
201      * TODO if the bean has not a certain property but a value is present, transformer.setProperty() should be called with a fake property descriptor
202      */
203     protected void setProperties(Map<String, Object> values, final Content2BeanTransformer transformer, TransformationState state) throws Content2BeanException {
204         Object bean = state.getCurrentBean();
205         log.debug("will populate bean {} with the values {}", bean.getClass().getName(), values);
206 
207         if(bean instanceof Map){
208             ((Map<String, Object>)bean).putAll(values);
209         }
210 
211         if(bean instanceof Collection){
212             ((Collection<Object>)bean).addAll(values.values());
213         }
214 
215         else{
216             TypeDescriptor beanTypeDescriptor = transformer.getTypeMapping().getTypeDescriptor(bean.getClass());
217             final Collection<PropertyTypeDescriptor> dscrs = beanTypeDescriptor.getPropertyDescriptors().values();
218 
219             for (PropertyTypeDescriptor descriptor : dscrs) {
220                 transformer.setProperty(state, descriptor, values);
221             }
222         }
223     }
224 
225     protected Content2BeanTransformer resolveTransformer(TypeDescriptor type,
226             Content2BeanTransformer transformer) {
227         Content2BeanTransformer customTransformer = type.getTransformer();
228         if(customTransformer != null){
229             transformer = customTransformer;
230         }
231         return transformer;
232     }
233 
234     public boolean isForceCreation() {
235         return this.forceCreation;
236     }
237 
238 
239     public void setForceCreation(boolean handleExceptions) {
240         this.forceCreation = handleExceptions;
241     }
242 
243 }