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.
10 package eu
.etaxonomy
.cdm
.model
.common
;
13 import java
.util
.ArrayList
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import javax
.persistence
.Column
;
19 import javax
.persistence
.Embedded
;
20 import javax
.persistence
.FetchType
;
21 import javax
.persistence
.MappedSuperclass
;
22 import javax
.persistence
.OneToMany
;
23 import javax
.persistence
.Transient
;
24 import javax
.xml
.bind
.annotation
.XmlAccessType
;
25 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
26 import javax
.xml
.bind
.annotation
.XmlElement
;
27 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
28 import javax
.xml
.bind
.annotation
.XmlTransient
;
29 import javax
.xml
.bind
.annotation
.XmlType
;
30 import javax
.xml
.bind
.annotation
.adapters
.XmlJavaTypeAdapter
;
32 import org
.apache
.log4j
.Logger
;
33 import org
.hibernate
.annotations
.Cascade
;
34 import org
.hibernate
.annotations
.CascadeType
;
35 import org
.hibernate
.annotations
.IndexColumn
;
36 import org
.hibernate
.search
.annotations
.Field
;
37 import org
.hibernate
.search
.annotations
.FieldBridge
;
38 import org
.hibernate
.search
.annotations
.Fields
;
40 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
41 import eu
.etaxonomy
.cdm
.hibernate
.StripHtmlBridge
;
42 import eu
.etaxonomy
.cdm
.jaxb
.FormattedTextAdapter
;
43 import eu
.etaxonomy
.cdm
.jaxb
.LSIDAdapter
;
44 import eu
.etaxonomy
.cdm
.model
.media
.Rights
;
45 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
46 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
48 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
49 import eu
.etaxonomy
.cdm
.strategy
.match
.Match
;
50 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchMode
;
51 import eu
.etaxonomy
.cdm
.strategy
.match
.Match
.ReplaceMode
;
52 import eu
.etaxonomy
.cdm
.strategy
.merge
.Merge
;
53 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeMode
;
56 * Superclass for the primary CDM classes that can be referenced from outside via LSIDs and contain a simple generated title string as a label for human reading.
57 * All subclasses inherit the ability to store additional properties that are stored as {@link Extension Extensions}, basically a string value with a type term.
58 * Any number of right statements can be attached as well as multiple {@link OriginalSource} objects.
59 * Original sources carry a reference to the source, an ID within that source and the original title/label of this object as it was used in that source (originalNameString).
60 * A Taxon for example that was taken from 2 sources like FaunaEuropaea and IPNI would have two originalSource objects.
61 * The originalSource representing that taxon as it was found in IPNI would contain IPNI as the reference, the IPNI id of the taxon and the name of the taxon exactly as it was used in IPNI.
65 * @created 08-Nov-2007 13:06:27
67 @XmlAccessorType(XmlAccessType
.FIELD
)
68 @XmlType(name
= "IdentifiableEntity", propOrder
= {
71 "protectedTitleCache",
78 public abstract class IdentifiableEntity
<S
extends IIdentifiableEntityCacheStrategy
> extends AnnotatableEntity
79 implements ISourceable
, IIdentifiableEntity
/*, Comparable<IdentifiableEntity> */{
80 private static final long serialVersionUID
= -5610995424730659058L;
81 private static final Logger logger
= Logger
.getLogger(IdentifiableEntity
.class);
84 public static final boolean PROTECTED
= true;
86 public static final boolean NOT_PROTECTED
= false;
88 @XmlElement(name
= "LSID", type
= String
.class)
89 @XmlJavaTypeAdapter(LSIDAdapter
.class)
93 @XmlElement(name
= "TitleCache", required
= true)
94 @XmlJavaTypeAdapter(FormattedTextAdapter
.class)
95 @Column(length
=255, name
="titleCache")
96 @Fields({@Field(index
= org
.hibernate
.search
.annotations
.Index
.TOKENIZED
),
97 @Field(name
= "titleCache_forSort", index
= org
.hibernate
.search
.annotations
.Index
.UN_TOKENIZED
)
99 @FieldBridge(impl
=StripHtmlBridge
.class)
100 @Match(value
=MatchMode
.CACHE
, cacheReplaceMode
=ReplaceMode
.ALL
)
101 private String titleCache
;
103 //if true titleCache will not be automatically generated/updated
104 @XmlElement(name
= "ProtectedTitleCache")
105 private boolean protectedTitleCache
;
107 @XmlElementWrapper(name
= "Rights")
108 @XmlElement(name
= "Rights")
109 @OneToMany(fetch
= FetchType
.LAZY
)
110 @Cascade({CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
})
112 @Merge(MergeMode
.ADD_CLONE
)
113 private Set
<Rights
> rights
= new HashSet
<Rights
>();
115 @XmlElementWrapper(name
= "Credits")
116 @XmlElement(name
= "Credit")
117 @IndexColumn(name
="sortIndex", base
= 0)
118 @OneToMany(fetch
= FetchType
.LAZY
)
119 @Cascade({CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
})
121 @Merge(MergeMode
.ADD_CLONE
)
122 private List
<Credit
> credits
= new ArrayList
<Credit
>();
124 @XmlElementWrapper(name
= "Extensions")
125 @XmlElement(name
= "Extension")
126 @OneToMany(fetch
= FetchType
.LAZY
)
127 @Cascade({CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
, CascadeType
.DELETE
})
128 @Merge(MergeMode
.ADD_CLONE
)
129 private Set
<Extension
> extensions
= new HashSet
<Extension
>();
131 @XmlElementWrapper(name
= "Sources")
132 @XmlElement(name
= "OriginalSource")
133 @OneToMany(fetch
= FetchType
.LAZY
)
134 @Cascade({CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
, CascadeType
.DELETE
})
135 @Merge(MergeMode
.ADD_CLONE
)
136 private Set
<OriginalSource
> sources
= new HashSet
<OriginalSource
>();
140 protected S cacheStrategy
;
143 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#getLsid()
145 public LSID
getLsid(){
149 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#setLsid(java.lang.String)
151 public void setLsid(LSID lsid
){
156 * By default, we expect most cdm objects to be abstract things
157 * i.e. unable to return a data representation.
159 * Specific subclasses (e.g. Sequence) can override if necessary.
161 public byte[] getData() {
166 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#getTitleCache()
169 public String
getTitleCache(){
170 if (protectedTitleCache
){
171 return this.titleCache
;
173 // is title dirty, i.e. equal NULL?
174 if (titleCache
== null){
175 this.setTitleCache(generateTitle(),protectedTitleCache
) ; //for truncating
180 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#setTitleCache(java.lang.String)
182 public void setTitleCache(String titleCache
){
183 setTitleCache(titleCache
, PROTECTED
);
187 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#setTitleCache(java.lang.String, boolean)
189 public void setTitleCache(String titleCache
, boolean protectCache
){
190 //TODO truncation of title cache
191 if (titleCache
!= null && titleCache
.length() > 254){
192 logger
.warn("Truncation of title cache: " + this.toString() + "/" + titleCache
);
193 titleCache
= titleCache
.substring(0, 251) + "...";
195 this.titleCache
= titleCache
;
196 this.setProtectedTitleCache(protectCache
);
200 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#getRights()
202 public Set
<Rights
> getRights() {
207 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#addRights(eu.etaxonomy.cdm.model.media.Rights)
209 public void addRights(Rights right
){
210 this.rights
.add(right
);
213 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#removeRights(eu.etaxonomy.cdm.model.media.Rights)
215 public void removeRights(Rights right
){
216 this.rights
.remove(right
);
221 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#getCredits()
223 public List
<Credit
> getCredits() {
228 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#getCredits(int)
230 public Credit
getCredits(int index
){
231 return this.credits
.get(index
);
235 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#addCredit(eu.etaxonomy.cdm.model.common.Credit)
237 public void addCredit(Credit credit
){
238 this.credits
.add(credit
);
243 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#addCredit(eu.etaxonomy.cdm.model.common.Credit, int)
245 public void addCredit(Credit credit
, int index
){
246 this.credits
.add(index
, credit
);
250 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#removeCredit(eu.etaxonomy.cdm.model.common.Credit)
252 public void removeCredit(Credit credit
){
253 this.credits
.remove(credit
);
257 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#removeCredit(int)
259 public void removeCredit(int index
){
260 this.credits
.remove(index
);
265 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#getExtensions()
267 public Set
<Extension
> getExtensions(){
268 return this.extensions
;
271 public void addExtension(String value
, ExtensionType extensionType
){
272 Extension
.NewInstance(this, value
, extensionType
);
276 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#addExtension(eu.etaxonomy.cdm.model.common.Extension)
278 public void addExtension(Extension extension
){
279 if (extension
!= null){
280 extension
.setExtendedObj(this);
281 this.extensions
.add(extension
);
285 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#removeExtension(eu.etaxonomy.cdm.model.common.Extension)
287 public void removeExtension(Extension extension
){
288 if (extension
!= null){
289 extension
.setExtendedObj(null);
290 this.extensions
.remove(extension
);
296 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#isProtectedTitleCache()
298 public boolean isProtectedTitleCache() {
299 return protectedTitleCache
;
303 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#setProtectedTitleCache(boolean)
305 public void setProtectedTitleCache(boolean protectedTitleCache
) {
306 this.protectedTitleCache
= protectedTitleCache
;
310 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#getSources()
312 public Set
<OriginalSource
> getSources() {
316 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#addSource(eu.etaxonomy.cdm.model.common.OriginalSource)
318 public void addSource(OriginalSource source
) {
320 IdentifiableEntity oldSourcedObj
= source
.getSourcedObj();
321 if (oldSourcedObj
!= null && oldSourcedObj
!= this){
322 oldSourcedObj
.getSources().remove(source
);
324 this.sources
.add(source
);
325 source
.setSourcedObj(this);
329 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#removeSource(eu.etaxonomy.cdm.model.common.OriginalSource)
331 public void removeSource(OriginalSource source
) {
332 this.sources
.remove(source
);
336 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#toString()
339 public String
toString() {
341 if (titleCache
== null){
342 result
= super.toString();
344 result
= this.titleCache
;
349 public int compareTo(IdentifiableEntity identifiableEntity
) {
353 if (identifiableEntity
== null) {
354 throw new NullPointerException("Cannot compare to null.");
357 // First, compare the name cache.
358 // TODO: Avoid using instanceof operator
359 // Use Class.getDeclaredMethod() instead to find out whether class has getNameCache() method?
361 String specifiedNameCache
= "";
362 String thisNameCache
= "";
363 String specifiedTitleCache
= "";
364 String thisTitleCache
= "";
365 String specifiedReferenceTitleCache
= "";
366 String thisReferenceTitleCache
= "";
368 if(identifiableEntity
instanceof NonViralName
) {
369 specifiedNameCache
= HibernateProxyHelper
.deproxy(identifiableEntity
, NonViralName
.class).getNameCache();
370 specifiedTitleCache
= identifiableEntity
.getTitleCache();
372 } else if(identifiableEntity
instanceof TaxonBase
) {
373 TaxonBase taxonBase
= HibernateProxyHelper
.deproxy(identifiableEntity
, TaxonBase
.class);
375 TaxonNameBase
<?
,?
> taxonNameBase
= taxonBase
.getName();
376 specifiedNameCache
= HibernateProxyHelper
.deproxy(taxonNameBase
, NonViralName
.class).getNameCache();
377 specifiedTitleCache
= taxonNameBase
.getTitleCache();
379 //specifiedReferenceTitleCache = ((TaxonBase)identifiableEntity).getSec().getTitleCache();
380 // ReferenceBase referenceBase = taxonBase.getSec();
381 // if (referenceBase != null) {
382 // FIXME: HibernateProxyHelper.deproxy(referenceBase, ReferenceBase.class) throws exception
383 // referenceBase = HibernateProxyHelper.deproxy(referenceBase, ReferenceBase.class);
384 // specifiedReferenceTitleCache = referenceBase.getTitleCache();
388 if(this instanceof NonViralName
) {
389 thisNameCache
= HibernateProxyHelper
.deproxy(this, NonViralName
.class).getNameCache();
390 thisTitleCache
= getTitleCache();
391 } else if(this instanceof TaxonBase
) {
392 TaxonNameBase
<?
,?
> taxonNameBase
= HibernateProxyHelper
.deproxy(this, TaxonBase
.class).getName();
393 thisNameCache
= HibernateProxyHelper
.deproxy(taxonNameBase
, NonViralName
.class).getNameCache();
394 thisTitleCache
= taxonNameBase
.getTitleCache();
395 thisReferenceTitleCache
= getTitleCache();
398 // Compare name cache of taxon names
400 if (!specifiedNameCache
.equals("") && !thisNameCache
.equals("")) {
401 result
= thisNameCache
.compareTo(specifiedNameCache
);
404 // Compare title cache of taxon names
406 if ((result
== 0) && (!specifiedTitleCache
.equals("") || !thisTitleCache
.equals(""))) {
407 result
= thisTitleCache
.compareTo(specifiedTitleCache
);
410 // Compare title cache of taxon references
412 if ((result
== 0) && (!specifiedReferenceTitleCache
.equals("") || !thisReferenceTitleCache
.equals(""))) {
413 result
= thisReferenceTitleCache
.compareTo(specifiedReferenceTitleCache
);
420 * Returns the {@link eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy cache strategy} used to generate
421 * several strings corresponding to <i>this</i> identifiable entity
422 * (in particular taxon name caches and author strings).
424 * @return the cache strategy used for <i>this</i> identifiable entity
425 * @see eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy
427 public S
getCacheStrategy() {
428 return this.cacheStrategy
;
431 * @see #getCacheStrategy()
434 public void setCacheStrategy(S cacheStrategy
) {
435 this.cacheStrategy
= cacheStrategy
;
438 public String
generateTitle() {
439 if (cacheStrategy
== null){
440 logger
.warn("No CacheStrategy defined for "+ this.getClass() + ": " + this.getUuid());
443 return cacheStrategy
.getTitleCache(this);
447 //****************** CLONE ************************************************/
450 * @see eu.etaxonomy.cdm.model.common.AnnotatableEntity#clone()
453 public Object
clone() throws CloneNotSupportedException
{
454 IdentifiableEntity result
= (IdentifiableEntity
)super.clone();
457 result
.extensions
= new HashSet
<Extension
>();
458 for (Extension extension
: this.extensions
){
459 Extension newExtension
= (Extension
)extension
.clone();
460 result
.addExtension(newExtension
);
464 result
.sources
= new HashSet
<OriginalSource
>();
465 for (OriginalSource originalSource
: this.sources
){
466 OriginalSource newSource
= (OriginalSource
)originalSource
.clone();
467 result
.addSource(newSource
);
471 result
.rights
= new HashSet
<Rights
>();
472 for(Rights rights
: this.rights
) {
473 result
.addRights(rights
);
478 result
.credits
= new ArrayList
<Credit
>();
479 for(Credit credit
: this.credits
) {
480 result
.addCredit(credit
);
484 //result.setLsid(lsid);
485 //result.setTitleCache(titleCache);
486 //result.setProtectedTitleCache(protectedTitleCache); //must be after setTitleCache
488 //no changes to: lsid, titleCache, protectedTitleCache
491 if (! protectedTitleCache
){
492 result
.titleCache
= null;