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.agent.Team;
|
15
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
16
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
17
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
18
|
|
19
|
/**
|
20
|
* Initializes the various title and name caches of cdm types.
|
21
|
* <p>
|
22
|
* Title caches are constructed by a cascade of methods which are calling other
|
23
|
* title cache methods. If a more general cache is protected we still need to
|
24
|
* check if the more special caches are not protected and initialize those.
|
25
|
* Otherwise unexpected <code>LazyLoadingExceptions</code> may occur. Since this
|
26
|
* cascade is different in the various name types we must check each of them
|
27
|
* individually.
|
28
|
*
|
29
|
* @author a.kohlbecker
|
30
|
* @since 30.07.2010
|
31
|
*/
|
32
|
public class TitleAndNameCacheAutoInitializer extends AutoPropertyInitializer<IdentifiableEntity<?>> {
|
33
|
|
34
|
@Override
|
35
|
public void initialize(IdentifiableEntity<?> bean) {
|
36
|
|
37
|
bean = HibernateProxyHelper.deproxy(bean, IdentifiableEntity.class);
|
38
|
// we will implement a bit of redundancy here in order
|
39
|
// to avoid too much casting
|
40
|
if(bean instanceof TaxonName){
|
41
|
// ---> NonViralName
|
42
|
TaxonName n = (TaxonName)bean;
|
43
|
if(!n.isProtectedFullTitleCache()) {
|
44
|
n.getFullTitleCache();
|
45
|
} else if(!bean.isProtectedTitleCache()){
|
46
|
n.getTitleCache();
|
47
|
} else if(!n.isProtectedNameCache()) {
|
48
|
// n.getNameCache(); not needed here
|
49
|
// since this is covered by
|
50
|
// getTaggedName special case below
|
51
|
} else if(!n.isProtectedAuthorshipCache()){
|
52
|
n.getAuthorshipCache();
|
53
|
}
|
54
|
|
55
|
if(!n.isProtectedFullTitleCache() || !bean.isProtectedTitleCache() || !n.isProtectedNameCache()){
|
56
|
/* getTaggedName special case
|
57
|
*
|
58
|
* if the name cache already is not null the generateNameCache()
|
59
|
* method will not be executed and no initialization of the name cache
|
60
|
* cascade will happen, therefore me must call the getTaggedName()
|
61
|
* explicitly in order to trigger the cascade. Otherwise a
|
62
|
* LazyInitializationException can occur:
|
63
|
* > failed to lazily initialize a collection of role:
|
64
|
* > eu.etaxonomy.cdm.model.name.TaxonName.relationsToThisName
|
65
|
* --------------------------------------------
|
66
|
* according code snipped from cachestrategy:
|
67
|
* if (nameCache == null){
|
68
|
* this.nameCache = generateNameCache();
|
69
|
* }
|
70
|
* --------------------------------------------
|
71
|
*/
|
72
|
n.getTaggedName();
|
73
|
}
|
74
|
} else if(bean instanceof TaxonBase) {
|
75
|
if (!bean.isProtectedTitleCache()){
|
76
|
TaxonBase<?> taxonBase = (TaxonBase<?>)bean;
|
77
|
taxonBase.getTaggedTitle();
|
78
|
//#10090 alternative solution for initializing sec.collectorTitleCache, but this solution initializes sec-author.teamMembers unnecessarily therefore it is not used.
|
79
|
//if(taxonBase.getSecSource()!=null && taxonBase.getSec() != null && taxonBase.getSec().getAuthorship() != null) {
|
80
|
// this.initialize(taxonBase.getSec().getAuthorship()); //to initialize xxxTitleCaches of authors (authors will be tried to be initialized
|
81
|
//}
|
82
|
}
|
83
|
} else if(bean instanceof Team) {
|
84
|
Team team = (Team)bean;
|
85
|
if (!bean.isProtectedTitleCache()){
|
86
|
bean.getTitleCache();
|
87
|
}
|
88
|
if (!team.isProtectedCollectorTitleCache()){
|
89
|
team.getCollectorTitleCache();
|
90
|
}
|
91
|
if (!team.isProtectedNomenclaturalTitleCache()){
|
92
|
team.getNomenclaturalTitleCache();
|
93
|
}
|
94
|
} else if(!bean.isProtectedTitleCache()){
|
95
|
// ---> all other IdentifiableEntity
|
96
|
bean.getTitleCache();
|
97
|
}
|
98
|
}
|
99
|
|
100
|
@Override
|
101
|
public Optional<String> hibernateFetchJoin(Class<?> clazz, String beanAlias){
|
102
|
|
103
|
String result = "";
|
104
|
if (TaxonName.class.isAssignableFrom(clazz)){
|
105
|
result += String.format(" LEFT JOIN FETCH %s.rank ", beanAlias);
|
106
|
result += String.format(" LEFT JOIN FETCH %s.relationsToThisName relTo LEFT JOIN FETCH relTo.relatedFrom ", beanAlias);
|
107
|
result += String.format(" LEFT JOIN FETCH %s.combinationAuthorship ", beanAlias);
|
108
|
result += String.format(" LEFT JOIN FETCH %s.exCombinationAuthorship ", beanAlias);
|
109
|
result += String.format(" LEFT JOIN FETCH %s.basionymAuthorship ", beanAlias);
|
110
|
result += String.format(" LEFT JOIN FETCH %s.exBasionymAuthorship ", beanAlias);
|
111
|
}
|
112
|
if (TaxonBase.class.isAssignableFrom(clazz)){
|
113
|
// TODO with the TaxonBase.getTaggedTtile() this is getting much more complicated
|
114
|
result += String.format(" JOIN FETCH %s.sec secRef LEFT JOIN FETCH secRef.authorship ", beanAlias);
|
115
|
// TODO initialize all instances related to the name titleCache, see above
|
116
|
}
|
117
|
|
118
|
// throw an exception since LEFT JOIN FETCH is not really working for titleCaches
|
119
|
// TODO test if the LEFT JOIN FETCHes are at least working for TaxonName and NonViralName
|
120
|
return Optional.empty();
|
121
|
|
122
|
}
|
123
|
}
|