Project

General

Profile

Download (4.94 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 java.util.Optional;
12

    
13
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
14
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
15
import eu.etaxonomy.cdm.model.name.TaxonName;
16
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
17

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

    
33
    @Override
34
    public void initialize(IdentifiableEntity<?> bean) {
35

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

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

    
89
    @Override
90
    public Optional<String> hibernateFetchJoin(Class<?> clazz, String beanAlias){
91

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

    
107
        // throw an exception since LEFT JOIN FETCH is not really working for titleCaches
108
        // TODO test if the LEFT JOIN FETCHes are at least working for TaxonName and NonViralName
109
        return Optional.empty();
110

    
111
    }
112
}
(14-14/15)