Project

General

Profile

Download (13.8 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 (! NewDefaultReferenceCacheStrategy.isNomRef(type)){
50
            throw new RuntimeException("getTitleWithoutYearAndAuthor should not be required"
51
                    + " for reference type " + type.getMessage());
52
        }else if (type == ReferenceType.Article){
53
            return getTitleWithoutYearAndAuthorArticle(ref, isAbbrev);
54
        }else if(type == ReferenceType.Book){
55
            return getTitleWithoutYearAndAuthorBook(ref, isAbbrev);
56
        }else if(type == ReferenceType.CdDvd){
57
            return getTitleWithoutYearAndAuthorCdDvd(ref, isAbbrev);
58
        }else if(type == ReferenceType.Generic){
59
            return getTitleWithoutYearAndAuthorGeneric(ref, isAbbrev);
60
        }else if (type == ReferenceType.WebPage || type == ReferenceType.Thesis) {
61
            return getTitleWithoutYearAndAuthorWebPageThesis(ref, isAbbrev);
62
        }else if (type == ReferenceType.Section || type == ReferenceType.BookSection){
63
            // not needed in Section
64
            logger.warn("Questionable procedure call. Procedure not implemented because not needed. ");
65
            return null;
66
        }else{
67
            //FIXME
68
            return null;
69
        }
70

    
71
    }
72

    
73

    
74

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

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

    
88
        String series = Nz(article.getSeriesPart()).trim();
89
        String volume = Nz(article.getVolume()).trim();
90

    
91
        boolean needsComma = false;
92

    
93
        String nomRefCache = "";
94

    
95
        //inJournal
96
        nomRefCache = prefixArticleReferenceJounal + blank;
97

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

    
107
        //series and vol.
108
        nomRefCache = getSeriesAndVolPartArticle(series, volume, needsComma, nomRefCache);
109

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

    
115
        return nomRefCache.trim();
116
    }
117

    
118

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

    
131

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

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

    
147

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

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

    
179

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

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

    
198
        return nomRefCache.trim();
199
    }
200

    
201

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

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

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

    
227

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

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

    
259

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

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

    
276
        return result.trim();
277
    }
278

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

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

    
301

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

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

    
317
        if (ref == null){
318
            return null;
319
        }
320

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

    
325
        //titelAbbrev
326
        String nomRefCache = titelAbbrev + blank;
327

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

    
333
        return nomRefCache.trim();
334
    }
335

    
336
//**************************** HELPER ********************************/
337

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

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

    
374

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

    
388
// ****************** COMMON **********************************************/
389

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

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

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

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