Project

General

Profile

Download (5.14 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.persistence.dao.initializer;
10

    
11
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
12
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
13
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
14
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
15

    
16
/**
17
 * Initializes the various title and name caches of cdm types.
18
 * <p>
19
 * Title caches are constructed by a cascade of methods which are calling other
20
 * title cache methods. If a more general cache is protected we still need to
21
 * check if the more special caches are not protected and initialize those.
22
 * Otherwise unexpected <code>LazyLoadingExceptions</code> may occur. Since this
23
 * cascade is different in the various name types we must check each of them
24
 * individually.
25
 *
26
 * @author a.kohlbecker
27
 * @date 30.07.2010
28
 *
29
 */
30
public class TitleAndNameCacheAutoInitializer extends AutoPropertyInitializer<IdentifiableEntity<?>> {
31

    
32

    
33
    /* (non-Javadoc)
34
     * @see eu.etaxonomy.cdm.persistence.dao.BeanAutoInitializer#initialize(eu.etaxonomy.cdm.model.common.CdmBase)
35
     */
36
    @Override
37
    public void initialize(IdentifiableEntity<?> bean) {
38

    
39
        bean = HibernateProxyHelper.deproxy(bean, IdentifiableEntity.class);
40
        // we will implement a bit of redundancy here in order
41
        // to avoid too much casting
42
        if(bean instanceof TaxonNameBase){
43
            // ---> NonViralName
44
            TaxonNameBase n = (TaxonNameBase)bean;
45
            if(!n.isProtectedFullTitleCache())  {
46
                n.getFullTitleCache();
47
            } else if(!bean.isProtectedTitleCache()){
48
                n.getTitleCache();
49
            } else if(!n.isProtectedNameCache())  {
50
                // n.getNameCache(); not needed here
51
                // since this is covered by
52
                // getTaggedName special case  below
53
            } else if(!n.isProtectedAuthorshipCache()){
54
                n.getAuthorshipCache();
55
            }
56

    
57
            if(!n.isProtectedFullTitleCache() || !bean.isProtectedTitleCache() || !n.isProtectedNameCache()){
58
                /* getTaggedName special case
59
                 *
60
                 * if the name cache already is non null the generateNameCache()
61
                 * method will not be executed and no initialization of the name cache
62
                 * cascade will happen, therefore me must call the getTaggedName()
63
                 * explicitly in order to trigger the cascade. Otherwise a
64
                 * LazyInitializationException can occur:
65
                 *   > failed to lazily initialize a collection of role:
66
                 *   > eu.etaxonomy.cdm.model.name.TaxonNameBase.relationsToThisName
67
                 * --------------------------------------------
68
                 * according code snipped from cachestrategy:
69
                 *  if (nameCache == null){
70
                 *    this.nameCache = generateNameCache();
71
                 *  }
72
                 * --------------------------------------------
73
                 */
74
                n.getTaggedName();
75
            }
76
        } else if(bean instanceof TaxonNameBase) {
77
             // ---> TaxonNameBase
78
            TaxonNameBase<?,?> n = (TaxonNameBase<?,?>)bean;
79
            if(!n.isProtectedFullTitleCache())  {
80
                n.getFullTitleCache();
81
            } else if(!bean.isProtectedTitleCache()){
82
                n.getTitleCache();
83
            }
84
        } else if(bean instanceof TaxonBase)  {
85
            ((TaxonBase)bean).getTaggedTitle();
86
        } else if(!bean.isProtectedTitleCache()){
87
            // ---> all other IdentifiableEntity
88
            bean.getTitleCache();
89
        }
90
    }
91

    
92
    @Override
93
    public String hibernateFetchJoin(Class<?> clazz, String beanAlias) throws Exception{
94

    
95
        String result = "";
96
        if (TaxonNameBase.class.isAssignableFrom(clazz)){
97
            result += String.format(" LEFT JOIN FETCH %s.rank ", beanAlias);
98
            result += String.format(" LEFT JOIN FETCH %s.relationsToThisName relTo LEFT JOIN FETCH relTo.relatedFrom ", beanAlias);
99
            result += String.format(" LEFT JOIN FETCH %s.combinationAuthorship ", beanAlias);
100
            result += String.format(" LEFT JOIN FETCH %s.exCombinationAuthorship ", beanAlias);
101
            result += String.format(" LEFT JOIN FETCH %s.basionymAuthorship ", beanAlias);
102
            result += String.format(" LEFT JOIN FETCH %s.exBasionymAuthorship ", beanAlias);
103
        }
104
        if (TaxonBase.class.isAssignableFrom(clazz)){
105
            // TODO with the TaxonBase.getTaggedTtile() this is getting much more complicated
106
            result += String.format(" JOIN FETCH %s.sec secRef LEFT JOIN FETCH secRef.authorship ", beanAlias);
107
            // TODO initialize all instances related to the name titleCache, see above
108
        }
109

    
110
        // throw an exception since LEFT JOIN FETCH is not really working for titleCaches
111
        // TODO test if the LEFT JOIN FETCHes are at least working for TaxonNameBase and NonViralName
112
        throw new Exception();
113

    
114
//        return result;
115
    }
116

    
117

    
118
}
(12-12/13)