1
|
/**
|
2
|
* Copyright (C) 2016 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.reference;
|
10
|
|
11
|
import java.util.UUID;
|
12
|
|
13
|
import org.apache.log4j.Logger;
|
14
|
import org.joda.time.DateTime;
|
15
|
import org.joda.time.format.DateTimeFormat;
|
16
|
import org.joda.time.format.DateTimeFormatter;
|
17
|
|
18
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
19
|
import eu.etaxonomy.cdm.common.UTF8;
|
20
|
import eu.etaxonomy.cdm.format.reference.NomenclaturalSourceFormatter;
|
21
|
import eu.etaxonomy.cdm.format.reference.OriginalSourceFormatter;
|
22
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
23
|
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
|
24
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
25
|
import eu.etaxonomy.cdm.model.reference.ReferenceType;
|
26
|
import eu.etaxonomy.cdm.strategy.StrategyBase;
|
27
|
|
28
|
/**
|
29
|
* #5833
|
30
|
* The new single default cache strategy for {@link Reference references}.
|
31
|
* As we do have only one {@link Reference} class left which implements multiple interfaces,
|
32
|
* we may also only need 1 single cache strategy. However, care must be taken as the formatting
|
33
|
* differs dependent on the type an the in-reference structure.
|
34
|
*
|
35
|
* Generally the cache strategy allows to compute 2 formats:<BR><BR>
|
36
|
*
|
37
|
* 1.) for bibliographic references (stored in {@link Reference#getTitleCache() titleCache}).<BR>
|
38
|
*
|
39
|
* 2.) for nomenclatural references (stored in {@link Reference#getAbbrevTitleCache() abbrevTitleCache}),
|
40
|
* but without micro reference (detail).<BR>
|
41
|
* <BR>
|
42
|
* The formatting of nomenclatural references with micro references has been moved to {@link NomenclaturalSourceFormatter}
|
43
|
* and the formatting of short citations has been moved to {@link OriginalSourceFormatter}.
|
44
|
*
|
45
|
* @author a.mueller
|
46
|
* @since 25.05.2016
|
47
|
*/
|
48
|
public class ReferenceDefaultCacheStrategy
|
49
|
extends StrategyBase
|
50
|
implements IReferenceCacheStrategy {
|
51
|
|
52
|
private static final long serialVersionUID = 6773742298840407263L;
|
53
|
@SuppressWarnings("unused")
|
54
|
private static final Logger logger = Logger.getLogger(ReferenceDefaultCacheStrategy.class);
|
55
|
|
56
|
private final static UUID uuid = UUID.fromString("63e669ca-c6be-4a8a-b157-e391c22580f9");
|
57
|
|
58
|
private static final String afterAuthor = ": ";
|
59
|
private static final String afterInRefAuthor = ", "; //TODO needs discussion
|
60
|
|
61
|
//article
|
62
|
public static final String UNDEFINED_JOURNAL = "undefined journal " + UTF8.EN_DASH;
|
63
|
|
64
|
//in reference
|
65
|
private static String biblioInSeparator = " " + UTF8.EN_DASH + " In: "; //#9529
|
66
|
private String biblioArticleInSeparator = UTF8.EN_DASH + " "; //#9529
|
67
|
private String webpageUriSeparator = " "+UTF8.EN_DASH+" ";
|
68
|
|
69
|
//common
|
70
|
private static final String blank = " ";
|
71
|
public static final String beforeYear = ". ";
|
72
|
private static final String afterYear = "";
|
73
|
|
74
|
private static final boolean trim = true;
|
75
|
|
76
|
// ************************ FACTORY ****************************/
|
77
|
|
78
|
public static ReferenceDefaultCacheStrategy NewInstance(){
|
79
|
return new ReferenceDefaultCacheStrategy();
|
80
|
}
|
81
|
|
82
|
// ******************************* Main methods ******************************/
|
83
|
|
84
|
@Override
|
85
|
protected UUID getUuid() {
|
86
|
return uuid;
|
87
|
}
|
88
|
|
89
|
@Override
|
90
|
public String getTitleCache(Reference reference) {
|
91
|
if (reference == null){
|
92
|
return null;
|
93
|
}
|
94
|
if (reference.isProtectedTitleCache()){
|
95
|
return reference.getTitleCache();
|
96
|
}
|
97
|
boolean isNotAbbrev = false;
|
98
|
|
99
|
String result;
|
100
|
ReferenceType type = reference.getType();
|
101
|
String authorAndYear = getAuthorAndYear(reference, isNotAbbrev, false);
|
102
|
|
103
|
if (isRealInRef(reference)){
|
104
|
//Section, Book-Section or Generic with inRef
|
105
|
result = titleCacheRealInRef(reference, isNotAbbrev);
|
106
|
}else if(isNomRef(type)){
|
107
|
//all Non-InRef NomRefs
|
108
|
//Article, CdDvd, Generic, Section, Thesis, WebPage (+Book, BookSection but not here)
|
109
|
String title = getTitleWithoutYearAndAuthor(reference, isNotAbbrev, false);
|
110
|
result = addPages(title, reference);
|
111
|
if (isNotBlank(authorAndYear)){
|
112
|
String authorSeparator = isNotBlank(title)? afterAuthor : "";
|
113
|
authorAndYear += authorSeparator;
|
114
|
}
|
115
|
result = authorAndYear + result;
|
116
|
if (!type.isWebPage()){
|
117
|
result = CdmUtils.addTrailingDotIfNotExists(result);
|
118
|
}
|
119
|
}else if (type == ReferenceType.Journal){
|
120
|
result = titleCacheJournal(reference, isNotAbbrev);
|
121
|
}else{
|
122
|
result = titleCacheDefaultReference(reference, isNotAbbrev);
|
123
|
}
|
124
|
if (reference.getType() == ReferenceType.WebPage && reference.getUri() != null && !result.contains(reference.getUri().toString())){
|
125
|
result = CdmUtils.concat(webpageUriSeparator, result, reference.getUri().toString());
|
126
|
}
|
127
|
if(reference.getAccessed() != null){
|
128
|
//TODO still a bit preliminary, also brackets may change in future
|
129
|
result = result + " [accessed " + getAccessedString(reference.getAccessed()) +"]";
|
130
|
}
|
131
|
return result == null ? null : result.trim();
|
132
|
}
|
133
|
|
134
|
private String getAuthorAndYear(Reference reference, boolean isAbbrev, boolean useFullDatePublished) {
|
135
|
TeamOrPersonBase<?> author = reference.getAuthorship();
|
136
|
String authorStr = (author == null)? "" : CdmUtils.getPreferredNonEmptyString(author.getTitleCache(),
|
137
|
author.getNomenclaturalTitle(), isAbbrev, trim);
|
138
|
String result = addAuthorYear(authorStr, reference, useFullDatePublished);
|
139
|
return result;
|
140
|
}
|
141
|
|
142
|
private String getAccessedString(DateTime accessed) {
|
143
|
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
|
144
|
String result = formatter.print(accessed);
|
145
|
if (result.endsWith(" 00:00")){
|
146
|
result = result.replace(" 00:00", "");
|
147
|
}
|
148
|
return result;
|
149
|
}
|
150
|
|
151
|
private String addPages(String result, Reference reference) {
|
152
|
//pages
|
153
|
if (isNotBlank(reference.getPages())){
|
154
|
//Removing trailing added just in case, maybe not necessary
|
155
|
result = removeTrailingDots(result).trim() + ": " + reference.getPages();
|
156
|
}
|
157
|
return result;
|
158
|
}
|
159
|
|
160
|
@Override
|
161
|
public String getFullAbbrevTitleString(Reference reference) {
|
162
|
if (reference == null){
|
163
|
return null;
|
164
|
}
|
165
|
String result;
|
166
|
ReferenceType type = reference.getType();
|
167
|
boolean isAbbrev = true;
|
168
|
|
169
|
if (reference.isProtectedAbbrevTitleCache()){
|
170
|
return reference.getAbbrevTitleCache();
|
171
|
}
|
172
|
|
173
|
if (type == ReferenceType.Article){
|
174
|
result = getTitleWithoutYearAndAuthor(reference, isAbbrev, false);
|
175
|
boolean useFullDatePublished = false;
|
176
|
String authorAndYear = getAuthorAndYear(reference, isAbbrev, useFullDatePublished);
|
177
|
if (isNotBlank(authorAndYear)){
|
178
|
// String authorSeparator = isNotBlank(reference.getTitle())? afterAuthor : " ";
|
179
|
String authorSeparator = afterAuthor;
|
180
|
authorAndYear += authorSeparator;
|
181
|
}
|
182
|
result = authorAndYear + result;
|
183
|
result = CdmUtils.addTrailingDotIfNotExists(result);
|
184
|
}else if (isRealInRef(reference)){
|
185
|
result = titleCacheRealInRef(reference, isAbbrev);
|
186
|
}else if (isNomRef(type)){
|
187
|
String authorAndYear = getAuthorAndYear(reference, isAbbrev, false);
|
188
|
String title = getTitleWithoutYearAndAuthor(reference, isAbbrev, false);
|
189
|
result = addPages(title, reference);
|
190
|
//if Book, CdDvd, flat Generic, Thesis, WebPage
|
191
|
if (isNotBlank(authorAndYear)){
|
192
|
String authorSeparator = isNotBlank(title)? afterAuthor : "";
|
193
|
authorAndYear += authorSeparator;
|
194
|
}
|
195
|
result = authorAndYear + result;
|
196
|
|
197
|
if (!type.isWebPage()){
|
198
|
result = CdmUtils.addTrailingDotIfNotExists(result);
|
199
|
}
|
200
|
}else if(type == ReferenceType.Journal){
|
201
|
result = titleCacheJournal(reference, isAbbrev);
|
202
|
}else{
|
203
|
result = titleCacheDefaultReference(reference, isAbbrev);
|
204
|
}
|
205
|
|
206
|
return result;
|
207
|
}
|
208
|
|
209
|
// ************************ TITLE CACHE SUBS ********************************************/
|
210
|
|
211
|
//section, book section or generic with inRef
|
212
|
private String titleCacheRealInRef(Reference reference, boolean isAbbrev) {
|
213
|
|
214
|
Reference inRef = reference.getInReference();
|
215
|
|
216
|
String inRefPart = getInRefAuthorAndTitle(inRef, reference.getType(), isAbbrev);
|
217
|
if (inRef != null && !inRef.isArticle()){
|
218
|
inRefPart = addInRefPages(inRef, inRefPart);
|
219
|
if (inRef.isJournal()){
|
220
|
inRefPart = addSeriesAndVolume(reference, inRefPart, isAbbrev); //usually only needed for journals
|
221
|
}
|
222
|
}
|
223
|
inRefPart = CdmUtils.addTrailingDotIfNotExists(inRefPart);
|
224
|
inRefPart = biblioInSeparator + inRefPart;
|
225
|
if (inRef != null && inRef.isBookSection()){
|
226
|
String inInRefPart = getInRefAuthorAndTitle(inRef.getInReference(), inRef.getType(), isAbbrev);
|
227
|
inInRefPart = CdmUtils.addTrailingDotIfNotExists(inInRefPart);
|
228
|
inInRefPart = biblioInSeparator + inInRefPart;
|
229
|
inRefPart += inInRefPart;
|
230
|
}
|
231
|
|
232
|
//section title
|
233
|
String title = CdmUtils.getPreferredNonEmptyString(
|
234
|
reference.getTitle(), reference.getAbbrevTitle(), isAbbrev, trim);
|
235
|
if (title.matches(".*[.!\\?]")){
|
236
|
title = title.substring(0, title.length() - 1);
|
237
|
}
|
238
|
//pages
|
239
|
title = addInRefPages(reference, title);
|
240
|
|
241
|
String result;
|
242
|
if (title.length() > 0){
|
243
|
result = title.trim() + "." + inRefPart;
|
244
|
}else{
|
245
|
result = inRefPart;
|
246
|
}
|
247
|
|
248
|
//section author
|
249
|
TeamOrPersonBase<?> author = reference.getAuthorship();
|
250
|
String authorStr = (author == null)? "" : CdmUtils.getPreferredNonEmptyString(author.getTitleCache(),
|
251
|
author.getNomenclaturalTitle(), isAbbrev, trim);
|
252
|
|
253
|
//date
|
254
|
String dateStr = null;
|
255
|
VerbatimTimePeriod date = reference.hasDatePublished() ? reference.getDatePublished() : null;
|
256
|
if (date == null && inRef != null && inRef.hasDatePublished()){
|
257
|
date = reference.getInReference().getDatePublished();
|
258
|
if (date == null && inRef.isSection() && inRef.getInReference() != null && inRef.getInReference().hasDatePublished()){
|
259
|
date = inRef.getInReference().getDatePublished();
|
260
|
}
|
261
|
}
|
262
|
if (date != null){
|
263
|
dateStr = date.getYear();
|
264
|
}
|
265
|
|
266
|
String authorAndYear = CdmUtils.concat(" ", authorStr, dateStr);
|
267
|
|
268
|
String sep = result.startsWith(biblioInSeparator)? "": afterAuthor;
|
269
|
result = CdmUtils.concat(sep, authorAndYear, result);
|
270
|
|
271
|
return result;
|
272
|
}
|
273
|
|
274
|
//copied from TitleWithoutYearAndAuthor.getTitleWithoutYearAndAuthorArticle
|
275
|
//may be somehow merged in future
|
276
|
private String addSeriesAndVolume(Reference ref, String inRefPart, boolean isAbbrev) {
|
277
|
String series = Nz(ref.getSeriesPart()).trim();
|
278
|
String volume = Nz(ref.getVolume()).trim();
|
279
|
|
280
|
String inRefTitle = ReferenceDefaultCacheStrategy.UNDEFINED_JOURNAL;
|
281
|
boolean needsComma = false;
|
282
|
Reference inRef = ref.getInReference();
|
283
|
if (inRef != null){
|
284
|
inRefTitle = CdmUtils.getPreferredNonEmptyString(inRef.getTitle(), inRef.getAbbrevTitle(), isAbbrev, true);
|
285
|
if (isNotBlank(inRefTitle)){
|
286
|
needsComma = TitleWithoutYearAndAuthorHelper.computeNeedsCommaArticle(inRefPart, volume, series);
|
287
|
if (! needsComma && (isNotBlank(volume)||isNotBlank(series))){
|
288
|
inRefPart += blank;
|
289
|
}
|
290
|
}
|
291
|
}
|
292
|
inRefPart = TitleWithoutYearAndAuthorHelper.getSeriesAndVolPartArticle(series, volume, needsComma, inRefPart);
|
293
|
|
294
|
//delete "."
|
295
|
while (inRefPart.endsWith(".")){
|
296
|
inRefPart = CdmUtils.removeTrailingDots(inRefPart);
|
297
|
}
|
298
|
return inRefPart;
|
299
|
}
|
300
|
|
301
|
private String addInRefPages(Reference reference, String title) {
|
302
|
String pages = getPages(reference);
|
303
|
if (isNotBlank(pages)){
|
304
|
title = CdmUtils.concat(", ", title, pages);
|
305
|
}
|
306
|
return title;
|
307
|
}
|
308
|
|
309
|
private String getInRefAuthorAndTitle(Reference inRef, ReferenceType type, boolean isAbbrev) {
|
310
|
String inRefAuthorAndTitle;
|
311
|
if (inRef != null){
|
312
|
String inRefTitle = TitleWithoutYearAndAuthorHelper.getTitleWithoutYearAndAuthor(inRef, isAbbrev, false);
|
313
|
TeamOrPersonBase<?> inRefAuthor = inRef.getAuthorship();
|
314
|
String authorStr = (inRefAuthor == null)? "" : CdmUtils.getPreferredNonEmptyString(inRefAuthor.getTitleCache(),
|
315
|
inRefAuthor.getNomenclaturalTitle(), isAbbrev, trim);
|
316
|
inRefAuthorAndTitle = CdmUtils.concat(afterInRefAuthor, authorStr, inRefTitle);
|
317
|
}else{
|
318
|
inRefAuthorAndTitle = String.format("- undefined %s -", getUndefinedLabel(type));
|
319
|
}
|
320
|
|
321
|
return inRefAuthorAndTitle;
|
322
|
}
|
323
|
|
324
|
private static final String pageNoRe = "[0-9iIvVxXlLcCdDmM]+";
|
325
|
private String getPages(Reference reference) {
|
326
|
|
327
|
if (reference == null || isBlank(reference.getPages())){
|
328
|
return null;
|
329
|
}else if (reference.getPages().matches(pageNoRe + "\\s*[-"+UTF8.EN_DASH+"]\\s*"+ pageNoRe)){
|
330
|
return "pp. " + reference.getPages();
|
331
|
}else if (reference.getPages().matches(pageNoRe)){
|
332
|
return "p. " + reference.getPages();
|
333
|
}else{
|
334
|
return reference.getPages();
|
335
|
}
|
336
|
}
|
337
|
|
338
|
private String titleCacheJournal(Reference reference, boolean isAbbrev) {
|
339
|
String result;
|
340
|
//copied from Journal
|
341
|
|
342
|
//title
|
343
|
result = CdmUtils.getPreferredNonEmptyString(reference.getTitle(),
|
344
|
reference.getAbbrevTitle(), isAbbrev, trim);
|
345
|
|
346
|
// //delete .
|
347
|
// while (result.endsWith(".")){
|
348
|
// result = result.substring(0, result.length()-1);
|
349
|
// }
|
350
|
// result = addYear(result, journal);
|
351
|
|
352
|
TeamOrPersonBase<?> team = reference.getAuthorship();
|
353
|
if (team != null){
|
354
|
String author = CdmUtils.getPreferredNonEmptyString(team.getTitleCache(),
|
355
|
team.getNomenclaturalTitle(), isAbbrev, trim);
|
356
|
if (isNotBlank(author)){
|
357
|
result = author + afterAuthor + result;
|
358
|
}
|
359
|
}
|
360
|
return result;
|
361
|
}
|
362
|
|
363
|
private String titleCacheDefaultReference(Reference reference, boolean isAbbrev) {
|
364
|
String result;
|
365
|
//copied from ReferenceDefaultCacheStrategy
|
366
|
result = "";
|
367
|
String titel = CdmUtils.getPreferredNonEmptyString(reference.getTitle(),
|
368
|
reference.getAbbrevTitle(), isAbbrev, trim);
|
369
|
if (isNotBlank(titel)){
|
370
|
result = titel + blank;
|
371
|
}
|
372
|
//delete .
|
373
|
while (result.endsWith(".")){
|
374
|
result = result.substring(0, result.length()-1);
|
375
|
}
|
376
|
|
377
|
result = addYearReferenceDefault(result, reference);
|
378
|
TeamOrPersonBase<?> team = reference.getAuthorship();
|
379
|
if (team != null){
|
380
|
String author = CdmUtils.getPreferredNonEmptyString(team.getTitleCache(),
|
381
|
team.getNomenclaturalTitle(), isAbbrev, trim);
|
382
|
if (isNotBlank(author)){
|
383
|
result = author + afterAuthor + result;
|
384
|
}
|
385
|
}
|
386
|
return result;
|
387
|
}
|
388
|
|
389
|
// ******************************* HELPER *****************************************/
|
390
|
|
391
|
/**
|
392
|
* Adds the year or full date of a reference to a given string
|
393
|
* @param currentStr the given string
|
394
|
* @param reference the reference
|
395
|
* @param useFullDatePublished wether to add the year only or the full date
|
396
|
* @return the concatenated string
|
397
|
*/
|
398
|
public static String addYear(String currentStr, Reference reference, boolean useFullDatePublished){
|
399
|
String result;
|
400
|
if (currentStr == null){
|
401
|
return null;
|
402
|
}
|
403
|
String year = useFullDatePublished ? reference.getDatePublishedString() : reference.getYear();
|
404
|
if (isBlank(year)){
|
405
|
result = currentStr + afterYear;
|
406
|
}else{
|
407
|
String concat = isBlank(currentStr) ? "" : currentStr.endsWith(".") ? " " : beforeYear;
|
408
|
result = currentStr + concat + year + afterYear;
|
409
|
}
|
410
|
return result;
|
411
|
}
|
412
|
|
413
|
private String addAuthorYear(String authorStr, Reference reference, boolean useFullDatePublished){
|
414
|
String year = useFullDatePublished ? reference.getDatePublishedString() : reference.getYear();
|
415
|
if (isBlank(year)){
|
416
|
return authorStr;
|
417
|
}else{
|
418
|
return CdmUtils.concat(" ", authorStr, year);
|
419
|
}
|
420
|
}
|
421
|
|
422
|
private String getTitleWithoutYearAndAuthor(Reference ref, boolean isAbbrev, boolean isNomRef){
|
423
|
return TitleWithoutYearAndAuthorHelper.getTitleWithoutYearAndAuthor(ref, isAbbrev, isNomRef);
|
424
|
}
|
425
|
|
426
|
private Object getUndefinedLabel(ReferenceType type) {
|
427
|
if (type == ReferenceType.BookSection){
|
428
|
return "book";
|
429
|
}else if (type == ReferenceType.Generic){
|
430
|
return "generic reference";
|
431
|
}else if (type == ReferenceType.Section){
|
432
|
return "in-reference";
|
433
|
} else {
|
434
|
return "in-reference for " + type.getLabel();
|
435
|
}
|
436
|
}
|
437
|
|
438
|
/**
|
439
|
* Returns <code>true</code> if the type of the reference originally corresponded to a cache strategy
|
440
|
* which inherited from {@link InRefDefaultCacheStrategyBase} and in case of type {@link ReferenceType#Generic}
|
441
|
* if it really has an inreference (reference.getInreference() != null).
|
442
|
* @param reference
|
443
|
*/
|
444
|
public static boolean isRealInRef(Reference reference) {
|
445
|
ReferenceType type = (reference.getType());
|
446
|
if (type == null){
|
447
|
return false;
|
448
|
}else if (type == ReferenceType.BookSection || type == ReferenceType.Section){
|
449
|
return true;
|
450
|
}else if (type == ReferenceType.Generic){
|
451
|
return reference.getInReference() != null;
|
452
|
}else{
|
453
|
return false;
|
454
|
}
|
455
|
}
|
456
|
|
457
|
/**
|
458
|
* Returns <code>true</code> if the type of the reference originally corresponded to a cache strategy
|
459
|
* which inherited from {@link NomRefDefaultCacheStrategyBase}.
|
460
|
* @param type
|
461
|
* @see ReferenceType#isNomRef()
|
462
|
*/
|
463
|
public static boolean isNomRef(ReferenceType type){
|
464
|
return type == null ? false : type.isNomRef();
|
465
|
}
|
466
|
|
467
|
/**
|
468
|
* Returns year information as originally computed by {@link ReferenceDefaultCacheStrategy}.
|
469
|
*/
|
470
|
private String addYearReferenceDefault(String string, Reference ref){
|
471
|
String result;
|
472
|
if (string == null){
|
473
|
return null;
|
474
|
}
|
475
|
String year = CdmUtils.Nz(ref.getYear());
|
476
|
if ("".equals(year)){
|
477
|
result = string + afterYear;
|
478
|
}else{
|
479
|
result = string.trim() + beforeYear + year + afterYear;
|
480
|
}
|
481
|
return result;
|
482
|
}
|
483
|
|
484
|
// *************************** EXTERNAL USE *******************************************/
|
485
|
|
486
|
public static String putAuthorToEndOfString(String referenceTitle, String authorTitle) {
|
487
|
if(authorTitle != null){
|
488
|
referenceTitle = referenceTitle.replace(authorTitle + ", ", "");
|
489
|
referenceTitle += " - " + authorTitle;
|
490
|
}
|
491
|
return referenceTitle;
|
492
|
}
|
493
|
}
|