Project

General

Profile

Download (15.4 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 = 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
            }
176
            if (needsComma){
177
                seriesPart = comma + seriesPart;
178
            }
179
            needsComma = true;
180
        }
181
        nomRefCache += seriesPart;
182

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

    
196
        //delete .
197
        nomRefCache = CdmUtils.removeTrailingDots(nomRefCache);
198

    
199
        return nomRefCache.trim();
200
    }
201

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

    
209
        return title.trim();
210
    }
211

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

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

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

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

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

    
268

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

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

    
285
        return result.trim();
286
    }
287

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

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

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

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

    
320
        if (ref == null){
321
            return null;
322
        }
323

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

    
328
        //titelAbbrev
329
        String nomRefCache = titelAbbrev + blank;
330

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

    
336
        return nomRefCache.trim();
337
    }
338

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

    
342
        if (ref == null){
343
            return null;
344
        }
345

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

    
353
        //titelAbbrev
354
        String nomRefCache = titleAbbrev + blank;
355

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

    
361
        return nomRefCache.trim();
362
    }
363

    
364
//**************************** HELPER ********************************/
365

    
366
    public static boolean computeNeedsCommaArticle(String nomRefCache, String volume, String series) {
367

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

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

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

    
412
// ****************** COMMON **********************************************/
413

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

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

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

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