Project

General

Profile

Download (26 KB) Statistics
| Branch: | Tag: | Revision:
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
            //don't throw exception as sometimes property paths will include non matching property names due to subclassing
259
            //#5077  comment 12
260
        }
261
    }
262

    
263
//    /**
264
//     * @param node
265
//     * @param property
266
//     * @param index
267
//     * @param parentBeans
268
//     * @throws IllegalAccessException
269
//     * @throws InvocationTargetException
270
//     * @throws NoSuchMethodException
271
//     */
272
//    private void initializeNodeNoWildcardOld(BeanInitNode node,
273
//                String property,
274
//                Integer index,
275
//                Set<Object> parentBeans)
276
//            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
277
//    {
278
//        for (Object bean : parentBeans){
279
//
280
//            PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(bean, property);
281
//            if (logger.isTraceEnabled()){logger.trace("   unwrap " + node.toStringNoWildcard() + " ... ");}
282
//            // [1] initialize the bean named by property
283
//            Object unwrappedPropertyBean = invokeInitialization(bean, propertyDescriptor);
284
//            if (logger.isTraceEnabled()){logger.trace("   unwrap " + node.toStringNoWildcard() + " - DONE ");}
285
//
286
//
287
//            // [2]
288
//            // handle property
289
//            if(unwrappedPropertyBean != null ){
290
//                initializeNodeSinglePropertyOld(node, property, index, bean, unwrappedPropertyBean);
291
//            }
292
//        }
293
//    }
294

    
295
    /**
296
     * @param node
297
     * @param propertyValue
298
     * @param parentBean
299
     * @param param
300
     */
301
    private void preparePropertyValueForBulkLoadOrStore(BeanInitNode node,
302
            Object parentBean,
303
            String param,
304
            Object propertyValue)
305
    {
306
        BeanInitNode sibling = node.getSibling(param);
307

    
308
        if (propertyValue instanceof AbstractPersistentCollection ){
309
            //collections
310
            if (!node.hasWildcardToManySibling()){  //if wildcard sibling exists the lazies are already prepared there
311
                AbstractPersistentCollection collection = (AbstractPersistentCollection)propertyValue;
312
                if (collection.wasInitialized()){
313
                    storeInitializedCollection(collection, node, param);
314
                }else{
315
//						Class<?> parentClass = parentBean.getClass();
316
//						int parentId = ((CdmBase)parentBean).getId();
317
                    if (sibling != null){
318
                        sibling.putLazyCollection(collection);
319
                    }else{
320
                        node.putLazyCollection(collection);
321
                    }
322
                }
323
            }
324
        } else if (propertyValue instanceof CollectionProxy
325
                || propertyValue instanceof MapProxy<?, ?>
326
                || propertyValue instanceof SortedMapProxy<?, ?>){
327
            //hibernate envers collections
328
            //TODO check if other code works with audited data at all as we use HQL queries
329
            if (!node.hasWildcardToManySibling()){  //if wildcard sibling exists the lazies are already prepared there
330
                Collection<?> collection = (Collection<?>)propertyValue;
331
                //TODO it is difficult to find out if an envers collection is initiallized
332
                //but possiblie via reflection. If the "delegate" parameter is null it is not yet initialized.
333
                //However, as we do not know if envers initialization works at all together with the AdvancedBeanInitializer
334
                //we initialize each collection immediately here by calling size()
335
                collection.size();  //initialize
336
                storeInitializedEnversCollection(collection, node, param);
337
            }
338
        }else{
339
            //singles
340
            if (!node.hasWildcardToOneSibling()){  //if wildcard exists the lazies are already prepared there
341
                if (! Hibernate.isInitialized(propertyValue)){
342
                    if (propertyValue instanceof HibernateProxy){
343
                        Serializable id = ((HibernateProxy)propertyValue).getHibernateLazyInitializer().getIdentifier();
344
                        Class<?> persistedClass = ((HibernateProxy)propertyValue).getHibernateLazyInitializer().getPersistentClass();
345
                        if (sibling != null){
346
                            sibling.putLazyBean(persistedClass, id);
347
                        }else{
348
                            node.putLazyBean(persistedClass, id);
349
                        }
350

    
351
                    }else{
352
                        logger.warn("Lazy value is not of type HibernateProxy. This is not yet handled.");
353
                    }
354
                }else if (propertyValue == null){
355
                    // do nothing
356
                }else{
357
                    if (propertyValue instanceof HibernateProxy){  //TODO remove hibernate dependency
358
                        propertyValue = initializeInstance(propertyValue);
359
                    }
360
                    autoinitializeBean(propertyValue);
361
                    node.addBean(propertyValue);
362
                }
363
            }
364
        }
365
    }
366

    
367
    private void autoinitializeBean(Object bean) {
368
        invokePropertyAutoInitializers(bean);
369
    }
370

    
371
    private void autoinitializeBean(CdmBase bean, AutoInit autoInit) {
372
        for(AutoPropertyInitializer<CdmBase> init : autoInit.initlializers) {
373
            init.initialize(bean);
374
        }
375
    }
376

    
377
	private void storeInitializedCollection(AbstractPersistentCollection persistedCollection,
378
			BeanInitNode node, String param) {
379

    
380
	    Collection<?> collection;
381
		if (persistedCollection  instanceof Collection) {
382
			collection = (Collection<?>) persistedCollection;
383
		}else if (persistedCollection instanceof Map) {
384
			collection = ((Map<?,?>)persistedCollection).values();
385
		}else{
386
			throw new RuntimeException ("Non Map and non Collection cas not handled in storeInitializedCollection()");
387
		}
388
		for (Object value : collection){
389
			preparePropertyValueForBulkLoadOrStore(node, null, param, value);
390
		}
391
	}
392

    
393
	/**
394
	 * @see #storeInitializedCollection(AbstractPersistentCollection, BeanInitNode, String)alizedCollection
395
	 */
396
	private void storeInitializedEnversCollection(Collection<?> enversCollection,
397
            BeanInitNode node, String param) {
398
	    if (enversCollection instanceof CollectionProxy
399
                || enversCollection instanceof MapProxy<?, ?>
400
                || enversCollection instanceof SortedMapProxy<?, ?>){
401
	        Collection<?> collection;
402
	        if (enversCollection instanceof MapProxy
403
	                || enversCollection instanceof SortedMapProxy<?, ?>) {
404
	            collection = ((Map<?,?>)enversCollection).values();
405
	        }else if (enversCollection instanceof CollectionProxy) {
406
	            collection = enversCollection;
407
	        }else{
408
	            throw new RuntimeException ("Non MapProxy and non CollectionProxy case not handled in storeInitializedEnversCollection()");
409
	        }
410
	        for (Object value : collection){
411
	            preparePropertyValueForBulkLoadOrStore(node, null, param, value);
412
	        }
413
	    }
414
    }
415

    
416

    
417
	private void bulkLoadLazies(BeanInitNode node) {
418

    
419
		if (logger.isTraceEnabled()){logger.trace("bulk load " +  node);}
420

    
421
		//beans
422
		for (Class<?> clazz : node.getLazyBeans().keySet()){
423
			Set<Serializable> idSet = node.getLazyBeans().get(clazz);
424
			if (idSet != null && ! idSet.isEmpty()){
425

    
426
				if (logger.isTraceEnabled()){logger.trace("bulk load beans of class " +  clazz.getSimpleName());}
427
				//TODO use entity name
428
				String hql = " SELECT c FROM %s as c %s WHERE c.id IN (:idSet) ";
429
				AutoInit autoInit = addAutoinitFetchLoading(clazz, "c");
430
                hql = String.format(hql, clazz.getSimpleName(), autoInit.leftJoinFetch);
431
				if (logger.isTraceEnabled()){logger.trace(hql);}
432
				Query query = genericDao.getHqlQuery(hql);
433
				query.setParameterList("idSet", idSet);
434
				List<Object> list = query.list();
435

    
436
				if (logger.isTraceEnabled()){logger.trace("initialize bulk loaded beans of class " +  clazz.getSimpleName());}
437
				for (Object object : list){
438
					if (object instanceof HibernateProxy){  //TODO remove hibernate dependency
439
						object = initializeInstance(object);
440
					}
441
					autoinitializeBean((CdmBase)object, autoInit);
442
					node.addBean(object);
443
				}
444
				if (logger.isTraceEnabled()){logger.trace("bulk load - DONE");}
445
			}
446
		}
447
		node.resetLazyBeans();
448

    
449
		//collections
450
		for (Class<?> ownerClazz : node.getLazyCollections().keySet()){
451
			Map<String, Set<Serializable>> lazyParams = node.getLazyCollections().get(ownerClazz);
452
			for (String param : lazyParams.keySet()){
453
				Set<Serializable> idSet = lazyParams.get(param);
454
				if (idSet != null && ! idSet.isEmpty()){
455
					if (logger.isTraceEnabled()){logger.trace("bulk load " + node + " collections ; ownerClass=" +  ownerClazz.getSimpleName() + " ; param = " + param);}
456

    
457
					Type collectionEntitiyType = null;
458
					PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(ownerClazz);
459
					for(PropertyDescriptor d : descriptors) {
460
					    if(d.getName().equals(param)) {
461
					        Method readMethod = d.getReadMethod();
462
                            ParameterizedType pt = (ParameterizedType) readMethod.getGenericReturnType();
463
                            Type[] actualTypeArguments = pt.getActualTypeArguments();
464
                            if(actualTypeArguments.length == 2) {
465
                                // this must be a map of <Language, String> (aka LanguageString) there is no other case like this in the cdm
466
                                // in case of Maps the returned Collection will be the Collection of the values, so collectionEntitiyType is the
467
                                // second typeArgument
468
                                collectionEntitiyType = actualTypeArguments[1];
469
                            } else {
470
                                collectionEntitiyType = actualTypeArguments[0];
471
                            }
472
                            if(collectionEntitiyType instanceof TypeVariable) {
473
                                collectionEntitiyType = ((TypeVariable)collectionEntitiyType).getBounds()[0];
474
                            }
475
					    }
476
					}
477

    
478
					//TODO use entity name ??
479
					//get from repository
480
					List<Object[]> list;
481
					String hql = "SELECT oc " +
482
							" FROM %s as oc LEFT JOIN FETCH oc.%s as col %s" +
483
							" WHERE oc.id IN (:idSet) ";
484

    
485
					AutoInit autoInit = addAutoinitFetchLoading((Class)collectionEntitiyType, "col");
486
                    hql = String.format(hql, ownerClazz.getSimpleName(), param,
487
					        autoInit.leftJoinFetch);
488

    
489
					try {
490
						if (logger.isTraceEnabled()){logger.trace(hql);}
491
						Query query = genericDao.getHqlQuery(hql);
492
						query.setParameterList("idSet", idSet);
493
						list = query.list();
494
						if (logger.isTraceEnabled()){logger.trace("size of retrieved list is " + list.size());}
495
					} catch (HibernateException e) {
496
						e.printStackTrace();
497
						throw e;
498
					}
499

    
500
					//getTarget and add to child node
501
					if (logger.isTraceEnabled()){logger.trace("initialize bulk loaded " + node + " collections - DONE");}
502
					for (Object parentBean : list){
503
                        try {
504
						    Object propValue = PropertyUtils.getProperty(
505
						            parentBean,
506
						            mapFieldToPropertyName(param, parentBean.getClass().getSimpleName())
507
						          );
508

    
509
							if (propValue == null){
510
							    logger.trace("Collection is null");
511
							}else {
512
							    if(propValue instanceof PersistentMap) {
513
							        propValue = ((PersistentMap)propValue).values();
514
							    }
515
							    for(Object newBean : (Collection<Object>)propValue ) {
516
							        if(newBean instanceof HibernateProxy){
517
							            newBean = initializeInstance(newBean);
518
							        }
519

    
520
							        autoinitializeBean((CdmBase)newBean, autoInit);
521

    
522
							        node.addBean(newBean);
523
							    }
524
							}
525
                        } catch (Exception e) {
526
                            // TODO better throw an exception ?
527
                            logger.error("error while getting collection property", e);
528
                        }
529
					}
530
					if (logger.isTraceEnabled()){logger.trace("bulk load " + node + " collections - DONE");}
531
				}
532
			}
533
		}
534
		for (AbstractPersistentCollection collection : node.getUninitializedCollections()){
535
			if (! collection.wasInitialized()){  //should not happen anymore
536
				collection.forceInitialization();
537
				if (logger.isTraceEnabled()){logger.trace("forceInitialization of collection " + collection);}
538
			} else {
539
			    if (logger.isTraceEnabled()){logger.trace("collection " + collection + " is initialized - OK!");}
540
			}
541
		}
542

    
543
		node.resetLazyCollections();
544

    
545
		if (logger.isDebugEnabled()){logger.debug("bulk load " +  node + " - DONE ");}
546

    
547
	}
548

    
549

    
550
    private AutoInit addAutoinitFetchLoading(Class<?> clazz, String beanAlias) {
551

    
552
        AutoInit autoInit = new AutoInit();
553
        if(clazz != null) {
554
            Set<AutoPropertyInitializer<CdmBase>> inits = getAutoInitializers(clazz);
555
            for (AutoPropertyInitializer<CdmBase> init: inits){
556
                try {
557
                    autoInit.leftJoinFetch +=init.hibernateFetchJoin(clazz, beanAlias);
558
                } catch (Exception e) {
559
                    // the AutoPropertyInitializer is not supporting LEFT JOIN FETCH so it needs to be
560
                    // used explicitly
561
                    autoInit.initlializers.add(init);
562
                }
563

    
564
            }
565
        }
566
        return autoInit;
567
    }
568

    
569
    private Set<AutoPropertyInitializer<CdmBase>> getAutoInitializers(Class<?> clazz) {
570
        Set<AutoPropertyInitializer<CdmBase>> result = new HashSet<AutoPropertyInitializer<CdmBase>>();
571
        for(Class<? extends CdmBase> superClass : getBeanAutoInitializers().keySet()){
572
            if(superClass.isAssignableFrom(clazz)){
573
                result.add(getBeanAutoInitializers().get(superClass));
574
            }
575
        }
576
        return result;
577
    }
578

    
579
    /**
580
     * Rename hibernate (field) attribute to Bean property name, due to bean inconsistencies
581
     * #3841
582
     * @param param
583
     * @param ownerClass
584
     * @return
585
     */
586
    private String mapFieldToPropertyName(String param, String ownerClass) {
587
        if (ownerClass.contains("Description") && param.equals("descriptionElements")){
588
            return "elements";
589
        }
590
        if (ownerClass.startsWith("FeatureNode") && param.equals("children")) {
591
            return "childNodes";
592
        }
593
        if (ownerClass.startsWith("Media") && param.equals("description")) {
594
            return "allDescriptions";
595
        }
596
        else{
597
            return param;
598
        }
599
    }
600

    
601
    /**
602
     * @param node
603
     * @param property
604
     * @param index
605
     * @param bean
606
     * @param unwrappedPropertyBean
607
     */
608
    private void initializeNodeSinglePropertyOld(BeanInitNode node, String property,
609
            Integer index, Object bean, Object unwrappedPropertyBean) {
610
        Collection<?> collection = null;
611
        if(Map.class.isAssignableFrom(unwrappedPropertyBean.getClass())) {
612
            collection = ((Map<?,?>)unwrappedPropertyBean).values();
613
        }else if (Collection.class.isAssignableFrom(unwrappedPropertyBean.getClass())) {
614
            collection =  (Collection<?>) unwrappedPropertyBean;
615
        }
616
        if (collection != null){
617
            //collection or map
618
            if (logger.isTraceEnabled()){logger.trace(" initialize collection for " + node.toStringNoWildcard() + " ... ");}
619
            int i = 0;
620
            for (Object entrybean : collection) {
621
                if(index == null){
622
                    node.addBean(entrybean);
623
                } else if(index.equals(i)){
624
                    node.addBean(entrybean);
625
                    break;
626
                }
627
                i++;
628
            }
629
            if (logger.isTraceEnabled()){logger.trace(" initialize collection for " + node.toString() + " - DONE ");}
630

    
631
        }else {
632
            // nested bean
633
            node.addBean(unwrappedPropertyBean);
634
            setProperty(bean, property, unwrappedPropertyBean);
635
        }
636
    }
637

    
638
    private class AutoInit{
639

    
640
        String leftJoinFetch = "";
641
        Set<AutoPropertyInitializer<CdmBase>> initlializers = new HashSet<AutoPropertyInitializer<CdmBase>>();
642

    
643
        /**
644
         * @param leftJoinFetch
645
         * @param initlializers
646
         */
647
        public AutoInit() {
648
        }
649
    }
650
}
(2-2/12)