1
|
package eu.etaxonomy.cdm.strategy.cache.taxon;
|
2
|
|
3
|
|
4
|
/**
|
5
|
* Copyright (C) 2015 EDIT
|
6
|
* European Distributed Institute of Taxonomy
|
7
|
* http://www.e-taxonomy.eu
|
8
|
*
|
9
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
10
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
11
|
*/
|
12
|
|
13
|
|
14
|
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.List;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import org.apache.commons.lang.StringUtils;
|
20
|
|
21
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
22
|
import eu.etaxonomy.cdm.model.agent.Person;
|
23
|
import eu.etaxonomy.cdm.model.agent.Team;
|
24
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
25
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
26
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
27
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
28
|
import eu.etaxonomy.cdm.strategy.StrategyBase;
|
29
|
import eu.etaxonomy.cdm.strategy.cache.HTMLTagRules;
|
30
|
import eu.etaxonomy.cdm.strategy.cache.TagEnum;
|
31
|
import eu.etaxonomy.cdm.strategy.cache.TaggedCacheHelper;
|
32
|
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
|
33
|
import eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy;
|
34
|
|
35
|
public class TaxonBaseShortSecCacheStrategy<T extends TaxonBase>
|
36
|
extends StrategyBase
|
37
|
implements ITaxonCacheStrategy<T> {
|
38
|
|
39
|
private static final long serialVersionUID = -2831618484053675222L;
|
40
|
final static UUID uuid = UUID.fromString("931e48f0-2033-11de-8c30-0800200c9a66");
|
41
|
|
42
|
@Override
|
43
|
protected UUID getUuid() {
|
44
|
return uuid;
|
45
|
}
|
46
|
|
47
|
|
48
|
@Override
|
49
|
public String getTitleCache(T taxonBase) {
|
50
|
String title;
|
51
|
if (taxonBase.getName() != null && taxonBase.getName().getTitleCache() != null){
|
52
|
title = getNamePart(taxonBase);
|
53
|
}else{
|
54
|
title = "???";
|
55
|
}
|
56
|
title += " sec. "; //TODO check if separator is required before, e.g. for nom. status. see TaxonBaseDefaultCacheStrategy
|
57
|
title += getSecundumPart(taxonBase);
|
58
|
if (taxonBase.isDoubtful()){
|
59
|
title = "?" + title;
|
60
|
}
|
61
|
return title;
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* @param taxonBase
|
66
|
* @param title
|
67
|
* @return
|
68
|
*/
|
69
|
private String getSecundumPart(T taxonBase) {
|
70
|
String result = "???";
|
71
|
Reference sec = taxonBase.getSec();
|
72
|
sec = HibernateProxyHelper.deproxy(sec, Reference.class);
|
73
|
if (sec != null){
|
74
|
if (sec.isProtectedTitleCache()){
|
75
|
return sec.getTitleCache();
|
76
|
}
|
77
|
if (sec.getAuthorship() != null){
|
78
|
|
79
|
if (sec.getAuthorship().isInstanceOf(Team.class)){
|
80
|
Team authorTeam = HibernateProxyHelper.deproxy(sec.getAuthorship(), Team.class);
|
81
|
if (authorTeam.getTeamMembers().size() > 2){
|
82
|
if (authorTeam.getTeamMembers().get(0).getLastname() != null){
|
83
|
result = authorTeam.getTeamMembers().get(0).getLastname() + " & al.";
|
84
|
} else {
|
85
|
result = authorTeam.getTeamMembers().get(0).getTitleCache();
|
86
|
result = result + " & al.";
|
87
|
}
|
88
|
} else if (authorTeam.getTeamMembers().size() == 2){
|
89
|
String firstAuthor;
|
90
|
if (authorTeam.getTeamMembers().get(0).getLastname() != null){
|
91
|
firstAuthor = authorTeam.getTeamMembers().get(0).getLastname();
|
92
|
}else{
|
93
|
firstAuthor = authorTeam.getTeamMembers().get(0).getTitleCache();
|
94
|
}
|
95
|
String secondAuthor;
|
96
|
if (authorTeam.getTeamMembers().get(1).getLastname() != null){
|
97
|
secondAuthor = authorTeam.getTeamMembers().get(1).getLastname();
|
98
|
}else{
|
99
|
secondAuthor = authorTeam.getTeamMembers().get(1).getTitleCache();
|
100
|
}
|
101
|
result = firstAuthor + " & " + secondAuthor;
|
102
|
|
103
|
} else{
|
104
|
if (authorTeam.getTeamMembers().get(0).getLastname() != null){
|
105
|
result = authorTeam.getTeamMembers().get(0).getLastname();
|
106
|
} else {
|
107
|
result = authorTeam.getTeamMembers().get(0).getTitleCache();
|
108
|
}
|
109
|
}
|
110
|
|
111
|
} else {
|
112
|
Person author = HibernateProxyHelper.deproxy(sec.getAuthorship(), Person.class);
|
113
|
if (author.getLastname() != null){
|
114
|
result = author.getLastname();
|
115
|
} else{
|
116
|
result = author.getTitleCache();
|
117
|
}
|
118
|
}
|
119
|
if (result != null){
|
120
|
result = result.replaceAll("[A-Z]\\.", "");
|
121
|
}
|
122
|
if (sec.getYear() != null && result != null){
|
123
|
result = result.concat(" (" + sec.getYear()+")");
|
124
|
}
|
125
|
}else{
|
126
|
result = taxonBase.getSec().getTitleCache();
|
127
|
}
|
128
|
}
|
129
|
return result;
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* @param name
|
134
|
*/
|
135
|
private String getNamePart(TaxonBase<?> taxonBase) {
|
136
|
TaxonNameBase<?,?> nameBase = taxonBase.getName();
|
137
|
String result = nameBase.getTitleCache();
|
138
|
//use name cache instead of title cache if required
|
139
|
if (taxonBase.isUseNameCache() && nameBase.isInstanceOf(NonViralName.class)){
|
140
|
NonViralName<?> nvn = HibernateProxyHelper.deproxy(nameBase, NonViralName.class);
|
141
|
result = nvn.getNameCache();
|
142
|
}
|
143
|
if (StringUtils.isNotBlank(taxonBase.getAppendedPhrase())){
|
144
|
result = result.trim() + " " + taxonBase.getAppendedPhrase().trim();
|
145
|
}
|
146
|
return result;
|
147
|
}
|
148
|
|
149
|
@Override
|
150
|
public List<TaggedText> getTaggedTitle(T taxonBase) {
|
151
|
if (taxonBase == null){
|
152
|
return null;
|
153
|
}
|
154
|
|
155
|
List<TaggedText> tags = new ArrayList<TaggedText>();
|
156
|
|
157
|
if (taxonBase.isProtectedTitleCache()){
|
158
|
//protected title cache
|
159
|
tags.add(new TaggedText(TagEnum.name, taxonBase.getTitleCache()));
|
160
|
return tags;
|
161
|
}else{
|
162
|
//name
|
163
|
TaxonNameBase<?,INameCacheStrategy<TaxonNameBase>> name = taxonBase.getName();
|
164
|
if (name != null){
|
165
|
//TODO
|
166
|
List<TaggedText> nameTags = name.getCacheStrategy().getTaggedTitle(name);
|
167
|
tags.addAll(nameTags);
|
168
|
}
|
169
|
|
170
|
//ref.
|
171
|
List<TaggedText> secTags;
|
172
|
Reference ref = taxonBase.getSec();
|
173
|
ref = HibernateProxyHelper.deproxy(ref, Reference.class);
|
174
|
if (ref != null){
|
175
|
secTags = getSecReferenceTags(ref);
|
176
|
}else{
|
177
|
secTags = new ArrayList<TaggedText>();
|
178
|
if (isBlank(taxonBase.getAppendedPhrase())){
|
179
|
secTags.add(new TaggedText(TagEnum.reference, "???"));
|
180
|
}
|
181
|
}
|
182
|
if(! secTags.isEmpty()){
|
183
|
//sec.
|
184
|
tags.add(new TaggedText(TagEnum.separator, "sec."));
|
185
|
tags.addAll(secTags);
|
186
|
}
|
187
|
}
|
188
|
return tags;
|
189
|
}
|
190
|
|
191
|
/**
|
192
|
* @param ref
|
193
|
*/
|
194
|
private List<TaggedText> getSecReferenceTags(Reference sec) {
|
195
|
List<TaggedText> tags = new ArrayList<TaggedText>();
|
196
|
|
197
|
if (sec.isProtectedTitleCache()){
|
198
|
tags.add(new TaggedText(TagEnum.reference, sec.getTitleCache()));
|
199
|
}else{
|
200
|
if (sec.getAuthorship() != null){
|
201
|
List<TaggedText> authorTags;
|
202
|
if (sec.getAuthorship().isInstanceOf(Team.class)){
|
203
|
authorTags = handleTeam(sec);
|
204
|
} else {
|
205
|
authorTags = handlePerson(sec);
|
206
|
}
|
207
|
tags.addAll(authorTags);
|
208
|
|
209
|
//FIXME why did we have this normalization? For removing first names??
|
210
|
// if (result != null){
|
211
|
// result = result.replaceAll("[A-Z]\\.", "");
|
212
|
// }
|
213
|
|
214
|
//year
|
215
|
String year = sec.getYear();
|
216
|
if (StringUtils.isNotBlank(year) && ! authorTags.isEmpty()){
|
217
|
tags.add(new TaggedText(TagEnum.separator, "("));
|
218
|
tags.add(new TaggedText(TagEnum.year, year));
|
219
|
tags.add(new TaggedText(TagEnum.separator, ")"));
|
220
|
}
|
221
|
}else{
|
222
|
|
223
|
}
|
224
|
}
|
225
|
|
226
|
return tags;
|
227
|
}
|
228
|
|
229
|
private List<TaggedText> handlePerson(Reference sec) {
|
230
|
List<TaggedText> tags = new ArrayList<TaggedText>();
|
231
|
|
232
|
Person author = HibernateProxyHelper.deproxy(sec.getAuthorship(), Person.class);
|
233
|
String authorStr;
|
234
|
if (author.getLastname() != null){
|
235
|
authorStr = author.getLastname();
|
236
|
} else{
|
237
|
authorStr = author.getTitleCache();
|
238
|
}
|
239
|
tags.add(new TaggedText(TagEnum.authors, authorStr));
|
240
|
return tags;
|
241
|
}
|
242
|
|
243
|
private List<TaggedText> handleTeam(Reference sec) {
|
244
|
List<TaggedText> tags = new ArrayList<TaggedText>();
|
245
|
|
246
|
Team authorTeam = HibernateProxyHelper.deproxy(sec.getAuthorship(), Team.class);
|
247
|
if (authorTeam.isProtectedTitleCache() || authorTeam.getTeamMembers().isEmpty()){
|
248
|
String authorStr = authorTeam.getTitleCache();
|
249
|
tags.add(new TaggedText(TagEnum.authors, authorStr));
|
250
|
}else if (authorTeam.getTeamMembers().size() > 2){
|
251
|
//>2 members
|
252
|
if (authorTeam.getTeamMembers().get(0).getLastname() != null){
|
253
|
String authorStr = authorTeam.getTeamMembers().get(0).getLastname() + " & al.";
|
254
|
tags.add(new TaggedText(TagEnum.authors, authorStr));
|
255
|
} else {
|
256
|
String authorStr = authorTeam.getTeamMembers().get(0).getTitleCache();
|
257
|
authorStr = authorStr + " & al.";
|
258
|
tags.add(new TaggedText(TagEnum.authors, authorStr));
|
259
|
}
|
260
|
} else if (authorTeam.getTeamMembers().size() == 2){
|
261
|
//2 members
|
262
|
String firstAuthor;
|
263
|
if (authorTeam.getTeamMembers().get(0).getLastname() != null){
|
264
|
firstAuthor = authorTeam.getTeamMembers().get(0).getLastname();
|
265
|
}else{
|
266
|
firstAuthor = authorTeam.getTeamMembers().get(0).getTitleCache();
|
267
|
}
|
268
|
String secondAuthor;
|
269
|
if (authorTeam.getTeamMembers().get(1).getLastname() != null){
|
270
|
secondAuthor = authorTeam.getTeamMembers().get(1).getLastname();
|
271
|
}else{
|
272
|
secondAuthor = authorTeam.getTeamMembers().get(1).getTitleCache();
|
273
|
}
|
274
|
String authorStr = firstAuthor + " & " + secondAuthor;
|
275
|
tags.add(new TaggedText(TagEnum.authors, authorStr));
|
276
|
} else{
|
277
|
//1 member
|
278
|
String authorStr;
|
279
|
if (authorTeam.getTeamMembers().get(0).getLastname() != null){
|
280
|
authorStr = authorTeam.getTeamMembers().get(0).getLastname();
|
281
|
} else {
|
282
|
authorStr = authorTeam.getTeamMembers().get(0).getTitleCache();
|
283
|
}
|
284
|
tags.add(new TaggedText(TagEnum.authors, authorStr));
|
285
|
}
|
286
|
return tags;
|
287
|
}
|
288
|
|
289
|
@Override
|
290
|
public String getTitleCache(T taxonBase, HTMLTagRules htmlTagRules) {
|
291
|
List<TaggedText> tags = getTaggedTitle(taxonBase);
|
292
|
if (tags == null){
|
293
|
return null;
|
294
|
}else{
|
295
|
String result = TaggedCacheHelper.createString(tags, htmlTagRules);
|
296
|
return result;
|
297
|
}
|
298
|
}
|
299
|
|
300
|
}
|