Project

General

Profile

Download (6.76 KB) Statistics
| Branch: | Tag: | Revision:
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.net.URI;
14
import java.util.HashSet;
15
import java.util.Iterator;
16
import java.util.List;
17
import java.util.Set;
18
import java.util.SortedSet;
19
import java.util.TreeSet;
20
import java.util.UUID;
21

    
22
import javax.persistence.Entity;
23
import javax.persistence.FetchType;
24
import javax.persistence.Inheritance;
25
import javax.persistence.InheritanceType;
26
import javax.persistence.OneToMany;
27
import javax.persistence.Transient;
28
import javax.xml.bind.annotation.XmlAccessType;
29
import javax.xml.bind.annotation.XmlAccessorType;
30
import javax.xml.bind.annotation.XmlElement;
31
import javax.xml.bind.annotation.XmlElementWrapper;
32
import javax.xml.bind.annotation.XmlIDREF;
33
import javax.xml.bind.annotation.XmlRootElement;
34
import javax.xml.bind.annotation.XmlSchemaType;
35
import javax.xml.bind.annotation.XmlType;
36

    
37
import org.apache.log4j.Logger;
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.Analyze;
43
import org.hibernate.search.annotations.Field;
44
import org.hibernate.search.annotations.Indexed;
45
import org.hibernate.search.annotations.IndexedEmbedded;
46

    
47

    
48
/**
49
 * A single enumeration must only contain DefinedTerm instances of one kind
50
 * (this means a subclass of DefinedTerm).
51
 * @author m.doering
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
	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
	@Type(type="uriUserType")
73
	@Field(analyze = Analyze.NO)
74
	private URI 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
// ********************************* FACTORY METHODS *****************************************/
89

    
90

    
91
	public static TermVocabulary NewInstance(TermType type){
92
		return new TermVocabulary(type);
93
	}
94
	
95
	public static TermVocabulary NewInstance(TermType type, String description, String label, String abbrev, URI termSourceUri){
96
		return new TermVocabulary(type, description, label, abbrev, termSourceUri);
97
	}
98

    
99
// ************************* CONSTRUCTOR *************************************************
100

    
101
	//for hibernate use only
102
	@Deprecated
103
	protected TermVocabulary() {
104
		super(TermType.Unknown);
105
	}
106

    
107
	protected TermVocabulary(TermType type) {
108
		super(type);
109
	}
110
	
111
	protected TermVocabulary(TermType type, String term, String label, String labelAbbrev, URI termSourceUri) {
112
		super(type, term, label, labelAbbrev);
113
		setTermSourceUri(termSourceUri);
114
	}
115

    
116

    
117
// ******************* METHODS *************************************************/
118

    
119
	public T findTermByUuid(UUID uuid){
120
		for(T t : terms) {
121
			if(t.getUuid().equals(uuid)) {
122
				return t;
123
			}
124
		}
125
		return null;
126
	}
127

    
128
	@Transient
129
	Set<T> getNewTermSet() {
130
		return new HashSet<T>();
131
	}
132

    
133
	public Set<T> getTerms() {
134
		return terms;
135
	}
136

    
137
	public void addTerm(T term) {
138
		term.setVocabulary(this);
139
		this.terms.add(term);
140
	}
141
	public void removeTerm(T term) {
142
		this.terms.remove(term);
143
		term.setVocabulary(null);
144
	}
145

    
146
	public URI getTermSourceUri() {
147
		return termSourceUri;
148
	}
149
	public void setTermSourceUri(URI vocabularyUri) {
150
		this.termSourceUri = vocabularyUri;
151
	}
152

    
153

    
154
	@Override
155
    public Iterator<T> iterator() {
156
		return terms.iterator();  // OLD: new TermIterator<T>(this.terms);
157
	}
158

    
159
	public int size(){
160
		return terms.size();
161
	}
162

    
163

    
164
	/**
165
	 * Returns all terms of this vocabulary sorted by their representation defined by the given language.
166
	 * If such an representation does not exist, the representation of the default language is testing instead for ordering.
167
	 * @param language
168
	 * @return
169
	 */
170
	public SortedSet<T> getTermsOrderedByLabels(Language language){
171
		TermLanguageComparator<T> comp = new TermLanguageComparator<T>();
172
		comp.setCompareLanguage(language);
173

    
174
		SortedSet<T> result = new TreeSet<T>(comp);
175
		result.addAll(getTerms());
176
		return result;
177
	}
178

    
179

    
180
	/* (non-Javadoc)
181
	 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
182
	 */
183
	public TermVocabulary<T> readCsvLine(List<String> csvLine) {
184
		return readCsvLine(csvLine, Language.CSV_LANGUAGE());
185
	}
186

    
187
	public TermVocabulary<T> readCsvLine(List<String> csvLine, Language lang) {
188
		this.setUuid(UUID.fromString(csvLine.get(0)));
189
		this.setUri(URI.create(csvLine.get(1)));
190
		String label = csvLine.get(2).trim();
191
		String description = csvLine.get(3);
192
		
193
		//see  http://dev.e-taxonomy.eu/trac/ticket/3550
194
		this.addRepresentation(Representation.NewInstance(description, label, null, lang) );
195
		//preliminary until above is solved
196
//		this.setTitleCache(label, true);
197
		
198
		TermType termType = TermType.getByKey(csvLine.get(4));
199
		if (termType == null){
200
			throw new IllegalArgumentException("TermType can not be mapped: " + csvLine.get(4));
201
		}
202
		this.setTermType(termType);
203
		
204
		return this;
205
	}
206

    
207
//*********************** CLONE ********************************************************/
208

    
209
	/**
210
	 * Clones <i>this</i> TermVocabulary. This is a shortcut that enables to create
211
	 * a new instance that differs only slightly from <i>this</i> TermVocabulary.
212
	 * The terms of the original vocabulary are cloned
213
	 *
214
	 * @see eu.etaxonomy.cdm.model.common.TermBase#clone()
215
	 * @see java.lang.Object#clone()
216
	 */
217
	@Override
218
	public Object clone() {
219
		TermVocabulary<T> result;
220
		try {
221
			result = (TermVocabulary<T>) super.clone();
222

    
223
		}catch (CloneNotSupportedException e) {
224
			logger.warn("Object does not implement cloneable");
225
			e.printStackTrace();
226
			return null;
227
		}
228
		result.terms = new HashSet<T>();
229
		for (T term: this.terms){
230
			result.addTerm((T)term.clone());
231
		}
232

    
233
		return result;
234
	}
235

    
236
}
(61-61/69)