(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / TermVocabulary.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
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.SortedSet;
18 import java.util.TreeSet;
19 import java.util.UUID;
20
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;
35
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;
45
46
47 /**
48 * A single enumeration must only contain DefinedTerm instances of one kind
49 * (this means a subclass of DefinedTerm).
50 * @author m.doering
51 * @version 1.0
52 * @created 08-Nov-2007 13:06:23
53 */
54 @XmlAccessorType(XmlAccessType.FIELD)
55 @XmlType(name = "TermVocabulary", propOrder = {
56 "termSourceUri",
57 "terms"
58 })
59 @XmlRootElement(name = "TermVocabulary")
60 @Entity
61 @Indexed(index = "eu.etaxonomy.cdm.model.common.TermVocabulary")
62 @Audited
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);
68
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;
75
76
77 //TODO Changed
78 @XmlElementWrapper(name = "Terms")
79 @XmlElement(name = "Term")
80 @XmlIDREF
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();
87
88 public T findTermByUuid(UUID uuid){
89 for(T t : terms) {
90 if(t.getUuid().equals(uuid)) {
91 return t;
92 }
93 }
94 return null;
95 }
96
97 public TermVocabulary(String term, String label, String labelAbbrev, String termSourceUri) {
98 super(term, label, labelAbbrev);
99 setTermSourceUri(termSourceUri);
100 }
101
102 public TermVocabulary() {
103 // TODO Auto-generated constructor stub
104 }
105
106 @Transient
107 Set<T> getNewTermSet() {
108 return new HashSet<T>();
109 }
110
111 public Set<T> getTerms() {
112 return terms;
113 }
114
115 public void addTerm(T term) {
116 term.setVocabulary(this);
117 this.terms.add(term);
118 }
119 public void removeTerm(T term) {
120 term.setVocabulary(null);
121 this.terms.remove(term);
122 }
123
124 public String getTermSourceUri() {
125 return termSourceUri;
126 }
127 public void setTermSourceUri(String vocabularyUri) {
128 this.termSourceUri = vocabularyUri;
129 }
130
131
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;
138 // private int i= 0;
139 // // ctor
140 // public TermIterator(Set<T> array) {
141 // // check for null being passed in etc.
142 // this.array= array;
143 // }
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(); }
148 // }
149
150 public Iterator<T> iterator() {
151 return terms.iterator(); // OLD: new TermIterator<T>(this.terms);
152 }
153
154 public int size(){
155 return terms.size();
156 }
157
158
159 /**
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.
162 * @param language
163 * @return
164 */
165 public SortedSet<T> getTermsOrderedByLabels(Language language){
166 TermLanguageComparator<T> comp = new TermLanguageComparator<T>();
167 comp.setCompareLanguage(language);
168
169 SortedSet<T> result = new TreeSet<T>(comp);
170 result.addAll(getTerms());
171 return result;
172 }
173
174
175 /* (non-Javadoc)
176 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
177 */
178 public TermVocabulary<T> readCsvLine(List<String> csvLine) {
179 return readCsvLine(csvLine, Language.CSV_LANGUAGE());
180 }
181
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) );
186 return this;
187 }
188
189 }