2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.strategy
.cache
.name
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Iterator
;
13 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import org
.apache
.commons
.lang
.StringUtils
;
18 import org
.apache
.log4j
.Logger
;
20 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
21 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
22 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
23 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
24 import eu
.etaxonomy
.cdm
.model
.name
.INonViralName
;
25 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
27 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
28 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
29 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
30 import eu
.etaxonomy
.cdm
.model
.reference
.INomenclaturalReference
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
32 import eu
.etaxonomy
.cdm
.model
.term
.Representation
;
33 import eu
.etaxonomy
.cdm
.ref
.TypedEntityReference
;
34 import eu
.etaxonomy
.cdm
.strategy
.StrategyBase
;
35 import eu
.etaxonomy
.cdm
.strategy
.cache
.HTMLTagRules
;
36 import eu
.etaxonomy
.cdm
.strategy
.cache
.TagEnum
;
37 import eu
.etaxonomy
.cdm
.strategy
.cache
.TaggedCacheHelper
;
38 import eu
.etaxonomy
.cdm
.strategy
.cache
.TaggedText
;
43 public abstract class NameCacheStrategyBase
45 implements INameCacheStrategy
{
46 private static final long serialVersionUID
= -2322348388258675517L;
48 private static final Logger logger
= Logger
.getLogger(NameCacheStrategyBase
.class);
50 final static UUID uuid
= UUID
.fromString("817ae5b5-3ac2-414b-a134-a9ae86cba040");
55 public NameCacheStrategyBase() {
61 public String
getFullTitleCache(TaxonName taxonName
, HTMLTagRules htmlTagRules
) {
62 List
<TaggedText
> tags
= getTaggedFullTitle(taxonName
);
66 String result
= createString(tags
, htmlTagRules
);
72 public String
getFullTitleCache(TaxonName taxonName
) {
73 return getFullTitleCache(taxonName
, null);
83 public List
<TaggedText
> getNomStatusTags(TaxonName taxonName
, boolean includeSeparatorBefore
,
84 boolean includeSeparatorAfter
) {
86 Set
<NomenclaturalStatus
> ncStati
= taxonName
.getStatus();
87 Iterator
<NomenclaturalStatus
> iterator
= ncStati
.iterator();
88 List
<TaggedText
> nomStatusTags
= new ArrayList
<>();
89 while (iterator
.hasNext()) {
90 NomenclaturalStatus ncStatus
= iterator
.next();
91 // since the NewInstance method of nomencatural status allows null as parameter
92 // we have to check for null values here
93 String nomStatusStr
= "not defined";
94 if(ncStatus
.getType() != null){
95 NomenclaturalStatusType statusType
= ncStatus
.getType();
96 Language lang
= Language
.LATIN();
97 Representation repr
= statusType
.getRepresentation(lang
);
99 nomStatusStr
= repr
.getAbbreviatedLabel();
101 String message
= "No latin representation available for nom. status. " + statusType
.getTitleCache();
102 logger
.warn(message
);
103 throw new IllegalStateException(message
);
105 }else if(StringUtils
.isNotBlank(ncStatus
.getRuleConsidered())){
106 nomStatusStr
= ncStatus
.getRuleConsidered();
108 String statusSeparator
= ", ";
109 if (includeSeparatorBefore
){
110 nomStatusTags
.add(new TaggedText(TagEnum
.separator
, statusSeparator
));
112 nomStatusTags
.add(new TaggedText(TagEnum
.nomStatus
, nomStatusStr
, new TypedEntityReference
<>(ncStatus
.getClass(), ncStatus
.getUuid())));
113 if (includeSeparatorAfter
){
114 nomStatusTags
.add(new TaggedText(TagEnum
.postSeparator
, ","));
117 return nomStatusTags
;
122 * Generates and returns the "name cache" (only scientific name without author teams and year).
123 * @see eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy#getNameCache(eu.etaxonomy.cdm.model.name.TaxonName)
126 public String
getNameCache(TaxonName nonViralName
) {
127 List
<TaggedText
> tags
= getTaggedName(nonViralName
);
131 String result
= createString(tags
);
138 * Generates and returns the title cache of the given name.
139 * The title cache in general includes the name and the authorship and year for some types of names.
141 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.common.CdmBase)
142 * @see eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.common.IdentifiableEntity)
145 public String
getTitleCache(TaxonName taxonName
) {
146 return getTitleCache(taxonName
, null);
150 public String
getTitleCache(TaxonName taxonName
, HTMLTagRules htmlTagRules
) {
151 List
<TaggedText
> tags
= getTaggedTitle(taxonName
);
155 String result
= createString(tags
, htmlTagRules
);
161 public List
<TaggedText
> getTaggedTitle(TaxonName taxonName
) {
162 if (taxonName
== null){
165 //TODO how to handle protected fullTitleCache here?
167 if (taxonName
.isProtectedTitleCache()){
168 //protected title cache
169 List
<TaggedText
> tags
= new ArrayList
<>();
170 tags
.add(new TaggedText(TagEnum
.name
, taxonName
.getTitleCache()));
173 return doGetTaggedTitle(taxonName
);
178 protected abstract List
<TaggedText
> doGetTaggedTitle(TaxonName taxonName
);
181 public List
<TaggedText
> getTaggedFullTitle(TaxonName taxonName
) {
182 List
<TaggedText
> tags
= new ArrayList
<>();
185 if (taxonName
== null){
189 //protected full title cache
190 if (taxonName
.isProtectedFullTitleCache()){
191 tags
.add(new TaggedText(TagEnum
.fullName
, taxonName
.getFullTitleCache()));
196 // String titleCache = nonViralName.getTitleCache();
197 List
<TaggedText
> titleTags
= getTaggedTitle(taxonName
);
198 tags
.addAll(titleTags
);
201 String microReference
= taxonName
.getNomenclaturalMicroReference();
202 INomenclaturalReference ref
= taxonName
.getNomenclaturalReference();
203 String referenceCache
= null;
205 Reference reference
= HibernateProxyHelper
.deproxy(ref
, Reference
.class);
206 referenceCache
= reference
.getNomenclaturalCitation(microReference
);
209 if (StringUtils
.isNotBlank(referenceCache
)){
210 if (! referenceCache
.trim().startsWith("in ")){
211 String refConcat
= ", ";
212 tags
.add(new TaggedText(TagEnum
.separator
, refConcat
));
214 tags
.add(new TaggedText(TagEnum
.reference
, referenceCache
));
217 addOriginalSpelling(tags
, taxonName
);
219 //nomenclatural status
220 tags
.addAll(getNomStatusTags(taxonName
, true, false));
225 protected void addOriginalSpelling(List
<TaggedText
> tags
, TaxonName taxonName
){
226 String originalName
= getOriginalNameString(taxonName
, tags
);
227 if (StringUtils
.isNotBlank(originalName
)){
228 tags
.add(new TaggedText(TagEnum
.name
, originalName
));
232 private String
getOriginalNameString(TaxonName currentName
, List
<TaggedText
> originalNameTaggs
) {
233 List
<String
> originalNameStrings
= new ArrayList
<>(1);
234 currentName
= CdmBase
.deproxy(currentName
);
235 //Hibernate.initialize(currentName.getRelationsToThisName());
236 for (NameRelationship nameRel
: currentName
.getRelationsToThisName()){ //handle list, just in case we have strange data; this may result in strange looking results
237 NameRelationshipType type
= nameRel
.getType();
238 if(type
!= null && type
.equals(NameRelationshipType
.ORIGINAL_SPELLING())){
239 String originalNameString
;
240 TaxonName originalName
= nameRel
.getFromName();
241 if (!originalName
.isNonViral()){
242 originalNameString
= originalName
.getTitleCache();
244 INonViralName originalNvName
= CdmBase
.deproxy(originalName
);
245 originalNameString
= makeOriginalNameString(currentName
, originalNvName
, originalNameTaggs
);
247 originalNameStrings
.add("[as \"" + originalNameString
+ "\"]");
250 if (originalNameStrings
.size() > 0){
251 String result
= CdmUtils
.concat("", originalNameStrings
.toArray(new String
[originalNameStrings
.size()])) ;
258 private String
makeOriginalNameString(TaxonName currentName
, INonViralName originalName
,
259 List
<TaggedText
> currentNameTags
) {
260 //use cache if necessary
261 String cacheToUse
= null;
262 if (originalName
.isProtectedNameCache() && StringUtils
.isNotBlank(originalName
.getNameCache())){
263 cacheToUse
= originalName
.getNameCache();
264 }else if (originalName
.isProtectedTitleCache() && StringUtils
.isNotBlank(originalName
.getTitleCache())){
265 cacheToUse
= originalName
.getTitleCache();
266 }else if (originalName
.isProtectedFullTitleCache() && StringUtils
.isNotBlank(originalName
.getFullTitleCache())){
267 cacheToUse
= originalName
.getFullTitleCache();
269 if (cacheToUse
!= null){
273 //get originalNameParts array
274 String originalNameString
= originalName
.getNameCache();
275 if (originalNameString
== null){
276 originalNameString
= originalName
.getTitleCache();
278 if (originalNameString
== null){ //should not happen
279 originalNameString
= originalName
.getFullTitleCache();
281 String
[] originalNameSplit
= originalNameString
.split("\\s+");
283 //get current name parts
284 String currentNameString
= createString(currentNameTags
);
285 String
[] currentNameSplit
= currentNameString
.split("\\s+");
288 String result
= originalNameString
;
289 for (int i
= 0; i
< Math
.min(originalNameSplit
.length
, currentNameSplit
.length
); i
++){
290 if (originalNameSplit
[i
].equals(currentNameSplit
[i
])){
291 result
= result
.replaceFirst(originalNameSplit
[i
], "").trim();
295 // if (originalName.getGenusOrUninomial() != null && originalName.getGenusOrUninomial().equals(currentName.getGenusOrUninomial())){
305 protected String
createString(List
<TaggedText
> tags
) {
306 return TaggedCacheHelper
.createString(tags
);
311 * @param htmlTagRules
314 protected String
createString(List
<TaggedText
> tags
, HTMLTagRules htmlTagRules
) {
315 return TaggedCacheHelper
.createString(tags
, htmlTagRules
);