View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 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;
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   * Utility class for content to bean transformations.
52   * In case you do not have to customize the transformation, you should use one of these methods:
53   * <ul>
54   * <li><code>toMap</code> is used to build a map from a node
55   * <li><code>toBean</code> transforms the nodes to beans
56   * <li><code>setProperties</code> tries to set the properties on the bean passed to the method
57   * <li><code>setNodeData</code> set the nodedatas based on the bean you pass
58   * </ul>
59   *
60   * @deprecated since 5.2.4 - will be dropped without replacement.
61   */
62  @Deprecated
63  public class Content2BeanUtil {
64  
65      /**
66       * Transforms all nodes to a map.
67       *
68       * @deprecated since 4.5 - use {@link ToMapTransformer}.
69       */
70      public static final Content2BeanTransformerImpl TO_MAP_TRANSFORMER = new ToMapTransformer();
71  
72      /**
73       * Provide a default class.
74       */
75      public static final class DefaultClassTransformer extends Content2BeanTransformerImpl {
76  
77          //        private TypeDescriptor defaultType;
78          private final Class defaultClass;
79  
80          public DefaultClassTransformer(Class defaultClass) {
81              // TODO - can't do this anymore since typeMapping is @Inject or passed, i.e not here yet.
82              // this.defaultType = getTypeMapping().getTypeDescriptor(defaultClass);
83              this.defaultClass = defaultClass;
84          }
85  
86          @Override
87          protected TypeDescriptorriptor">TypeDescriptor onResolveType(TypeMapping typeMapping, TransformationState state, TypeDescriptor resolvedType, ComponentProvider componentProvider) {
88              // return resolvedType==null? defaultType : resolvedType;
89              return resolvedType == null ? typeMapping.getTypeDescriptor(defaultClass) : resolvedType;
90          }
91      }
92  
93      /**
94       * A {@link Content2BeanTransformer} transforming all nodes to Maps.
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      * Get the current processor.
108      *
109      * @deprecated since 4.5, use IoC. - TODO only used locally
110      */
111     public static Content2BeanProcessor getContent2BeanProcessor() {
112         return Components.getSingleton(Content2BeanProcessor.class);
113     }
114 
115     /**
116      * Get the current processor.
117      *
118      * @deprecated since 4.5 - unused, Bean2Content is not implemented yet.
119      */
120     public static Bean2ContentProcessor getBean2ContentProcessor() {
121         return Bean2ContentProcessor.Factory.getProcessor();
122     }
123 
124     /**
125      * Get the current mapping.
126      *
127      * @deprecated since 4.5, use IoC.
128      */
129     public static TypeMapping getTypeMapping() {
130         return Components.getSingleton(TypeMapping.class);
131     }
132 
133     /**
134      * Get the current transformer.
135      *
136      * @deprecated since 4.5, use IoC.
137      */
138     public static Content2BeanTransformer getContent2BeanTransformer() {
139         return Components.getSingleton(Content2BeanTransformer.class);
140     }
141 
142     /**
143      * @see Content2BeanProcessor
144      * @deprecated since 4.5 - only used in tests - use {@link Content2Bean}
145      */
146     public static Object toBean(Content node) throws Content2BeanException {
147         return toBean(node, false);
148     }
149 
150     /**
151      * @see Content2BeanProcessor
152      * @deprecated since 4.5 - only used in tests - use {@link Content2Bean}
153      */
154     public static Object toBean(Content node, final Class defaultClass) throws Content2BeanException {
155         return toBean(node, false, defaultClass);
156     }
157 
158     /**
159      * @see Content2BeanProcessor
160      * @deprecated since 4.5 - TODO used in FilterManagerImpl, ParagraphManager and TemplateManager - use {@link Content2Bean}
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      * @see Content2BeanProcessor
176      * @deprecated since 4.5- only used in DelegateVoter - use {@link Content2Bean}
177      */
178     public static Object toBean(Content node, boolean recursive) throws Content2BeanException {
179         return toBean(node, recursive, getContent2BeanTransformer());
180     }
181 
182     /**
183      * @see Content2BeanProcessor
184      * @deprecated since 4.5- only used in DelegateVoter - use {@link Content2Bean}
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      * @see Content2BeanProcessor
192      * @deprecated since 4.5 use {@link Content2Bean}
193      *             TODO -- this method has a bunch of usage points
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      * @see Content2BeanProcessor
201      * @deprecated since 4.5 use {@link Content2Bean}
202      *             TODO -- this method has a bunch of usage points
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      * Transforms the nodes data into a map containing the names and values.
211      *
212      * @return a flat map
213      * @deprecated since 4.5 - not used - use {@link Content2Bean}
214      */
215     public static Map toMap(Content node) throws Content2BeanException {
216         return toMap(node, false);
217     }
218 
219 
220     /**
221      * Transforms the nodes data into a map containing the names and values. In case recursive is true the sub-nodes are
222      * transformed to beans using the transformer. To avoid that use toMaps() instead
223      *
224      * @deprecated since 4.5 - only used in info.magnolia.setup.for3_5.UpdateI18nConfiguration - use {@link Content2Bean}
225      */
226     public static Map toMap(Content node, boolean recursive) throws Content2BeanException {
227         return (Map) setProperties(new LinkedHashMap(), node, recursive);
228     }
229 
230     /**
231      * @deprecated since 4.5 - TODO only used in DefaultMessagesManager - use {@link Content2Bean}
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      * Transforms the nodes data into a map containing the names and values. In case recursive is true the sub-nodes are
239      * transformed to maps as well
240      *
241      * @deprecated since 4.5 - TODO only used in info.magnolia.setup.for3_5.CheckAndUpdateExistingFilters - use {@link Content2Bean}
242      */
243     public static Map toPureMaps(Content node, boolean recursive) throws Content2BeanException {
244         return (Map) toBean(node, recursive, new ToMapTransformer());
245     }
246 
247     /**
248      * @see Content2BeanProcessor
249      * @deprecated since 4.5 - unused - use {@link Content2Bean}
250      */
251     public static Object setProperties(Object bean, Content node) throws Content2BeanException {
252         return setProperties(bean, node, false);
253     }
254 
255     /**
256      * @see Content2BeanProcessor
257      * @deprecated since 4.5 - TODO - only used locally and by ModuleManagerImpl and TreeHandlerManager
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      * @deprecated since 4.5 - TODO - only used locally - use {@link Content2Bean}
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      * @see Content2BeanProcessor
280      * @deprecated since 4.5 - use {@link Content2Bean}
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      * @see Content2BeanProcessor
288      * @deprecated since 4.5 - use {@link Content2Bean}
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      * @see Content2BeanProcessor
297      * @deprecated since 4.5 - only used in tests
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      * TODO use the <code>Bean2ContentProcessor</code>.
306      *
307      * @deprecated since 4.5 - unused - use {@link Content2Bean}
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      * TODO use the <code>Bean2ContentProcessor</code>.
322      *
323      * @deprecated since 4.5 - unused - use {@link Content2Bean}
324      */
325     public static void setNodeDatas(Content node, Object obj) throws Content2BeanException {
326         setNodeDatas(node, toMap(obj));
327     }
328 
329     /**
330      * TODO use the <code>Bean2ContentProcessor</code>.
331      *
332      * @deprecated since 4.5 - TODO - only used locally - use {@link Content2Bean}
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      * Used to fake the <code>setNodeDatas()</code> methods.
347      *
348      * @deprecated since 4.5 - TODO - only used locally - use {@link Content2Bean}
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