Project

General

Profile

Download (7.35 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
import org.apache.log4j.Logger;
13
import org.hibernate.annotations.Cascade;
14
import org.hibernate.annotations.CascadeType;
15
import org.hibernate.collection.AbstractPersistentCollection;
16

    
17
import au.com.bytecode.opencsv.CSVWriter;
18
import eu.etaxonomy.cdm.model.common.init.DefaultVocabularyStore;
19
import eu.etaxonomy.cdm.model.common.init.IVocabularyStore;
20
import eu.etaxonomy.cdm.model.media.Media;
21
import java.lang.reflect.Field;
22
import java.util.*;
23

    
24
import javax.persistence.*;
25
import javax.xml.bind.annotation.XmlAccessType;
26
import javax.xml.bind.annotation.XmlAccessorType;
27
import javax.xml.bind.annotation.XmlElement;
28
import javax.xml.bind.annotation.XmlElementWrapper;
29
import javax.xml.bind.annotation.XmlRootElement;
30
import javax.xml.bind.annotation.XmlTransient;
31
import javax.xml.bind.annotation.XmlType;
32

    
33

    
34
/**
35
 * walkaround for enumerations, base type according to TDWG.  For linear ordering
36
 * use partOf relation and BreadthFirst. Default iterator order should therefore
37
 * be BreadthFirst (not DepthFirst)
38
 * @author m.doering
39
 * @version 1.0
40
 * @created 08-Nov-2007 13:06:19
41
 */
42
@XmlAccessorType(XmlAccessType.FIELD)
43
@XmlType(name = "DefinedTermBase", propOrder = {
44
    "kindOf",
45
    "generalizationOf",
46
    "partOf",
47
    "includes",
48
    "media",
49
    "vocabulary"
50
})
51
@XmlRootElement(name = "DefinedTermBase")
52
@Entity
53
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
54
public abstract class DefinedTermBase<T extends DefinedTermBase> extends TermBase implements ILoadableTerm{
55
	static Logger logger = Logger.getLogger(DefinedTermBase.class);
56
	
57
	static protected IVocabularyStore vocabularyStore = new DefaultVocabularyStore();
58

    
59
	public static void setVocabularyStore(IVocabularyStore vocabularyStore){
60
		DefinedTermBase.vocabularyStore = vocabularyStore;
61
	}
62
	
63
	@XmlElement(name = "KindOf")
64
	private DefinedTermBase kindOf;
65
	
66
	@XmlElement(name = "GeneralizationOf")
67
	private Set<DefinedTermBase> generalizationOf = new HashSet<DefinedTermBase>();
68
	
69
	@XmlElement(name = "PartOf")
70
	private DefinedTermBase partOf;
71
	
72
	@XmlElementWrapper(name = "Includes")
73
	@XmlElement(name = "Include")
74
	private Set<DefinedTermBase> includes = new HashSet<DefinedTermBase>();
75
	
76
	@XmlElementWrapper(name = "Media")
77
	@XmlElement(name = "Medium")
78
	private Set<Media> media = new HashSet<Media>();
79
	
80
	@XmlElement(name = "Vocabulary")
81
	protected TermVocabulary<T> vocabulary;
82
	
83

    
84
	public static DefinedTermBase findByUuid(UUID uuid){
85
		return vocabularyStore.getTermByUuid(uuid);
86
	}
87
	
88
	public DefinedTermBase() {
89
		super();
90
	}
91
	public DefinedTermBase(String term, String label, String labelAbbrev) {
92
		super(term, label, labelAbbrev);
93
	}
94

    
95

    
96
	/* (non-Javadoc)
97
	 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
98
	 */
99
	public ILoadableTerm readCsvLine(List<String> csvLine) {
100
		return readCsvLine(csvLine, Language.ENGLISH());
101
	}
102
	public ILoadableTerm readCsvLine(List<String> csvLine, Language lang) {
103
		this.setUuid(UUID.fromString(csvLine.get(0)));
104
		this.setUri(csvLine.get(1));
105
		String label = csvLine.get(2).trim();
106
		String text = csvLine.get(3);
107
		String abbreviatedLabel = null;
108
		this.addRepresentation(Representation.NewInstance(text, label, abbreviatedLabel, lang) );
109
		return this;
110
	}
111

    
112
	/* (non-Javadoc)
113
	 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#writeCsvLine(au.com.bytecode.opencsv.CSVWriter)
114
	 */
115
	public void writeCsvLine(CSVWriter writer) {
116
		String [] line = new String[4];
117
		line[0] = getUuid().toString();
118
		line[1] = getUri();
119
		line[2] = getLabel();
120
		line[3] = getDescription();
121
		writer.writeNext(line);
122
	}
123
	
124
	@Transient
125
	//@ManyToOne
126
	//@Cascade({CascadeType.SAVE_UPDATE})
127
	public DefinedTermBase getKindOf(){
128
		return this.kindOf;
129
	}
130
	public void setKindOf(DefinedTermBase kindOf){
131
		this.kindOf = kindOf;
132
	}
133

    
134
	@Transient
135
	//@OneToMany(fetch=FetchType.LAZY)
136
	//@Cascade({CascadeType.SAVE_UPDATE})
137
	public Set<DefinedTermBase> getGeneralizationOf(){
138
		return this.generalizationOf;
139
	}
140
	public void setGeneralizationOf(Set<DefinedTermBase> generalizationOf) {
141
		this.generalizationOf = generalizationOf;
142
	}
143

    
144

    
145
	@Transient
146
	//@ManyToOne
147
	//@Cascade({CascadeType.SAVE_UPDATE})
148
	public DefinedTermBase getPartOf(){
149
		return this.partOf;
150
	}
151
	public void setPartOf(DefinedTermBase partOf){
152
		this.partOf = partOf;
153
	}
154

    
155
	@Transient
156
	//@OneToMany(fetch=FetchType.LAZY)
157
	//@Cascade({CascadeType.SAVE_UPDATE})
158
	public Set<DefinedTermBase> getIncludes(){
159
		return this.includes;
160
	}
161
	public void setIncludes(Set<DefinedTermBase> includes) {
162
		this.includes = includes;
163
	}
164
	public void addIncludes(DefinedTermBase includes) {
165
		this.includes.add(includes);
166
	}
167
	public void removeIncludes(TermBase includes) {
168
		this.includes.remove(includes);
169
	}
170

    
171

    
172
	@OneToMany
173
	@Cascade({CascadeType.SAVE_UPDATE})
174
	public Set<Media> getMedia(){
175
		return this.media;
176
	}
177
	public void setMedia(Set<Media> media) {
178
		this.media = media;
179
	}
180
	public void addMedia(Media media) {
181
		this.media.add(media);
182
	}
183
	public void removeMedia(Media media) {
184
		this.media.remove(media);
185
	}
186

    
187
	/* (non-Javadoc)
188
	 * @see eu.etaxonomy.cdm.model.common.IDefTerm#getVocabulary()
189
	 */
190
	@Transient
191
	@XmlTransient
192
	public TermVocabulary getVocabulary() {
193
		return this.vocabulary;
194
	}
195
	/* (non-Javadoc)
196
	 * @see eu.etaxonomy.cdm.model.common.IDefTerm#setVocabulary(eu.etaxonomy.cdm.model.common.TermVocabulary)
197
	 */
198
	public void setVocabulary(TermVocabulary newVocabulary) {
199
		// Hibernate bidirectional cascade hack: 
200
		// http://opensource.atlassian.com/projects/hibernate/browse/HHH-1054
201
		if(this.vocabulary == newVocabulary){ return;}
202
		if (this.vocabulary != null) { 
203
			this.vocabulary.terms.remove(this);
204
		}
205
		if (newVocabulary!= null) { 
206
			newVocabulary.terms.add(this);
207
		}
208
		this.vocabulary = newVocabulary;		
209
	}
210
	
211
	
212
	/* (non-Javadoc)
213
	 * @see eu.etaxonomy.cdm.model.common.IDefTerm#getVocabulary()
214
	 */
215
	@ManyToOne(fetch=FetchType.LAZY)
216
	@Cascade( { CascadeType.SAVE_UPDATE })
217
	protected TermVocabulary getPersistentVocabulary() {
218
		return this.vocabulary;
219
	}
220
	/* (non-Javadoc)
221
	 * @see eu.etaxonomy.cdm.model.common.IDefTerm#setVocabulary(eu.etaxonomy.cdm.model.common.TermVocabulary)
222
	 */
223
	protected void setPersistentVocabulary(TermVocabulary newVocabulary) {
224
		// Hibernate bidirectional cascade hack: 
225
		// http://opensource.atlassian.com/projects/hibernate/browse/HHH-1054
226
		if(this.vocabulary == newVocabulary){ return;}
227
		if (this.vocabulary != null) { 
228
			this.vocabulary.terms.remove(this);
229
		}
230
		if (newVocabulary!= null) { 
231
			try {
232
				Field fieldInitializing = AbstractPersistentCollection.class.getDeclaredField("initializing");
233
				fieldInitializing.setAccessible(true);
234
				if (AbstractPersistentCollection.class.isAssignableFrom(newVocabulary.terms.getClass())){
235
					boolean initValue = fieldInitializing.getBoolean(newVocabulary.terms);
236
					if (initValue == false){
237
						newVocabulary.terms.add(this);
238
					}else{
239
						//nothing
240
					}
241
				}else{
242
					newVocabulary.terms.add(this);
243
				}
244
			} catch (SecurityException e) {
245
				e.printStackTrace();
246
			} catch (IllegalArgumentException e) {
247
				e.printStackTrace();
248
			} catch (NoSuchFieldException e) {
249
				e.printStackTrace();
250
			} catch (IllegalAccessException e) {
251
				e.printStackTrace();
252
			}
253
		}
254
		this.vocabulary = newVocabulary;		
255
	}
256
	
257
}
(4-4/41)