Project

General

Profile

Download (14.8 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.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
 \* @since 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) {
62
            return getTitleWithoutYearAndAuthorWebPage(ref, isAbbrev);
63
        }else if (type == ReferenceType.Thesis) {
64
            return getTitleWithoutYearAndAuthorThesis(ref, isAbbrev);
65
        }else if (type == ReferenceType.Section || type == ReferenceType.BookSection){
66
            // not needed in Section
67
            logger.warn("Questionable procedure call. Procedure not implemented because not needed. ");
68
            return null;
69
        }else{
70
            //FIXME
71
            return null;
72
        }
73

    
74
    }
75

    
76

    
77

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

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

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

    
94
        boolean needsComma = false;
95

    
96
        String nomRefCache = "";
97

    
98
        //inJournal
99
        nomRefCache = prefixArticleReferenceJounal + blank;
100

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

    
110
        //series and vol.
111
        nomRefCache = getSeriesAndVolPartArticle(series, volume, needsComma, nomRefCache);
112

    
113
        //delete "."
114
        while (nomRefCache.endsWith(".")){
115
            nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
116
        }
117

    
118
        return nomRefCache.trim();
119
    }
120

    
121

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

    
134

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

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

    
150

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

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

    
182

    
183
        //volume Part
184
        String volumePart = "";
185
        if (StringUtils.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
        while (nomRefCache.endsWith(".")){
198
            nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
199
        }
200

    
201
        return nomRefCache.trim();
202
    }
203

    
204

    
205
    public static  String getTitleWithoutYearAndAuthorGeneric(Reference genericReference, boolean isAbbrev){
206
        if (genericReference == null){
207
            return null;
208
        }
209
        //TODO
210
        String titel = CdmUtils.getPreferredNonEmptyString(genericReference.getTitle(), genericReference.getAbbrevTitle(), isAbbrev, true);
211
        String edition = CdmUtils.Nz(genericReference.getEdition());
212
        //TODO
213
        String series = CdmUtils.Nz(genericReference.getSeriesPart()).trim(); //nomenclaturalReference.getSeries();
214
        String volume = CdmUtils.Nz(genericReference.getVolume()).trim();
215

    
216
        String result = "";
217
        boolean lastCharIsDouble;
218
        Integer len;
219
        String lastChar ="";
220
        String character =".";
221
        len = titel.length();
222
        if (len > 0){lastChar = titel.substring(len-1, len);}
223
        //lastCharIsDouble = f_core_CompareStrings(RIGHT(@TitelAbbrev,1),character);
224
        lastCharIsDouble = titel.equals(character);
225

    
226
//      if(lastCharIsDouble  && edition.length() == 0 && series.length() == 0 && volume.length() == 0 && refYear.length() > 0 ){
227
//          titelAbbrev =  titelAbbrev.substring(1, len-1); //  SUBSTRING(@TitelAbbrev,1,@LEN-1)
228
//      }
229

    
230

    
231
        boolean needsComma = false;
232
        //titelAbbrev
233
        if (titel.length() > 0 ){
234
            String postfix = StringUtils.isNotBlank(edition) ? "" : blank;
235
            result = titel + postfix;
236
        }
237
        //edition
238
        String editionPart = "";
239
        if (StringUtils.isNotBlank(edition)){
240
            editionPart = edition;
241
            if (isNumeric(edition)){
242
                editionPart = prefixEditionGeneric + blank + editionPart;
243
            }
244
            needsComma = true;
245
        }
246
        result = CdmUtils.concat(", ", result, editionPart);
247

    
248
        //inSeries
249
        String seriesPart = "";
250
        if (isNotBlank(series)){
251
            seriesPart = series;
252
            if (isNumeric(series)){
253
                seriesPart = prefixSeriesGeneric + blank + seriesPart;
254
            }
255
            if (needsComma){
256
                seriesPart = comma + seriesPart;
257
            }
258
            needsComma = true;
259
        }
260
        result += seriesPart;
261

    
262

    
263
        //volume Part
264
        String volumePart = "";
265
        if (!"".equals(volume)){
266
            volumePart = volume;
267
            if (needsComma){
268
                volumePart = comma + blank + volumePart;
269
            }
270
            //needsComma = false;
271
        }
272
        result += volumePart;
273

    
274
        //delete .
275
        while (result.endsWith(".")){
276
            result = result.substring(0, result.length()-1);
277
        }
278

    
279
        return result.trim();
280
    }
281

    
282
    private static String getTitleWithoutYearAndAuthorCdDvd(Reference ref, boolean isAbbrev){
283
        if (ref == null){
284
            return null;
285
        }
286
        String nomRefCache = "";
287
        //TODO
288
        String titel = CdmUtils.getPreferredNonEmptyString(ref.getTitle(), ref.getAbbrevTitle(), isAbbrev, true);
289
//      String publisher = CdmUtils.Nz(nomenclaturalReference.getPublisher());
290

    
291
        boolean needsComma = false;
292
        //titelAbbrev
293
        if (titel.length() > 0){
294
            nomRefCache = titel + blank;
295
        }
296
//      //publisher
297
//      String publisherPart = "";
298
//      if (!"".equals(publisher)){
299
//          publisherPart = publisher;
300
//          needsComma = true;
301
//      }
302
//      nomRefCache += publisherPart;
303

    
304

    
305
        //delete .
306
        while (nomRefCache.endsWith(".")){
307
            nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
308
        }
309
        return nomRefCache.trim();
310
    }
311

    
312
    /**
313
     * @param ref
314
     * @param isAbbrev
315
     * @return
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
    /**
340
     * @param ref
341
     * @param isAbbrev
342
     * @return
343
     */
344
    private static String getTitleWithoutYearAndAuthorWebPage(Reference ref, boolean isAbbrev) {
345
        //FIXME this is only a very fast copy and paste from "Generic". Must still be cleaned !
346

    
347
        if (ref == null){
348
            return null;
349
        }
350

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

    
358
        //titelAbbrev
359
        String nomRefCache = titleAbbrev + blank;
360

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

    
366
        return nomRefCache.trim();
367
    }
368

    
369
//**************************** HELPER ********************************/
370

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

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

    
407

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

    
421
// ****************** COMMON **********************************************/
422

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

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

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

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