(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / IdentifiableEntity.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.model.common;
11
12
13 import org.apache.log4j.Logger;
14 import org.hibernate.annotations.Cascade;
15 import org.hibernate.annotations.CascadeType;
16 import org.hibernate.annotations.Index;
17
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;
24
25 import eu.etaxonomy.cdm.model.media.Rights;
26
27
28 import java.util.HashSet;
29 import java.util.Set;
30
31 import javax.persistence.*;
32
33 /**
34 * 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.
35 * All subclasses inherit the ability to store additional properties that are stored as {@link Extension Extensions}, basically a string value with a type term.
36 * Any number of right statements can be attached as well as multiple {@link OriginalSource} objects.
37 * 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).
38 * A Taxon for example that was taken from 2 sources like FaunaEuropaea and IPNI would have two originalSource objects.
39 * 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.
40 *
41 * @author m.doering
42 * @version 1.0
43 * @created 08-Nov-2007 13:06:27
44 */
45 @XmlAccessorType(XmlAccessType.FIELD)
46 @XmlType(name = "IdentifiableEntity", propOrder = {
47 "lsid",
48 "titleCache",
49 "protectedTitleCache",
50 "rights",
51 "extensions",
52 "sources"
53 })
54 @MappedSuperclass
55 public abstract class IdentifiableEntity<T extends IdentifiableEntity> extends AnnotatableEntity<T> implements IOriginalSource, IIdentifiableEntitiy<T> {
56 static Logger logger = Logger.getLogger(IdentifiableEntity.class);
57
58 @XmlTransient
59 public final boolean PROTECTED = true;
60 @XmlTransient
61 public final boolean NOT_PROTECTED = false;
62
63 @XmlElement(name = "LSID")
64 private String lsid;
65
66 @XmlElement(name = "TitleCache", required = true)
67 private String titleCache;
68
69 //if true titleCache will not be automatically generated/updated
70 @XmlElement(name = "ProtectedTitleCache")
71 private boolean protectedTitleCache;
72
73 @XmlElementWrapper(name = "Rights")
74 @XmlElement(name = "Rights")
75 private Set<Rights> rights = new HashSet<Rights>();
76
77 @XmlElementWrapper(name = "Extensions")
78 @XmlElement(name = "Extension")
79 private Set<Extension> extensions = new HashSet<Extension>();
80
81 @XmlElementWrapper(name = "Sources")
82 @XmlElement(name = "OriginalSource")
83 private Set<OriginalSource> sources = new HashSet<OriginalSource>();
84
85
86 /* (non-Javadoc)
87 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getLsid()
88 */
89 public String getLsid(){
90 return this.lsid;
91 }
92 /* (non-Javadoc)
93 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#setLsid(java.lang.String)
94 */
95 public void setLsid(String lsid){
96 this.lsid = lsid;
97 }
98
99 /* (non-Javadoc)
100 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#generateTitle()
101 */
102 public abstract String generateTitle();
103
104 //@Index(name="titleCacheIndex")
105 /* (non-Javadoc)
106 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getTitleCache()
107 */
108 public String getTitleCache(){
109 if (protectedTitleCache){
110 return this.titleCache;
111 }
112 // is title dirty, i.e. equal NULL?
113 if (titleCache == null){
114 this.titleCache = generateTitle();
115 }
116 return titleCache;
117 }
118 /* (non-Javadoc)
119 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#setTitleCache(java.lang.String)
120 */
121 public void setTitleCache(String titleCache){
122 //TODO truncation of title cach
123 if (titleCache != null && titleCache.length() > 254){
124 logger.warn("Truncation of title cache: " + this.toString());
125 titleCache = titleCache.substring(0, 252) + "...";
126 }
127 this.titleCache = titleCache;
128 this.setProtectedTitleCache(PROTECTED);
129 }
130 /* (non-Javadoc)
131 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#setTitleCache(java.lang.String, boolean)
132 */
133 public void setTitleCache(String titleCache, boolean protectCache){
134 this.titleCache = titleCache;
135 this.setProtectedTitleCache(protectCache);
136 }
137
138 /* (non-Javadoc)
139 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getRights()
140 */
141 @ManyToMany
142 @Cascade({CascadeType.SAVE_UPDATE})
143 public Set<Rights> getRights(){
144 return this.rights;
145 }
146
147 protected void setRights(Set<Rights> rights) {
148 this.rights = rights;
149 }
150 /* (non-Javadoc)
151 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#addRights(eu.etaxonomy.cdm.model.media.Rights)
152 */
153 public void addRights(Rights right){
154 this.rights.add(right);
155 }
156 /* (non-Javadoc)
157 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#removeRights(eu.etaxonomy.cdm.model.media.Rights)
158 */
159 public void removeRights(Rights right){
160 this.rights.remove(right);
161 }
162
163 /* (non-Javadoc)
164 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getExtensions()
165 */
166 @OneToMany//(mappedBy="extendedObj")
167 @Cascade({CascadeType.SAVE_UPDATE})
168 public Set<Extension> getExtensions(){
169 return this.extensions;
170 }
171 protected void setExtensions(Set<Extension> extensions) {
172 this.extensions = extensions;
173 }
174 /* (non-Javadoc)
175 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#addExtension(eu.etaxonomy.cdm.model.common.Extension)
176 */
177 public void addExtension(Extension extension){
178 this.extensions.add(extension);
179 }
180 /* (non-Javadoc)
181 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#removeExtension(eu.etaxonomy.cdm.model.common.Extension)
182 */
183 public void removeExtension(Extension extension){
184 this.extensions.remove(extension);
185 }
186
187
188 /* (non-Javadoc)
189 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#isProtectedTitleCache()
190 */
191 public boolean isProtectedTitleCache() {
192 return protectedTitleCache;
193 }
194
195 /* (non-Javadoc)
196 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#setProtectedTitleCache(boolean)
197 */
198 public void setProtectedTitleCache(boolean protectedTitleCache) {
199 this.protectedTitleCache = protectedTitleCache;
200 }
201
202 /* (non-Javadoc)
203 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#getSources()
204 */
205 @OneToMany //(mappedBy="sourcedObj")
206 @Cascade({CascadeType.SAVE_UPDATE})
207 public Set<OriginalSource> getSources() {
208 return this.sources;
209 }
210 protected void setSources(Set<OriginalSource> sources) {
211 this.sources = sources;
212 }
213 /* (non-Javadoc)
214 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#addSource(eu.etaxonomy.cdm.model.common.OriginalSource)
215 */
216 public void addSource(OriginalSource source) {
217 this.sources.add(source);
218 }
219 /* (non-Javadoc)
220 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#removeSource(eu.etaxonomy.cdm.model.common.OriginalSource)
221 */
222 public void removeSource(OriginalSource source) {
223 this.sources.remove(source);
224 }
225
226 /* (non-Javadoc)
227 * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntitiy#toString()
228 */
229 @Override
230 public String toString() {
231 String result;
232 if (titleCache == null){
233 result = super.toString();
234 }else{
235 result = this.titleCache;
236 }
237 return result;
238 }
239
240 }