eb6a5c3cf2365dd613815b1a11e96bcdaebfd955
[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.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;
44
45
46 /**
47 * A single enumeration must only contain DefinedTerm instances of one kind
48 * (this means a subclass of DefinedTerm).
49 * @author m.doering
50 * @version 1.0
51 * @created 08-Nov-2007 13:06:23
52 */
53 @XmlAccessorType(XmlAccessType.FIELD)
54 @XmlType(name = "TermVocabulary", propOrder = {
55 "termSourceUri",
56 "terms"
57 })
58 @XmlRootElement(name = "TermVocabulary")
59 @Entity
60 @Indexed(index = "eu.etaxonomy.cdm.model.common.TermVocabulary")
61 @Audited
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);
67
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;
74
75
76 //TODO Changed
77 @XmlElementWrapper(name = "Terms")
78 @XmlElement(name = "Term")
79 @XmlIDREF
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();
86
87 // ********************************* FACTORY METHODS *****************************************/
88
89 public static TermVocabulary NewInstance(String description, String label, String abbrev, String termSourceUri){
90 return new TermVocabulary(description, label, abbrev, termSourceUri);
91 }
92
93 // ************************* CONSTRUCTOR *************************************************
94
95 protected TermVocabulary() {
96 }
97
98 protected TermVocabulary(String term, String label, String labelAbbrev, String termSourceUri) {
99 super(term, label, labelAbbrev);
100 setTermSourceUri(termSourceUri);
101 }
102
103
104 // ******************* METHODS *************************************************/
105
106 public T findTermByUuid(UUID uuid){
107 for(T t : terms) {
108 if(t.getUuid().equals(uuid)) {
109 return t;
110 }
111 }
112 return null;
113 }
114
115 @Transient
116 Set<T> getNewTermSet() {
117 return new HashSet<T>();
118 }
119
120 public Set<T> getTerms() {
121 return terms;
122 }
123
124 public void addTerm(T term) {
125 term.setVocabulary(this);
126 this.terms.add(term);
127 }
128 public void removeTerm(T term) {
129 this.terms.remove(term);
130 term.setVocabulary(null);
131 }
132
133 public String getTermSourceUri() {
134 return termSourceUri;
135 }
136 public void setTermSourceUri(String vocabularyUri) {
137 this.termSourceUri = vocabularyUri;
138 }
139
140
141 public Iterator<T> iterator() {
142 return terms.iterator(); // OLD: new TermIterator<T>(this.terms);
143 }
144
145 public int size(){
146 return terms.size();
147 }
148
149
150 /**
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.
153 * @param language
154 * @return
155 */
156 public SortedSet<T> getTermsOrderedByLabels(Language language){
157 TermLanguageComparator<T> comp = new TermLanguageComparator<T>();
158 comp.setCompareLanguage(language);
159
160 SortedSet<T> result = new TreeSet<T>(comp);
161 result.addAll(getTerms());
162 return result;
163 }
164
165
166 /* (non-Javadoc)
167 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
168 */
169 public TermVocabulary<T> readCsvLine(List<String> csvLine) {
170 return readCsvLine(csvLine, Language.CSV_LANGUAGE());
171 }
172
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) );
177 return this;
178 }
179
180 }