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