fix mumissing multilanaguage text (and similar) bug
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / persistence / dao / initializer / TitleAndNameCacheAutoInitializer.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.cdm.persistence.dao.initializer;
11
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;
16
17 /**
18 * Initializes the various title and name caches of cdm types.
19 * <p>
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
25 * individually.
26 *
27 * @author a.kohlbecker
28 * @date 30.07.2010
29 *
30 */
31 public class TitleAndNameCacheAutoInitializer extends AutoPropertyInitializer<IdentifiableEntity<?>> {
32
33
34 /* (non-Javadoc)
35 * @see eu.etaxonomy.cdm.persistence.dao.BeanAutoInitializer#initialize(eu.etaxonomy.cdm.model.common.CdmBase)
36 */
37 @Override
38 public void initialize(IdentifiableEntity<?> bean) {
39
40
41 // we will implement a bit of redundancy here in order
42 // to avoid too much casting
43 if(bean instanceof NonViralName){
44 // ---> NonViralName
45 NonViralName<?> n = (NonViralName<?>)bean;
46 if(!n.isProtectedFullTitleCache()) {
47 n.getFullTitleCache();
48 } else if(!bean.isProtectedTitleCache()){
49 n.getTitleCache();
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();
56 }
57
58 if(!n.isProtectedFullTitleCache() || !bean.isProtectedTitleCache() || !n.isProtectedNameCache()){
59 /* getTaggedName special case
60 *
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();
72 * }
73 * --------------------------------------------
74 */
75 n.getTaggedName();
76 }
77 } else if(bean instanceof TaxonNameBase) {
78 // ---> TaxonNameBase
79 TaxonNameBase<?,?> n = (TaxonNameBase<?,?>)bean;
80 if(!n.isProtectedFullTitleCache()) {
81 n.getFullTitleCache();
82 } else if(!bean.isProtectedTitleCache()){
83 n.getTitleCache();
84 }
85 } else if(!bean.isProtectedTitleCache()){
86 // ---> all other IdentifiableEntity
87 bean.getTitleCache();
88 }
89 }
90
91 @Override
92 public String hibernateFetchJoin(Class<?> clazz, String beanAlias){
93 String result = "";
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);
102 }
103 }
104 return result;
105 }
106
107
108 }