1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.cdm.persistence.dao.initializer;
|
5
|
|
6
|
import java.beans.PropertyDescriptor;
|
7
|
import java.io.Serializable;
|
8
|
import java.lang.reflect.InvocationTargetException;
|
9
|
import java.lang.reflect.Method;
|
10
|
import java.lang.reflect.ParameterizedType;
|
11
|
import java.lang.reflect.Type;
|
12
|
import java.lang.reflect.TypeVariable;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Collection;
|
15
|
import java.util.Collections;
|
16
|
import java.util.HashSet;
|
17
|
import java.util.List;
|
18
|
import java.util.Map;
|
19
|
import java.util.Set;
|
20
|
|
21
|
import org.apache.commons.beanutils.PropertyUtils;
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.hibernate.Hibernate;
|
24
|
import org.hibernate.HibernateException;
|
25
|
import org.hibernate.Query;
|
26
|
import org.hibernate.collection.internal.AbstractPersistentCollection;
|
27
|
import org.hibernate.collection.internal.PersistentMap;
|
28
|
import org.hibernate.envers.entities.mapper.relation.lazy.proxy.CollectionProxy;
|
29
|
import org.hibernate.envers.entities.mapper.relation.lazy.proxy.MapProxy;
|
30
|
import org.hibernate.envers.entities.mapper.relation.lazy.proxy.SortedMapProxy;
|
31
|
import org.hibernate.proxy.HibernateProxy;
|
32
|
import org.springframework.beans.factory.annotation.Autowired;
|
33
|
|
34
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
35
|
import eu.etaxonomy.cdm.persistence.dao.common.ICdmGenericDao;
|
36
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.HibernateBeanInitializer;
|
37
|
|
38
|
/**
|
39
|
* For now this is a test if we can improve performance for bean initializing
|
40
|
* @author a.mueller
|
41
|
* @date 2013-10-25
|
42
|
*
|
43
|
*/
|
44
|
public class AdvancedBeanInitializer extends HibernateBeanInitializer {
|
45
|
|
46
|
public static final Logger logger = Logger.getLogger(AdvancedBeanInitializer.class);
|
47
|
|
48
|
@Autowired
|
49
|
ICdmGenericDao genericDao;
|
50
|
|
51
|
@Override
|
52
|
public void initialize(Object bean, List<String> propertyPaths) {
|
53
|
List<Object> beanList = new ArrayList<Object>(1);
|
54
|
beanList.add(bean);
|
55
|
initializeAll(beanList, propertyPaths);
|
56
|
}
|
57
|
|
58
|
//TODO optimize algorithm ..
|
59
|
@Override
|
60
|
public <C extends Collection<?>> C initializeAll(C beanList, List<String> propertyPaths) {
|
61
|
|
62
|
if (beanList == null || beanList.isEmpty()){
|
63
|
return beanList;
|
64
|
}
|
65
|
|
66
|
//autoinitialize
|
67
|
for (Object bean : beanList){
|
68
|
autoinitializeBean(bean);
|
69
|
}
|
70
|
|
71
|
if(propertyPaths == null){
|
72
|
return beanList;
|
73
|
}
|
74
|
|
75
|
|
76
|
//new
|
77
|
BeanInitNode rootPath = BeanInitNode.createInitTree(propertyPaths);
|
78
|
if (logger.isTraceEnabled()){logger.trace(rootPath.toStringTree());}
|
79
|
|
80
|
|
81
|
if(logger.isDebugEnabled()){ logger.debug(">> starting to initialize beanlist ; class(e.g.):" + beanList.iterator().next().getClass().getSimpleName());}
|
82
|
rootPath.addBeans(beanList);
|
83
|
initializeNodeRecursive(rootPath);
|
84
|
|
85
|
|
86
|
//old - keep for safety (this may help to initialize those beans that are not yet correctly initialized by the AdvancedBeanInitializer
|
87
|
if(logger.isTraceEnabled()){logger.trace("Start old initalizer ... ");};
|
88
|
for (Object bean :beanList){
|
89
|
Collections.sort(propertyPaths);
|
90
|
for(String propPath : propertyPaths){
|
91
|
// initializePropertyPath(bean, propPath);
|
92
|
}
|
93
|
}
|
94
|
|
95
|
if(logger.isDebugEnabled()){ logger.debug(" Completed initialization of beanlist "); }
|
96
|
return beanList;
|
97
|
|
98
|
}
|
99
|
|
100
|
|
101
|
//new
|
102
|
private void initializeNodeRecursive(BeanInitNode rootPath) {
|
103
|
initializeNode(rootPath);
|
104
|
for (BeanInitNode childPath : rootPath.getChildrenList()){
|
105
|
initializeNodeRecursive(childPath);
|
106
|
}
|
107
|
rootPath.resetBeans();
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Initializes the given single <code>propPath</code> String.
|
112
|
*
|
113
|
* @param bean
|
114
|
* @param propPath
|
115
|
*/
|
116
|
private void initializeNode(BeanInitNode node) {
|
117
|
if(logger.isDebugEnabled()){logger.debug(" processing " + node.toString());}
|
118
|
if (node.isRoot()){
|
119
|
return;
|
120
|
}else if (node.isWildcard()){
|
121
|
initializeNodeWildcard(node);
|
122
|
} else {
|
123
|
initializeNodeNoWildcard(node);
|
124
|
}
|
125
|
}
|
126
|
|
127
|
// if propPath only contains a wildcard (* or $)
|
128
|
// => do a batch initialization of *toOne or *toMany relations
|
129
|
private void initializeNodeWildcard(BeanInitNode node) {
|
130
|
// boolean initToMany = node.isToManyWildcard();
|
131
|
Map<Class<?>, Set<Object>> parentBeans = node.getParentBeans();
|
132
|
for (Class<?> clazz : parentBeans.keySet()){
|
133
|
//new
|
134
|
for (Object bean : parentBeans.get(clazz)){
|
135
|
|
136
|
if(Collection.class.isAssignableFrom(bean.getClass())){
|
137
|
// old: initializeAllEntries((Collection<?>)bean, true, initToMany); //TODO is this a possible case at all??
|
138
|
throw new RuntimeException("Collection no longer expected in 'initializeNodeWildcard()'. Therefore an exception is thrown.");
|
139
|
} else if(Map.class.isAssignableFrom(bean.getClass())) {
|
140
|
// old: initializeAllEntries(((Map<?,?>)bean).values(), true, initToMany); ////TODO is this a possible case at all??
|
141
|
throw new RuntimeException("Map no longer expected in 'initializeNodeWildcard()'. Therefore an exception is thrown.");
|
142
|
} else{
|
143
|
prepareBeanWildcardForBulkLoad(node, bean);
|
144
|
}
|
145
|
}
|
146
|
//end new
|
147
|
|
148
|
// initializeNodeWildcardOld(initToMany, beans, clazz); //if switched on move bulkLoadLazies up
|
149
|
}
|
150
|
|
151
|
//
|
152
|
bulkLoadLazies(node);
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* @param initToMany
|
157
|
* @param beans
|
158
|
* @param clazz
|
159
|
*/
|
160
|
private void initializeNodeWildcardOld(boolean initToMany,
|
161
|
Map<Class<?>, Set<Object>> beans, Class<?> clazz) {
|
162
|
for (Object bean : beans.get(clazz)){
|
163
|
|
164
|
if(Collection.class.isAssignableFrom(bean.getClass())){
|
165
|
initializeAllEntries((Collection<?>)bean, true, initToMany);
|
166
|
} else if(Map.class.isAssignableFrom(bean.getClass())) {
|
167
|
initializeAllEntries(((Map<?,?>)bean).values(), true, initToMany);
|
168
|
} else{
|
169
|
initializeBean(bean, true, initToMany);
|
170
|
}
|
171
|
}
|
172
|
}
|
173
|
|
174
|
private void prepareBeanWildcardForBulkLoad(BeanInitNode node, Object bean){
|
175
|
|
176
|
if(logger.isTraceEnabled()){logger.trace(">> prepare bulk wildcard initialization of a bean of type " + bean.getClass().getSimpleName()); }
|
177
|
Set<Class<?>> restrictions = new HashSet<Class<?>>();
|
178
|
restrictions.add(CdmBase.class);
|
179
|
if(node.isToManyWildcard()){
|
180
|
restrictions.add(Collection.class);
|
181
|
}
|
182
|
Set<PropertyDescriptor> props = getProperties(bean, restrictions);
|
183
|
for(PropertyDescriptor propertyDescriptor : props){
|
184
|
try {
|
185
|
String property = propertyDescriptor.getName();
|
186
|
|
187
|
// invokeInitialization(bean, propertyDescriptor);
|
188
|
Object propertyValue = PropertyUtils.getProperty( bean, property);
|
189
|
|
190
|
preparePropertyValueForBulkLoadOrStore(node, bean, property, propertyValue );
|
191
|
|
192
|
} catch (IllegalAccessException e) {
|
193
|
logger.error("Illegal access on property " + propertyDescriptor.getName());
|
194
|
} catch (InvocationTargetException e) {
|
195
|
logger.info("Cannot invoke property " + propertyDescriptor.getName() + " not found");
|
196
|
} catch (NoSuchMethodException e) {
|
197
|
logger.info("Property " + propertyDescriptor.getName() + " not found");
|
198
|
}
|
199
|
}
|
200
|
if(logger.isTraceEnabled()){logger.trace(" completed bulk wildcard initialization of a bean");}
|
201
|
}
|
202
|
|
203
|
|
204
|
// propPath contains either a single field or a nested path
|
205
|
// split next path token off and keep the remaining as nestedPath
|
206
|
private void initializeNodeNoWildcard(BeanInitNode node) {
|
207
|
|
208
|
String property = node.getPath();
|
209
|
int pos;
|
210
|
|
211
|
// is the property indexed?
|
212
|
Integer index = null;
|
213
|
if((pos = property.indexOf('[')) > 0){
|
214
|
String indexString = property.substring(pos + 1, property.indexOf(']'));
|
215
|
index = Integer.valueOf(indexString);
|
216
|
property = property.substring(0, pos);
|
217
|
}
|
218
|
|
219
|
//Class targetClass = HibernateProxyHelper.getClassWithoutInitializingProxy(bean); // used for debugging
|
220
|
|
221
|
for (Class<?> parentClazz : node.getParentBeans().keySet()){
|
222
|
if (logger.isTraceEnabled()){logger.trace(" invoke initialization on "+ node.toString()+ " beans of class " + parentClazz.getSimpleName() + " ... ");}
|
223
|
|
224
|
Set<Object> parentBeans = node.getParentBeans().get(parentClazz);
|
225
|
|
226
|
if (index != null){
|
227
|
logger.warn("Property path index not yet implemented for 'new'");
|
228
|
}
|
229
|
//new
|
230
|
for (Object parentBean : parentBeans){
|
231
|
preparePropertyForSingleBean(node, property, parentClazz, parentBean);
|
232
|
}//end for
|
233
|
//end new
|
234
|
// initializeNodeNoWildcardOld(node, property, index, parentBeans); //move bulkLoadLazies up again, if uncomment this line
|
235
|
} //end for
|
236
|
bulkLoadLazies(node);
|
237
|
}
|
238
|
|
239
|
/**
|
240
|
* Prepare a single property of a non-collection bean. Collections are handled elsewhere.
|
241
|
*/
|
242
|
private void preparePropertyForSingleBean(BeanInitNode node, String property, Class<?> parentClazz, Object bean) {
|
243
|
String propertyName = mapFieldToPropertyName(property, bean.getClass().getSimpleName());
|
244
|
try{
|
245
|
Object propertyValue = PropertyUtils.getProperty(bean, propertyName);
|
246
|
preparePropertyValueForBulkLoadOrStore(node, bean, property, propertyValue);
|
247
|
} catch (IllegalAccessException e) {
|
248
|
String message = "Illegal access on property " + property;
|
249
|
logger.error(message);
|
250
|
throw new RuntimeException(message, e);
|
251
|
} catch (InvocationTargetException e) {
|
252
|
String message = "Cannot invoke property " + property + " not found";
|
253
|
logger.error(message);
|
254
|
throw new RuntimeException(message, e);
|
255
|
} catch (NoSuchMethodException e) {
|
256
|
String message = "Property " + propertyName + " not found for class " + parentClazz;
|
257
|
logger.error(message);
|
258
|
throw new RuntimeException(message, e);
|
259
|
}
|
260
|
}
|
261
|
|
262
|
// /**
|
263
|
// * @param node
|
264
|
// * @param property
|
265
|
// * @param index
|
266
|
// * @param parentBeans
|
267
|
// * @throws IllegalAccessException
|
268
|
// * @throws InvocationTargetException
|
269
|
// * @throws NoSuchMethodException
|
270
|
// */
|
271
|
// private void initializeNodeNoWildcardOld(BeanInitNode node,
|
272
|
// String property,
|
273
|
// Integer index,
|
274
|
// Set<Object> parentBeans)
|
275
|
// throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
|
276
|
// {
|
277
|
// for (Object bean : parentBeans){
|
278
|
//
|
279
|
// PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(bean, property);
|
280
|
// if (logger.isTraceEnabled()){logger.trace(" unwrap " + node.toStringNoWildcard() + " ... ");}
|
281
|
// // [1] initialize the bean named by property
|
282
|
// Object unwrappedPropertyBean = invokeInitialization(bean, propertyDescriptor);
|
283
|
// if (logger.isTraceEnabled()){logger.trace(" unwrap " + node.toStringNoWildcard() + " - DONE ");}
|
284
|
//
|
285
|
//
|
286
|
// // [2]
|
287
|
// // handle property
|
288
|
// if(unwrappedPropertyBean != null ){
|
289
|
// initializeNodeSinglePropertyOld(node, property, index, bean, unwrappedPropertyBean);
|
290
|
// }
|
291
|
// }
|
292
|
// }
|
293
|
|
294
|
/**
|
295
|
* @param node
|
296
|
* @param propertyValue
|
297
|
* @param parentBean
|
298
|
* @param param
|
299
|
*/
|
300
|
private void preparePropertyValueForBulkLoadOrStore(BeanInitNode node,
|
301
|
Object parentBean,
|
302
|
String param,
|
303
|
Object propertyValue)
|
304
|
{
|
305
|
BeanInitNode sibling = node.getSibling(param);
|
306
|
|
307
|
if (propertyValue instanceof AbstractPersistentCollection ){
|
308
|
//collections
|
309
|
if (!node.hasWildcardToManySibling()){ //if wildcard sibling exists the lazies are already prepared there
|
310
|
AbstractPersistentCollection collection = (AbstractPersistentCollection)propertyValue;
|
311
|
if (collection.wasInitialized()){
|
312
|
storeInitializedCollection(collection, node, param);
|
313
|
}else{
|
314
|
// Class<?> parentClass = parentBean.getClass();
|
315
|
// int parentId = ((CdmBase)parentBean).getId();
|
316
|
if (sibling != null){
|
317
|
sibling.putLazyCollection(collection);
|
318
|
}else{
|
319
|
node.putLazyCollection(collection);
|
320
|
}
|
321
|
}
|
322
|
}
|
323
|
} else if (propertyValue instanceof CollectionProxy
|
324
|
|| propertyValue instanceof MapProxy<?, ?>
|
325
|
|| propertyValue instanceof SortedMapProxy<?, ?>){
|
326
|
//hibernate envers collections
|
327
|
//TODO check if other code works with audited data at all as we use HQL queries
|
328
|
if (!node.hasWildcardToManySibling()){ //if wildcard sibling exists the lazies are already prepared there
|
329
|
Collection<?> collection = (Collection<?>)propertyValue;
|
330
|
//TODO it is difficult to find out if an envers collection is initiallized
|
331
|
//but possiblie via reflection. If the "delegate" parameter is null it is not yet initialized.
|
332
|
//However, as we do not know if envers initialization works at all together with the AdvancedBeanInitializer
|
333
|
//we initialize each collection immediately here by calling size()
|
334
|
collection.size(); //initialize
|
335
|
storeInitializedEnversCollection(collection, node, param);
|
336
|
}
|
337
|
}else{
|
338
|
//singles
|
339
|
if (!node.hasWildcardToOneSibling()){ //if wildcard exists the lazies are already prepared there
|
340
|
if (! Hibernate.isInitialized(propertyValue)){
|
341
|
if (propertyValue instanceof HibernateProxy){
|
342
|
Serializable id = ((HibernateProxy)propertyValue).getHibernateLazyInitializer().getIdentifier();
|
343
|
Class<?> persistedClass = ((HibernateProxy)propertyValue).getHibernateLazyInitializer().getPersistentClass();
|
344
|
if (sibling != null){
|
345
|
sibling.putLazyBean(persistedClass, id);
|
346
|
}else{
|
347
|
node.putLazyBean(persistedClass, id);
|
348
|
}
|
349
|
|
350
|
}else{
|
351
|
logger.warn("Lazy value is not of type HibernateProxy. This is not yet handled.");
|
352
|
}
|
353
|
}else if (propertyValue == null){
|
354
|
// do nothing
|
355
|
}else{
|
356
|
if (propertyValue instanceof HibernateProxy){ //TODO remove hibernate dependency
|
357
|
propertyValue = initializeInstance(propertyValue);
|
358
|
}
|
359
|
autoinitializeBean(propertyValue);
|
360
|
node.addBean(propertyValue);
|
361
|
}
|
362
|
}
|
363
|
}
|
364
|
}
|
365
|
|
366
|
private void autoinitializeBean(Object bean) {
|
367
|
invokePropertyAutoInitializers(bean);
|
368
|
}
|
369
|
|
370
|
private void autoinitializeBean(CdmBase bean, AutoInit autoInit) {
|
371
|
for(AutoPropertyInitializer<CdmBase> init : autoInit.initlializers) {
|
372
|
init.initialize(bean);
|
373
|
}
|
374
|
}
|
375
|
|
376
|
private void storeInitializedCollection(AbstractPersistentCollection persistedCollection,
|
377
|
BeanInitNode node, String param) {
|
378
|
|
379
|
Collection<?> collection;
|
380
|
if (persistedCollection instanceof Collection) {
|
381
|
collection = (Collection<?>) persistedCollection;
|
382
|
}else if (persistedCollection instanceof Map) {
|
383
|
collection = ((Map<?,?>)persistedCollection).values();
|
384
|
}else{
|
385
|
throw new RuntimeException ("Non Map and non Collection cas not handled in storeInitializedCollection()");
|
386
|
}
|
387
|
for (Object value : collection){
|
388
|
preparePropertyValueForBulkLoadOrStore(node, null, param, value);
|
389
|
}
|
390
|
}
|
391
|
|
392
|
/**
|
393
|
* @see #storeInitializedCollection(AbstractPersistentCollection, BeanInitNode, String)alizedCollection
|
394
|
*/
|
395
|
private void storeInitializedEnversCollection(Collection<?> enversCollection,
|
396
|
BeanInitNode node, String param) {
|
397
|
if (enversCollection instanceof CollectionProxy
|
398
|
|| enversCollection instanceof MapProxy<?, ?>
|
399
|
|| enversCollection instanceof SortedMapProxy<?, ?>){
|
400
|
Collection<?> collection;
|
401
|
if (enversCollection instanceof MapProxy
|
402
|
|| enversCollection instanceof SortedMapProxy<?, ?>) {
|
403
|
collection = ((Map<?,?>)enversCollection).values();
|
404
|
}else if (enversCollection instanceof CollectionProxy) {
|
405
|
collection = enversCollection;
|
406
|
}else{
|
407
|
throw new RuntimeException ("Non MapProxy and non CollectionProxy case not handled in storeInitializedEnversCollection()");
|
408
|
}
|
409
|
for (Object value : collection){
|
410
|
preparePropertyValueForBulkLoadOrStore(node, null, param, value);
|
411
|
}
|
412
|
}
|
413
|
}
|
414
|
|
415
|
|
416
|
private void bulkLoadLazies(BeanInitNode node) {
|
417
|
|
418
|
if (logger.isTraceEnabled()){logger.trace("bulk load " + node);}
|
419
|
|
420
|
//beans
|
421
|
for (Class<?> clazz : node.getLazyBeans().keySet()){
|
422
|
Set<Serializable> idSet = node.getLazyBeans().get(clazz);
|
423
|
if (idSet != null && ! idSet.isEmpty()){
|
424
|
|
425
|
if (logger.isTraceEnabled()){logger.trace("bulk load beans of class " + clazz.getSimpleName());}
|
426
|
//TODO use entity name
|
427
|
String hql = " SELECT c FROM %s as c %s WHERE c.id IN (:idSet) ";
|
428
|
AutoInit autoInit = addAutoinitFetchLoading(clazz, "c");
|
429
|
hql = String.format(hql, clazz.getSimpleName(), autoInit.leftJoinFetch);
|
430
|
if (logger.isTraceEnabled()){logger.trace(hql);}
|
431
|
Query query = genericDao.getHqlQuery(hql);
|
432
|
query.setParameterList("idSet", idSet);
|
433
|
List<Object> list = query.list();
|
434
|
|
435
|
if (logger.isTraceEnabled()){logger.trace("initialize bulk loaded beans of class " + clazz.getSimpleName());}
|
436
|
for (Object object : list){
|
437
|
if (object instanceof HibernateProxy){ //TODO remove hibernate dependency
|
438
|
object = initializeInstance(object);
|
439
|
}
|
440
|
autoinitializeBean((CdmBase)object, autoInit);
|
441
|
node.addBean(object);
|
442
|
}
|
443
|
if (logger.isTraceEnabled()){logger.trace("bulk load - DONE");}
|
444
|
}
|
445
|
}
|
446
|
node.resetLazyBeans();
|
447
|
|
448
|
//collections
|
449
|
for (Class<?> ownerClazz : node.getLazyCollections().keySet()){
|
450
|
Map<String, Set<Serializable>> lazyParams = node.getLazyCollections().get(ownerClazz);
|
451
|
for (String param : lazyParams.keySet()){
|
452
|
Set<Serializable> idSet = lazyParams.get(param);
|
453
|
if (idSet != null && ! idSet.isEmpty()){
|
454
|
if (logger.isTraceEnabled()){logger.trace("bulk load " + node + " collections ; ownerClass=" + ownerClazz.getSimpleName() + " ; param = " + param);}
|
455
|
|
456
|
Type collectionEntitiyType = null;
|
457
|
PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(ownerClazz);
|
458
|
for(PropertyDescriptor d : descriptors) {
|
459
|
if(d.getName().equals(param)) {
|
460
|
Method readMethod = d.getReadMethod();
|
461
|
ParameterizedType pt = (ParameterizedType) readMethod.getGenericReturnType();
|
462
|
Type[] actualTypeArguments = pt.getActualTypeArguments();
|
463
|
if(actualTypeArguments.length == 2) {
|
464
|
// this must be a map of <Language, String> (aka LanguageString) there is no other case like this in the cdm
|
465
|
// in case of Maps the returned Collection will be the Collection of the values, so collectionEntitiyType is the
|
466
|
// second typeArgument
|
467
|
collectionEntitiyType = actualTypeArguments[1];
|
468
|
} else {
|
469
|
collectionEntitiyType = actualTypeArguments[0];
|
470
|
}
|
471
|
if(collectionEntitiyType instanceof TypeVariable) {
|
472
|
collectionEntitiyType = ((TypeVariable)collectionEntitiyType).getBounds()[0];
|
473
|
}
|
474
|
}
|
475
|
}
|
476
|
|
477
|
//TODO use entity name ??
|
478
|
//get from repository
|
479
|
List<Object[]> list;
|
480
|
String hql = "SELECT oc " +
|
481
|
" FROM %s as oc LEFT JOIN FETCH oc.%s as col %s" +
|
482
|
" WHERE oc.id IN (:idSet) ";
|
483
|
|
484
|
AutoInit autoInit = addAutoinitFetchLoading((Class)collectionEntitiyType, "col");
|
485
|
hql = String.format(hql, ownerClazz.getSimpleName(), param,
|
486
|
autoInit.leftJoinFetch);
|
487
|
|
488
|
try {
|
489
|
if (logger.isTraceEnabled()){logger.trace(hql);}
|
490
|
Query query = genericDao.getHqlQuery(hql);
|
491
|
query.setParameterList("idSet", idSet);
|
492
|
list = query.list();
|
493
|
if (logger.isTraceEnabled()){logger.trace("size of retrieved list is " + list.size());}
|
494
|
} catch (HibernateException e) {
|
495
|
e.printStackTrace();
|
496
|
throw e;
|
497
|
}
|
498
|
|
499
|
//getTarget and add to child node
|
500
|
if (logger.isTraceEnabled()){logger.trace("initialize bulk loaded " + node + " collections - DONE");}
|
501
|
for (Object parentBean : list){
|
502
|
try {
|
503
|
Object propValue = PropertyUtils.getProperty(
|
504
|
parentBean,
|
505
|
mapFieldToPropertyName(param, parentBean.getClass().getSimpleName())
|
506
|
);
|
507
|
|
508
|
if (propValue == null){
|
509
|
logger.trace("Collection is null");
|
510
|
}else {
|
511
|
if(propValue instanceof PersistentMap) {
|
512
|
propValue = ((PersistentMap)propValue).values();
|
513
|
}
|
514
|
for(Object newBean : (Collection<Object>)propValue ) {
|
515
|
if(newBean instanceof HibernateProxy){
|
516
|
newBean = initializeInstance(newBean);
|
517
|
}
|
518
|
|
519
|
autoinitializeBean((CdmBase)newBean, autoInit);
|
520
|
|
521
|
node.addBean(newBean);
|
522
|
}
|
523
|
}
|
524
|
} catch (Exception e) {
|
525
|
// TODO better throw an exception ?
|
526
|
logger.error("error while getting collection property", e);
|
527
|
}
|
528
|
}
|
529
|
if (logger.isTraceEnabled()){logger.trace("bulk load " + node + " collections - DONE");}
|
530
|
}
|
531
|
}
|
532
|
}
|
533
|
for (AbstractPersistentCollection collection : node.getUninitializedCollections()){
|
534
|
if (! collection.wasInitialized()){ //should not happen anymore
|
535
|
collection.forceInitialization();
|
536
|
if (logger.isTraceEnabled()){logger.trace("forceInitialization of collection " + collection);}
|
537
|
} else {
|
538
|
if (logger.isTraceEnabled()){logger.trace("collection " + collection + " is initialized - OK!");}
|
539
|
}
|
540
|
}
|
541
|
|
542
|
node.resetLazyCollections();
|
543
|
|
544
|
if (logger.isDebugEnabled()){logger.debug("bulk load " + node + " - DONE ");}
|
545
|
|
546
|
}
|
547
|
|
548
|
|
549
|
private AutoInit addAutoinitFetchLoading(Class<?> clazz, String beanAlias) {
|
550
|
|
551
|
AutoInit autoInit = new AutoInit();
|
552
|
if(clazz != null) {
|
553
|
Set<AutoPropertyInitializer<CdmBase>> inits = getAutoInitializers(clazz);
|
554
|
for (AutoPropertyInitializer<CdmBase> init: inits){
|
555
|
try {
|
556
|
autoInit.leftJoinFetch +=init.hibernateFetchJoin(clazz, beanAlias);
|
557
|
} catch (Exception e) {
|
558
|
// the AutoPropertyInitializer is not supporting LEFT JOIN FETCH so it needs to be
|
559
|
// used explicitly
|
560
|
autoInit.initlializers.add(init);
|
561
|
}
|
562
|
|
563
|
}
|
564
|
}
|
565
|
return autoInit;
|
566
|
}
|
567
|
|
568
|
private Set<AutoPropertyInitializer<CdmBase>> getAutoInitializers(Class<?> clazz) {
|
569
|
Set<AutoPropertyInitializer<CdmBase>> result = new HashSet<AutoPropertyInitializer<CdmBase>>();
|
570
|
for(Class<? extends CdmBase> superClass : getBeanAutoInitializers().keySet()){
|
571
|
if(superClass.isAssignableFrom(clazz)){
|
572
|
result.add(getBeanAutoInitializers().get(superClass));
|
573
|
}
|
574
|
}
|
575
|
return result;
|
576
|
}
|
577
|
|
578
|
/**
|
579
|
* Rename hibernate (field) attribute to Bean property name, due to bean inconsistencies
|
580
|
* #3841
|
581
|
* @param param
|
582
|
* @param ownerClass
|
583
|
* @return
|
584
|
*/
|
585
|
private String mapFieldToPropertyName(String param, String ownerClass) {
|
586
|
if (ownerClass.contains("Description") && param.equals("descriptionElements")){
|
587
|
return "elements";
|
588
|
}
|
589
|
if (ownerClass.startsWith("FeatureNode") && param.equals("children")) {
|
590
|
return "childNodes";
|
591
|
}
|
592
|
if (ownerClass.startsWith("Media") && param.equals("description")) {
|
593
|
return "allDescriptions";
|
594
|
}
|
595
|
else{
|
596
|
return param;
|
597
|
}
|
598
|
}
|
599
|
|
600
|
/**
|
601
|
* @param node
|
602
|
* @param property
|
603
|
* @param index
|
604
|
* @param bean
|
605
|
* @param unwrappedPropertyBean
|
606
|
*/
|
607
|
private void initializeNodeSinglePropertyOld(BeanInitNode node, String property,
|
608
|
Integer index, Object bean, Object unwrappedPropertyBean) {
|
609
|
Collection<?> collection = null;
|
610
|
if(Map.class.isAssignableFrom(unwrappedPropertyBean.getClass())) {
|
611
|
collection = ((Map<?,?>)unwrappedPropertyBean).values();
|
612
|
}else if (Collection.class.isAssignableFrom(unwrappedPropertyBean.getClass())) {
|
613
|
collection = (Collection<?>) unwrappedPropertyBean;
|
614
|
}
|
615
|
if (collection != null){
|
616
|
//collection or map
|
617
|
if (logger.isTraceEnabled()){logger.trace(" initialize collection for " + node.toStringNoWildcard() + " ... ");}
|
618
|
int i = 0;
|
619
|
for (Object entrybean : collection) {
|
620
|
if(index == null){
|
621
|
node.addBean(entrybean);
|
622
|
} else if(index.equals(i)){
|
623
|
node.addBean(entrybean);
|
624
|
break;
|
625
|
}
|
626
|
i++;
|
627
|
}
|
628
|
if (logger.isTraceEnabled()){logger.trace(" initialize collection for " + node.toString() + " - DONE ");}
|
629
|
|
630
|
}else {
|
631
|
// nested bean
|
632
|
node.addBean(unwrappedPropertyBean);
|
633
|
setProperty(bean, property, unwrappedPropertyBean);
|
634
|
}
|
635
|
}
|
636
|
|
637
|
private class AutoInit{
|
638
|
|
639
|
String leftJoinFetch = "";
|
640
|
Set<AutoPropertyInitializer<CdmBase>> initlializers = new HashSet<AutoPropertyInitializer<CdmBase>>();
|
641
|
|
642
|
/**
|
643
|
* @param leftJoinFetch
|
644
|
* @param initlializers
|
645
|
*/
|
646
|
public AutoInit() {
|
647
|
}
|
648
|
}
|
649
|
}
|