(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
17
18 import java.util.HashSet;
19 import java.util.Set;
20
21 import javax.persistence.*;
22
23 /**
24 * 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.
25 * All subclasses inherit the ability to store additional properties that are stored as {@link Extension Extensions}, basically a string value with a type term.
26 * Any number of right statements can be attached as well as multiple {@link OriginalSource} objects.
27 * 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).
28 * A Taxon for example that was taken from 2 sources like FaunaEuropaea and IPNI would have two originalSource objects.
29 * 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.
30 *
31 * @author m.doering
32 * @version 1.0
33 * @created 08-Nov-2007 13:06:27
34 */
35 @MappedSuperclass
36 public abstract class IdentifiableEntity<T extends IdentifiableEntity> extends AnnotatableEntity<T> implements IOriginalSource {
37 static Logger logger = Logger.getLogger(IdentifiableEntity.class);
38
39 public final boolean PROTECTED = true;
40 public final boolean NOT_PROTECTED = true;
41
42 private String lsid;
43 private String titleCache;
44 //if true titleCache will not be automatically generated/updated
45 private boolean protectedTitleCache;
46 private Set<Rights> rights = new HashSet<Rights>();
47 private Set<Extension> extensions = new HashSet<Extension>();
48 private Set<OriginalSource> sources = new HashSet<OriginalSource>();
49
50
51 public String getLsid(){
52 return this.lsid;
53 }
54 public void setLsid(String lsid){
55 this.lsid = lsid;
56 }
57
58 public abstract String generateTitle();
59
60 public String getTitleCache(){
61 if (protectedTitleCache){
62 return this.titleCache;
63 }
64 // is title dirty, i.e. equal NULL?
65 if (titleCache == null){
66 this.titleCache = generateTitle();
67 }
68 return titleCache;
69 }
70 public void setTitleCache(String titleCache){
71 this.titleCache = titleCache;
72 this.setProtectedTitleCache(true);
73 }
74 public void setTitleCache(String titleCache, boolean protectCache){
75 this.titleCache = titleCache;
76 this.setProtectedTitleCache(protectCache);
77 }
78
79 @ManyToMany
80 @Cascade({CascadeType.SAVE_UPDATE})
81 public Set<Rights> getRights(){
82 return this.rights;
83 }
84
85 protected void setRights(Set<Rights> rights) {
86 this.rights = rights;
87 }
88 public void addRights(Rights right){
89 this.rights.add(right);
90 }
91 public void removeRights(Rights right){
92 this.rights.remove(right);
93 }
94
95 @OneToMany//(mappedBy="extendedObj")
96 @Cascade({CascadeType.SAVE_UPDATE})
97 public Set<Extension> getExtensions(){
98 return this.extensions;
99 }
100 protected void setExtensions(Set<Extension> extensions) {
101 this.extensions = extensions;
102 }
103 public void addExtension(Extension extension){
104 this.extensions.add(extension);
105 }
106 public void removeExtension(Extension extension){
107 this.extensions.remove(extension);
108 }
109
110
111 public boolean isProtectedTitleCache() {
112 return protectedTitleCache;
113 }
114
115 public void setProtectedTitleCache(boolean protectedTitleCache) {
116 this.protectedTitleCache = protectedTitleCache;
117 }
118
119 @OneToMany //(mappedBy="sourcedObj")
120 @Cascade({CascadeType.SAVE_UPDATE})
121 public Set<OriginalSource> getSources() {
122 return this.sources;
123 }
124 protected void setSources(Set<OriginalSource> sources) {
125 this.sources = sources;
126 }
127 public void addSource(OriginalSource source) {
128 this.sources.add(source);
129 }
130 public void removeSource(OriginalSource source) {
131 this.sources.remove(source);
132 }
133
134 private boolean executesToString = false;
135 @Override
136 public String toString() {
137 String result;
138 if (executesToString){
139 result = super.toString();
140 }else{
141 //avoid recursiv toString call
142 executesToString = true;
143 result = this.getTitleCache();
144 executesToString = false;
145 }
146 return result;
147 }
148
149 }