Project

General

Profile

Download (15.7 KB) Statistics
| Branch: | Tag: | Revision:
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.lang3.StringUtils;
12
import org.apache.log4j.Logger;
13

    
14
import eu.etaxonomy.cdm.common.CdmUtils;
15
import eu.etaxonomy.cdm.common.UTF8;
16
import eu.etaxonomy.cdm.model.reference.IJournal;
17
import eu.etaxonomy.cdm.model.reference.Reference;
18
import eu.etaxonomy.cdm.model.reference.ReferenceType;
19
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImplRegExBase;
20

    
21
/**
22
 * @author a.mueller
23
 * @since 25.05.2016
24
 */
25
public class TitleWithoutYearAndAuthorHelper {
26
    private static final Logger logger = Logger.getLogger(TitleWithoutYearAndAuthorHelper.class);
27

    
28
    //article
29
    private static final String prefixArticleReferenceJounal = UTF8.EN_DASH + " ";
30
    private static final String prefixSeriesArticle = "ser.";
31

    
32
    //book
33
    private static final String prefixBookEdition = "ed.";
34
    private static final String prefixBookSeries = ", ser.";
35

    
36
    //generic
37
    private static final String prefixEditionGeneric = "ed.";
38
    private static final String prefixSeriesGeneric = "ser.";
39

    
40
    //common
41
    private static final String blank = " ";
42
    private static final String comma = ",";
43

    
44

    
45
// *************************Main METHODS ***********************************/
46

    
47
    public static String getTitleWithoutYearAndAuthor(Reference ref, boolean isAbbrev, boolean isNomRef){
48
        ReferenceType type = ref.getType();
49
        if (! ReferenceDefaultCacheStrategy.isNomRef(type)){
50
            //NOTE: this method is now called always for generating the reference abbrevTitleCache, therefore it is also called for non-nomenclatural reference.
51
//            logger.warn("getTitleWithoutYearAndAuthor should not be required"
52
//                    + " for reference type " + type.getLabel() +
53
//                    " and does not exist. Use Generic getTitleWithoutYearAndAuthorGeneric instead");
54
            return getTitleWithoutYearAndAuthorGeneric(ref, isAbbrev);
55
        }else if (type == ReferenceType.Article){
56
            return getTitleWithoutYearAndAuthorArticle(ref, isAbbrev, isNomRef);
57
        }else if(type == ReferenceType.Book){
58
            return getTitleWithoutYearAndAuthorBook(ref, isAbbrev);
59
        }else if(type == ReferenceType.CdDvd){
60
            return getTitleWithoutYearAndAuthorCdDvd(ref, isAbbrev);
61
        }else if(type == ReferenceType.Generic){
62
            return getTitleWithoutYearAndAuthorGeneric(ref, isAbbrev);
63
        }else if (type == ReferenceType.WebPage) {
64
            return getTitleWithoutYearAndAuthorWebPage(ref, isAbbrev);
65
        }else if (type == ReferenceType.Thesis) {
66
            return getTitleWithoutYearAndAuthorThesis(ref, isAbbrev);
67
        }else if(type == ReferenceType.BookSection){
68
            return getTitleWithoutYearAndAuthorBookSection(ref, isAbbrev);
69
        }else if (type == ReferenceType.Section || type == ReferenceType.BookSection ){
70
            // not needed in Section
71
            logger.warn("Questionable procedure call. Procedure not implemented because not needed. ");
72
            return null;
73
        }else{
74
            //FIXME
75
            return null;
76
        }
77
    }
78

    
79
    private static String getTitleWithoutYearAndAuthorArticle(Reference article, boolean isAbbrev, boolean isNomRef){
80
        if (article == null){
81
            return null;
82
        }
83
        IJournal journal = article.getInReference();
84

    
85
        String journalTitel;
86
        if (journal != null){
87
            journalTitel = CdmUtils.getPreferredNonEmptyString(journal.getTitle(), journal.getAbbrevTitle(), isAbbrev, true);
88
        }else{
89
            journalTitel = ReferenceDefaultCacheStrategy.UNDEFINED_JOURNAL;
90
        }
91

    
92
        String series = Nz(article.getSeriesPart()).trim();
93
        String volume = Nz(article.getVolume()).trim();
94

    
95
        boolean needsComma = false;
96

    
97
        //inJournal
98
        String result = isNomRef? "in ": prefixArticleReferenceJounal;
99

    
100
        //titelAbbrev
101
        if (isNotBlank(journalTitel)){
102
            result = result + journalTitel;
103
            needsComma = computeNeedsCommaArticle(result, volume, series);
104
            if (! needsComma){
105
                result = result + blank;
106
            }
107
        }
108

    
109
        //series and vol.
110
        result = getSeriesAndVolPartArticle(series, volume, needsComma, result);
111

    
112
        //delete "."
113
        while (result.endsWith(".")){
114
            result = CdmUtils.removeTrailingDots(result);
115
        }
116

    
117
        String articleTitle = CdmUtils.getPreferredNonEmptyString(article.getTitle(), article.getAbbrevTitle(), isAbbrev, true);
118
        if (!isNomRef && isNotBlank(articleTitle)){
119
            articleTitle = CdmUtils.addTrailingDotIfNotExists(articleTitle);
120
            result = articleTitle + " " + result;
121
        }
122
        return result.trim();
123
    }
124

    
125
    private static String getTitleWithoutYearAndAuthorBook(Reference ref, boolean isAbbrev){
126
        if (ref == null){
127
            return null;
128
        }
129
        //TODO
130
        String title = CdmUtils.getPreferredNonEmptyString(ref.getTitle(), ref.getAbbrevTitle(), isAbbrev, true);
131
        String edition = Nz(ref.getEdition()).trim();
132
        String series = ""; //TODO ref.getSeries();  //SeriesPart is handled later
133
        String refSeriesPart = Nz(ref.getSeriesPart());
134
        String volume = CdmUtils.Nz(ref.getVolume()).trim();
135
        String refYear = "";  //TODO nomenclaturalReference.getYear();
136

    
137
        String nomRefCache = "";
138
        Integer len;
139
        String lastChar = "";
140
        String character =".";
141
        len = title.length();
142
        if (len > 0){
143
            lastChar = title.substring(len-1, len);
144
        }
145
        //lastCharIsDouble = f_core_CompareStrings(RIGHT(@TitelAbbrev,1),character);
146
        boolean lastCharIsDouble = title.equals(character);
147

    
148
        if(lastCharIsDouble && edition.length() == 0 && refSeriesPart.length() == 0 && volume.length() == 0 && refYear.length() > 0 ){
149
            title =  title.substring(1, len-1); //  SUBSTRING(@TitelAbbrev,1,@LEN-1)
150
        }
151

    
152
        boolean needsComma = false;
153
        //titelAbbrev
154
        if (!"".equals(title) ){
155
            String postfix = isNotBlank(edition + refSeriesPart) ? "" : blank;
156
            nomRefCache = title + postfix;
157
        }
158
        //edition
159
        String editionPart = "";
160
        if (isNotBlank(edition)){
161
            editionPart = edition;
162
            if (isNumeric(edition)){
163
                editionPart = prefixBookEdition + blank + editionPart;
164
            }
165
            needsComma = true;
166
        }
167
        nomRefCache = CdmUtils.concat(", ", nomRefCache, editionPart);
168

    
169
        //inSeries
170
        String seriesPart = "";
171
        if (isNotBlank(refSeriesPart)){
172
            seriesPart = refSeriesPart;
173
            if (isNumeric(refSeriesPart)){
174
                seriesPart = prefixBookSeries + blank + seriesPart;
175
            }else if (needsComma){
176
                seriesPart = comma + seriesPart;
177
            }else if (seriesPart.matches("^\\p{L}.*")){ //
178
                //for characters we expect a space
179
                seriesPart = " " + seriesPart;
180
            }
181
            needsComma = true;
182
        }
183
        nomRefCache += seriesPart;
184

    
185
        //volume Part
186
        String volumePart = "";
187
        if (isNotBlank(volume)){
188
            volumePart = volume;
189
            if (needsComma){
190
                volumePart = comma + blank + volumePart;
191
            }else{
192
                volumePart = "" + volumePart;
193
            }
194
            //needsComma = false;
195
        }
196
        nomRefCache += volumePart;
197

    
198
        //delete .
199
        nomRefCache = CdmUtils.removeTrailingDots(nomRefCache);
200

    
201
        return nomRefCache.trim();
202
    }
203

    
204
    private static String getTitleWithoutYearAndAuthorBookSection(Reference ref, boolean isAbbrev){
205
        if (ref == null){
206
            return null;
207
        }
208
        String title = CdmUtils.getPreferredNonEmptyString(ref.getTitle(), ref.getAbbrevTitle(), isAbbrev, true);
209
        title = CdmUtils.removeTrailingDots(title);
210

    
211
        return title.trim();
212
    }
213

    
214
    public static  String getTitleWithoutYearAndAuthorGeneric(Reference genericReference, boolean isAbbrev){
215
        if (genericReference == null){
216
            return null;
217
        }
218
        //TODO
219
        String titel = CdmUtils.getPreferredNonEmptyString(genericReference.getTitle(), genericReference.getAbbrevTitle(), isAbbrev, true);
220
        String edition = CdmUtils.Nz(genericReference.getEdition());
221
        //TODO
222
        String series = CdmUtils.Nz(genericReference.getSeriesPart()).trim(); //nomenclaturalReference.getSeries();
223
        String volume = CdmUtils.Nz(genericReference.getVolume()).trim();
224

    
225
        String result = "";
226
        boolean lastCharIsDouble;
227
        Integer len;
228
        String lastChar ="";
229
        String character =".";
230
        len = titel.length();
231
        if (len > 0){lastChar = titel.substring(len-1, len);}
232
        //lastCharIsDouble = f_core_CompareStrings(RIGHT(@TitelAbbrev,1),character);
233
        lastCharIsDouble = titel.equals(character);
234

    
235
//      if(lastCharIsDouble  && edition.length() == 0 && series.length() == 0 && volume.length() == 0 && refYear.length() > 0 ){
236
//          titelAbbrev =  titelAbbrev.substring(1, len-1); //  SUBSTRING(@TitelAbbrev,1,@LEN-1)
237
//      }
238

    
239
        boolean needsComma = false;
240
        //titelAbbrev
241
        if (titel.length() > 0 ){
242
            String postfix = isNotBlank(edition) ? "" : blank;
243
            result = titel + postfix;
244
        }
245
        //edition
246
        String editionPart = "";
247
        if (isNotBlank(edition)){
248
            editionPart = edition;
249
            if (edition.matches(NonViralNameParserImplRegExBase.pEdition)){
250
                editionPart = prefixEditionGeneric + blank + editionPart;
251
            }
252
            needsComma = true;
253
        }
254
        result = CdmUtils.concat(", ", result, editionPart);
255

    
256
        //inSeries
257
        String seriesPart = "";
258
        if (isNotBlank(series)){
259
            seriesPart = series;
260
            if (isNumeric(series)){
261
                seriesPart = prefixSeriesGeneric + blank + seriesPart;
262
            }
263
            if (needsComma){
264
                seriesPart = comma + seriesPart;
265
            }
266
            needsComma = true;
267
        }
268
        result += seriesPart;
269

    
270

    
271
        //volume Part
272
        String volumePart = "";
273
        if (!"".equals(volume)){
274
            volumePart = volume;
275
            if (needsComma){
276
                volumePart = comma + blank + volumePart;
277
            }
278
            //needsComma = false;
279
        }
280
        result += volumePart;
281

    
282
        //delete .   //TODO needed? Creates problems e.g. if vol ends with dot, like vol="3, Suppl.", this is not handled correctly
283
        while (result.endsWith(".")){
284
            result = result.substring(0, result.length()-1);
285
        }
286

    
287
        return result.trim();
288
    }
289

    
290
    private static String getTitleWithoutYearAndAuthorCdDvd(Reference ref, boolean isAbbrev){
291
        if (ref == null){
292
            return null;
293
        }
294
        String nomRefCache = "";
295
        //TODO
296
        String titel = CdmUtils.getPreferredNonEmptyString(ref.getTitle(), ref.getAbbrevTitle(), isAbbrev, true);
297
//      String publisher = CdmUtils.Nz(nomenclaturalReference.getPublisher());
298

    
299
        boolean needsComma = false;
300
        //titelAbbrev
301
        if (titel.length() > 0){
302
            nomRefCache = titel + blank;
303
        }
304
//      //publisher
305
//      String publisherPart = "";
306
//      if (!"".equals(publisher)){
307
//          publisherPart = publisher;
308
//          needsComma = true;
309
//      }
310
//      nomRefCache += publisherPart;
311

    
312
        //delete .
313
        while (nomRefCache.endsWith(".")){
314
            nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
315
        }
316
        return nomRefCache.trim();
317
    }
318

    
319
    private static String getTitleWithoutYearAndAuthorThesis(Reference ref, boolean isAbbrev) {
320
        //FIXME this is only a very fast copy and paste from "Generic". Must still be cleaned !
321

    
322
        if (ref == null){
323
            return null;
324
        }
325

    
326
        //titelAbbrev
327
        //TODO
328
        String titelAbbrev = CdmUtils.getPreferredNonEmptyString(ref.getTitle(), ref.getAbbrevTitle(), isAbbrev, true);
329

    
330
        //titelAbbrev
331
        String nomRefCache = titelAbbrev + blank;
332

    
333
        //delete .
334
        while (nomRefCache.endsWith(".")){
335
            nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
336
        }
337

    
338
        return nomRefCache.trim();
339
    }
340

    
341
    private static String getTitleWithoutYearAndAuthorWebPage(Reference ref, boolean isAbbrev) {
342
        //FIXME this is only a very fast copy and paste from "Generic". Must still be cleaned !
343

    
344
        if (ref == null){
345
            return null;
346
        }
347

    
348
        //titleAbbrev
349
        //TODO
350
        String titleAbbrev = CdmUtils.getPreferredNonEmptyString(ref.getTitle(), ref.getAbbrevTitle(), isAbbrev, true);
351
        if (isBlank(titleAbbrev) && ref.getUri() != null){
352
            titleAbbrev = ref.getUri().toString();
353
        }
354

    
355
        //titelAbbrev
356
        String nomRefCache = titleAbbrev + blank;
357

    
358
        //delete .
359
        while (nomRefCache.endsWith(".")){
360
            nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
361
        }
362

    
363
        return nomRefCache.trim();
364
    }
365

    
366
//**************************** HELPER ********************************/
367

    
368
    public static boolean computeNeedsCommaArticle(String nomRefCache, String volume, String series) {
369

    
370
        nomRefCache = nomRefCache.toLowerCase();
371
        int serIndex = nomRefCache.indexOf(" ser. ");
372
        int sectIndex = nomRefCache.indexOf(" sect. ");
373
        int abtIndex = nomRefCache.indexOf(" abt. ");
374
        int index = Math.max(Math.max(serIndex, sectIndex), abtIndex);
375
        int commaIndex = nomRefCache.indexOf(",", index);
376
        if (index > -1 && commaIndex == -1 && isNotBlank(volume)){
377
            return true;
378
        }else if (isNotBlank(series)){
379
            return true;
380
        }else{
381
            return false;
382
        }
383
    }
384

    
385
    public static String getSeriesAndVolPartArticle(String series, String volume,
386
            boolean needsComma, String nomRefCache) {
387
        //inSeries
388
        String seriesPart = "";
389
        if (isNotBlank(series)){
390
            seriesPart = series;
391
            if (CdmUtils.isNumeric(series)){
392
                seriesPart = prefixSeriesArticle + blank + seriesPart;
393
            }
394
//          if (needsComma){
395
                seriesPart = comma + blank + seriesPart;
396
//          }
397
            needsComma = true;
398
        }
399
        nomRefCache += seriesPart;
400

    
401
        //volume Part
402
        String volumePart = "";
403
        if (!"".equals(volume)){
404
            volumePart = volume;
405
            if (needsComma){
406
                volumePart = comma + blank + volumePart;
407
            }
408
            //needsComma = false;
409
        }
410
        nomRefCache += volumePart;
411
        return nomRefCache;
412
    }
413

    
414
// ****************** COMMON **********************************************/
415

    
416
    /**
417
     * Null safe string. Returns the given string if it is not <code>null</code>.
418
     * Empty string otherwise.
419
     * @see CdmUtils#Nz(String)
420
     * @return the null-safe string
421
     */
422
    private static String Nz(String str){
423
        return CdmUtils.Nz(str);
424
    }
425

    
426
    /**
427
     * Checks if a string is not blank.
428
     * @see StringUtils#isNotBlank(String)
429
     */
430
    private static boolean isNotBlank(String str){
431
        return StringUtils.isNotBlank(str);
432
    }
433

    
434
    /**
435
     * Checks if a string is blank.
436
     * @see StringUtils#isNotBlank(String)
437
     */
438
    private static boolean isBlank(String str){
439
        return StringUtils.isBlank(str);
440
    }
441

    
442
    private static boolean isNumeric(String string){
443
        if (string == null){
444
            return false;
445
        }
446
        try {
447
            Double.valueOf(string);
448
            return true;
449
        } catch (NumberFormatException e) {
450
            return false;
451
        }
452
    }
453
}
(3-3/3)