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
;
12 import org
.apache
.log4j
.Logger
;
13 import org
.hibernate
.annotations
.Cascade
;
14 import org
.hibernate
.annotations
.CascadeType
;
15 import org
.hibernate
.collection
.AbstractPersistentCollection
;
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
;
23 import java
.lang
.reflect
.Field
;
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
;
37 * workaround 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)
42 * @created 08-Nov-2007 13:06:19
44 @XmlAccessorType(XmlAccessType
.FIELD
)
45 @XmlType(name
= "DefinedTermBase", propOrder
= {
53 @XmlRootElement(name
= "DefinedTermBase")
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);
59 static protected IVocabularyStore vocabularyStore
= new DefaultVocabularyStore();
61 public static void setVocabularyStore(IVocabularyStore vocabularyStore
){
62 DefinedTermBase
.vocabularyStore
= vocabularyStore
;
65 @XmlElement(name
= "KindOf")
66 private DefinedTermBase kindOf
;
68 @XmlElement(name
= "GeneralizationOf")
69 private Set
<DefinedTermBase
> generalizationOf
= new HashSet
<DefinedTermBase
>();
71 @XmlElement(name
= "PartOf")
72 private DefinedTermBase partOf
;
74 @XmlElementWrapper(name
= "Includes")
75 @XmlElement(name
= "Include")
76 private Set
<DefinedTermBase
> includes
= new HashSet
<DefinedTermBase
>();
78 @XmlElementWrapper(name
= "Media")
79 @XmlElement(name
= "Medium")
80 private Set
<Media
> media
= new HashSet
<Media
>();
82 @XmlElement(name
= "Vocabulary")
83 protected TermVocabulary
<T
> vocabulary
;
86 public static DefinedTermBase
findByUuid(UUID uuid
){
87 return vocabularyStore
.getTermByUuid(uuid
);
90 public DefinedTermBase() {
93 public DefinedTermBase(String term
, String label
, String labelAbbrev
) {
94 super(term
, label
, labelAbbrev
);
99 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
101 public ILoadableTerm
readCsvLine(List
<String
> csvLine
) {
102 return readCsvLine(csvLine
, Language
.ENGLISH());
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
) );
115 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#writeCsvLine(au.com.bytecode.opencsv.CSVWriter)
117 public void writeCsvLine(CSVWriter writer
) {
118 String
[] line
= new String
[4];
119 line
[0] = getUuid().toString();
121 line
[2] = getLabel();
122 line
[3] = getDescription();
123 writer
.writeNext(line
);
128 //@Cascade({CascadeType.SAVE_UPDATE})
129 public DefinedTermBase
getKindOf(){
132 public void setKindOf(DefinedTermBase kindOf
){
133 this.kindOf
= kindOf
;
137 //@OneToMany(fetch=FetchType.LAZY)
138 //@Cascade({CascadeType.SAVE_UPDATE})
139 public Set
<DefinedTermBase
> getGeneralizationOf(){
140 return this.generalizationOf
;
142 public void setGeneralizationOf(Set
<DefinedTermBase
> generalizationOf
) {
143 this.generalizationOf
= generalizationOf
;
149 //@Cascade({CascadeType.SAVE_UPDATE})
150 public DefinedTermBase
getPartOf(){
153 public void setPartOf(DefinedTermBase partOf
){
154 this.partOf
= partOf
;
158 //@OneToMany(fetch=FetchType.LAZY)
159 //@Cascade({CascadeType.SAVE_UPDATE})
160 public Set
<DefinedTermBase
> getIncludes(){
161 return this.includes
;
163 public void setIncludes(Set
<DefinedTermBase
> includes
) {
164 this.includes
= includes
;
166 public void addIncludes(DefinedTermBase includes
) {
167 this.includes
.add(includes
);
169 public void removeIncludes(TermBase includes
) {
170 this.includes
.remove(includes
);
175 @Cascade({CascadeType
.SAVE_UPDATE
})
176 public Set
<Media
> getMedia(){
179 public void setMedia(Set
<Media
> media
) {
182 public void addMedia(Media media
) {
183 this.media
.add(media
);
185 public void removeMedia(Media media
) {
186 this.media
.remove(media
);
190 * @see eu.etaxonomy.cdm.model.common.IDefTerm#getVocabulary()
194 public TermVocabulary
getVocabulary() {
195 return this.vocabulary
;
198 * @see eu.etaxonomy.cdm.model.common.IDefTerm#setVocabulary(eu.etaxonomy.cdm.model.common.TermVocabulary)
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);
207 if (newVocabulary
!= null) {
208 newVocabulary
.terms
.add(this);
210 this.vocabulary
= newVocabulary
;
215 * @see eu.etaxonomy.cdm.model.common.IDefTerm#getVocabulary()
217 @ManyToOne(fetch
=FetchType
.LAZY
)
218 @Cascade( { CascadeType
.SAVE_UPDATE
})
219 protected TermVocabulary
getPersistentVocabulary() {
220 return this.vocabulary
;
223 * @see eu.etaxonomy.cdm.model.common.IDefTerm#setVocabulary(eu.etaxonomy.cdm.model.common.TermVocabulary)
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);
232 if (newVocabulary
!= null) {
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);
244 newVocabulary
.terms
.add(this);
246 } catch (SecurityException e
) {
248 } catch (IllegalArgumentException e
) {
250 } catch (NoSuchFieldException e
) {
252 } catch (IllegalAccessException e
) {
256 this.vocabulary
= newVocabulary
;