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
;
13 import java
.util
.HashSet
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
17 import java
.util
.SortedSet
;
18 import java
.util
.TreeSet
;
19 import java
.util
.UUID
;
21 import javax
.persistence
.Entity
;
22 import javax
.persistence
.FetchType
;
23 import javax
.persistence
.Inheritance
;
24 import javax
.persistence
.InheritanceType
;
25 import javax
.persistence
.OneToMany
;
26 import javax
.persistence
.Transient
;
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
.XmlIDREF
;
32 import javax
.xml
.bind
.annotation
.XmlRootElement
;
33 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
34 import javax
.xml
.bind
.annotation
.XmlType
;
36 import org
.apache
.log4j
.Logger
;
37 import org
.hibernate
.annotations
.Cascade
;
38 import org
.hibernate
.annotations
.CascadeType
;
39 import org
.hibernate
.annotations
.Type
;
40 import org
.hibernate
.envers
.Audited
;
41 import org
.hibernate
.search
.annotations
.Field
;
42 import org
.hibernate
.search
.annotations
.Indexed
;
43 import org
.hibernate
.search
.annotations
.IndexedEmbedded
;
47 * A single enumeration must only contain DefinedTerm instances of one kind
48 * (this means a subclass of DefinedTerm).
51 * @created 08-Nov-2007 13:06:23
53 @XmlAccessorType(XmlAccessType
.FIELD
)
54 @XmlType(name
= "TermVocabulary", propOrder
= {
58 @XmlRootElement(name
= "TermVocabulary")
60 @Indexed(index
= "eu.etaxonomy.cdm.model.common.TermVocabulary")
62 @Inheritance(strategy
=InheritanceType
.SINGLE_TABLE
)
63 public class TermVocabulary
<T
extends DefinedTermBase
> extends TermBase
implements Iterable
<T
> {
64 private static final long serialVersionUID
= 1925052321596648672L;
65 @SuppressWarnings("unused")
66 private static final Logger logger
= Logger
.getLogger(TermVocabulary
.class);
68 //The vocabulary source (e.g. ontology) defining the terms to be loaded when a database is created for the first time.
69 // Software can go and grap these terms incl labels and description.
70 // UUID needed? Further vocs can be setup through our own ontology.
71 @XmlElement(name
= "TermSourceURI")
72 @Field(index
=org
.hibernate
.search
.annotations
.Index
.UN_TOKENIZED
)
73 private String termSourceUri
;
77 @XmlElementWrapper(name
= "Terms")
78 @XmlElement(name
= "Term")
80 @XmlSchemaType(name
= "IDREF")
81 @OneToMany(mappedBy
="vocabulary", fetch
=FetchType
.LAZY
, targetEntity
= DefinedTermBase
.class)
82 @Type(type
="DefinedTermBase")
83 @Cascade({CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
})
84 @IndexedEmbedded(depth
= 2)
85 protected Set
<T
> terms
= getNewTermSet();
87 // ********************************* FACTORY METHODS *****************************************/
89 public static TermVocabulary
NewInstance(String description
, String label
, String abbrev
, String termSourceUri
){
90 return new TermVocabulary(description
, label
, abbrev
, termSourceUri
);
93 // ************************* CONSTRUCTOR *************************************************
95 protected TermVocabulary() {
98 protected TermVocabulary(String term
, String label
, String labelAbbrev
, String termSourceUri
) {
99 super(term
, label
, labelAbbrev
);
100 setTermSourceUri(termSourceUri
);
104 // ******************* METHODS *************************************************/
106 public T
findTermByUuid(UUID uuid
){
108 if(t
.getUuid().equals(uuid
)) {
116 Set
<T
> getNewTermSet() {
117 return new HashSet
<T
>();
120 public Set
<T
> getTerms() {
124 public void addTerm(T term
) {
125 term
.setVocabulary(this);
126 this.terms
.add(term
);
128 public void removeTerm(T term
) {
129 this.terms
.remove(term
);
130 term
.setVocabulary(null);
133 public String
getTermSourceUri() {
134 return termSourceUri
;
136 public void setTermSourceUri(String vocabularyUri
) {
137 this.termSourceUri
= vocabularyUri
;
141 public Iterator
<T
> iterator() {
142 return terms
.iterator(); // OLD: new TermIterator<T>(this.terms);
151 * Returns all terms of this vocabulary sorted by their representation defined by the given language.
152 * If such an representation does not exist, the representation of the default language is testing instead for ordering.
156 public SortedSet
<T
> getTermsOrderedByLabels(Language language
){
157 TermLanguageComparator
<T
> comp
= new TermLanguageComparator
<T
>();
158 comp
.setCompareLanguage(language
);
160 SortedSet
<T
> result
= new TreeSet
<T
>(comp
);
161 result
.addAll(getTerms());
167 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
169 public TermVocabulary
<T
> readCsvLine(List
<String
> csvLine
) {
170 return readCsvLine(csvLine
, Language
.CSV_LANGUAGE());
173 public TermVocabulary
<T
> readCsvLine(List
<String
> csvLine
, Language lang
) {
174 this.setUuid(UUID
.fromString(csvLine
.get(0)));
175 this.setUri(csvLine
.get(1));
176 //this.addRepresentation(Representation.NewInstance(csvLine.get(3), csvLine.get(2).trim(), lang) );