merging delete functionality into 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.commons.lang.StringUtils;
14 import org.apache.log4j.Logger;
15
16 import eu.etaxonomy.cdm.common.CdmUtils;
17 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
18 import eu.etaxonomy.cdm.model.reference.IJournal;
19 import eu.etaxonomy.cdm.model.reference.Reference;
20
21 public class ArticleDefaultCacheStrategy <T extends Reference> extends NomRefDefaultCacheStrategyBase<T> implements INomenclaturalReferenceCacheStrategy<T> {
22 @SuppressWarnings("unused")
23 private static final Logger logger = Logger.getLogger(ArticleDefaultCacheStrategy.class);
24
25 public static final String UNDEFINED_JOURNAL = "- undefined journal -";
26 private String prefixReferenceJounal = "in";
27 private String blank = " ";
28 private String comma = ",";
29 private String prefixSeries = "ser.";
30
31 final static UUID uuid = UUID.fromString("0d45343a-0c8a-4a64-97ca-e94974b65c96");
32
33 /* (non-Javadoc)
34 * @see eu.etaxonomy.cdm.strategy.StrategyBase#getUuid()
35 */
36 @Override
37 protected UUID getUuid() {
38 return uuid;
39 }
40
41
42 /**
43 * Factory method
44 * @return
45 */
46 public static ArticleDefaultCacheStrategy NewInstance(){
47 return new ArticleDefaultCacheStrategy();
48 }
49
50 /**
51 * Constructor
52 */
53 private ArticleDefaultCacheStrategy(){
54 super();
55 }
56
57
58
59 @Override
60 public String getTitleCache(T article) {
61 if (article.isProtectedTitleCache()){
62 return article.getTitleCache();
63 }
64 String result = getTitleWithoutYearAndAuthor(article, false);
65 result = addYear(result, article, false);
66 TeamOrPersonBase<?> team = article.getAuthorTeam();
67 result = CdmUtils.concat(" ", article.getTitle(), result);
68 if (team != null && StringUtils.isNotBlank(team.getTitleCache())){
69 String authorSeparator = StringUtils.isNotBlank(article.getTitle())? afterAuthor : " ";
70 result = team.getTitleCache() + authorSeparator + result;
71 }
72 return result;
73 }
74
75 @Override
76 public String getAbbrevTitleCache(T article) {
77 if (article.isProtectedAbbrevTitleCache()){
78 return article.getAbbrevTitleCache();
79 }
80 String result = getTitleWithoutYearAndAuthor(article, true);
81 result = addYear(result, article, false);
82 TeamOrPersonBase<?> team = article.getAuthorTeam();
83 String articleTitle = CdmUtils.getPreferredNonEmptyString(article.getAbbrevTitle(), article.getTitle(), false, true);
84 result = CdmUtils.concat(" ", articleTitle, result); //Article should maybe left out for nomenclatural references (?)
85 if (team != null && StringUtils.isNotBlank(team.getNomenclaturalTitle())){
86 String authorSeparator = StringUtils.isNotBlank(articleTitle) ? afterAuthor : " ";
87 result = team.getNomenclaturalTitle() + authorSeparator + result;
88 }
89 return result;
90 }
91
92 @Override
93 protected String getTitleWithoutYearAndAuthor(T article, boolean isAbbrev){
94 if (article == null){
95 return null;
96 }
97 IJournal journal = article.getInReference();
98 boolean hasJournal = (journal != null);
99
100 String journalTitel;
101 if (hasJournal){
102 journalTitel = CdmUtils.getPreferredNonEmptyString(journal.getTitle(), journal.getAbbrevTitle(), isAbbrev, true);
103 }else{
104 journalTitel = UNDEFINED_JOURNAL;
105 }
106
107 String series = Nz(article.getSeries()).trim();
108 String volume = Nz(article.getVolume()).trim();
109
110 boolean needsComma = false;
111
112 String nomRefCache = "";
113
114 //inJournal
115 nomRefCache = prefixReferenceJounal + blank;
116
117 //titelAbbrev
118 if (isNotBlank(journalTitel)){
119 nomRefCache = nomRefCache + journalTitel + blank;
120 }
121
122 nomRefCache = getSeriesAndVolPart(series, volume, needsComma, nomRefCache);
123
124 //delete "."
125 while (nomRefCache.endsWith(".")){
126 nomRefCache = nomRefCache.substring(0, nomRefCache.length()-1);
127 }
128
129 return nomRefCache.trim();
130 }
131
132 protected String getSeriesAndVolPart(String series, String volume,
133 boolean needsComma, String nomRefCache) {
134 //inSeries
135 String seriesPart = "";
136 if (isNotBlank(series)){
137 seriesPart = series;
138 if (CdmUtils.isNumeric(series)){
139 seriesPart = prefixSeries + blank + seriesPart;
140 }
141 if (needsComma){
142 seriesPart = comma + seriesPart;
143 }
144 needsComma = true;
145 }
146 nomRefCache += seriesPart;
147
148
149 //volume Part
150 String volumePart = "";
151 if (!"".equals(volume)){
152 volumePart = volume;
153 if (needsComma){
154 volumePart = comma + blank + volumePart;
155 }
156 //needsComma = false;
157 }
158 nomRefCache += volumePart;
159 return nomRefCache;
160 }
161
162 }