merge-update from trunk
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / strategy / cache / reference / ArticleDefaultCacheStrategy.java
1 /**
2 * Copyright (C) 2007 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 java.util.UUID;
12
13 import org.apache.log4j.Logger;
14
15 import eu.etaxonomy.cdm.common.CdmUtils;
16 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
17 import eu.etaxonomy.cdm.model.reference.IJournal;
18 import eu.etaxonomy.cdm.model.reference.Reference;
19
20 public class ArticleDefaultCacheStrategy extends NomRefDefaultCacheStrategyBase implements INomenclaturalReferenceCacheStrategy {
21 private static final long serialVersionUID = -1639068590864589314L;
22
23 @SuppressWarnings("unused")
24 private static final Logger logger = Logger.getLogger(ArticleDefaultCacheStrategy.class);
25
26 public static final String UNDEFINED_JOURNAL = "- undefined journal -";
27 private String prefixReferenceJounal = "in";
28 private String blank = " ";
29 private String comma = ",";
30 private String prefixSeries = "ser.";
31
32
33 final static UUID uuid = UUID.fromString("0d45343a-0c8a-4a64-97ca-e94974b65c96");
34
35 @Override
36 protected UUID getUuid() {
37 return uuid;
38 }
39
40
41 /**
42 * Factory method
43 * @return
44 */
45 public static ArticleDefaultCacheStrategy NewInstance(){
46 return new ArticleDefaultCacheStrategy();
47 }
48
49 /**
50 * Constructor
51 */
52 private ArticleDefaultCacheStrategy(){
53 super();
54 }
55
56 @Override
57 public String getTitleCache(Reference article) {
58 if (article.isProtectedTitleCache()){
59 return article.getTitleCache();
60 }
61 String result = getTitleWithoutYearAndAuthor(article, false);
62 result = addYear(result, article, false);
63 TeamOrPersonBase<?> team = article.getAuthorship();
64 result = CdmUtils.concat(" ", article.getTitle(), result);
65 if (team != null && isNotBlank(team.getTitleCache())){
66 String authorSeparator = isNotBlank(article.getTitle())? afterAuthor : " ";
67 result = team.getTitleCache() + authorSeparator + result;
68 }
69 return result;
70 }
71
72 @Override
73 public String getAbbrevTitleCache(Reference article) {
74 if (article.isProtectedAbbrevTitleCache()){
75 return article.getAbbrevTitleCache();
76 }
77 String result = getTitleWithoutYearAndAuthor(article, true);
78 result = addYear(result, article, false);
79 TeamOrPersonBase<?> team = article.getAuthorship();
80 String articleTitle = CdmUtils.getPreferredNonEmptyString(article.getAbbrevTitle(), article.getTitle(), false, true);
81 result = CdmUtils.concat(" ", articleTitle, result); //Article should maybe left out for nomenclatural references (?)
82 if (team != null && isNotBlank(team.getNomenclaturalTitle())){
83 String authorSeparator = isNotBlank(articleTitle) ? afterAuthor : " ";
84 result = team.getNomenclaturalTitle() + authorSeparator + result;
85 }
86 return result;
87 }
88
89 @Override
90 protected String getTitleWithoutYearAndAuthor(Reference article, boolean isAbbrev){
91 if (article == null){
92 return null;
93 }
94 IJournal journal = article.getInReference();
95 boolean hasJournal = (journal != null);
96
97 String journalTitel;
98 if (hasJournal){
99 journalTitel = CdmUtils.getPreferredNonEmptyString(journal.getTitle(), journal.getAbbrevTitle(), isAbbrev, true);
100 }else{
101 journalTitel = UNDEFINED_JOURNAL;
102 }
103
104 String series = Nz(article.getSeriesPart()).trim();
105 String volume = Nz(article.getVolume()).trim();
106
107 boolean needsComma = false;
108
109 String nomRefCache = "";
110
111 //inJournal
112 nomRefCache = prefixReferenceJounal + blank;
113
114 //titelAbbrev
115 if (isNotBlank(journalTitel)){
116 nomRefCache = nomRefCache + journalTitel;
117 needsComma = makeNeedsComma(needsComma, nomRefCache, volume, series);
118 if (! needsComma){
119 nomRefCache = nomRefCache + blank;
120 }
121 }
122
123 //series and vol.
124 nomRefCache = getSeriesAndVolPart(series, volume, needsComma, nomRefCache);
125
126 //delete "."
127 while (nomRefCache.endsWith(".")){
128 nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
129 }
130
131 return nomRefCache.trim();
132 }
133
134 private boolean makeNeedsComma(boolean needsComma, String nomRefCache, String volume, String series) {
135 if (needsComma){
136 return true;
137 }else{
138 nomRefCache = nomRefCache.toLowerCase();
139 int serIndex = nomRefCache.indexOf(" ser. ");
140 int sectIndex = nomRefCache.indexOf(" sect. ");
141 int abtIndex = nomRefCache.indexOf(" abt. ");
142 int index = Math.max(Math.max(serIndex, sectIndex), abtIndex);
143 int commaIndex = nomRefCache.indexOf(",", index);
144 if (index > -1 && commaIndex == -1 && isNotBlank(volume)){
145 return true;
146 }else if (isNotBlank(series)){
147 return true;
148 }else{
149 return false;
150 }
151 }
152 }
153
154 protected String getSeriesAndVolPart(String series, String volume,
155 boolean needsComma, String nomRefCache) {
156 //inSeries
157 String seriesPart = "";
158 if (isNotBlank(series)){
159 seriesPart = series;
160 if (CdmUtils.isNumeric(series)){
161 seriesPart = prefixSeries + blank + seriesPart;
162 }
163 // if (needsComma){
164 seriesPart = comma + blank + seriesPart;
165 // }
166 needsComma = true;
167 }
168 nomRefCache += seriesPart;
169
170
171 //volume Part
172 String volumePart = "";
173 if (!"".equals(volume)){
174 volumePart = volume;
175 if (needsComma){
176 volumePart = comma + blank + volumePart;
177 }
178 //needsComma = false;
179 }
180 nomRefCache += volumePart;
181 return nomRefCache;
182 }
183
184
185
186
187 }