merging delete functionality into trunk
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / strategy / cache / reference / GenericDefaultCacheStrategy.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.commons.lang.StringUtils;
14 import org.apache.log4j.Logger;
15
16 import eu.etaxonomy.cdm.common.CdmUtils;
17 import eu.etaxonomy.cdm.model.reference.Reference;
18
19 public class GenericDefaultCacheStrategy <T extends Reference> extends InRefDefaultCacheStrategyBase<T> implements INomenclaturalReferenceCacheStrategy<T> {
20 private static final long serialVersionUID = 6687224678019228192L;
21
22 @SuppressWarnings("unused")
23 private static final Logger logger = Logger.getLogger(GenericDefaultCacheStrategy.class);
24
25
26 private static final String inRefTypeStr = "generic reference";
27
28 @Override
29 protected String getInRefType() {
30 return inRefTypeStr;
31 }
32
33 private String prefixEdition = "ed.";
34 private String prefixSeries = "ser.";
35 private String prefixVolume = "vol.";
36 private String blank = " ";
37 private String comma = ",";
38 private String dot =".";
39
40 final static UUID uuid = UUID.fromString("95cceb30-6b16-4dc3-8243-c15e746565bc");
41
42 /* (non-Javadoc)
43 * @see eu.etaxonomy.cdm.strategy.StrategyBase#getUuid()
44 */
45 @Override
46 protected UUID getUuid() {
47 return uuid;
48 }
49
50
51 /**
52 * Factory method
53 * @return
54 */
55 public static GenericDefaultCacheStrategy<Reference> NewInstance(){
56 return new GenericDefaultCacheStrategy();
57 }
58
59 /**
60 * Constructor
61 */
62 private GenericDefaultCacheStrategy(){
63 super();
64 }
65
66
67 @Override
68 protected String getTitleWithoutYearAndAuthor(T genericReference, boolean isAbbrev){
69 if (genericReference == null){
70 return null;
71 }
72 //TODO
73 String titel = CdmUtils.getPreferredNonEmptyString(genericReference.getTitle(), genericReference.getAbbrevTitle(), isAbbrev, true);
74 String edition = CdmUtils.Nz(genericReference.getEdition());
75 //TODO
76 String series = CdmUtils.Nz(genericReference.getSeries()).trim(); //nomenclaturalReference.getSeries();
77 String volume = CdmUtils.Nz(genericReference.getVolume()).trim();
78
79 String nomRefCache = "";
80 boolean lastCharIsDouble;
81 Integer len;
82 String lastChar ="";
83 String character =".";
84 len = titel.length();
85 if (len > 0){lastChar = titel.substring(len-1, len);}
86 //lastCharIsDouble = f_core_CompareStrings(RIGHT(@TitelAbbrev,1),character);
87 lastCharIsDouble = titel.equals(character);
88
89 // if(lastCharIsDouble && edition.length() == 0 && series.length() == 0 && volume.length() == 0 && refYear.length() > 0 ){
90 // titelAbbrev = titelAbbrev.substring(1, len-1); // SUBSTRING(@TitelAbbrev,1,@LEN-1)
91 // }
92
93
94 boolean needsComma = false;
95 //titelAbbrev
96 if (titel.length() > 0 ){
97 String postfix = StringUtils.isNotBlank(edition) ? "" : blank;
98 nomRefCache = titel + postfix;
99 }
100 //edition
101 String editionPart = "";
102 if (StringUtils.isNotBlank(edition)){
103 editionPart = edition;
104 if (isNumeric(edition)){
105 editionPart = prefixEdition + blank + editionPart;
106 }
107 needsComma = true;
108 }
109 nomRefCache = CdmUtils.concat(", ", nomRefCache, editionPart);
110
111 //inSeries
112 String seriesPart = "";
113 if (!"".equals(series)){
114 seriesPart = series;
115 if (isNumeric(series)){
116 seriesPart = prefixSeries + blank + seriesPart;
117 }
118 if (needsComma){
119 seriesPart = comma + seriesPart;
120 }
121 needsComma = true;
122 }
123 nomRefCache += seriesPart;
124
125
126 //volume Part
127 String volumePart = "";
128 if (!"".equals(volume)){
129 volumePart = volume;
130 if (needsComma){
131 volumePart = comma + blank + volumePart;
132 }
133 //needsComma = false;
134 }
135 nomRefCache += volumePart;
136
137 //delete .
138 while (nomRefCache.endsWith(".")){
139 nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
140 }
141
142
143
144 // --Edition and series are null or numeric
145
146 // if (isNumeric(edition) ){
147 // if (titelAbbrev.length() > 0 && edition.length() > 0 && series.length() > 0 && isNumeric(series) && volume.length() > 0 && refYear.length() > 0 ){
148 // nomRefCache = titelAbbrev + blank + prefixEdition + blank + edition + comma + blank + prefixSeries + blank + series + comma + blank + volume + dot + blank + refYear + dot;
149 // }
150 // }
151
152
153 // FROM BERLIN MODEL TRIGGER:
154 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=1 AND len(@Series) > 0 AND isnumeric(@Series)=1 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @prefixEdition + @blank + @Edition + @comma + @blank + @prefixSeries + @blank + @Series + @comma + @blank + @Volume + @dot
155 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=1 AND len(@Series) > 0 AND isnumeric(@Series)=1 AND len(@Volume) = 0 AND len(@RefYear) > 0) SET @NomRefCache = @TitelAbbrev + @blank + @prefixEdition + @blank + @Edition + @comma + @blank + @prefixSeries + @blank + @Series + @comma + @blank + @Refyear + @dot
156 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=1 AND len(@Series) > 0 AND isnumeric(@Series)=1 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @prefixEdition + @blank + @Edition + @comma + @blank + @prefixSeries + @blank + @Series + @dot
157 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=1 AND len(@Series) = 0 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @prefixEdition+ @blank + @Edition + @comma + @blank + @Volume + @dot
158 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=1 AND len(@Series) = 0 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @prefixEdition + @blank + @Edition + @dot
159 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) = 0 AND len(@Series) > 0 AND isnumeric(@Series)=1 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @prefixSeries + @blank + @Series + @comma + @blank + @Volume + @dot
160 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) = 0 AND len(@Series) > 0 AND isnumeric(@Series)=1 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @prefixSeries + @blank + @Series + @dot
161 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) = 0 AND len(@Series) = 0 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @Volume + @dot
162 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) = 0 AND len(@Series) = 0 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @dot
163 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=1 AND len(@Series) > 0 AND isnumeric(@Series)=1 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @prefixEdition + @blank + @Edition + @comma + @blank + @prefixSeries + @blank + @Series + @comma + @blank + @Volume + @dot
164 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=1 AND len(@Series) > 0 AND isnumeric(@Series)=1 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @prefixEdition + @blank + @Edition + @comma + @blank + @prefixSeries + @blank + @Series + @dot
165 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=1 AND len(@Series) = 0 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @prefixEdition + @blank + @Edition + @comma + @blank + @Volume + @dot
166 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=1 AND len(@Series) = 0 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @prefixEdition + @blank + @Edition + @dot
167 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) = 0 AND len(@Series) > 0 AND isnumeric(@Series)=1 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @prefixSeries + @blank + @Series + @comma + @blank + @Volume + @dot
168 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) = 0 AND len(@Series) > 0 AND isnumeric(@Series)=1 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @prefixSeries + @blank + @Series + @dot
169 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) = 0 AND len(@Series) = 0 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @Volume + @dot
170 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) = 0 AND len(@Series) = 0 AND len(@Volume) = 0 AND len(@RefYear) > 0) SET @NomRefCache = @Refyear + @dot
171 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) = 0 AND len(@Series) = 0 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = NULL
172 //
173 //
174 //
175 //
176 //
177 //
178 // --Edition and/or Series is not numeric
179 //
180 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=0 AND len(@Series) > 0 AND isnumeric(@Series)=0 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @Edition + @comma + @blank + @Series + @comma + @blank + @Volume + @dot
181 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=0 AND len(@Series) > 0 AND isnumeric(@Series)=0 AND len(@Volume) = 0 AND len(@RefYear) > 0) SET @NomRefCache = @TitelAbbrev + @blank + @Edition + @comma + @blank + @Series + @comma + @blank + @Refyear + @dot
182 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=0 AND len(@Series) > 0 AND isnumeric(@Series)=0 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @Edition + @comma + @blank + @Series + @dot
183 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=0 AND len(@Series) = 0 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @Edition + @comma + @blank + @Volume + @dot
184 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=0 AND len(@Series) = 0 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @Edition + @dot
185 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) = 0 AND len(@Series) > 0 AND isnumeric(@Series)=0 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @Series + @comma + @blank + @Volume + @dot
186 //
187 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) = 0 AND len(@Series) > 0 AND isnumeric(@Series)=0 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @Series + @dot
188 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=0 AND len(@Series) > 0 AND isnumeric(@Series)=0 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @Edition + @comma + @blank + @Series + @comma + @blank + @Volume + @dot
189 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=0 AND len(@Series) > 0 AND isnumeric(@Series)=0 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @Edition + @comma + @blank + @Series + @dot
190 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=0 AND len(@Series) = 0 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @Edition + @comma + @blank + @Volume + @dot
191 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=0 AND len(@Series) = 0 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @Edition + @dot
192 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) = 0 AND len(@Series) > 0 AND isnumeric(@Series)=0 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @Series + @comma + @blank + @Volume + @dot
193 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) = 0 AND len(@Series) > 0 AND isnumeric(@Series)=0 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @Series + @dot
194 //
195 //
196 //
197 //
198 // --Edition is numeric and series is not numeric
199 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=1 AND len(@Series) > 0 AND isnumeric(@Series)=0 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @prefixEdition + @blank + @Edition + @comma + @blank + @Series + @comma + @blank + @Volume + @dot
200 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=1 AND len(@Series) > 0 AND isnumeric(@Series)=0 AND len(@Volume) = 0 AND len(@RefYear) > 0) SET @NomRefCache = @TitelAbbrev + @blank + @prefixEdition + @blank + @Edition + @comma + @blank + @Series + @comma + @blank + @Refyear + @dot
201 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=1 AND len(@Series) > 0 AND isnumeric(@Series)=0 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @prefixEdition + @blank + @Edition + @comma + @blank + @Series + @comma + @blank + @Volume + @dot
202 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=1 AND len(@Series) > 0 AND isnumeric(@Series)=0 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @prefixEdition + @blank + @Edition+ @comma + @blank + @Series + @dot
203 //
204 //
205 //
206 // --Series is numeric and editon is not numeric
207 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=0 AND len(@Series) > 0 AND isnumeric(@Series)=1 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @TitelAbbrev + @blank + @Edition + @comma + @blank + @prefixSeries + @blank + @Series + @comma + @blank + @Volume + @dot
208 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) > 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=0 AND len(@Series) > 0 AND isnumeric(@Series)=1 AND len(@Volume) = 0 AND len(@RefYear) > 0) SET @NomRefCache = @TitelAbbrev + @blank + @Edition + @comma + @blank + @prefixSeries + @blank + @Series + @comma + @blank + @Refyear + @dot
209 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=0 AND len(@Series) > 0 AND isnumeric(@Series)=1 AND len(@Volume) > 0 AND len(@RefYear) = 0) SET @NomRefCache = @Edition + @comma + @blank + @prefixSeries + @blank + @Series + @comma + @blank + @Volume + @dot
210 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0 AND len(@Edition) > 0 AND isnumeric(@Edition)=0 AND len(@Series) > 0 AND isnumeric(@Series)=1 AND len(@Volume) = 0 AND len(@RefYear) = 0) SET @NomRefCache = @Edition+ @comma + @blank + @prefixSeries + @blank + @Series + @dot
211 //
212 // --Changes (Marc Geoffroy)
213 //
214 // IF (len(@Authorteam) = 0 AND len(@TitelAbbrev) = 0) SET @NomRefCache = NULL
215 //
216 //
217 // Return @NomRefCache
218
219 return nomRefCache.trim();
220 }
221
222 private boolean isNumeric(String string){
223 if (string == null){
224 return false;
225 }
226 try {
227 Double.valueOf(string);
228 return true;
229 } catch (NumberFormatException e) {
230 return false;
231 }
232
233 }
234
235
236
237 }