View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.service.data.impl.util;
20  
21  import java.beans.PropertyDescriptor;
22  import java.lang.reflect.InvocationTargetException;
23  import java.lang.reflect.UndeclaredThrowableException;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.IdentityHashMap;
27  import java.util.Set;
28  
29  import org.apache.commons.beanutils.PropertyUtils;
30  import org.hibernate.Hibernate;
31  import org.hibernate.collection.spi.PersistentCollection;
32  import org.hibernate.proxy.HibernateProxy;
33  
34  /**
35   * The Class LoadHelper.
36   */
37  public final class LoadHelper {
38  
39  	/**
40  	 * Instantiates a new load helper.
41  	 */
42  	private LoadHelper() {
43  		super();
44  	}
45  
46  	/**
47  	 * Handle reflection exception.
48  	 *
49  	 * @param ex
50  	 *            the ex
51  	 */
52  	private static void handleReflectionException(final Exception ex) {
53  		if (ex instanceof NoSuchMethodException) {
54  			throw new IllegalStateException("Method not found: " + ex.getMessage());
55  		}
56  		if (ex instanceof IllegalAccessException) {
57  			throw new IllegalStateException("Could not access method: " + ex.getMessage());
58  		}
59  		if (ex instanceof RuntimeException) {
60  			throw (RuntimeException) ex;
61  		}
62  		throw new UndeclaredThrowableException(ex);
63  	}
64  
65  	/**
66  	 * Recursive initliaze.
67  	 *
68  	 * @param obj
69  	 *            the obj
70  	 * @param dejaVu
71  	 *            the deja vu
72  	 * @throws IllegalAccessException
73  	 *             the illegal access exception
74  	 * @throws InvocationTargetException
75  	 *             the invocation target exception
76  	 * @throws NoSuchMethodException
77  	 *             the no such method exception
78  	 */
79  	private static void recursiveInitialize(final Object obj, final Set<Object> dejaVu)
80  			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
81  		if (dejaVu.contains(obj)) {
82  			return;
83  		} else {
84  			dejaVu.add(obj);
85  
86  			if (!Hibernate.isInitialized(obj)) {
87  				Hibernate.initialize(obj);
88  			}
89  
90  			if (obj instanceof HibernateProxy || obj instanceof PersistentCollection) {
91  
92  				initProxyAndCollections(obj, PropertyUtils.getPropertyDescriptors(obj), dejaVu);
93  			}
94  		}
95  	}
96  
97  	/**
98  	 * Inits the proxy and collections.
99  	 *
100 	 * @param obj
101 	 *            the obj
102 	 * @param properties
103 	 *            the properties
104 	 * @param dejaVu
105 	 *            the deja vu
106 	 * @throws IllegalAccessException
107 	 *             the illegal access exception
108 	 * @throws InvocationTargetException
109 	 *             the invocation target exception
110 	 * @throws NoSuchMethodException
111 	 *             the no such method exception
112 	 */
113 	private static void initProxyAndCollections(final Object obj, final PropertyDescriptor[] properties,
114 			final Set<Object> dejaVu) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
115 		for (final PropertyDescriptor propertyDescriptor : properties) {
116 			if (PropertyUtils.getReadMethod(propertyDescriptor) != null) {
117 				initProxies(dejaVu, PropertyUtils.getProperty(obj, propertyDescriptor.getName()));
118 			}
119 		}
120 	}
121 
122 	/**
123 	 * Inits the proxies.
124 	 *
125 	 * @param dejaVu
126 	 *            the deja vu
127 	 * @param origProp
128 	 *            the orig prop
129 	 * @throws IllegalAccessException
130 	 *             the illegal access exception
131 	 * @throws InvocationTargetException
132 	 *             the invocation target exception
133 	 * @throws NoSuchMethodException
134 	 *             the no such method exception
135 	 */
136 	private static void initProxies(final Set<Object> dejaVu, final Object origProp)
137 			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
138 		if (origProp != null) {
139 			recursiveInitialize(origProp, dejaVu);
140 		}
141 		if (origProp instanceof Collection) {
142 			for (final Object item : (Collection<?>) origProp) {
143 				recursiveInitialize(item, dejaVu);
144 			}
145 		}
146 	}
147 
148 	/**
149 	 * Recursive initliaze.
150 	 *
151 	 * @param <T>
152 	 *            the generic type
153 	 * @param obj
154 	 *            the obj
155 	 * @return the t
156 	 */
157 	public static <T> T recursiveInitialize(final T obj) {
158 		if (obj != null) {
159 
160 			final Set<Object> dejaVu = Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>());
161 			try {
162 				recursiveInitialize(obj, dejaVu);
163 			} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
164 				handleReflectionException(e);
165 			}
166 		}
167 		return obj;
168 	}
169 
170 }