Project

General

Profile

Download (6.34 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

    
21
import java.lang.reflect.Field;
22
import java.util.*;
23

    
24
import javax.persistence.*;
25

    
26

    
27
/**
28
 * workaround for enumerations, base type according to TDWG.  For linear ordering
29
 * use partOf relation and BreadthFirst. Default iterator order should therefore
30
 * be BreadthFirst (not DepthFirst)
31
 * @author m.doering
32
 * @version 1.0
33
 * @created 08-Nov-2007 13:06:19
34
 */
35
@Entity
36
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
37
public abstract class DefinedTermBase<T extends DefinedTermBase> extends TermBase implements ILoadableTerm{
38
	static Logger logger = Logger.getLogger(DefinedTermBase.class);
39
	
40
	static protected IVocabularyStore vocabularyStore = new DefaultVocabularyStore();
41

    
42
	public static void setVocabularyStore(IVocabularyStore vocabularyStore){
43
		DefinedTermBase.vocabularyStore = vocabularyStore;
44
	}
45
	
46
	private DefinedTermBase kindOf;
47
	private Set<DefinedTermBase> generalizationOf = new HashSet<DefinedTermBase>();
48
	private DefinedTermBase partOf;
49
	private Set<DefinedTermBase> includes = new HashSet<DefinedTermBase>();
50
	private Set<Media> media = new HashSet<Media>();
51
	protected TermVocabulary<T> vocabulary;
52
	
53

    
54
	public static DefinedTermBase findByUuid(UUID uuid){
55
		return vocabularyStore.getTermByUuid(uuid);
56
	}
57
	
58
	public DefinedTermBase() {
59
		super();
60
	}
61
	public DefinedTermBase(String term, String label) {
62
		super(term, label);
63
	}
64

    
65

    
66
	/* (non-Javadoc)
67
	 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
68
	 */
69
	public ILoadableTerm readCsvLine(List<String> csvLine) {
70
		return readCsvLine(csvLine, Language.ENGLISH());
71
	}
72
	public ILoadableTerm readCsvLine(List<String> csvLine, Language lang) {
73
		this.setUuid(UUID.fromString(csvLine.get(0)));
74
		this.setUri(csvLine.get(1));
75
		this.addRepresentation(Representation.NewInstance(csvLine.get(3), csvLine.get(2).trim(), lang) );
76
		return this;
77
	}
78

    
79
	/* (non-Javadoc)
80
	 * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#writeCsvLine(au.com.bytecode.opencsv.CSVWriter)
81
	 */
82
	public void writeCsvLine(CSVWriter writer) {
83
		String [] line = new String[4];
84
		line[0] = getUuid().toString();
85
		line[1] = getUri();
86
		line[2] = getLabel();
87
		line[3] = getDescription();
88
		writer.writeNext(line);
89
	}
90
	
91
	@Transient
92
	//@ManyToOne
93
	//@Cascade({CascadeType.SAVE_UPDATE})
94
	public DefinedTermBase getKindOf(){
95
		return this.kindOf;
96
	}
97
	public void setKindOf(DefinedTermBase kindOf){
98
		this.kindOf = kindOf;
99
	}
100

    
101
	@Transient
102
	//@OneToMany(fetch=FetchType.LAZY)
103
	//@Cascade({CascadeType.SAVE_UPDATE})
104
	public Set<DefinedTermBase> getGeneralizationOf(){
105
		return this.generalizationOf;
106
	}
107
	public void setGeneralizationOf(Set<DefinedTermBase> generalizationOf) {
108
		this.generalizationOf = generalizationOf;
109
	}
110

    
111

    
112
	@Transient
113
	//@ManyToOne
114
	//@Cascade({CascadeType.SAVE_UPDATE})
115
	public DefinedTermBase getPartOf(){
116
		return this.partOf;
117
	}
118
	public void setPartOf(DefinedTermBase partOf){
119
		this.partOf = partOf;
120
	}
121

    
122
	@Transient
123
	//@OneToMany(fetch=FetchType.LAZY)
124
	//@Cascade({CascadeType.SAVE_UPDATE})
125
	public Set<DefinedTermBase> getIncludes(){
126
		return this.includes;
127
	}
128
	public void setIncludes(Set<DefinedTermBase> includes) {
129
		this.includes = includes;
130
	}
131
	public void addIncludes(DefinedTermBase includes) {
132
		this.includes.add(includes);
133
	}
134
	public void removeIncludes(TermBase includes) {
135
		this.includes.remove(includes);
136
	}
137

    
138

    
139
	@OneToMany
140
	@Cascade({CascadeType.SAVE_UPDATE})
141
	public Set<Media> getMedia(){
142
		return this.media;
143
	}
144
	public void setMedia(Set<Media> media) {
145
		this.media = media;
146
	}
147
	public void addMedia(Media media) {
148
		this.media.add(media);
149
	}
150
	public void removeMedia(Media media) {
151
		this.media.remove(media);
152
	}
153

    
154
	/* (non-Javadoc)
155
	 * @see eu.etaxonomy.cdm.model.common.IDefTerm#getVocabulary()
156
	 */
157
	@Transient
158
	public TermVocabulary getVocabulary() {
159
		return this.vocabulary;
160
	}
161
	/* (non-Javadoc)
162
	 * @see eu.etaxonomy.cdm.model.common.IDefTerm#setVocabulary(eu.etaxonomy.cdm.model.common.TermVocabulary)
163
	 */
164
	public void setVocabulary(TermVocabulary newVocabulary) {
165
		// Hibernate bidirectional cascade hack: 
166
		// http://opensource.atlassian.com/projects/hibernate/browse/HHH-1054
167
		if(this.vocabulary == newVocabulary){ return;}
168
		if (this.vocabulary != null) { 
169
			this.vocabulary.terms.remove(this);
170
		}
171
		if (newVocabulary!= null) { 
172
			newVocabulary.terms.add(this);
173
		}
174
		this.vocabulary = newVocabulary;		
175
	}
176
	
177
	
178
	/* (non-Javadoc)
179
	 * @see eu.etaxonomy.cdm.model.common.IDefTerm#getVocabulary()
180
	 */
181
	@ManyToOne(fetch=FetchType.EAGER)
182
	@Cascade( { CascadeType.SAVE_UPDATE })
183
	protected TermVocabulary getPersistentVocabulary() {
184
		return this.vocabulary;
185
	}
186
	/* (non-Javadoc)
187
	 * @see eu.etaxonomy.cdm.model.common.IDefTerm#setVocabulary(eu.etaxonomy.cdm.model.common.TermVocabulary)
188
	 */
189
	protected void setPersistentVocabulary(TermVocabulary newVocabulary) {
190
		// Hibernate bidirectional cascade hack: 
191
		// http://opensource.atlassian.com/projects/hibernate/browse/HHH-1054
192
		if(this.vocabulary == newVocabulary){ return;}
193
		if (this.vocabulary != null) { 
194
			this.vocabulary.terms.remove(this);
195
		}
196
		if (newVocabulary!= null) { 
197
			try {
198
				Field fieldInitializing = AbstractPersistentCollection.class.getDeclaredField("initializing");
199
				fieldInitializing.setAccessible(true);
200
				if (AbstractPersistentCollection.class.isAssignableFrom(newVocabulary.terms.getClass())){
201
					boolean initValue = fieldInitializing.getBoolean(newVocabulary.terms);
202
					if (initValue == false){
203
						newVocabulary.terms.add(this);
204
					}else{
205
						//nothing
206
					}
207
				}else{
208
					newVocabulary.terms.add(this);
209
				}
210
			} catch (SecurityException e) {
211
				e.printStackTrace();
212
			} catch (IllegalArgumentException e) {
213
				e.printStackTrace();
214
			} catch (NoSuchFieldException e) {
215
				e.printStackTrace();
216
			} catch (IllegalAccessException e) {
217
				e.printStackTrace();
218
			}
219
		}
220
		this.vocabulary = newVocabulary;		
221
	}
222
	
223
}
(5-5/46)