2 * Copyright (C) 2016 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.strategy
.cache
.reference
;
11 import org
.apache
.commons
.lang3
.StringUtils
;
12 import org
.apache
.log4j
.Logger
;
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
;
26 public class TitleWithoutYearAndAuthorHelper
{
27 private static final Logger logger
= Logger
.getLogger(TitleWithoutYearAndAuthorHelper
.class);
30 private static final String prefixArticleReferenceJounal
= UTF8
.EN_DASH
+ " ";
31 private static final String prefixSeriesArticle
= "ser.";
34 private static final String prefixBookEdition
= "ed.";
35 private static final String prefixBookSeries
= ", ser.";
38 private static final String prefixEditionGeneric
= "ed.";
39 private static final String prefixSeriesGeneric
= "ser.";
42 private static final String blank
= " ";
43 private static final String comma
= ",";
46 // *************************Main METHODS ***********************************/
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
.Section
|| type
== ReferenceType
.BookSection
){
68 // not needed in Section
69 logger
.warn("Questionable procedure call. Procedure not implemented because not needed. ");
77 private static String
getTitleWithoutYearAndAuthorArticle(Reference article
, boolean isAbbrev
, boolean isNomRef
){
81 IJournal journal
= article
.getInReference();
85 journalTitel
= CdmUtils
.getPreferredNonEmptyString(journal
.getTitle(), journal
.getAbbrevTitle(), isAbbrev
, true);
87 journalTitel
= ReferenceDefaultCacheStrategy
.UNDEFINED_JOURNAL
;
90 String series
= Nz(article
.getSeriesPart()).trim();
91 String volume
= Nz(article
.getVolume()).trim();
93 boolean needsComma
= false;
96 String nomRefCache
= isNomRef?
"in ": prefixArticleReferenceJounal
;
99 if (isNotBlank(journalTitel
)){
100 nomRefCache
= nomRefCache
+ journalTitel
;
101 needsComma
= makeNeedsCommaArticle(needsComma
, nomRefCache
, volume
, series
);
103 nomRefCache
= nomRefCache
+ blank
;
108 nomRefCache
= getSeriesAndVolPartArticle(series
, volume
, needsComma
, nomRefCache
);
111 while (nomRefCache
.endsWith(".")){
112 nomRefCache
= CdmUtils
.removeTrailingDots(nomRefCache
);
115 return nomRefCache
.trim();
119 private static String
getTitleWithoutYearAndAuthorBook(Reference ref
, boolean isAbbrev
){
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();
132 String nomRefCache
= "";
134 String lastChar
= "";
135 String character
=".";
136 len
= title
.length();
138 lastChar
= title
.substring(len
-1, len
);
140 //lastCharIsDouble = f_core_CompareStrings(RIGHT(@TitelAbbrev,1),character);
141 boolean lastCharIsDouble
= title
.equals(character
);
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)
148 boolean needsComma
= false;
150 if (!"".equals(title
) ){
151 String postfix
= isNotBlank(edition
+ refSeriesPart
) ?
"" : blank
;
152 nomRefCache
= title
+ postfix
;
155 String editionPart
= "";
156 if (isNotBlank(edition
)){
157 editionPart
= edition
;
158 if (isNumeric(edition
)){
159 editionPart
= prefixBookEdition
+ blank
+ editionPart
;
163 nomRefCache
= CdmUtils
.concat(", ", nomRefCache
, editionPart
);
166 String seriesPart
= "";
167 if (isNotBlank(refSeriesPart
)){
168 seriesPart
= refSeriesPart
;
169 if (isNumeric(refSeriesPart
)){
170 seriesPart
= prefixBookSeries
+ blank
+ seriesPart
;
173 seriesPart
= comma
+ seriesPart
;
177 nomRefCache
+= seriesPart
;
181 String volumePart
= "";
182 if (isNotBlank(volume
)){
185 volumePart
= comma
+ blank
+ volumePart
;
187 volumePart
= "" + volumePart
;
189 //needsComma = false;
191 nomRefCache
+= volumePart
;
194 while (nomRefCache
.endsWith(".")){
195 nomRefCache
= nomRefCache
.substring(0, nomRefCache
.length()-1);
198 return nomRefCache
.trim();
201 public static String
getTitleWithoutYearAndAuthorGeneric(Reference genericReference
, boolean isAbbrev
){
202 if (genericReference
== null){
206 String titel
= CdmUtils
.getPreferredNonEmptyString(genericReference
.getTitle(), genericReference
.getAbbrevTitle(), isAbbrev
, true);
207 String edition
= CdmUtils
.Nz(genericReference
.getEdition());
209 String series
= CdmUtils
.Nz(genericReference
.getSeriesPart()).trim(); //nomenclaturalReference.getSeries();
210 String volume
= CdmUtils
.Nz(genericReference
.getVolume()).trim();
213 boolean lastCharIsDouble
;
216 String character
=".";
217 len
= titel
.length();
218 if (len
> 0){lastChar
= titel
.substring(len
-1, len
);}
219 //lastCharIsDouble = f_core_CompareStrings(RIGHT(@TitelAbbrev,1),character);
220 lastCharIsDouble
= titel
.equals(character
);
222 // if(lastCharIsDouble && edition.length() == 0 && series.length() == 0 && volume.length() == 0 && refYear.length() > 0 ){
223 // titelAbbrev = titelAbbrev.substring(1, len-1); // SUBSTRING(@TitelAbbrev,1,@LEN-1)
226 boolean needsComma
= false;
228 if (titel
.length() > 0 ){
229 String postfix
= isNotBlank(edition
) ?
"" : blank
;
230 result
= titel
+ postfix
;
233 String editionPart
= "";
234 if (isNotBlank(edition
)){
235 editionPart
= edition
;
236 if (edition
.matches(NonViralNameParserImplRegExBase
.pEdition
)){
237 editionPart
= prefixEditionGeneric
+ blank
+ editionPart
;
241 result
= CdmUtils
.concat(", ", result
, editionPart
);
244 String seriesPart
= "";
245 if (isNotBlank(series
)){
247 if (isNumeric(series
)){
248 seriesPart
= prefixSeriesGeneric
+ blank
+ seriesPart
;
251 seriesPart
= comma
+ seriesPart
;
255 result
+= seriesPart
;
259 String volumePart
= "";
260 if (!"".equals(volume
)){
263 volumePart
= comma
+ blank
+ volumePart
;
265 //needsComma = false;
267 result
+= volumePart
;
269 //delete . //TODO needed? Creates problems e.g. if vol ends with dot, like vol="3, Suppl.", this is not handled correctly
270 while (result
.endsWith(".")){
271 result
= result
.substring(0, result
.length()-1);
274 return result
.trim();
277 private static String
getTitleWithoutYearAndAuthorCdDvd(Reference ref
, boolean isAbbrev
){
281 String nomRefCache
= "";
283 String titel
= CdmUtils
.getPreferredNonEmptyString(ref
.getTitle(), ref
.getAbbrevTitle(), isAbbrev
, true);
284 // String publisher = CdmUtils.Nz(nomenclaturalReference.getPublisher());
286 boolean needsComma
= false;
288 if (titel
.length() > 0){
289 nomRefCache
= titel
+ blank
;
292 // String publisherPart = "";
293 // if (!"".equals(publisher)){
294 // publisherPart = publisher;
295 // needsComma = true;
297 // nomRefCache += publisherPart;
300 while (nomRefCache
.endsWith(".")){
301 nomRefCache
= nomRefCache
.substring(0, nomRefCache
.length()-1);
303 return nomRefCache
.trim();
306 private static String
getTitleWithoutYearAndAuthorThesis(Reference ref
, boolean isAbbrev
) {
307 //FIXME this is only a very fast copy and paste from "Generic". Must still be cleaned !
315 String titelAbbrev
= CdmUtils
.getPreferredNonEmptyString(ref
.getTitle(), ref
.getAbbrevTitle(), isAbbrev
, true);
318 String nomRefCache
= titelAbbrev
+ blank
;
321 while (nomRefCache
.endsWith(".")){
322 nomRefCache
= nomRefCache
.substring(0, nomRefCache
.length()-1);
325 return nomRefCache
.trim();
328 private static String
getTitleWithoutYearAndAuthorWebPage(Reference ref
, boolean isAbbrev
) {
329 //FIXME this is only a very fast copy and paste from "Generic". Must still be cleaned !
337 String titleAbbrev
= CdmUtils
.getPreferredNonEmptyString(ref
.getTitle(), ref
.getAbbrevTitle(), isAbbrev
, true);
338 if (isBlank(titleAbbrev
) && ref
.getUri() != null){
339 titleAbbrev
= ref
.getUri().toString();
343 String nomRefCache
= titleAbbrev
+ blank
;
346 while (nomRefCache
.endsWith(".")){
347 nomRefCache
= nomRefCache
.substring(0, nomRefCache
.length()-1);
350 return nomRefCache
.trim();
353 //**************************** HELPER ********************************/
355 private static boolean makeNeedsCommaArticle(boolean needsComma
, String nomRefCache
, String volume
, String series
) {
359 nomRefCache
= nomRefCache
.toLowerCase();
360 int serIndex
= nomRefCache
.indexOf(" ser. ");
361 int sectIndex
= nomRefCache
.indexOf(" sect. ");
362 int abtIndex
= nomRefCache
.indexOf(" abt. ");
363 int index
= Math
.max(Math
.max(serIndex
, sectIndex
), abtIndex
);
364 int commaIndex
= nomRefCache
.indexOf(",", index
);
365 if (index
> -1 && commaIndex
== -1 && isNotBlank(volume
)){
367 }else if (isNotBlank(series
)){
375 private static String
getSeriesAndVolPartArticle(String series
, String volume
,
376 boolean needsComma
, String nomRefCache
) {
378 String seriesPart
= "";
379 if (isNotBlank(series
)){
381 if (CdmUtils
.isNumeric(series
)){
382 seriesPart
= prefixSeriesArticle
+ blank
+ seriesPart
;
385 seriesPart
= comma
+ blank
+ seriesPart
;
389 nomRefCache
+= seriesPart
;
393 String volumePart
= "";
394 if (!"".equals(volume
)){
397 volumePart
= comma
+ blank
+ volumePart
;
399 //needsComma = false;
401 nomRefCache
+= volumePart
;
405 // ****************** COMMON **********************************************/
408 * Null safe string. Returns the given string if it is not <code>null</code>.
409 * Empty string otherwise.
410 * @see CdmUtils#Nz(String)
411 * @return the null-safe string
413 private static String
Nz(String str
){
414 return CdmUtils
.Nz(str
);
418 * Checks if a string is not blank.
419 * @see StringUtils#isNotBlank(String)
421 private static boolean isNotBlank(String str
){
422 return StringUtils
.isNotBlank(str
);
426 * Checks if a string is blank.
427 * @see StringUtils#isNotBlank(String)
429 private static boolean isBlank(String str
){
430 return StringUtils
.isBlank(str
);
433 private static boolean isNumeric(String string
){
438 Double
.valueOf(string
);
440 } catch (NumberFormatException e
) {