2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.persistence
.dao
.initializer
;
11 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
12 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
13 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
14 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
17 * Initializes the various title and name caches of cdm types.
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
26 * @author a.kohlbecker
30 public class TitleAndNameCacheAutoInitializer
extends AutoPropertyInitializer
<IdentifiableEntity
<?
>> {
34 public void initialize(IdentifiableEntity
<?
> bean
) {
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
){
41 TaxonName n
= (TaxonName
)bean
;
42 if(!n
.isProtectedFullTitleCache()) {
43 n
.getFullTitleCache();
44 } else if(!bean
.isProtectedTitleCache()){
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();
54 if(!n
.isProtectedFullTitleCache() || !bean
.isProtectedTitleCache() || !n
.isProtectedNameCache()){
55 /* getTaggedName special case
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();
69 * --------------------------------------------
73 } else if(bean
instanceof TaxonName
) {
75 TaxonName n
= (TaxonName
)bean
;
76 if(!n
.isProtectedFullTitleCache()) {
77 n
.getFullTitleCache();
78 } else if(!bean
.isProtectedTitleCache()){
81 } else if(bean
instanceof TaxonBase
) {
82 ((TaxonBase
)bean
).getTaggedTitle();
83 } else if(!bean
.isProtectedTitleCache()){
84 // ---> all other IdentifiableEntity
90 public String
hibernateFetchJoin(Class
<?
> clazz
, String beanAlias
) throws Exception
{
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
);
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
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 throw new Exception();