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
.apache
.lucene
.document
.Field
.Index
;
38 import org
.hibernate
.annotations
.Cascade
;
39 import org
.hibernate
.annotations
.CascadeType
;
40 import org
.hibernate
.annotations
.Type
;
41 import org
.hibernate
.envers
.Audited
;
42 import org
.hibernate
.search
.annotations
.Field
;
43 import org
.hibernate
.search
.annotations
.Indexed
;
44 import org
.hibernate
.search
.annotations
.IndexedEmbedded
;
48 * A single enumeration must only contain DefinedTerm instances of one kind
49 * (this means a subclass of DefinedTerm).
52 * @created 08-Nov-2007 13:06:23
54 @XmlAccessorType(XmlAccessType
.FIELD
)
55 @XmlType(name
= "TermVocabulary", propOrder
= {
59 @XmlRootElement(name
= "TermVocabulary")
61 @Indexed(index
= "eu.etaxonomy.cdm.model.common.TermVocabulary")
63 @Inheritance(strategy
=InheritanceType
.SINGLE_TABLE
)
64 public class TermVocabulary
<T
extends DefinedTermBase
> extends TermBase
implements Iterable
<T
> {
65 private static final long serialVersionUID
= 1925052321596648672L;
66 @SuppressWarnings("unused")
67 private static final Logger logger
= Logger
.getLogger(TermVocabulary
.class);
69 //The vocabulary source (e.g. ontology) defining the terms to be loaded when a database is created for the first time.
70 // Software can go and grap these terms incl labels and description.
71 // UUID needed? Further vocs can be setup through our own ontology.
72 @XmlElement(name
= "TermSourceURI")
73 @Field(index
=org
.hibernate
.search
.annotations
.Index
.UN_TOKENIZED
)
74 private String termSourceUri
;
78 @XmlElementWrapper(name
= "Terms")
79 @XmlElement(name
= "Term")
81 @XmlSchemaType(name
= "IDREF")
82 @OneToMany(mappedBy
="vocabulary", fetch
=FetchType
.LAZY
, targetEntity
= DefinedTermBase
.class)
83 @Type(type
="DefinedTermBase")
84 @Cascade({CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
})
85 @IndexedEmbedded(depth
= 2)
86 protected Set
<T
> terms
= getNewTermSet();
88 public T
findTermByUuid(UUID uuid
){
90 if(t
.getUuid().equals(uuid
)) {
97 public TermVocabulary(String term
, String label
, String labelAbbrev
, String termSourceUri
) {
98 super(term
, label
, labelAbbrev
);
99 setTermSourceUri(termSourceUri
);
102 public TermVocabulary() {
103 // TODO Auto-generated constructor stub
107 Set
<T
> getNewTermSet() {
108 return new HashSet
<T
>();
111 public Set
<T
> getTerms() {
115 public void addTerm(T term
) {
116 term
.setVocabulary(this);
117 this.terms
.add(term
);
119 public void removeTerm(T term
) {
120 term
.setVocabulary(null);
121 this.terms
.remove(term
);
124 public String
getTermSourceUri() {
125 return termSourceUri
;
127 public void setTermSourceUri(String vocabularyUri
) {
128 this.termSourceUri
= vocabularyUri
;
132 // // inner iterator class for the iterable interface
133 // private class TermIterator<T> implements Iterator<T> {
134 // // FIXME: using a list here is probably not safe. Sth passed by value, an array, would be better
135 // // but arrays cause generics problems: http://forum.java.sun.com/thread.jspa?threadID=651276&messageID=3832182
136 // // hack for now ;(
137 // private Set<T> array;
140 // public TermIterator(Set<T> array) {
141 // // check for null being passed in etc.
142 // this.array= array;
144 // // interface implementation
145 // public boolean hasNext() { return i < array.size(); }
146 // public T next() { return array.get(i++); }
147 // public void remove() { throw new UnsupportedOperationException(); }
150 public Iterator
<T
> iterator() {
151 return terms
.iterator(); // OLD: new TermIterator<T>(this.terms);
160 * Returns all terms of this vocabulary sorted by their representation defined by the given language.
161 * If such an representation does not exist, the representation of the default language is testing instead for ordering.
165 public SortedSet
<T
> getTermsOrderedByLabels(Language language
){
166 TermLanguageComparator
<T
> comp
= new TermLanguageComparator
<T
>();
167 comp
.setCompareLanguage(language
);
169 SortedSet
<T
> result
= new TreeSet
<T
>(comp
);
170 result
.addAll(getTerms());
176 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
178 public TermVocabulary
<T
> readCsvLine(List
<String
> csvLine
) {
179 return readCsvLine(csvLine
, Language
.CSV_LANGUAGE());
182 public TermVocabulary
<T
> readCsvLine(List
<String
> csvLine
, Language lang
) {
183 this.setUuid(UUID
.fromString(csvLine
.get(0)));
184 this.setUri(csvLine
.get(1));
185 //this.addRepresentation(Representation.NewInstance(csvLine.get(3), csvLine.get(2).trim(), lang) );