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
.HashSet
;
16 import javax
.persistence
.Column
;
17 import javax
.persistence
.ManyToMany
;
18 import javax
.persistence
.MappedSuperclass
;
19 import javax
.persistence
.OneToMany
;
20 import javax
.persistence
.Transient
;
21 import javax
.xml
.bind
.annotation
.XmlAccessType
;
22 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
23 import javax
.xml
.bind
.annotation
.XmlElement
;
24 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
25 import javax
.xml
.bind
.annotation
.XmlTransient
;
26 import javax
.xml
.bind
.annotation
.XmlType
;
28 import org
.apache
.log4j
.Logger
;
29 import org
.hibernate
.annotations
.Cascade
;
30 import org
.hibernate
.annotations
.CascadeType
;
32 import eu
.etaxonomy
.cdm
.model
.media
.Rights
;
35 * 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.
36 * All subclasses inherit the ability to store additional properties that are stored as {@link Extension Extensions}, basically a string value with a type term.
37 * Any number of right statements can be attached as well as multiple {@link OriginalSource} objects.
38 * 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).
39 * A Taxon for example that was taken from 2 sources like FaunaEuropaea and IPNI would have two originalSource objects.
40 * 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.
44 * @created 08-Nov-2007 13:06:27
46 @XmlAccessorType(XmlAccessType
.FIELD
)
47 @XmlType(name
= "IdentifiableEntity", propOrder
= {
50 "protectedTitleCache",
56 public abstract class IdentifiableEntity
<T
extends IdentifiableEntity
> extends AnnotatableEntity
<T
> implements IOriginalSource
, IIdentifiableEntitiy
<T
> {
57 static Logger logger
= Logger
.getLogger(IdentifiableEntity
.class);
60 public final boolean PROTECTED
= true;
62 public final boolean NOT_PROTECTED
= false;
64 @XmlElement(name
= "LSID")
67 @XmlElement(name
= "TitleCache", required
= true)
68 private String titleCache
;
70 //if true titleCache will not be automatically generated/updated
71 @XmlElement(name
= "ProtectedTitleCache")
72 private boolean protectedTitleCache
;
74 @XmlElementWrapper(name
= "Rights")
75 @XmlElement(name
= "Rights")
76 private Set
<Rights
> rights
= getNewRightsSet();
78 @XmlElementWrapper(name
= "Extensions")
79 @XmlElement(name
= "Extension")
80 private Set
<Extension
> extensions
= getNewExtensionSet();
82 @XmlElementWrapper(name
= "Sources")
83 @XmlElement(name
= "OriginalSource")
84 private Set
<OriginalSource
> sources
= getNewOriginalSourcesSet();
88 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getLsid()
90 public String
getLsid(){
94 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#setLsid(java.lang.String)
96 public void setLsid(String lsid
){
101 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#generateTitle()
103 public abstract String
generateTitle();
106 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getTitleCache()
109 public String
getTitleCache(){
110 if (protectedTitleCache
){
111 return this.titleCache
;
113 // is title dirty, i.e. equal NULL?
114 if (titleCache
== null){
115 this.setTitleCache(generateTitle(),protectedTitleCache
) ; //for truncating
120 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#setTitleCache(java.lang.String)
122 public void setTitleCache(String titleCache
){
123 setTitleCache(titleCache
, PROTECTED
);
126 //@Index(name="titleCacheIndex")
128 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getTitleCache()
131 @Deprecated //for hibernate use only
132 protected String
getPersistentTitleCache(){
133 return getTitleCache();
135 @Deprecated //for hibernate use only
136 protected void setPersistentTitleCache(String titleCache
){
137 this.titleCache
= titleCache
;
143 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#setTitleCache(java.lang.String, boolean)
145 public void setTitleCache(String titleCache
, boolean protectCache
){
146 //TODO truncation of title cache
147 if (titleCache
!= null && titleCache
.length() > 250){
148 logger
.warn("Truncation of title cache: " + this.toString());
149 titleCache
= titleCache
.substring(0, 249) + "...";
151 this.titleCache
= titleCache
;
152 this.setProtectedTitleCache(protectCache
);
156 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getRights()
159 @Cascade({CascadeType
.SAVE_UPDATE
})
160 public Set
<Rights
> getRights(){
164 protected void setRights(Set
<Rights
> rights
) {
165 this.rights
= rights
;
168 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#addRights(eu.etaxonomy.cdm.model.media.Rights)
170 public void addRights(Rights right
){
171 this.rights
.add(right
);
174 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#removeRights(eu.etaxonomy.cdm.model.media.Rights)
176 public void removeRights(Rights right
){
177 this.rights
.remove(right
);
181 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getExtensions()
183 @OneToMany//(mappedBy="extendedObj")
184 @Cascade({CascadeType
.SAVE_UPDATE
})
185 public Set
<Extension
> getExtensions(){
186 return this.extensions
;
188 protected void setExtensions(Set
<Extension
> extensions
) {
189 this.extensions
= extensions
;
192 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#addExtension(eu.etaxonomy.cdm.model.common.Extension)
194 public void addExtension(Extension extension
){
195 this.extensions
.add(extension
);
198 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#removeExtension(eu.etaxonomy.cdm.model.common.Extension)
200 public void removeExtension(Extension extension
){
201 this.extensions
.remove(extension
);
206 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#isProtectedTitleCache()
208 public boolean isProtectedTitleCache() {
209 return protectedTitleCache
;
213 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#setProtectedTitleCache(boolean)
215 public void setProtectedTitleCache(boolean protectedTitleCache
) {
216 this.protectedTitleCache
= protectedTitleCache
;
220 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getSources()
222 @OneToMany //(mappedBy="sourcedObj")
223 @Cascade({CascadeType
.SAVE_UPDATE
})
224 public Set
<OriginalSource
> getSources() {
227 protected void setSources(Set
<OriginalSource
> sources
) {
228 this.sources
= sources
;
231 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#addSource(eu.etaxonomy.cdm.model.common.OriginalSource)
233 public void addSource(OriginalSource source
) {
235 IdentifiableEntity oldSourcedObj
= source
.getSourcedObj();
236 if (oldSourcedObj
!= null && oldSourcedObj
!= this){
237 oldSourcedObj
.getSources().remove(source
);
239 this.sources
.add(source
);
240 source
.setSourcedObj(this);
244 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#removeSource(eu.etaxonomy.cdm.model.common.OriginalSource)
246 public void removeSource(OriginalSource source
) {
247 this.sources
.remove(source
);
251 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#toString()
254 public String
toString() {
256 if (titleCache
== null){
257 result
= super.toString();
259 result
= this.titleCache
;
264 //****************** CLONE ************************************************/
267 * @see eu.etaxonomy.cdm.model.common.AnnotatableEntity#clone()
269 public Object
clone() throws CloneNotSupportedException
{
270 IdentifiableEntity result
= (IdentifiableEntity
)super.clone();
273 Set
<Extension
> newExtensions
= getNewExtensionSet();
274 for (Extension extension
: this.extensions
){
275 Extension newExtension
= (Extension
)extension
.clone(this);
276 newExtensions
.add(newExtension
);
278 result
.setExtensions(newExtensions
);
281 Set
<OriginalSource
> newOriginalSources
= getNewOriginalSourcesSet();
282 for (OriginalSource originalSource
: this.sources
){
283 OriginalSource newSource
= (OriginalSource
)originalSource
.clone(this);
284 newOriginalSources
.add(newSource
);
286 result
.setSources(newOriginalSources
);
289 Set
<Rights
> rights
= getNewRightsSet();
290 rights
.addAll(this.rights
);
291 result
.setRights(rights
);
293 //result.setLsid(lsid);
294 //result.setTitleCache(titleCache);
295 //result.setProtectedTitleCache(protectedTitleCache); //must be after setTitleCache
297 //no changes to: lsid, titleCache, protectedTitleCache
302 private Set
<Extension
> getNewExtensionSet(){
303 return new HashSet
<Extension
>();
307 private Set
<OriginalSource
> getNewOriginalSourcesSet(){
308 return new HashSet
<OriginalSource
>();
312 private Set
<Rights
> getNewRightsSet(){
313 return new HashSet
<Rights
>();