cleanup
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / strategy / cache / reference / TitleWithoutYearAndAuthorHelper.java
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.Section || type == ReferenceType.BookSection){
68 // not needed in Section
69 logger.warn("Questionable procedure call. Procedure not implemented because not needed. ");
70 return null;
71 }else{
72 //FIXME
73 return null;
74 }
75 }
76
77 private static String getTitleWithoutYearAndAuthorArticle(Reference article, boolean isAbbrev, boolean isNomRef){
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 = ReferenceDefaultCacheStrategy.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 //inJournal
96 String nomRefCache = isNomRef? "in ": prefixArticleReferenceJounal;
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 = CdmUtils.removeTrailingDots(nomRefCache);
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 (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 (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 (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 public static String getTitleWithoutYearAndAuthorGeneric(Reference genericReference, boolean isAbbrev){
202 if (genericReference == null){
203 return null;
204 }
205 //TODO
206 String titel = CdmUtils.getPreferredNonEmptyString(genericReference.getTitle(), genericReference.getAbbrevTitle(), isAbbrev, true);
207 String edition = CdmUtils.Nz(genericReference.getEdition());
208 //TODO
209 String series = CdmUtils.Nz(genericReference.getSeriesPart()).trim(); //nomenclaturalReference.getSeries();
210 String volume = CdmUtils.Nz(genericReference.getVolume()).trim();
211
212 String result = "";
213 boolean lastCharIsDouble;
214 Integer len;
215 String lastChar ="";
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);
221
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)
224 // }
225
226 boolean needsComma = false;
227 //titelAbbrev
228 if (titel.length() > 0 ){
229 String postfix = isNotBlank(edition) ? "" : blank;
230 result = titel + postfix;
231 }
232 //edition
233 String editionPart = "";
234 if (isNotBlank(edition)){
235 editionPart = edition;
236 if (edition.matches(NonViralNameParserImplRegExBase.pEdition)){
237 editionPart = prefixEditionGeneric + blank + editionPart;
238 }
239 needsComma = true;
240 }
241 result = CdmUtils.concat(", ", result, editionPart);
242
243 //inSeries
244 String seriesPart = "";
245 if (isNotBlank(series)){
246 seriesPart = series;
247 if (isNumeric(series)){
248 seriesPart = prefixSeriesGeneric + blank + seriesPart;
249 }
250 if (needsComma){
251 seriesPart = comma + seriesPart;
252 }
253 needsComma = true;
254 }
255 result += seriesPart;
256
257
258 //volume Part
259 String volumePart = "";
260 if (!"".equals(volume)){
261 volumePart = volume;
262 if (needsComma){
263 volumePart = comma + blank + volumePart;
264 }
265 //needsComma = false;
266 }
267 result += volumePart;
268
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);
272 }
273
274 return result.trim();
275 }
276
277 private static String getTitleWithoutYearAndAuthorCdDvd(Reference ref, boolean isAbbrev){
278 if (ref == null){
279 return null;
280 }
281 String nomRefCache = "";
282 //TODO
283 String titel = CdmUtils.getPreferredNonEmptyString(ref.getTitle(), ref.getAbbrevTitle(), isAbbrev, true);
284 // String publisher = CdmUtils.Nz(nomenclaturalReference.getPublisher());
285
286 boolean needsComma = false;
287 //titelAbbrev
288 if (titel.length() > 0){
289 nomRefCache = titel + blank;
290 }
291 // //publisher
292 // String publisherPart = "";
293 // if (!"".equals(publisher)){
294 // publisherPart = publisher;
295 // needsComma = true;
296 // }
297 // nomRefCache += publisherPart;
298
299 //delete .
300 while (nomRefCache.endsWith(".")){
301 nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
302 }
303 return nomRefCache.trim();
304 }
305
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 !
308
309 if (ref == null){
310 return null;
311 }
312
313 //titelAbbrev
314 //TODO
315 String titelAbbrev = CdmUtils.getPreferredNonEmptyString(ref.getTitle(), ref.getAbbrevTitle(), isAbbrev, true);
316
317 //titelAbbrev
318 String nomRefCache = titelAbbrev + blank;
319
320 //delete .
321 while (nomRefCache.endsWith(".")){
322 nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
323 }
324
325 return nomRefCache.trim();
326 }
327
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 !
330
331 if (ref == null){
332 return null;
333 }
334
335 //titleAbbrev
336 //TODO
337 String titleAbbrev = CdmUtils.getPreferredNonEmptyString(ref.getTitle(), ref.getAbbrevTitle(), isAbbrev, true);
338 if (isBlank(titleAbbrev) && ref.getUri() != null){
339 titleAbbrev = ref.getUri().toString();
340 }
341
342 //titelAbbrev
343 String nomRefCache = titleAbbrev + blank;
344
345 //delete .
346 while (nomRefCache.endsWith(".")){
347 nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
348 }
349
350 return nomRefCache.trim();
351 }
352
353 //**************************** HELPER ********************************/
354
355 private static boolean makeNeedsCommaArticle(boolean needsComma, String nomRefCache, String volume, String series) {
356 if (needsComma){
357 return true;
358 }else{
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)){
366 return true;
367 }else if (isNotBlank(series)){
368 return true;
369 }else{
370 return false;
371 }
372 }
373 }
374
375 private static String getSeriesAndVolPartArticle(String series, String volume,
376 boolean needsComma, String nomRefCache) {
377 //inSeries
378 String seriesPart = "";
379 if (isNotBlank(series)){
380 seriesPart = series;
381 if (CdmUtils.isNumeric(series)){
382 seriesPart = prefixSeriesArticle + blank + seriesPart;
383 }
384 // if (needsComma){
385 seriesPart = comma + blank + seriesPart;
386 // }
387 needsComma = true;
388 }
389 nomRefCache += seriesPart;
390
391
392 //volume Part
393 String volumePart = "";
394 if (!"".equals(volume)){
395 volumePart = volume;
396 if (needsComma){
397 volumePart = comma + blank + volumePart;
398 }
399 //needsComma = false;
400 }
401 nomRefCache += volumePart;
402 return nomRefCache;
403 }
404
405 // ****************** COMMON **********************************************/
406
407 /**
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
412 */
413 private static String Nz(String str){
414 return CdmUtils.Nz(str);
415 }
416
417 /**
418 * Checks if a string is not blank.
419 * @see StringUtils#isNotBlank(String)
420 */
421 private static boolean isNotBlank(String str){
422 return StringUtils.isNotBlank(str);
423 }
424
425 /**
426 * Checks if a string is blank.
427 * @see StringUtils#isNotBlank(String)
428 */
429 private static boolean isBlank(String str){
430 return StringUtils.isBlank(str);
431 }
432
433 private static boolean isNumeric(String string){
434 if (string == null){
435 return false;
436 }
437 try {
438 Double.valueOf(string);
439 return true;
440 } catch (NumberFormatException e) {
441 return false;
442 }
443 }
444 }