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.strategy.cache.taxon;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.List;
|
13
|
import java.util.UUID;
|
14
|
|
15
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
16
|
import eu.etaxonomy.cdm.format.reference.OriginalSourceFormatter;
|
17
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
18
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
19
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
20
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
21
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
22
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
23
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
24
|
import eu.etaxonomy.cdm.strategy.StrategyBase;
|
25
|
import eu.etaxonomy.cdm.strategy.cache.HTMLTagRules;
|
26
|
import eu.etaxonomy.cdm.strategy.cache.TagEnum;
|
27
|
import eu.etaxonomy.cdm.strategy.cache.TaggedCacheHelper;
|
28
|
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
|
29
|
import eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy;
|
30
|
import eu.etaxonomy.cdm.strategy.cache.name.INonViralNameCacheStrategy;
|
31
|
|
32
|
public class TaxonBaseDefaultCacheStrategy<T extends TaxonBase>
|
33
|
extends StrategyBase
|
34
|
implements ITaxonCacheStrategy<T> {
|
35
|
|
36
|
private static final long serialVersionUID = 5769890979070021350L;
|
37
|
|
38
|
final static UUID uuid = UUID.fromString("931e48f0-2033-11de-8c30-0800200c9a66");
|
39
|
|
40
|
@Override
|
41
|
protected UUID getUuid() {
|
42
|
return uuid;
|
43
|
}
|
44
|
|
45
|
@Override
|
46
|
public String getTitleCache(T taxonBase) {
|
47
|
return getTitleCache(taxonBase, null);
|
48
|
}
|
49
|
|
50
|
@Override
|
51
|
public List<TaggedText> getTaggedTitle(T taxonBase) {
|
52
|
if (taxonBase == null){
|
53
|
return null;
|
54
|
}
|
55
|
|
56
|
List<TaggedText> tags = new ArrayList<>();
|
57
|
|
58
|
if (taxonBase.isProtectedTitleCache()){
|
59
|
//protected title cache
|
60
|
tags.add(new TaggedText(TagEnum.name, taxonBase.getTitleCache()));
|
61
|
return tags;
|
62
|
}
|
63
|
|
64
|
if (taxonBase.isDoubtful()){
|
65
|
tags.add(new TaggedText(TagEnum.separator, "?"));
|
66
|
}
|
67
|
|
68
|
boolean isMisapplication = isMisapplication(taxonBase);
|
69
|
//name
|
70
|
List<TaggedText> nameTags = getNameTags(taxonBase, isMisapplication);
|
71
|
|
72
|
if (nameTags.size() > 0){
|
73
|
tags.addAll(nameTags);
|
74
|
}else{
|
75
|
tags.add(new TaggedText(TagEnum.fullName, "???"));
|
76
|
}
|
77
|
|
78
|
boolean isSynonym = taxonBase.isInstanceOf(Synonym.class);
|
79
|
String secSeparator = isMisapplication ? " sensu " : (isSynonym? " syn." : "") + " sec. ";
|
80
|
//not used: we currently use a post-separator in the name tags
|
81
|
// if (nameTags.get(nameTags.size() - 1).getType().equals(TagEnum.nomStatus)){
|
82
|
// secSeparator = "," + secSeparator;
|
83
|
// }
|
84
|
|
85
|
//sec.
|
86
|
List<TaggedText> secTags = getSecundumTags(taxonBase);
|
87
|
if (!secTags.isEmpty()){
|
88
|
tags.add(new TaggedText(TagEnum.separator, secSeparator));
|
89
|
tags.addAll(secTags);
|
90
|
}
|
91
|
|
92
|
if (isMisapplication){
|
93
|
TaxonName name = CdmBase.deproxy(taxonBase.getName());
|
94
|
if (name != null && isNotBlank(name.getAuthorshipCache())){
|
95
|
tags.add(new TaggedText(TagEnum.separator, ", non "));
|
96
|
tags.add(new TaggedText(TagEnum.authors, name.getAuthorshipCache()));
|
97
|
}
|
98
|
}
|
99
|
|
100
|
return tags;
|
101
|
}
|
102
|
|
103
|
private boolean isMisapplication(T taxonBase) {
|
104
|
if (! taxonBase.isInstanceOf(Taxon.class)){
|
105
|
return false;
|
106
|
}else{
|
107
|
return CdmBase.deproxy(taxonBase, Taxon.class).isMisapplicationOnly();
|
108
|
}
|
109
|
}
|
110
|
|
111
|
private List<TaggedText> getNameTags(T taxonBase, boolean useNameCache) {
|
112
|
List<TaggedText> tags = new ArrayList<>();
|
113
|
TaxonName name = CdmBase.deproxy(taxonBase.getName());
|
114
|
|
115
|
if (name != null){
|
116
|
INameCacheStrategy nameCacheStrategy = name.getCacheStrategy();
|
117
|
useNameCache = (useNameCache || taxonBase.isUseNameCache()) && name.isNonViral() && nameCacheStrategy instanceof INonViralNameCacheStrategy;
|
118
|
if (useNameCache){
|
119
|
INonViralNameCacheStrategy nvnCacheStrategy = (INonViralNameCacheStrategy)nameCacheStrategy;
|
120
|
List<TaggedText> nameCacheTags = nvnCacheStrategy.getTaggedName(name);
|
121
|
tags.addAll(nameCacheTags);
|
122
|
}else{
|
123
|
List<TaggedText> nameTags = nameCacheStrategy.getTaggedTitle(name);
|
124
|
tags.addAll(nameTags);
|
125
|
List<TaggedText> statusTags = nameCacheStrategy.getNomStatusTags(name, true, true);
|
126
|
tags.addAll(statusTags);
|
127
|
}
|
128
|
if (isNotBlank(taxonBase.getAppendedPhrase())){
|
129
|
tags.add(new TaggedText(TagEnum.appendedPhrase, taxonBase.getAppendedPhrase().trim()));
|
130
|
}
|
131
|
}
|
132
|
|
133
|
return tags;
|
134
|
}
|
135
|
|
136
|
private List<TaggedText> getSecundumTags(T taxonBase) {
|
137
|
List<TaggedText> tags = new ArrayList<>();
|
138
|
|
139
|
Reference sec = taxonBase.getSec();
|
140
|
sec = HibernateProxyHelper.deproxy(sec);
|
141
|
String secRef;
|
142
|
if (sec == null){
|
143
|
//missing sec
|
144
|
if (isBlank(taxonBase.getAppendedPhrase())){
|
145
|
secRef = "???";
|
146
|
}else{
|
147
|
secRef = null;
|
148
|
}
|
149
|
}
|
150
|
else{
|
151
|
//existing sec
|
152
|
if (sec.isProtectedTitleCache() == false &&
|
153
|
sec.getCacheStrategy() != null &&
|
154
|
sec.getAuthorship() != null &&
|
155
|
isNotBlank(sec.getAuthorship().getTitleCache()) &&
|
156
|
isNotBlank(sec.getYear())){
|
157
|
secRef = OriginalSourceFormatter.INSTANCE.format(sec, null); //microRef is handled later
|
158
|
}else if ((sec.isWebPage() || sec.isDatabase() || sec.isMap())
|
159
|
&& titleExists(sec)){ //maybe we should also test protected caches (but which one, the abbrev cache or the titleCache?
|
160
|
secRef = isNotBlank(sec.getAbbrevTitle())? sec.getAbbrevTitle() : sec.getTitle();
|
161
|
String secDate = sec.getYear();
|
162
|
if (isBlank(secDate) && sec.getAccessed() != null){
|
163
|
secDate = String.valueOf(sec.getAccessed().getYear());
|
164
|
}
|
165
|
secRef = CdmUtils.concat(" ", secRef, secDate);
|
166
|
}else{
|
167
|
secRef = sec.getTitleCache();
|
168
|
//TODO maybe not always correct
|
169
|
if (secTitleTrailingDotShouldBeRemoved(sec)){
|
170
|
secRef = CdmUtils.removeTrailingDots(secRef);
|
171
|
}
|
172
|
}
|
173
|
}
|
174
|
if (secRef != null){
|
175
|
tags.add(new TaggedText(TagEnum.secReference, secRef));
|
176
|
}
|
177
|
//secMicroReference
|
178
|
if (isNotBlank(taxonBase.getSecMicroReference())){
|
179
|
tags.add(new TaggedText(TagEnum.separator, ": "));
|
180
|
tags.add(new TaggedText(TagEnum.secMicroReference, taxonBase.getSecMicroReference()));
|
181
|
}
|
182
|
return tags;
|
183
|
}
|
184
|
|
185
|
|
186
|
private boolean secTitleTrailingDotShouldBeRemoved(Reference sec) {
|
187
|
if (sec.isProtectedTitleCache()){
|
188
|
return false;
|
189
|
}else if (sec.getAbbrevTitle()!= null && sec.getTitleCache().endsWith(sec.getAbbrevTitle())){
|
190
|
return false;
|
191
|
}else if (sec.getTitle() != null && sec.getTitle().endsWith(".") && sec.getTitleCache().endsWith(sec.getTitle())){
|
192
|
return false;
|
193
|
}
|
194
|
return true;
|
195
|
}
|
196
|
|
197
|
private boolean titleExists(Reference ref) {
|
198
|
return isNotBlank(ref.getAbbrevTitle()) || isNotBlank(ref.getTitle());
|
199
|
}
|
200
|
|
201
|
@Override
|
202
|
public String getTitleCache(T taxonBase, HTMLTagRules htmlTagRules) {
|
203
|
List<TaggedText> tags = getTaggedTitle(taxonBase);
|
204
|
if (tags == null){
|
205
|
return null;
|
206
|
}else{
|
207
|
String result = TaggedCacheHelper.createString(tags, htmlTagRules);
|
208
|
return result;
|
209
|
}
|
210
|
}
|
211
|
}
|