Project

General

Profile

Download (5.29 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.NonViralName;
14
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
15
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
16

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

    
33

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

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

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

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

    
96

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

    
114
        // throw an exception since LEFT JOIN FETCH is not really working for titleCaches
115
        // TODO test if the LEFT JOIN FETCHes are at least working for TaxonNameBase and NonViralName
116
        throw new Exception();
117

    
118
//        return result;
119
    }
120

    
121

    
122
}
(12-12/13)