Project

General

Profile

Download (15.6 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
 */
26
public class TitleWithoutYearAndAuthorHelper {
27
    private static final Logger logger = Logger.getLogger(TitleWithoutYearAndAuthorHelper.class);
28

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

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

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

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

    
45

    
46
// *************************Main METHODS ***********************************/
47

    
48
    public static String getTitleWithoutYearAndAuthor(Reference ref, boolean isAbbrev, boolean isNomRef){
49
        ReferenceType type = ref.getType();
50
        if (! ReferenceDefaultCacheStrategy.isNomRef(type)){
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 = makeNeedsCommaArticle(needsComma, 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

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

    
138

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

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

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

    
171
        //inSeries
172
        String seriesPart = "";
173
        if (isNotBlank(refSeriesPart)){
174
            seriesPart = refSeriesPart;
175
            if (isNumeric(refSeriesPart)){
176
                seriesPart = prefixBookSeries + blank + seriesPart;
177
            }
178
            if (needsComma){
179
                seriesPart = comma + seriesPart;
180
            }
181
            needsComma = true;
182
        }
183
        nomRefCache += seriesPart;
184

    
185

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

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

    
202
        return nomRefCache.trim();
203
    }
204

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

    
212
        return title.trim();
213
    }
214

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

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

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

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

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

    
271

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

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

    
288
        return result.trim();
289
    }
290

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

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

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

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

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

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

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

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

    
339
        return nomRefCache.trim();
340
    }
341

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

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

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

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

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

    
364
        return nomRefCache.trim();
365
    }
366

    
367
//**************************** HELPER ********************************/
368

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

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

    
405

    
406
        //volume Part
407
        String volumePart = "";
408
        if (!"".equals(volume)){
409
            volumePart = volume;
410
            if (needsComma){
411
                volumePart = comma + blank + volumePart;
412
            }
413
            //needsComma = false;
414
        }
415
        nomRefCache += volumePart;
416
        return nomRefCache;
417
    }
418

    
419
// ****************** COMMON **********************************************/
420

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

    
431
    /**
432
     * Checks if a string is not blank.
433
     * @see StringUtils#isNotBlank(String)
434
     */
435
    private static boolean isNotBlank(String str){
436
        return StringUtils.isNotBlank(str);
437
    }
438

    
439
    /**
440
     * Checks if a string is blank.
441
     * @see StringUtils#isNotBlank(String)
442
     */
443
    private static boolean isBlank(String str){
444
        return StringUtils.isBlank(str);
445
    }
446

    
447
    private static boolean isNumeric(String string){
448
        if (string == null){
449
            return false;
450
        }
451
        try {
452
            Double.valueOf(string);
453
            return true;
454
        } catch (NumberFormatException e) {
455
            return false;
456
        }
457
    }
458
}
(3-3/3)