merge trunk to cdm-3.3 branch
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / agent / TeamOrPersonBase.java
1 /**
2 * Copyright (C) 2007 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.model.agent;
10
11 import javax.persistence.Entity;
12 import javax.persistence.Transient;
13 import javax.validation.constraints.Size;
14 import javax.xml.bind.annotation.XmlAccessType;
15 import javax.xml.bind.annotation.XmlAccessorType;
16 import javax.xml.bind.annotation.XmlElement;
17 import javax.xml.bind.annotation.XmlTransient;
18 import javax.xml.bind.annotation.XmlType;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.apache.log4j.Logger;
22 import org.hibernate.envers.Audited;
23 import org.hibernate.search.annotations.Field;
24 import org.hibernate.search.annotations.Index;
25 import org.hibernate.search.annotations.Indexed;
26
27 import eu.etaxonomy.cdm.strategy.cache.agent.INomenclaturalAuthorCacheStrategy;
28 import eu.etaxonomy.cdm.validation.annotation.NullOrNotEmpty;
29
30
31 /**
32 * The abstract class for such {@link AgentBase agents} ({@link Person persons} or {@link Team teams}) who might also be used
33 * for authorship of {@link eu.etaxonomy.cdm.model.reference.Reference references} or of {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon names}.
34 *
35 * @author a.mueller
36 * @version 1.0
37 * @created 17-APR-2008
38 */
39 @XmlAccessorType(XmlAccessType.FIELD)
40 @XmlType(name = "TeamOrPersonBase", propOrder = {
41 "nomenclaturalTitle"
42 })
43 @Entity
44 @Audited
45 // even if hibernate complains "Abstract classes can never insert index documents. Remove @Indexed."
46 // this is needed, otherwise the fields of the also abstract super class are missed during indexing
47 @Indexed(index = "eu.etaxonomy.cdm.model.agent.AgentBase")
48 public abstract class TeamOrPersonBase<T extends TeamOrPersonBase<?>> extends AgentBase<INomenclaturalAuthorCacheStrategy<T>> implements INomenclaturalAuthor {
49 private static final long serialVersionUID = 5216821307314001961L;
50 public static final Logger logger = Logger.getLogger(TeamOrPersonBase.class);
51
52 @XmlElement(name="NomenclaturalTitle")
53 @Field(index=Index.YES)
54 //TODO Val #3379
55 // @NullOrNotEmpty
56 @Size(max = 255)
57 protected String nomenclaturalTitle;
58
59 @Transient
60 @XmlTransient
61 protected boolean isGeneratingTitleCache = false; //state variable to avoid recursions when generating title cache and nomenclatural title
62
63 /**
64 * Returns the identification string (nomenclatural abbreviation) used in
65 * nomenclature for this {@link Person person} or this {@link Team team}.
66 *
67 * @see INomenclaturalAuthor#getNomenclaturalTitle()
68 */
69 @Override
70 @Transient
71 public String getNomenclaturalTitle() {
72 String result = nomenclaturalTitle;
73 if (StringUtils.isBlank(nomenclaturalTitle) && (isGeneratingTitleCache == false)){
74 result = getTitleCache();
75 }
76 return result;
77 }
78
79 /**
80 * @see #getNomenclaturalTitle()
81 */
82 @Override
83 public void setNomenclaturalTitle(String nomenclaturalTitle) {
84 this.nomenclaturalTitle = nomenclaturalTitle;
85 }
86
87 /* (non-Javadoc)
88 * @see eu.etaxonomy.cdm.model.common.IdentifiableEntity#getTitleCache()
89 */
90 @Override
91 @Transient /*
92 TODO is the transient annotation still needed, can't we remove this ??
93 @Transient is an absolutely special case and thus leads to several
94 special implementations in order to harmonize this exception again
95 in other parts of the library:
96 - eu.etaxonomy.cdm.remote.controller.AgentController.doGetTitleCache()
97 - eu.etaxonomy.cdm.remote.json.processor.bean.TeamOrPersonBaseBeanProcessor
98
99 [a.kohlbecker May 2011]
100 */
101 public String getTitleCache() {
102 isGeneratingTitleCache = true;
103 String result = super.getTitleCache();
104 result = replaceEmptyTitleByNomTitle(result);
105 isGeneratingTitleCache = false;
106 return result;
107 }
108
109 /**
110 * @param result
111 * @return
112 */
113 protected String replaceEmptyTitleByNomTitle(String result) {
114 if (StringUtils.isBlank(result)){
115 result = nomenclaturalTitle;
116 }
117 if (StringUtils.isBlank(result)){
118 result = super.getTitleCache();
119 }
120 return result;
121 }
122
123
124 }