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