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 java
.lang
.reflect
.Field
;
24 import javax
.persistence
.*;
25 import javax
.xml
.bind
.annotation
.XmlAccessType
;
26 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
27 import javax
.xml
.bind
.annotation
.XmlElement
;
28 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
29 import javax
.xml
.bind
.annotation
.XmlRootElement
;
30 import javax
.xml
.bind
.annotation
.XmlTransient
;
31 import javax
.xml
.bind
.annotation
.XmlType
;
35 * walkaround for enumerations, base type according to TDWG. For linear ordering
36 * use partOf relation and BreadthFirst. Default iterator order should therefore
37 * be BreadthFirst (not DepthFirst)
40 * @created 08-Nov-2007 13:06:19
42 @XmlAccessorType(XmlAccessType
.FIELD
)
43 @XmlType(name
= "DefinedTermBase", propOrder
= {
51 @XmlRootElement(name
= "DefinedTermBase")
53 @Inheritance(strategy
=InheritanceType
.SINGLE_TABLE
)
54 public abstract class DefinedTermBase
<T
extends DefinedTermBase
> extends TermBase
implements ILoadableTerm
{
55 static Logger logger
= Logger
.getLogger(DefinedTermBase
.class);
57 static protected IVocabularyStore vocabularyStore
= new DefaultVocabularyStore();
59 public static void setVocabularyStore(IVocabularyStore vocabularyStore
){
60 DefinedTermBase
.vocabularyStore
= vocabularyStore
;
63 @XmlElement(name
= "KindOf")
64 private DefinedTermBase kindOf
;
66 @XmlElement(name
= "GeneralizationOf")
67 private Set
<DefinedTermBase
> generalizationOf
= new HashSet
<DefinedTermBase
>();
69 @XmlElement(name
= "PartOf")
70 private DefinedTermBase partOf
;
72 @XmlElementWrapper(name
= "Includes")
73 @XmlElement(name
= "Include")
74 private Set
<DefinedTermBase
> includes
= new HashSet
<DefinedTermBase
>();
76 @XmlElementWrapper(name
= "Media")
77 @XmlElement(name
= "Medium")
78 private Set
<Media
> media
= new HashSet
<Media
>();
80 @XmlElement(name
= "Vocabulary")
81 protected TermVocabulary
<T
> vocabulary
;
84 public static DefinedTermBase
findByUuid(UUID uuid
){
85 return vocabularyStore
.getTermByUuid(uuid
);
88 public DefinedTermBase() {
91 public DefinedTermBase(String term
, String label
, String labelAbbrev
) {
92 super(term
, label
, labelAbbrev
);
97 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
99 public ILoadableTerm
readCsvLine(List
<String
> csvLine
) {
100 return readCsvLine(csvLine
, Language
.ENGLISH());
102 public ILoadableTerm
readCsvLine(List
<String
> csvLine
, Language lang
) {
103 this.setUuid(UUID
.fromString(csvLine
.get(0)));
104 this.setUri(csvLine
.get(1));
105 String label
= csvLine
.get(2).trim();
106 String text
= csvLine
.get(3);
107 String abbreviatedLabel
= null;
108 this.addRepresentation(Representation
.NewInstance(text
, label
, abbreviatedLabel
, lang
) );
113 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#writeCsvLine(au.com.bytecode.opencsv.CSVWriter)
115 public void writeCsvLine(CSVWriter writer
) {
116 String
[] line
= new String
[4];
117 line
[0] = getUuid().toString();
119 line
[2] = getLabel();
120 line
[3] = getDescription();
121 writer
.writeNext(line
);
126 //@Cascade({CascadeType.SAVE_UPDATE})
127 public DefinedTermBase
getKindOf(){
130 public void setKindOf(DefinedTermBase kindOf
){
131 this.kindOf
= kindOf
;
135 //@OneToMany(fetch=FetchType.LAZY)
136 //@Cascade({CascadeType.SAVE_UPDATE})
137 public Set
<DefinedTermBase
> getGeneralizationOf(){
138 return this.generalizationOf
;
140 public void setGeneralizationOf(Set
<DefinedTermBase
> generalizationOf
) {
141 this.generalizationOf
= generalizationOf
;
147 //@Cascade({CascadeType.SAVE_UPDATE})
148 public DefinedTermBase
getPartOf(){
151 public void setPartOf(DefinedTermBase partOf
){
152 this.partOf
= partOf
;
156 //@OneToMany(fetch=FetchType.LAZY)
157 //@Cascade({CascadeType.SAVE_UPDATE})
158 public Set
<DefinedTermBase
> getIncludes(){
159 return this.includes
;
161 public void setIncludes(Set
<DefinedTermBase
> includes
) {
162 this.includes
= includes
;
164 public void addIncludes(DefinedTermBase includes
) {
165 this.includes
.add(includes
);
167 public void removeIncludes(TermBase includes
) {
168 this.includes
.remove(includes
);
173 @Cascade({CascadeType
.SAVE_UPDATE
})
174 public Set
<Media
> getMedia(){
177 public void setMedia(Set
<Media
> media
) {
180 public void addMedia(Media media
) {
181 this.media
.add(media
);
183 public void removeMedia(Media media
) {
184 this.media
.remove(media
);
188 * @see eu.etaxonomy.cdm.model.common.IDefTerm#getVocabulary()
192 public TermVocabulary
getVocabulary() {
193 return this.vocabulary
;
196 * @see eu.etaxonomy.cdm.model.common.IDefTerm#setVocabulary(eu.etaxonomy.cdm.model.common.TermVocabulary)
198 public void setVocabulary(TermVocabulary newVocabulary
) {
199 // Hibernate bidirectional cascade hack:
200 // http://opensource.atlassian.com/projects/hibernate/browse/HHH-1054
201 if(this.vocabulary
== newVocabulary
){ return;}
202 if (this.vocabulary
!= null) {
203 this.vocabulary
.terms
.remove(this);
205 if (newVocabulary
!= null) {
206 newVocabulary
.terms
.add(this);
208 this.vocabulary
= newVocabulary
;
213 * @see eu.etaxonomy.cdm.model.common.IDefTerm#getVocabulary()
215 @ManyToOne(fetch
=FetchType
.LAZY
)
216 @Cascade( { CascadeType
.SAVE_UPDATE
})
217 protected TermVocabulary
getPersistentVocabulary() {
218 return this.vocabulary
;
221 * @see eu.etaxonomy.cdm.model.common.IDefTerm#setVocabulary(eu.etaxonomy.cdm.model.common.TermVocabulary)
223 protected void setPersistentVocabulary(TermVocabulary newVocabulary
) {
224 // Hibernate bidirectional cascade hack:
225 // http://opensource.atlassian.com/projects/hibernate/browse/HHH-1054
226 if(this.vocabulary
== newVocabulary
){ return;}
227 if (this.vocabulary
!= null) {
228 this.vocabulary
.terms
.remove(this);
230 if (newVocabulary
!= null) {
232 Field fieldInitializing
= AbstractPersistentCollection
.class.getDeclaredField("initializing");
233 fieldInitializing
.setAccessible(true);
234 if (AbstractPersistentCollection
.class.isAssignableFrom(newVocabulary
.terms
.getClass())){
235 boolean initValue
= fieldInitializing
.getBoolean(newVocabulary
.terms
);
236 if (initValue
== false){
237 newVocabulary
.terms
.add(this);
242 newVocabulary
.terms
.add(this);
244 } catch (SecurityException e
) {
246 } catch (IllegalArgumentException e
) {
248 } catch (NoSuchFieldException e
) {
250 } catch (IllegalAccessException e
) {
254 this.vocabulary
= newVocabulary
;