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