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 org
.apache
.log4j
.Logger
;
14 import org
.hibernate
.annotations
.Cascade
;
15 import org
.hibernate
.annotations
.CascadeType
;
16 import org
.hibernate
.annotations
.Index
;
18 import javax
.xml
.bind
.annotation
.XmlAccessType
;
19 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
20 import javax
.xml
.bind
.annotation
.XmlElement
;
21 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
22 import javax
.xml
.bind
.annotation
.XmlTransient
;
23 import javax
.xml
.bind
.annotation
.XmlType
;
25 import eu
.etaxonomy
.cdm
.model
.media
.IdentifyableMediaEntity
;
26 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
27 import eu
.etaxonomy
.cdm
.model
.media
.Rights
;
30 import java
.util
.HashSet
;
33 import javax
.persistence
.*;
36 * 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.
37 * All subclasses inherit the ability to store additional properties that are stored as {@link Extension Extensions}, basically a string value with a type term.
38 * Any number of right statements can be attached as well as multiple {@link OriginalSource} objects.
39 * 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).
40 * A Taxon for example that was taken from 2 sources like FaunaEuropaea and IPNI would have two originalSource objects.
41 * 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.
45 * @created 08-Nov-2007 13:06:27
47 @XmlAccessorType(XmlAccessType
.FIELD
)
48 @XmlType(name
= "IdentifiableEntity", propOrder
= {
51 "protectedTitleCache",
57 public abstract class IdentifiableEntity
<T
extends IdentifiableEntity
> extends AnnotatableEntity
<T
> implements IOriginalSource
, IIdentifiableEntitiy
<T
> {
58 static Logger logger
= Logger
.getLogger(IdentifiableEntity
.class);
61 public final boolean PROTECTED
= true;
63 public final boolean NOT_PROTECTED
= false;
65 @XmlElement(name
= "LSID")
68 @XmlElement(name
= "TitleCache", required
= true)
69 private String titleCache
;
71 //if true titleCache will not be automatically generated/updated
72 @XmlElement(name
= "ProtectedTitleCache")
73 private boolean protectedTitleCache
;
75 @XmlElementWrapper(name
= "Rights")
76 @XmlElement(name
= "Rights")
77 private Set
<Rights
> rights
= getNewRightsSet();
79 @XmlElementWrapper(name
= "Extensions")
80 @XmlElement(name
= "Extension")
81 private Set
<Extension
> extensions
= getNewExtensionSet();
83 @XmlElementWrapper(name
= "Sources")
84 @XmlElement(name
= "OriginalSource")
85 private Set
<OriginalSource
> sources
= getNewOriginalSourcesSet();
89 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getLsid()
91 public String
getLsid(){
95 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#setLsid(java.lang.String)
97 public void setLsid(String lsid
){
102 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#generateTitle()
104 public abstract String
generateTitle();
106 //@Index(name="titleCacheIndex")
108 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getTitleCache()
110 public String
getTitleCache(){
111 if (protectedTitleCache
){
112 return this.titleCache
;
114 // is title dirty, i.e. equal NULL?
115 if (titleCache
== null){
116 this.titleCache
= generateTitle();
121 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#setTitleCache(java.lang.String)
123 public void setTitleCache(String titleCache
){
124 //TODO truncation of title cach
125 if (titleCache
!= null && titleCache
.length() > 254){
126 logger
.warn("Truncation of title cache: " + this.toString());
127 titleCache
= titleCache
.substring(0, 252) + "...";
129 this.titleCache
= titleCache
;
130 this.setProtectedTitleCache(PROTECTED
);
133 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#setTitleCache(java.lang.String, boolean)
135 public void setTitleCache(String titleCache
, boolean protectCache
){
136 this.titleCache
= titleCache
;
137 this.setProtectedTitleCache(protectCache
);
141 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getRights()
144 @Cascade({CascadeType
.SAVE_UPDATE
})
145 public Set
<Rights
> getRights(){
149 protected void setRights(Set
<Rights
> rights
) {
150 this.rights
= rights
;
153 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#addRights(eu.etaxonomy.cdm.model.media.Rights)
155 public void addRights(Rights right
){
156 this.rights
.add(right
);
159 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#removeRights(eu.etaxonomy.cdm.model.media.Rights)
161 public void removeRights(Rights right
){
162 this.rights
.remove(right
);
166 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getExtensions()
168 @OneToMany//(mappedBy="extendedObj")
169 @Cascade({CascadeType
.SAVE_UPDATE
})
170 public Set
<Extension
> getExtensions(){
171 return this.extensions
;
173 protected void setExtensions(Set
<Extension
> extensions
) {
174 this.extensions
= extensions
;
177 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#addExtension(eu.etaxonomy.cdm.model.common.Extension)
179 public void addExtension(Extension extension
){
180 this.extensions
.add(extension
);
183 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#removeExtension(eu.etaxonomy.cdm.model.common.Extension)
185 public void removeExtension(Extension extension
){
186 this.extensions
.remove(extension
);
191 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#isProtectedTitleCache()
193 public boolean isProtectedTitleCache() {
194 return protectedTitleCache
;
198 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#setProtectedTitleCache(boolean)
200 public void setProtectedTitleCache(boolean protectedTitleCache
) {
201 this.protectedTitleCache
= protectedTitleCache
;
205 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getSources()
207 @OneToMany //(mappedBy="sourcedObj")
208 @Cascade({CascadeType
.SAVE_UPDATE
})
209 public Set
<OriginalSource
> getSources() {
212 protected void setSources(Set
<OriginalSource
> sources
) {
213 this.sources
= sources
;
216 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#addSource(eu.etaxonomy.cdm.model.common.OriginalSource)
218 public void addSource(OriginalSource source
) {
220 IdentifiableEntity oldSourcedObj
= source
.getSourcedObj();
221 if (oldSourcedObj
!= null && oldSourcedObj
!= this){
222 oldSourcedObj
.getSources().remove(source
);
224 this.sources
.add(source
);
225 source
.setSourcedObj(this);
229 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#removeSource(eu.etaxonomy.cdm.model.common.OriginalSource)
231 public void removeSource(OriginalSource source
) {
232 this.sources
.remove(source
);
236 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#toString()
239 public String
toString() {
241 if (titleCache
== null){
242 result
= super.toString();
244 result
= this.titleCache
;
249 //****************** CLONE ************************************************/
252 * @see eu.etaxonomy.cdm.model.common.AnnotatableEntity#clone()
254 public Object
clone() throws CloneNotSupportedException
{
255 IdentifiableEntity result
= (IdentifiableEntity
)super.clone();
258 Set
<Extension
> newExtensions
= getNewExtensionSet();
259 for (Extension extension
: this.extensions
){
260 Extension newExtension
= (Extension
)extension
.clone(this);
261 newExtensions
.add(newExtension
);
263 result
.setExtensions(newExtensions
);
266 Set
<OriginalSource
> newOriginalSources
= getNewOriginalSourcesSet();
267 for (OriginalSource originalSource
: this.sources
){
268 OriginalSource newSource
= (OriginalSource
)originalSource
.clone(this);
269 newOriginalSources
.add(newSource
);
271 result
.setSources(newOriginalSources
);
274 Set
<Rights
> rights
= getNewRightsSet();
275 rights
.addAll(this.rights
);
276 result
.setRights(rights
);
278 //result.setLsid(lsid);
279 //result.setTitleCache(titleCache);
280 //result.setProtectedTitleCache(protectedTitleCache); //must be after setTitleCache
282 //no changes to: lsid, titleCache, protectedTitleCache
287 private Set
<Extension
> getNewExtensionSet(){
288 return new HashSet
<Extension
>();
292 private Set
<OriginalSource
> getNewOriginalSourcesSet(){
293 return new HashSet
<OriginalSource
>();
297 private Set
<Rights
> getNewRightsSet(){
298 return new HashSet
<Rights
>();