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 org.apache.commons.lang.StringUtils;
|
12
|
import org.apache.log4j.Logger;
|
13
|
|
14
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
15
|
import eu.etaxonomy.cdm.model.reference.IJournal;
|
16
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
17
|
import eu.etaxonomy.cdm.model.reference.ReferenceType;
|
18
|
|
19
|
/**
|
20
|
* @author a.mueller
|
21
|
* @date 25.05.2016
|
22
|
*
|
23
|
*/
|
24
|
public class TitleWithoutYearAndAuthorHelper {
|
25
|
private static final Logger logger = Logger.getLogger(TitleWithoutYearAndAuthorHelper.class);
|
26
|
|
27
|
//article
|
28
|
private static final String prefixArticleReferenceJounal = "in";
|
29
|
private static final String prefixSeriesArticle = "ser.";
|
30
|
|
31
|
//book
|
32
|
private static final String prefixBookEdition = "ed.";
|
33
|
private static final String prefixBookSeries = ", ser.";
|
34
|
|
35
|
//generic
|
36
|
private static final String prefixEditionGeneric = "ed.";
|
37
|
private static final String prefixSeriesGeneric = "ser.";
|
38
|
|
39
|
//common
|
40
|
private static final String blank = " ";
|
41
|
private static final String comma = ",";
|
42
|
|
43
|
|
44
|
// *************************Main METHODS ***********************************/
|
45
|
|
46
|
public static String getTitleWithoutYearAndAuthor(Reference ref, boolean isAbbrev){
|
47
|
ReferenceType type = ref.getType();
|
48
|
if (! DefaultReferenceCacheStrategy.isNomRef(type)){
|
49
|
logger.warn("getTitleWithoutYearAndAuthor should not be required"
|
50
|
+ " for reference type " + type.getMessage() +
|
51
|
" and does not exist. Use Generic getTitleWithoutYearAndAuthorGeneric instead");
|
52
|
return getTitleWithoutYearAndAuthorGeneric(ref, isAbbrev);
|
53
|
}else if (type == ReferenceType.Article){
|
54
|
return getTitleWithoutYearAndAuthorArticle(ref, isAbbrev);
|
55
|
}else if(type == ReferenceType.Book){
|
56
|
return getTitleWithoutYearAndAuthorBook(ref, isAbbrev);
|
57
|
}else if(type == ReferenceType.CdDvd){
|
58
|
return getTitleWithoutYearAndAuthorCdDvd(ref, isAbbrev);
|
59
|
}else if(type == ReferenceType.Generic){
|
60
|
return getTitleWithoutYearAndAuthorGeneric(ref, isAbbrev);
|
61
|
}else if (type == ReferenceType.WebPage || type == ReferenceType.Thesis) {
|
62
|
return getTitleWithoutYearAndAuthorWebPageThesis(ref, isAbbrev);
|
63
|
}else if (type == ReferenceType.Section || type == ReferenceType.BookSection){
|
64
|
// not needed in Section
|
65
|
logger.warn("Questionable procedure call. Procedure not implemented because not needed. ");
|
66
|
return null;
|
67
|
}else{
|
68
|
//FIXME
|
69
|
return null;
|
70
|
}
|
71
|
|
72
|
}
|
73
|
|
74
|
|
75
|
|
76
|
private static String getTitleWithoutYearAndAuthorArticle(Reference article, boolean isAbbrev){
|
77
|
if (article == null){
|
78
|
return null;
|
79
|
}
|
80
|
IJournal journal = article.getInReference();
|
81
|
|
82
|
String journalTitel;
|
83
|
if (journal != null){
|
84
|
journalTitel = CdmUtils.getPreferredNonEmptyString(journal.getTitle(), journal.getAbbrevTitle(), isAbbrev, true);
|
85
|
}else{
|
86
|
journalTitel = DefaultReferenceCacheStrategy.UNDEFINED_JOURNAL;
|
87
|
}
|
88
|
|
89
|
String series = Nz(article.getSeriesPart()).trim();
|
90
|
String volume = Nz(article.getVolume()).trim();
|
91
|
|
92
|
boolean needsComma = false;
|
93
|
|
94
|
String nomRefCache = "";
|
95
|
|
96
|
//inJournal
|
97
|
nomRefCache = prefixArticleReferenceJounal + blank;
|
98
|
|
99
|
//titelAbbrev
|
100
|
if (isNotBlank(journalTitel)){
|
101
|
nomRefCache = nomRefCache + journalTitel;
|
102
|
needsComma = makeNeedsCommaArticle(needsComma, nomRefCache, volume, series);
|
103
|
if (! needsComma){
|
104
|
nomRefCache = nomRefCache + blank;
|
105
|
}
|
106
|
}
|
107
|
|
108
|
//series and vol.
|
109
|
nomRefCache = getSeriesAndVolPartArticle(series, volume, needsComma, nomRefCache);
|
110
|
|
111
|
//delete "."
|
112
|
while (nomRefCache.endsWith(".")){
|
113
|
nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
|
114
|
}
|
115
|
|
116
|
return nomRefCache.trim();
|
117
|
}
|
118
|
|
119
|
|
120
|
private static String getTitleWithoutYearAndAuthorBook(Reference ref, boolean isAbbrev){
|
121
|
if (ref == null){
|
122
|
return null;
|
123
|
}
|
124
|
//TODO
|
125
|
String title = CdmUtils.getPreferredNonEmptyString(ref.getTitle(), ref.getAbbrevTitle(), isAbbrev, true);
|
126
|
String edition = Nz(ref.getEdition()).trim();
|
127
|
String series = ""; //TODO ref.getSeries(); //SeriesPart is handled later
|
128
|
String refSeriesPart = Nz(ref.getSeriesPart());
|
129
|
String volume = CdmUtils.Nz(ref.getVolume()).trim();
|
130
|
String refYear = ""; //TODO nomenclaturalReference.getYear();
|
131
|
|
132
|
|
133
|
String nomRefCache = "";
|
134
|
Integer len;
|
135
|
String lastChar = "";
|
136
|
String character =".";
|
137
|
len = title.length();
|
138
|
if (len > 0){
|
139
|
lastChar = title.substring(len-1, len);
|
140
|
}
|
141
|
//lastCharIsDouble = f_core_CompareStrings(RIGHT(@TitelAbbrev,1),character);
|
142
|
boolean lastCharIsDouble = title.equals(character);
|
143
|
|
144
|
if(lastCharIsDouble && edition.length() == 0 && refSeriesPart.length() == 0 && volume.length() == 0 && refYear.length() > 0 ){
|
145
|
title = title.substring(1, len-1); // SUBSTRING(@TitelAbbrev,1,@LEN-1)
|
146
|
}
|
147
|
|
148
|
|
149
|
boolean needsComma = false;
|
150
|
//titelAbbrev
|
151
|
if (!"".equals(title) ){
|
152
|
String postfix = isNotBlank(edition + refSeriesPart) ? "" : blank;
|
153
|
nomRefCache = title + postfix;
|
154
|
}
|
155
|
//edition
|
156
|
String editionPart = "";
|
157
|
if (StringUtils.isNotBlank(edition)){
|
158
|
editionPart = edition;
|
159
|
if (isNumeric(edition)){
|
160
|
editionPart = prefixBookEdition + blank + editionPart;
|
161
|
}
|
162
|
needsComma = true;
|
163
|
}
|
164
|
nomRefCache = CdmUtils.concat(", ", nomRefCache, editionPart);
|
165
|
|
166
|
//inSeries
|
167
|
String seriesPart = "";
|
168
|
if (StringUtils.isNotBlank(refSeriesPart)){
|
169
|
seriesPart = refSeriesPart;
|
170
|
if (isNumeric(refSeriesPart)){
|
171
|
seriesPart = prefixBookSeries + blank + seriesPart;
|
172
|
}
|
173
|
if (needsComma){
|
174
|
seriesPart = comma + seriesPart;
|
175
|
}
|
176
|
needsComma = true;
|
177
|
}
|
178
|
nomRefCache += seriesPart;
|
179
|
|
180
|
|
181
|
//volume Part
|
182
|
String volumePart = "";
|
183
|
if (StringUtils.isNotBlank(volume)){
|
184
|
volumePart = volume;
|
185
|
if (needsComma){
|
186
|
volumePart = comma + blank + volumePart;
|
187
|
}else{
|
188
|
volumePart = "" + volumePart;
|
189
|
}
|
190
|
//needsComma = false;
|
191
|
}
|
192
|
nomRefCache += volumePart;
|
193
|
|
194
|
//delete .
|
195
|
while (nomRefCache.endsWith(".")){
|
196
|
nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
|
197
|
}
|
198
|
|
199
|
return nomRefCache.trim();
|
200
|
}
|
201
|
|
202
|
|
203
|
public static String getTitleWithoutYearAndAuthorGeneric(Reference genericReference, boolean isAbbrev){
|
204
|
if (genericReference == null){
|
205
|
return null;
|
206
|
}
|
207
|
//TODO
|
208
|
String titel = CdmUtils.getPreferredNonEmptyString(genericReference.getTitle(), genericReference.getAbbrevTitle(), isAbbrev, true);
|
209
|
String edition = CdmUtils.Nz(genericReference.getEdition());
|
210
|
//TODO
|
211
|
String series = CdmUtils.Nz(genericReference.getSeriesPart()).trim(); //nomenclaturalReference.getSeries();
|
212
|
String volume = CdmUtils.Nz(genericReference.getVolume()).trim();
|
213
|
|
214
|
String result = "";
|
215
|
boolean lastCharIsDouble;
|
216
|
Integer len;
|
217
|
String lastChar ="";
|
218
|
String character =".";
|
219
|
len = titel.length();
|
220
|
if (len > 0){lastChar = titel.substring(len-1, len);}
|
221
|
//lastCharIsDouble = f_core_CompareStrings(RIGHT(@TitelAbbrev,1),character);
|
222
|
lastCharIsDouble = titel.equals(character);
|
223
|
|
224
|
// if(lastCharIsDouble && edition.length() == 0 && series.length() == 0 && volume.length() == 0 && refYear.length() > 0 ){
|
225
|
// titelAbbrev = titelAbbrev.substring(1, len-1); // SUBSTRING(@TitelAbbrev,1,@LEN-1)
|
226
|
// }
|
227
|
|
228
|
|
229
|
boolean needsComma = false;
|
230
|
//titelAbbrev
|
231
|
if (titel.length() > 0 ){
|
232
|
String postfix = StringUtils.isNotBlank(edition) ? "" : blank;
|
233
|
result = titel + postfix;
|
234
|
}
|
235
|
//edition
|
236
|
String editionPart = "";
|
237
|
if (StringUtils.isNotBlank(edition)){
|
238
|
editionPart = edition;
|
239
|
if (isNumeric(edition)){
|
240
|
editionPart = prefixEditionGeneric + blank + editionPart;
|
241
|
}
|
242
|
needsComma = true;
|
243
|
}
|
244
|
result = CdmUtils.concat(", ", result, editionPart);
|
245
|
|
246
|
//inSeries
|
247
|
String seriesPart = "";
|
248
|
if (isNotBlank(series)){
|
249
|
seriesPart = series;
|
250
|
if (isNumeric(series)){
|
251
|
seriesPart = prefixSeriesGeneric + blank + seriesPart;
|
252
|
}
|
253
|
if (needsComma){
|
254
|
seriesPart = comma + seriesPart;
|
255
|
}
|
256
|
needsComma = true;
|
257
|
}
|
258
|
result += seriesPart;
|
259
|
|
260
|
|
261
|
//volume Part
|
262
|
String volumePart = "";
|
263
|
if (!"".equals(volume)){
|
264
|
volumePart = volume;
|
265
|
if (needsComma){
|
266
|
volumePart = comma + blank + volumePart;
|
267
|
}
|
268
|
//needsComma = false;
|
269
|
}
|
270
|
result += volumePart;
|
271
|
|
272
|
//delete .
|
273
|
while (result.endsWith(".")){
|
274
|
result = result.substring(0, result.length()-1);
|
275
|
}
|
276
|
|
277
|
return result.trim();
|
278
|
}
|
279
|
|
280
|
private static String getTitleWithoutYearAndAuthorCdDvd(Reference ref, boolean isAbbrev){
|
281
|
if (ref == null){
|
282
|
return null;
|
283
|
}
|
284
|
String nomRefCache = "";
|
285
|
//TODO
|
286
|
String titel = CdmUtils.getPreferredNonEmptyString(ref.getTitle(), ref.getAbbrevTitle(), isAbbrev, true);
|
287
|
// String publisher = CdmUtils.Nz(nomenclaturalReference.getPublisher());
|
288
|
|
289
|
boolean needsComma = false;
|
290
|
//titelAbbrev
|
291
|
if (titel.length() > 0){
|
292
|
nomRefCache = titel + blank;
|
293
|
}
|
294
|
// //publisher
|
295
|
// String publisherPart = "";
|
296
|
// if (!"".equals(publisher)){
|
297
|
// publisherPart = publisher;
|
298
|
// needsComma = true;
|
299
|
// }
|
300
|
// nomRefCache += publisherPart;
|
301
|
|
302
|
|
303
|
//delete .
|
304
|
while (nomRefCache.endsWith(".")){
|
305
|
nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
|
306
|
}
|
307
|
return nomRefCache.trim();
|
308
|
}
|
309
|
|
310
|
/**
|
311
|
* @param ref
|
312
|
* @param isAbbrev
|
313
|
* @return
|
314
|
*/
|
315
|
private static String getTitleWithoutYearAndAuthorWebPageThesis(Reference ref, boolean isAbbrev) {
|
316
|
//FIXME this is only a very fast copy and paste from "Generic". Must still be cleaned !
|
317
|
|
318
|
if (ref == null){
|
319
|
return null;
|
320
|
}
|
321
|
|
322
|
//titelAbbrev
|
323
|
//TODO
|
324
|
String titelAbbrev = CdmUtils.getPreferredNonEmptyString(ref.getTitle(), ref.getAbbrevTitle(), isAbbrev, true);
|
325
|
|
326
|
//titelAbbrev
|
327
|
String nomRefCache = titelAbbrev + blank;
|
328
|
|
329
|
//delete .
|
330
|
while (nomRefCache.endsWith(".")){
|
331
|
nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
|
332
|
}
|
333
|
|
334
|
return nomRefCache.trim();
|
335
|
}
|
336
|
|
337
|
//**************************** HELPER ********************************/
|
338
|
|
339
|
private static boolean makeNeedsCommaArticle(boolean needsComma, String nomRefCache, String volume, String series) {
|
340
|
if (needsComma){
|
341
|
return true;
|
342
|
}else{
|
343
|
nomRefCache = nomRefCache.toLowerCase();
|
344
|
int serIndex = nomRefCache.indexOf(" ser. ");
|
345
|
int sectIndex = nomRefCache.indexOf(" sect. ");
|
346
|
int abtIndex = nomRefCache.indexOf(" abt. ");
|
347
|
int index = Math.max(Math.max(serIndex, sectIndex), abtIndex);
|
348
|
int commaIndex = nomRefCache.indexOf(",", index);
|
349
|
if (index > -1 && commaIndex == -1 && isNotBlank(volume)){
|
350
|
return true;
|
351
|
}else if (isNotBlank(series)){
|
352
|
return true;
|
353
|
}else{
|
354
|
return false;
|
355
|
}
|
356
|
}
|
357
|
}
|
358
|
|
359
|
private static String getSeriesAndVolPartArticle(String series, String volume,
|
360
|
boolean needsComma, String nomRefCache) {
|
361
|
//inSeries
|
362
|
String seriesPart = "";
|
363
|
if (isNotBlank(series)){
|
364
|
seriesPart = series;
|
365
|
if (CdmUtils.isNumeric(series)){
|
366
|
seriesPart = prefixSeriesArticle + blank + seriesPart;
|
367
|
}
|
368
|
// if (needsComma){
|
369
|
seriesPart = comma + blank + seriesPart;
|
370
|
// }
|
371
|
needsComma = true;
|
372
|
}
|
373
|
nomRefCache += seriesPart;
|
374
|
|
375
|
|
376
|
//volume Part
|
377
|
String volumePart = "";
|
378
|
if (!"".equals(volume)){
|
379
|
volumePart = volume;
|
380
|
if (needsComma){
|
381
|
volumePart = comma + blank + volumePart;
|
382
|
}
|
383
|
//needsComma = false;
|
384
|
}
|
385
|
nomRefCache += volumePart;
|
386
|
return nomRefCache;
|
387
|
}
|
388
|
|
389
|
// ****************** COMMON **********************************************/
|
390
|
|
391
|
/**
|
392
|
* Null safe string. Returns the given string if it is not <code>null</code>.
|
393
|
* Empty string otherwise.
|
394
|
* @see CdmUtils#Nz(String)
|
395
|
* @return the null-safe string
|
396
|
*/
|
397
|
private static String Nz(String str){
|
398
|
return CdmUtils.Nz(str);
|
399
|
}
|
400
|
|
401
|
/**
|
402
|
* Checks if a string is not blank.
|
403
|
* @see StringUtils#isNotBlank(String)
|
404
|
*/
|
405
|
private static boolean isNotBlank(String str){
|
406
|
return StringUtils.isNotBlank(str);
|
407
|
}
|
408
|
|
409
|
/**
|
410
|
* Checks if a string is blank.
|
411
|
* @see StringUtils#isNotBlank(String)
|
412
|
*/
|
413
|
private static boolean isBlank(String str){
|
414
|
return StringUtils.isBlank(str);
|
415
|
}
|
416
|
|
417
|
private static boolean isNumeric(String string){
|
418
|
if (string == null){
|
419
|
return false;
|
420
|
}
|
421
|
try {
|
422
|
Double.valueOf(string);
|
423
|
return true;
|
424
|
} catch (NumberFormatException e) {
|
425
|
return false;
|
426
|
}
|
427
|
}
|
428
|
}
|