1 |
9479da48
|
Andreas Müller
|
/**
|
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 |
95e756cb
|
Andreas Müller
|
import org.hibernate.collection.AbstractPersistentCollection;
|
16 |
9479da48
|
Andreas Müller
|
|
17 |
|
|
import au.com.bytecode.opencsv.CSVWriter;
|
18 |
fbd23d1d
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.common.init.DefaultVocabularyStore;
|
19 |
|
|
import eu.etaxonomy.cdm.model.common.init.IVocabularyStore;
|
20 |
ca21f846
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.media.Media;
|
21 |
95e756cb
|
Andreas Müller
|
|
22 |
|
|
import java.lang.reflect.Field;
|
23 |
9479da48
|
Andreas Müller
|
import java.util.*;
|
24 |
|
|
|
25 |
|
|
import javax.persistence.*;
|
26 |
|
|
|
27 |
fbd23d1d
|
Andreas Müller
|
|
28 |
9479da48
|
Andreas Müller
|
/**
|
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 |
8bc5f53a
|
Andreas Müller
|
public abstract class DefinedTermBase<T extends DefinedTermBase> extends TermBase implements ILoadableTerm{
|
39 |
9479da48
|
Andreas Müller
|
static Logger logger = Logger.getLogger(DefinedTermBase.class);
|
40 |
fbd23d1d
|
Andreas Müller
|
|
41 |
|
|
static protected IVocabularyStore vocabularyStore = new DefaultVocabularyStore();
|
42 |
9479da48
|
Andreas Müller
|
|
43 |
c72afb6c
|
Andreas Müller
|
public static void setVocabularyStore(IVocabularyStore vocabularyStore){
|
44 |
|
|
DefinedTermBase.vocabularyStore = vocabularyStore;
|
45 |
|
|
}
|
46 |
|
|
|
47 |
9479da48
|
Andreas Müller
|
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 |
0a5d50a0
|
Andreas Müller
|
protected TermVocabulary<T> vocabulary;
|
53 |
|
|
|
54 |
fbd23d1d
|
Andreas Müller
|
|
55 |
9479da48
|
Andreas Müller
|
public static DefinedTermBase findByUuid(UUID uuid){
|
56 |
fbd23d1d
|
Andreas Müller
|
return vocabularyStore.getTermByUuid(uuid);
|
57 |
9479da48
|
Andreas Müller
|
}
|
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 |
8bc5f53a
|
Andreas Müller
|
* @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
|
69 |
9479da48
|
Andreas Müller
|
*/
|
70 |
8bc5f53a
|
Andreas Müller
|
public ILoadableTerm readCsvLine(List<String> csvLine) {
|
71 |
|
|
return readCsvLine(csvLine, Language.ENGLISH());
|
72 |
9479da48
|
Andreas Müller
|
}
|
73 |
8bc5f53a
|
Andreas Müller
|
public ILoadableTerm readCsvLine(List<String> csvLine, Language lang) {
|
74 |
9479da48
|
Andreas Müller
|
this.setUuid(UUID.fromString(csvLine.get(0)));
|
75 |
|
|
this.setUri(csvLine.get(1));
|
76 |
8bc5f53a
|
Andreas Müller
|
this.addRepresentation(Representation.NewInstance(csvLine.get(3), csvLine.get(2).trim(), lang) );
|
77 |
|
|
return this;
|
78 |
9479da48
|
Andreas Müller
|
}
|
79 |
|
|
|
80 |
|
|
/* (non-Javadoc)
|
81 |
8bc5f53a
|
Andreas Müller
|
* @see eu.etaxonomy.cdm.model.common.ILoadableTerm#writeCsvLine(au.com.bytecode.opencsv.CSVWriter)
|
82 |
9479da48
|
Andreas Müller
|
*/
|
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 |
95e756cb
|
Andreas Müller
|
/* (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 |
0cbc24c5
|
Andreas Müller
|
newVocabulary.terms.add(this);
|
174 |
95e756cb
|
Andreas Müller
|
}
|
175 |
|
|
this.vocabulary = newVocabulary;
|
176 |
|
|
}
|
177 |
|
|
|
178 |
9479da48
|
Andreas Müller
|
|
179 |
|
|
/* (non-Javadoc)
|
180 |
|
|
* @see eu.etaxonomy.cdm.model.common.IDefTerm#getVocabulary()
|
181 |
|
|
*/
|
182 |
|
|
@ManyToOne(fetch=FetchType.EAGER)
|
183 |
|
|
@Cascade( { CascadeType.SAVE_UPDATE })
|
184 |
95e756cb
|
Andreas Müller
|
protected TermVocabulary getPersistentVocabulary() {
|
185 |
9479da48
|
Andreas Müller
|
return this.vocabulary;
|
186 |
|
|
}
|
187 |
|
|
/* (non-Javadoc)
|
188 |
|
|
* @see eu.etaxonomy.cdm.model.common.IDefTerm#setVocabulary(eu.etaxonomy.cdm.model.common.TermVocabulary)
|
189 |
|
|
*/
|
190 |
95e756cb
|
Andreas Müller
|
protected void setPersistentVocabulary(TermVocabulary newVocabulary) {
|
191 |
9479da48
|
Andreas Müller
|
// Hibernate bidirectional cascade hack:
|
192 |
|
|
// http://opensource.atlassian.com/projects/hibernate/browse/HHH-1054
|
193 |
0a5d50a0
|
Andreas Müller
|
if(this.vocabulary == newVocabulary){ return;}
|
194 |
9479da48
|
Andreas Müller
|
if (this.vocabulary != null) {
|
195 |
|
|
this.vocabulary.terms.remove(this);
|
196 |
|
|
}
|
197 |
|
|
if (newVocabulary!= null) {
|
198 |
95e756cb
|
Andreas Müller
|
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 |
9479da48
|
Andreas Müller
|
}
|
221 |
|
|
this.vocabulary = newVocabulary;
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
}
|