Project

General

Profile

Download (7.49 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.term;
11

    
12
import java.net.URI;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16
import java.util.SortedSet;
17
import java.util.TreeSet;
18
import java.util.UUID;
19

    
20
import javax.persistence.Entity;
21
import javax.persistence.FetchType;
22
import javax.persistence.OneToMany;
23
import javax.xml.bind.annotation.XmlAccessType;
24
import javax.xml.bind.annotation.XmlAccessorType;
25
import javax.xml.bind.annotation.XmlElement;
26
import javax.xml.bind.annotation.XmlElementWrapper;
27
import javax.xml.bind.annotation.XmlIDREF;
28
import javax.xml.bind.annotation.XmlRootElement;
29
import javax.xml.bind.annotation.XmlSchemaType;
30
import javax.xml.bind.annotation.XmlType;
31

    
32
import org.apache.log4j.Logger;
33
import org.hibernate.annotations.Cascade;
34
import org.hibernate.annotations.CascadeType;
35
import org.hibernate.annotations.Type;
36
import org.hibernate.envers.Audited;
37
import org.hibernate.search.annotations.Analyze;
38
import org.hibernate.search.annotations.Field;
39
import org.hibernate.search.annotations.IndexedEmbedded;
40

    
41
import eu.etaxonomy.cdm.common.CdmUtils;
42
import eu.etaxonomy.cdm.model.common.ExternallyManaged;
43
import eu.etaxonomy.cdm.model.common.Language;
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
 * @since 08-Nov-2007 13:06:23
51
 */
52
@XmlAccessorType(XmlAccessType.FIELD)
53
@XmlType(name = "TermVocabulary", propOrder = {
54
    "termSourceUri",
55
    "terms",
56
    "externallyManaged"
57
})
58
@XmlRootElement(name = "TermVocabulary")
59
@Entity
60
//@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
61
//@Indexed(index = "eu.etaxonomy.cdm.model.term.TermVocabulary")
62
@Audited
63
public class TermVocabulary<T extends DefinedTermBase>
64
        extends TermCollection<T,TermNode> {
65

    
66
    private static final long serialVersionUID = 1925052321596648672L;
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
70
	//is created for the first time.
71
	// Software can go and grap these terms incl. labels and description.
72
	// UUID needed? Further vocs can be setup through our own ontology.
73
	@XmlElement(name = "TermSourceURI")
74
	@Type(type="uriUserType")
75
	@Field(analyze = Analyze.NO)
76
	private URI termSourceUri;
77

    
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 = newTermSet();
87

    
88
    private ExternallyManaged externallyManaged;
89

    
90
// ********************************* FACTORY METHODS *****************************************/
91

    
92

    
93
	public static TermVocabulary NewInstance(TermType type){
94
		return new TermVocabulary(type);
95
	}
96

    
97
	public static <T extends DefinedTermBase<T>> TermVocabulary<T> NewInstance(TermType type, Class<T> clazz){
98
		return new TermVocabulary<T>(type);
99
	}
100

    
101
	public static TermVocabulary NewInstance(TermType type, String description, String label, String abbrev, URI termSourceUri){
102
		return new TermVocabulary(type, description, label, abbrev, termSourceUri);
103
	}
104

    
105
// ************************* CONSTRUCTOR *************************************************
106

    
107
	//for hibernate use only
108
	@Deprecated
109
	protected TermVocabulary() {
110
		super(TermType.Unknown);
111
	}
112

    
113
	protected TermVocabulary(TermType type) {
114
		super(type);
115
	}
116

    
117
	protected TermVocabulary(TermType type, String term, String label, String labelAbbrev, URI termSourceUri) {
118
		super(type, term, label, labelAbbrev);
119
		setTermSourceUri(termSourceUri);
120
	}
121

    
122

    
123
	protected Set<T> newTermSet(){
124
	    return new HashSet<>();
125
	}
126

    
127
// ******************* METHODS *************************************************/
128

    
129
	public T findTermByUuid(UUID uuid){
130
		for(T t : terms) {
131
			if(t.getUuid().equals(uuid)) {
132
				return t;
133
			}
134
		}
135
		return null;
136
	}
137

    
138

    
139
	public Set<T> getTerms() {
140
		return terms;
141
	}
142

    
143
	public void addTerm(T term) {
144
	    checkTermType(term);
145
		term.setVocabulary(this);
146
		this.terms.add(term);
147
	}
148
	public void removeTerm(T term) {
149
		this.terms.remove(term);
150
		term.setVocabulary(null);
151
	}
152

    
153
	public URI getTermSourceUri() {
154
		return termSourceUri;
155
	}
156
	public void setTermSourceUri(URI vocabularyUri) {
157
		this.termSourceUri = vocabularyUri;
158
	}
159

    
160
    /**
161
     * Returns the first term found having the defined idInVocabulary.
162
     * If number of terms with given idInVoc > 1 the result is not deterministic.
163
     * @param idInVoc
164
     * @return the term with the given idInVoc
165
     */
166
    public T getTermByIdInvocabulary(String idInVoc) {
167
        for (T term : getTerms() ){
168
            if (CdmUtils.nullSafeEqual(idInVoc, term.getIdInVocabulary())){
169
                return term;
170
            }
171
        }
172
        return null;
173
    }
174

    
175
	public int size(){
176
		return terms.size();
177
	}
178

    
179
	/**
180
	 * Returns all terms of this vocabulary sorted by their representation defined by the given language.
181
	 * If such an representation does not exist, the representation of the default language is testing instead for ordering.
182
	 * @param language
183
	 * @return
184
	 */
185
	public SortedSet<T> getTermsOrderedByLabels(Language language){
186
		TermLanguageComparator<T> comp = new TermLanguageComparator<>();
187
		comp.setCompareLanguage(language);
188

    
189
		SortedSet<T> result = new TreeSet<>(comp);
190
		result.addAll(getTerms());
191
		return result;
192
	}
193

    
194

    
195
	public TermVocabulary<T> readCsvLine(List<String> csvLine) {
196
		return readCsvLine(csvLine, Language.CSV_LANGUAGE());
197
	}
198

    
199
	public TermVocabulary<T> readCsvLine(List<String> csvLine, Language lang) {
200
		this.setUuid(UUID.fromString(csvLine.get(0)));
201
        String uriStr = CdmUtils.Ne(csvLine.get(1));
202
        this.setUri(uriStr == null? null: URI.create(uriStr));
203
		String label = csvLine.get(2).trim();
204
		String description = csvLine.get(3);
205

    
206
		//see  http://dev.e-taxonomy.eu/trac/ticket/3550
207
		this.addRepresentation(Representation.NewInstance(description, label, null, lang) );
208

    
209
		TermType termType = TermType.getByKey(csvLine.get(4));
210
		if (termType == null){
211
			throw new IllegalArgumentException("TermType can not be mapped: " + csvLine.get(4));
212
		}
213
		this.setTermType(termType);
214

    
215
		return this;
216
	}
217

    
218
	/**
219
     * Throws {@link IllegalArgumentException} if the given
220
     * term has not the same term type as this term or if term type is null.
221
     * @param term
222
     */
223
    private void checkTermType(IHasTermType term) {
224
        IHasTermType.checkTermTypes(term, this);
225
    }
226

    
227
//*********************** CLONE ********************************************************/
228

    
229
	/**
230
	 * Clones <i>this</i> TermVocabulary. This is a shortcut that enables to create
231
	 * a new instance that differs only slightly from <i>this</i> TermVocabulary.
232
	 * The terms of the original vocabulary are cloned
233
	 *
234
	 * @see eu.etaxonomy.cdm.model.term.TermBase#clone()
235
	 * @see java.lang.Object#clone()
236
	 */
237
	@Override
238
	public Object clone() {
239
		TermVocabulary<T> result;
240
		try {
241
			result = (TermVocabulary<T>) super.clone();
242

    
243
		}catch (CloneNotSupportedException e) {
244
			logger.warn("Object does not implement cloneable");
245
			e.printStackTrace();
246
			return null;
247
		}
248
		result.terms = new HashSet<T>();
249
		for (T term: this.terms){
250
			result.addTerm((T)term.clone());
251
		}
252

    
253
		return result;
254
	}
255
}
(31-31/33)