(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / DefinedTermBase.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 import org.apache.log4j.Logger;
13 import org.hibernate.annotations.Cascade;
14 import org.hibernate.annotations.CascadeType;
15 import org.hibernate.collection.AbstractPersistentCollection;
16
17 import au.com.bytecode.opencsv.CSVWriter;
18 import eu.etaxonomy.cdm.model.common.init.DefaultVocabularyStore;
19 import eu.etaxonomy.cdm.model.common.init.IVocabularyStore;
20 import eu.etaxonomy.cdm.model.media.Media;
21 import eu.etaxonomy.cdm.model.name.Rank;
22
23 import java.lang.reflect.Field;
24 import java.util.*;
25
26 import javax.persistence.*;
27 import javax.xml.bind.annotation.XmlAccessType;
28 import javax.xml.bind.annotation.XmlAccessorType;
29 import javax.xml.bind.annotation.XmlElement;
30 import javax.xml.bind.annotation.XmlElementWrapper;
31 import javax.xml.bind.annotation.XmlRootElement;
32 import javax.xml.bind.annotation.XmlTransient;
33 import javax.xml.bind.annotation.XmlType;
34
35
36 /**
37 * walkaround for enumerations, base type according to TDWG. For linear ordering
38 * use partOf relation and BreadthFirst. Default iterator order should therefore
39 * be BreadthFirst (not DepthFirst)
40 * @author m.doering
41 * @version 1.0
42 * @created 08-Nov-2007 13:06:19
43 */
44 @XmlAccessorType(XmlAccessType.FIELD)
45 @XmlType(name = "DefinedTermBase", propOrder = {
46 "kindOf",
47 "generalizationOf",
48 "partOf",
49 "includes",
50 "media",
51 "vocabulary"
52 })
53 @XmlRootElement(name = "DefinedTermBase")
54 @Entity
55 @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
56 public abstract class DefinedTermBase<T extends DefinedTermBase> extends TermBase implements ILoadableTerm{
57 static Logger logger = Logger.getLogger(DefinedTermBase.class);
58
59 static protected IVocabularyStore vocabularyStore = new DefaultVocabularyStore();
60
61 public static void setVocabularyStore(IVocabularyStore vocabularyStore){
62 DefinedTermBase.vocabularyStore = vocabularyStore;
63 }
64
65 @XmlElement(name = "KindOf")
66 private DefinedTermBase kindOf;
67
68 @XmlElement(name = "GeneralizationOf")
69 private Set<DefinedTermBase> generalizationOf = new HashSet<DefinedTermBase>();
70
71 @XmlElement(name = "PartOf")
72 private DefinedTermBase partOf;
73
74 @XmlElementWrapper(name = "Includes")
75 @XmlElement(name = "Include")
76 private Set<DefinedTermBase> includes = new HashSet<DefinedTermBase>();
77
78 @XmlElementWrapper(name = "Media")
79 @XmlElement(name = "Medium")
80 private Set<Media> media = new HashSet<Media>();
81
82 @XmlElement(name = "Vocabulary")
83 protected TermVocabulary<T> vocabulary;
84
85
86 public static DefinedTermBase findByUuid(UUID uuid){
87 return vocabularyStore.getTermByUuid(uuid);
88 }
89
90 public DefinedTermBase() {
91 super();
92 }
93 public DefinedTermBase(String term, String label, String labelAbbrev) {
94 super(term, label, labelAbbrev);
95 }
96
97
98 /* (non-Javadoc)
99 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
100 */
101 public ILoadableTerm readCsvLine(List<String> csvLine) {
102 return readCsvLine(csvLine, Language.ENGLISH());
103 }
104 public ILoadableTerm readCsvLine(List<String> csvLine, Language lang) {
105 this.setUuid(UUID.fromString(csvLine.get(0)));
106 this.setUri(csvLine.get(1));
107 String label = csvLine.get(2).trim();
108 String text = csvLine.get(3);
109 String abbreviatedLabel = null;
110 this.addRepresentation(Representation.NewInstance(text, label, abbreviatedLabel, lang) );
111 return this;
112 }
113
114 /* (non-Javadoc)
115 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#writeCsvLine(au.com.bytecode.opencsv.CSVWriter)
116 */
117 public void writeCsvLine(CSVWriter writer) {
118 String [] line = new String[4];
119 line[0] = getUuid().toString();
120 line[1] = getUri();
121 line[2] = getLabel();
122 line[3] = getDescription();
123 writer.writeNext(line);
124 }
125
126 @Transient
127 //@ManyToOne
128 //@Cascade({CascadeType.SAVE_UPDATE})
129 public DefinedTermBase getKindOf(){
130 return this.kindOf;
131 }
132 public void setKindOf(DefinedTermBase kindOf){
133 this.kindOf = kindOf;
134 }
135
136 @Transient
137 //@OneToMany(fetch=FetchType.LAZY)
138 //@Cascade({CascadeType.SAVE_UPDATE})
139 public Set<DefinedTermBase> getGeneralizationOf(){
140 return this.generalizationOf;
141 }
142 public void setGeneralizationOf(Set<DefinedTermBase> generalizationOf) {
143 this.generalizationOf = generalizationOf;
144 }
145
146
147 @Transient
148 //@ManyToOne
149 //@Cascade({CascadeType.SAVE_UPDATE})
150 public DefinedTermBase getPartOf(){
151 return this.partOf;
152 }
153 public void setPartOf(DefinedTermBase partOf){
154 this.partOf = partOf;
155 }
156
157 @Transient
158 //@OneToMany(fetch=FetchType.LAZY)
159 //@Cascade({CascadeType.SAVE_UPDATE})
160 public Set<DefinedTermBase> getIncludes(){
161 return this.includes;
162 }
163 public void setIncludes(Set<DefinedTermBase> includes) {
164 this.includes = includes;
165 }
166 public void addIncludes(DefinedTermBase includes) {
167 this.includes.add(includes);
168 }
169 public void removeIncludes(TermBase includes) {
170 this.includes.remove(includes);
171 }
172
173
174 @OneToMany
175 @Cascade({CascadeType.SAVE_UPDATE})
176 public Set<Media> getMedia(){
177 return this.media;
178 }
179 public void setMedia(Set<Media> media) {
180 this.media = media;
181 }
182 public void addMedia(Media media) {
183 this.media.add(media);
184 }
185 public void removeMedia(Media media) {
186 this.media.remove(media);
187 }
188
189 /* (non-Javadoc)
190 * @see eu.etaxonomy.cdm.model.common.IDefTerm#getVocabulary()
191 */
192 @Transient
193 @XmlTransient
194 public TermVocabulary getVocabulary() {
195 return this.vocabulary;
196 }
197 /* (non-Javadoc)
198 * @see eu.etaxonomy.cdm.model.common.IDefTerm#setVocabulary(eu.etaxonomy.cdm.model.common.TermVocabulary)
199 */
200 public void setVocabulary(TermVocabulary newVocabulary) {
201 // Hibernate bidirectional cascade hack:
202 // http://opensource.atlassian.com/projects/hibernate/browse/HHH-1054
203 if(this.vocabulary == newVocabulary){ return;}
204 if (this.vocabulary != null) {
205 this.vocabulary.terms.remove(this);
206 }
207 if (newVocabulary!= null) {
208 newVocabulary.terms.add(this);
209 }
210 this.vocabulary = newVocabulary;
211 }
212
213
214 /* (non-Javadoc)
215 * @see eu.etaxonomy.cdm.model.common.IDefTerm#getVocabulary()
216 */
217 @ManyToOne(fetch=FetchType.LAZY)
218 @Cascade( { CascadeType.SAVE_UPDATE })
219 protected TermVocabulary getPersistentVocabulary() {
220 return this.vocabulary;
221 }
222 /* (non-Javadoc)
223 * @see eu.etaxonomy.cdm.model.common.IDefTerm#setVocabulary(eu.etaxonomy.cdm.model.common.TermVocabulary)
224 */
225 protected void setPersistentVocabulary(TermVocabulary newVocabulary) {
226 // Hibernate bidirectional cascade hack:
227 // http://opensource.atlassian.com/projects/hibernate/browse/HHH-1054
228 if(this.vocabulary == newVocabulary){ return;}
229 if (this.vocabulary != null) {
230 this.vocabulary.terms.remove(this);
231 }
232 if (newVocabulary!= null) {
233 try {
234 Field fieldInitializing = AbstractPersistentCollection.class.getDeclaredField("initializing");
235 fieldInitializing.setAccessible(true);
236 if (AbstractPersistentCollection.class.isAssignableFrom(newVocabulary.terms.getClass())){
237 boolean initValue = fieldInitializing.getBoolean(newVocabulary.terms);
238 if (initValue == false){
239 newVocabulary.terms.add(this);
240 }else{
241 //nothing
242 }
243 }else{
244 newVocabulary.terms.add(this);
245 }
246 } catch (SecurityException e) {
247 e.printStackTrace();
248 } catch (IllegalArgumentException e) {
249 e.printStackTrace();
250 } catch (NoSuchFieldException e) {
251 e.printStackTrace();
252 } catch (IllegalAccessException e) {
253 e.printStackTrace();
254 }
255 }
256 this.vocabulary = newVocabulary;
257 }
258
259 }