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 ISourceable
, IIdentifiableEntity
{
57 private static final long serialVersionUID
= -5610995424730659058L;
58 private static final 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.IIdentifiableEntity#getLsid()
91 public String
getLsid(){
95 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#setLsid(java.lang.String)
97 public void setLsid(String lsid
){
102 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#generateTitle()
104 public abstract String
generateTitle();
107 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#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.setTitleCache(generateTitle(),protectedTitleCache
) ; //for truncating
121 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#setTitleCache(java.lang.String)
123 public void setTitleCache(String titleCache
){
124 setTitleCache(titleCache
, PROTECTED
);
127 //@Index(name="titleCacheIndex")
129 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#getTitleCache()
131 @Column(length
=255, name
="titleCache")
132 @Deprecated //for hibernate use only
133 protected String
getPersistentTitleCache(){
134 return getTitleCache();
136 @Deprecated //for hibernate use only
137 protected void setPersistentTitleCache(String titleCache
){
138 this.titleCache
= titleCache
;
144 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#setTitleCache(java.lang.String, boolean)
146 public void setTitleCache(String titleCache
, boolean protectCache
){
147 //TODO truncation of title cache
148 if (titleCache
!= null && titleCache
.length() > 254){
149 logger
.warn("Truncation of title cache: " + this.toString() + "/" + titleCache
);
150 titleCache
= titleCache
.substring(0, 249) + "...";
152 this.titleCache
= titleCache
;
153 this.setProtectedTitleCache(protectCache
);
157 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#getRights()
160 @Cascade({CascadeType
.SAVE_UPDATE
})
161 public Set
<Rights
> getRights(){
165 protected void setRights(Set
<Rights
> rights
) {
166 this.rights
= rights
;
169 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#addRights(eu.etaxonomy.cdm.model.media.Rights)
171 public void addRights(Rights right
){
172 this.rights
.add(right
);
175 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#removeRights(eu.etaxonomy.cdm.model.media.Rights)
177 public void removeRights(Rights right
){
178 this.rights
.remove(right
);
182 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#getExtensions()
184 @OneToMany//(mappedBy="extendedObj")
185 @Cascade({CascadeType
.SAVE_UPDATE
})
186 public Set
<Extension
> getExtensions(){
187 return this.extensions
;
189 protected void setExtensions(Set
<Extension
> extensions
) {
190 this.extensions
= extensions
;
193 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#addExtension(eu.etaxonomy.cdm.model.common.Extension)
195 public void addExtension(Extension extension
){
196 this.extensions
.add(extension
);
199 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#removeExtension(eu.etaxonomy.cdm.model.common.Extension)
201 public void removeExtension(Extension extension
){
202 this.extensions
.remove(extension
);
207 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#isProtectedTitleCache()
209 public boolean isProtectedTitleCache() {
210 return protectedTitleCache
;
214 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#setProtectedTitleCache(boolean)
216 public void setProtectedTitleCache(boolean protectedTitleCache
) {
217 this.protectedTitleCache
= protectedTitleCache
;
221 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#getSources()
223 @OneToMany //(mappedBy="sourcedObj")
224 @Cascade({CascadeType
.SAVE_UPDATE
})
225 public Set
<OriginalSource
> getSources() {
228 protected void setSources(Set
<OriginalSource
> sources
) {
229 this.sources
= sources
;
232 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#addSource(eu.etaxonomy.cdm.model.common.OriginalSource)
234 public void addSource(OriginalSource source
) {
236 IdentifiableEntity oldSourcedObj
= source
.getSourcedObj();
237 if (oldSourcedObj
!= null && oldSourcedObj
!= this){
238 oldSourcedObj
.getSources().remove(source
);
240 this.sources
.add(source
);
241 source
.setSourcedObj(this);
245 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#removeSource(eu.etaxonomy.cdm.model.common.OriginalSource)
247 public void removeSource(OriginalSource source
) {
248 this.sources
.remove(source
);
252 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#toString()
255 public String
toString() {
257 if (titleCache
== null){
258 result
= super.toString();
260 result
= this.titleCache
;
265 //****************** CLONE ************************************************/
268 * @see eu.etaxonomy.cdm.model.common.AnnotatableEntity#clone()
271 public Object
clone() throws CloneNotSupportedException
{
272 IdentifiableEntity result
= (IdentifiableEntity
)super.clone();
275 Set
<Extension
> newExtensions
= getNewExtensionSet();
276 for (Extension extension
: this.extensions
){
277 Extension newExtension
= extension
.clone(this);
278 newExtensions
.add(newExtension
);
280 result
.setExtensions(newExtensions
);
283 Set
<OriginalSource
> newOriginalSources
= getNewOriginalSourcesSet();
284 for (OriginalSource originalSource
: this.sources
){
285 OriginalSource newSource
= originalSource
.clone(this);
286 newOriginalSources
.add(newSource
);
288 result
.setSources(newOriginalSources
);
291 Set
<Rights
> rights
= getNewRightsSet();
292 rights
.addAll(this.rights
);
293 result
.setRights(rights
);
295 //result.setLsid(lsid);
296 //result.setTitleCache(titleCache);
297 //result.setProtectedTitleCache(protectedTitleCache); //must be after setTitleCache
299 //no changes to: lsid, titleCache, protectedTitleCache
302 if (! protectedTitleCache
){
309 private Set
<Extension
> getNewExtensionSet(){
310 return new HashSet
<Extension
>();
314 private Set
<OriginalSource
> getNewOriginalSourcesSet(){
315 return new HashSet
<OriginalSource
>();
319 private Set
<Rights
> getNewRightsSet(){
320 return new HashSet
<Rights
>();