3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.persistence
.dao
.initializer
;
12 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
13 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
14 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
15 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
18 * Initializes the various title and name caches of cdm types.
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
27 * @author a.kohlbecker
31 public class TitleAndNameCacheAutoInitializer
extends AutoPropertyInitializer
<IdentifiableEntity
<?
>> {
35 * @see eu.etaxonomy.cdm.persistence.dao.BeanAutoInitializer#initialize(eu.etaxonomy.cdm.model.common.CdmBase)
38 public void initialize(IdentifiableEntity
<?
> bean
) {
41 // we will implement a bit of redundancy here in order
42 // to avoid too much casting
43 if(bean
instanceof NonViralName
){
45 NonViralName
<?
> n
= (NonViralName
<?
>)bean
;
46 if(!n
.isProtectedFullTitleCache()) {
47 n
.getFullTitleCache();
48 } else if(!bean
.isProtectedTitleCache()){
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();
58 if(!n
.isProtectedFullTitleCache() || !bean
.isProtectedTitleCache() || !n
.isProtectedNameCache()){
59 /* getTaggedName special case
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();
73 * --------------------------------------------
77 } else if(bean
instanceof TaxonNameBase
) {
79 TaxonNameBase
<?
,?
> n
= (TaxonNameBase
<?
,?
>)bean
;
80 if(!n
.isProtectedFullTitleCache()) {
81 n
.getFullTitleCache();
82 } else if(!bean
.isProtectedTitleCache()){
85 } else if(!bean
.isProtectedTitleCache()){
86 // ---> all other IdentifiableEntity
92 public String
hibernateFetchJoin(Class
<?
> clazz
, String beanAlias
){
94 if (TaxonNameBase
.class.isAssignableFrom(clazz
)){
95 result
+= String
.format(" LEFT JOIN FETCH %s.rank ", beanAlias
);
96 result
+= String
.format(" LEFT JOIN FETCH %s.relationsToThisName rel LEFT JOIN FETCH rel.relatedFrom ", beanAlias
);
97 if (NonViralName
.class.isAssignableFrom(clazz
)){
98 result
+= String
.format(" LEFT JOIN FETCH %s.combinationAuthorTeam ", beanAlias
);
99 result
+= String
.format(" LEFT JOIN FETCH %s.exCombinationAuthorTeam ", beanAlias
);
100 result
+= String
.format(" LEFT JOIN FETCH %s.basionymAuthorTeam ", beanAlias
);
101 result
+= String
.format(" LEFT JOIN FETCH %s.exBasionymAuthorTeam ", beanAlias
);