View Javadoc

1   /**
2    * This file Copyright (c) 2012 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.jcr.node2bean.impl;
35  
36  import info.magnolia.cms.core.MgnlNodeType;
37  import info.magnolia.jcr.iterator.FilteringPropertyIterator;
38  import info.magnolia.jcr.node2bean.Node2BeanException;
39  import info.magnolia.jcr.node2bean.Node2BeanProcessor;
40  import info.magnolia.jcr.node2bean.Node2BeanTransformer;
41  import info.magnolia.jcr.node2bean.PropertyTypeDescriptor;
42  import info.magnolia.jcr.node2bean.TransformationState;
43  import info.magnolia.jcr.node2bean.TypeDescriptor;
44  import info.magnolia.jcr.node2bean.TypeMapping;
45  import info.magnolia.jcr.predicate.AbstractPredicate;
46  import info.magnolia.jcr.util.NodeUtil;
47  import info.magnolia.jcr.util.PropertyUtil;
48  import info.magnolia.jcr.wrapper.ExtendingNodeWrapper;
49  import info.magnolia.objectfactory.ComponentProvider;
50  import info.magnolia.objectfactory.Components;
51  
52  import java.util.Collection;
53  import java.util.LinkedHashMap;
54  import java.util.Map;
55  
56  import javax.inject.Inject;
57  import javax.inject.Singleton;
58  import javax.jcr.Node;
59  import javax.jcr.NodeIterator;
60  import javax.jcr.Property;
61  import javax.jcr.PropertyIterator;
62  import javax.jcr.RepositoryException;
63  
64  import org.slf4j.Logger;
65  import org.slf4j.LoggerFactory;
66  
67  /**
68   * Contains the logic for traversing the hierarchy and do the calls to the transformer.
69   */
70  @Singleton
71  public class Node2BeanProcessorImpl implements Node2BeanProcessor {
72      private static final Logger log = LoggerFactory.getLogger(Node2BeanProcessorImpl.class);
73  
74      private final TypeMapping typeMapping;
75  
76      private final Node2BeanTransformer defaultTransformer;
77  
78      private boolean forceCreation = true;
79  
80      @Inject
81      public Node2BeanProcessorImpl(TypeMapping typeMapping, Node2BeanTransformer transformer) {
82          this.typeMapping = typeMapping;
83          this.defaultTransformer = transformer;
84      }
85  
86      @Override
87      public Object toBean(Node node) throws Node2BeanException, RepositoryException {
88          return toBean(node, true, defaultTransformer, defaultTransformer.newState(), Components.getComponentProvider());
89      }
90  
91      @Override
92      public Object toBean(Node node, final Class<?> defaultClass) throws Node2BeanException, RepositoryException {
93          return toBean(node, true, new Node2BeanTransformerImpl() {
94              @Override
95              protected TypeDescriptor onResolveType(TypeMapping mapping, TransformationState state, TypeDescriptor resolvedType, ComponentProvider componentProvider) {
96                  if(resolvedType==null && state.getLevel() == 1){
97                      return mapping.getTypeDescriptor(defaultClass);
98                  }
99                  return resolvedType;
100             }
101         }, defaultTransformer.newState(), Components.getComponentProvider());
102     }
103 
104     @Override
105     public Object toBean(Node node, boolean recursive, final Node2BeanTransformer transformer, ComponentProvider componentProvider) throws Node2BeanException, RepositoryException {
106         return toBean(node, recursive, transformer, transformer.newState(), componentProvider);
107     }
108 
109     protected Object toBean(Node node, boolean recursive, Node2BeanTransformer transformer, TransformationState state, ComponentProvider componentProvider) throws Node2BeanException, RepositoryException{
110         if (!NodeUtil.isWrappedWith(node, ExtendingNodeWrapper.class)) {
111             node = new ExtendingNodeWrapper(node);
112         }
113         state.pushNode(node);
114         TypeDescriptor type = null;
115         try {
116             type = transformer.resolveType(typeMapping, state, componentProvider);
117         } catch (Throwable e) {
118             if(isForceCreation()){
119                 log.warn("can't resolve class for node " +  node.getPath(), e);
120             } else {
121                 throw new Node2BeanException("can't resolve class for node " +  node.getPath(), e);
122             }
123         }
124 
125         Object bean = null;
126         if (type != null) {
127             state.pushType(type);
128 
129             transformer = resolveTransformer(type, transformer);
130 
131             Map<String, Object> values = toMap(node, recursive, transformer, state, componentProvider);
132 
133             try {
134                 bean = transformer.newBeanInstance(state, values, componentProvider);
135             } catch (Throwable e) {
136                 if(isForceCreation()){
137                     log.warn("Can't instantiate bean for " +  node.getPath(), e);
138                 } else {
139                     throw new Node2BeanException("Can't instantiate bean for " +  node.getPath(), e);
140                 }
141             }
142 
143             if (bean != null) {
144                 state.pushBean(bean);
145 
146                 setProperties(values, transformer, state);
147 
148                 transformer.initBean(state, values);
149 
150                 bean = state.getCurrentBean();
151 
152                 state.popBean();
153             } else {
154                 if(forceCreation){
155                     log.warn("can't instantiate bean of type " + type.getType().getName() + " for node " + node.getPath());
156                 } else {
157                     throw new Node2BeanException("can't instantiate bean of type " + type.getType().getName());
158                 }
159             }
160 
161             state.popType();
162         }
163         state.popNode();
164 
165         return bean;
166     }
167 
168     @Override
169     public Object setProperties(final Object bean, Node node, boolean recursive, Node2BeanTransformer transformer, ComponentProvider componentProvider) throws Node2BeanException, RepositoryException {
170         // enable extending feature
171         node = new ExtendingNodeWrapper(node);
172 
173         TransformationState state = transformer.newState();
174         state.pushBean(bean);
175         state.pushNode(node);
176 
177         TypeDescriptor type = typeMapping.getTypeDescriptor(bean.getClass());
178 
179         state.pushType(type);
180 
181         transformer = resolveTransformer(type, transformer);
182 
183         Map<String, Object> values = toMap(node, recursive, transformer, state, componentProvider);
184 
185         setProperties(values, transformer, state);
186 
187         transformer.initBean(state, values);
188 
189         state.popBean();
190         state.popType();
191         state.popNode();
192 
193         return bean;
194     }
195 
196     /**
197      * Transforms the children of provided content into a map.
198      * @throws RepositoryException
199      */
200     protected Map<String, Object> toMap(Node node, boolean recursive, Node2BeanTransformer transformer, TransformationState state, ComponentProvider componentProvider) throws Node2BeanException, RepositoryException {
201         Map<String, Object> map = new LinkedHashMap<String, Object>();
202         PropertyIterator it = new FilteringPropertyIterator(node.getProperties(), new AbstractPredicate<Property>() {
203             @Override
204             public boolean evaluateTyped(Property t) {
205                 try {
206                     return !(t.getName().startsWith(MgnlNodeType.JCR_PREFIX) || t.getName().startsWith(MgnlNodeType.MGNL_PREFIX));
207                 } catch (RepositoryException e) {
208                     return false;
209                 }
210             }
211         }, null);
212         while (it.hasNext()) {
213             Property p = it.nextProperty();
214             Object val = PropertyUtil.getValueObject(p.getValue());
215             if (val != null) {
216                 map.put(p.getName(), val);
217             }
218         }
219         if(recursive){
220             final NodeIterator children = transformer.getChildren(node);
221 
222             while (children.hasNext()) {
223                 Node childNode = (Node) children.next();
224                 // in case the the class can not get resolved we can use now
225                 // the parent bean to resolve the class
226 
227                 Object childBean = toBean(childNode, true, transformer, state, componentProvider);
228                 // can be null if forceCreation is true
229                 if(childBean != null){
230                     String name = childNode.getName();
231                     try {
232                         if(childNode.getIndex() > 1){
233                             name += childNode.getIndex();
234                         }
235                     }
236                     catch (RepositoryException e) {
237                         log.error("can't read index of the node [" + childNode + "]", e);
238                     }
239                     map.put(name, childBean);
240                 }
241             }
242         }
243 
244         return map;
245     }
246 
247     /**
248      * Populates the properties of the bean with values from the map.
249      * TODO in case the bean is a map / collection the transfomer.setProperty() method should be called too
250      * TODO if the bean has not a certain property but a value is present, transformer.setProperty() should be called with a fake property descriptor
251      */
252     protected void setProperties(Map<String, Object> values, final Node2BeanTransformer transformer, TransformationState state) throws Node2BeanException, RepositoryException {
253         Object bean = state.getCurrentBean();
254         log.debug("will populate bean {} with the values {}", bean.getClass().getName(), values);
255 
256         if (bean instanceof Map) {
257             ((Map<String, Object>)bean).putAll(values);
258         }
259 
260         if (bean instanceof Collection) {
261             ((Collection<Object>)bean).addAll(values.values());
262         } else {
263             TypeDescriptor beanTypeDescriptor = typeMapping.getTypeDescriptor(bean.getClass());
264             final Collection<PropertyTypeDescriptor> dscrs = beanTypeDescriptor.getPropertyDescriptors(typeMapping).values();
265 
266             for (PropertyTypeDescriptor descriptor : dscrs) {
267                 transformer.setProperty(typeMapping, state, descriptor, values);
268             }
269         }
270     }
271 
272     /**
273      * @todo javadoc
274      * @param type
275      * @param transformer
276      * @return
277      */
278     protected Node2BeanTransformer resolveTransformer(TypeDescriptor type, Node2BeanTransformer transformer) {
279         Node2BeanTransformer customTransformer = type.getTransformer();
280         if(customTransformer != null){
281             transformer = customTransformer;
282         }
283         return transformer;
284     }
285 
286     public boolean isForceCreation() {
287         return this.forceCreation;
288     }
289 
290     public void setForceCreation(boolean forceCreation) {
291         this.forceCreation = forceCreation;
292     }
293 }