Project

General

Profile

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

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

    
25
import javax.persistence.*;
26

    
27

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

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

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

    
66

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

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

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

    
112

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

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

    
139

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

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