Project

General

Profile

Download (13.9 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2016 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.strategy.cache.reference;
11

    
12
import org.apache.commons.lang.StringUtils;
13
import org.apache.log4j.Logger;
14

    
15
import eu.etaxonomy.cdm.common.CdmUtils;
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

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

    
28
    //article
29
    private static final String prefixArticleReferenceJounal = "in";
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){
48
        ReferenceType type = ref.getType();
49
        if (! DefaultReferenceCacheStrategy.isNomRef(type)){
50
            logger.warn("getTitleWithoutYearAndAuthor should not be required"
51
                    + " for reference type " + type.getMessage() +
52
                    " and does not exist. Use Generic getTitleWithoutYearAndAuthorGeneric instead");
53
            return getTitleWithoutYearAndAuthorGeneric(ref, isAbbrev);
54
        }else if (type == ReferenceType.Article){
55
            return getTitleWithoutYearAndAuthorArticle(ref, isAbbrev);
56
        }else if(type == ReferenceType.Book){
57
            return getTitleWithoutYearAndAuthorBook(ref, isAbbrev);
58
        }else if(type == ReferenceType.CdDvd){
59
            return getTitleWithoutYearAndAuthorCdDvd(ref, isAbbrev);
60
        }else if(type == ReferenceType.Generic){
61
            return getTitleWithoutYearAndAuthorGeneric(ref, isAbbrev);
62
        }else if (type == ReferenceType.WebPage || type == ReferenceType.Thesis) {
63
            return getTitleWithoutYearAndAuthorWebPageThesis(ref, isAbbrev);
64
        }else if (type == ReferenceType.Section || type == ReferenceType.BookSection){
65
            // not needed in Section
66
            logger.warn("Questionable procedure call. Procedure not implemented because not needed. ");
67
            return null;
68
        }else{
69
            //FIXME
70
            return null;
71
        }
72

    
73
    }
74

    
75

    
76

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

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

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

    
93
        boolean needsComma = false;
94

    
95
        String nomRefCache = "";
96

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

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

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

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

    
117
        return nomRefCache.trim();
118
    }
119

    
120

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

    
133

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

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

    
149

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

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

    
181

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

    
195
        //delete .
196
        while (nomRefCache.endsWith(".")){
197
            nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
198
        }
199

    
200
        return nomRefCache.trim();
201
    }
202

    
203

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

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

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

    
229

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

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

    
261

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

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

    
278
        return result.trim();
279
    }
280

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

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

    
303

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

    
311
    /**
312
     * @param ref
313
     * @param isAbbrev
314
     * @return
315
     */
316
    private static String getTitleWithoutYearAndAuthorWebPageThesis(Reference ref, boolean isAbbrev) {
317
        //FIXME this is only a very fast copy and paste from "Generic". Must still be cleaned !
318

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

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

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

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

    
335
        return nomRefCache.trim();
336
    }
337

    
338
//**************************** HELPER ********************************/
339

    
340
    private static boolean makeNeedsCommaArticle(boolean needsComma, String nomRefCache, String volume, String series) {
341
        if (needsComma){
342
            return true;
343
        }else{
344
            nomRefCache = nomRefCache.toLowerCase();
345
            int serIndex = nomRefCache.indexOf(" ser. ");
346
            int sectIndex = nomRefCache.indexOf(" sect. ");
347
            int abtIndex = nomRefCache.indexOf(" abt. ");
348
            int index = Math.max(Math.max(serIndex, sectIndex), abtIndex);
349
            int commaIndex = nomRefCache.indexOf(",", index);
350
            if (index > -1 && commaIndex == -1 && isNotBlank(volume)){
351
                return true;
352
            }else if (isNotBlank(series)){
353
                return true;
354
            }else{
355
                return false;
356
            }
357
        }
358
    }
359

    
360
    private static String getSeriesAndVolPartArticle(String series, String volume,
361
            boolean needsComma, String nomRefCache) {
362
        //inSeries
363
        String seriesPart = "";
364
        if (isNotBlank(series)){
365
            seriesPart = series;
366
            if (CdmUtils.isNumeric(series)){
367
                seriesPart = prefixSeriesArticle + blank + seriesPart;
368
            }
369
//          if (needsComma){
370
                seriesPart = comma + blank + seriesPart;
371
//          }
372
            needsComma = true;
373
        }
374
        nomRefCache += seriesPart;
375

    
376

    
377
        //volume Part
378
        String volumePart = "";
379
        if (!"".equals(volume)){
380
            volumePart = volume;
381
            if (needsComma){
382
                volumePart = comma + blank + volumePart;
383
            }
384
            //needsComma = false;
385
        }
386
        nomRefCache += volumePart;
387
        return nomRefCache;
388
    }
389

    
390
// ****************** COMMON **********************************************/
391

    
392
    /**
393
     * Null safe string. Returns the given string if it is not <code>null</code>.
394
     * Empty string otherwise.
395
     * @see CdmUtils#Nz(String)
396
     * @return the null-safe string
397
     */
398
    private static String Nz(String str){
399
        return CdmUtils.Nz(str);
400
    }
401

    
402
    /**
403
     * Checks if a string is not blank.
404
     * @see StringUtils#isNotBlank(String)
405
     */
406
    private static boolean isNotBlank(String str){
407
        return StringUtils.isNotBlank(str);
408
    }
409

    
410
    /**
411
     * Checks if a string is blank.
412
     * @see StringUtils#isNotBlank(String)
413
     */
414
    private static boolean isBlank(String str){
415
        return StringUtils.isBlank(str);
416
    }
417

    
418
    private static boolean isNumeric(String string){
419
        if (string == null){
420
            return false;
421
        }
422
        try {
423
            Double.valueOf(string);
424
            return true;
425
        } catch (NumberFormatException e) {
426
            return false;
427
        }
428
    }
429
}
(4-4/4)