ref #6630 and ref #6368 remove generics from TaxonName
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / persistence / dao / initializer / TitleAndNameCacheAutoInitializer.java
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.TaxonName;
14 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
15
16 /**
17 * Initializes the various title and name caches of cdm types.
18 * <p>
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
24 * individually.
25 *
26 * @author a.kohlbecker
27 * @date 30.07.2010
28 *
29 */
30 public class TitleAndNameCacheAutoInitializer extends AutoPropertyInitializer<IdentifiableEntity<?>> {
31
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 String hibernateFetchJoin(Class<?> clazz, String beanAlias) throws Exception{
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 throw new Exception();
110
111 // return result;
112 }
113
114
115 }