1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.cdm.model.description;
|
5
|
|
6
|
import java.util.HashMap;
|
7
|
import java.util.List;
|
8
|
import java.util.Map;
|
9
|
import java.util.Map.Entry;
|
10
|
|
11
|
import javax.persistence.Entity;
|
12
|
import javax.persistence.FetchType;
|
13
|
import javax.persistence.MapKeyJoinColumn;
|
14
|
import javax.persistence.OneToMany;
|
15
|
import javax.xml.bind.annotation.XmlAccessType;
|
16
|
import javax.xml.bind.annotation.XmlAccessorType;
|
17
|
import javax.xml.bind.annotation.XmlElement;
|
18
|
import javax.xml.bind.annotation.XmlRootElement;
|
19
|
import javax.xml.bind.annotation.XmlType;
|
20
|
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
21
|
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.hibernate.annotations.Cascade;
|
24
|
import org.hibernate.annotations.CascadeType;
|
25
|
import org.hibernate.envers.Audited;
|
26
|
|
27
|
import eu.etaxonomy.cdm.jaxb.MultilanguageTextAdapter;
|
28
|
import eu.etaxonomy.cdm.model.common.IMultiLanguageTextHolder;
|
29
|
import eu.etaxonomy.cdm.model.common.Language;
|
30
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
31
|
import eu.etaxonomy.cdm.model.common.MultilanguageTextHelper;
|
32
|
import eu.etaxonomy.cdm.model.common.TermBase;
|
33
|
import eu.etaxonomy.cdm.model.common.VersionableEntity;
|
34
|
|
35
|
/**
|
36
|
* This class represents a statement or a question within a (polytomous) key.
|
37
|
* Compare with SDD SimpleRepresentation.
|
38
|
*
|
39
|
* @author a.mueller
|
40
|
*
|
41
|
*/
|
42
|
@XmlAccessorType(XmlAccessType.FIELD)
|
43
|
@XmlType(name = "KeyStatement", propOrder = {
|
44
|
"label"
|
45
|
// ,"mediaObject"
|
46
|
})
|
47
|
@XmlRootElement(name = "KeyStatement")
|
48
|
@Entity
|
49
|
@Audited
|
50
|
public class KeyStatement extends VersionableEntity implements IMultiLanguageTextHolder{
|
51
|
private static final long serialVersionUID = 3771323100914695139L;
|
52
|
private static final Logger logger = Logger.getLogger(KeyStatement.class);
|
53
|
|
54
|
|
55
|
@XmlElement(name = "MultiLanguageText")
|
56
|
@XmlJavaTypeAdapter(MultilanguageTextAdapter.class)
|
57
|
@OneToMany (fetch= FetchType.LAZY, orphanRemoval=true)
|
58
|
@MapKeyJoinColumn(name="label_mapkey_id")
|
59
|
@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.DELETE })
|
60
|
// @IndexedEmbedded
|
61
|
private Map<Language, LanguageString> label = new HashMap<Language, LanguageString>();
|
62
|
|
63
|
//private mediaObjects needs to be discussed (how to implement the role of the media)
|
64
|
|
65
|
|
66
|
|
67
|
public static KeyStatement NewInstance(){
|
68
|
KeyStatement result = new KeyStatement();
|
69
|
return result;
|
70
|
}
|
71
|
|
72
|
|
73
|
public static KeyStatement NewInstance(String defaultLanguageLabel){
|
74
|
KeyStatement result = new KeyStatement();
|
75
|
result.putLabel(Language.DEFAULT(), defaultLanguageLabel);
|
76
|
return result;
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* Factory method for a key statement.
|
81
|
* @param language the language of the first representation of the statement. Must not be <code>null</code>.
|
82
|
* @param label the text of statement in the given language.
|
83
|
* @return
|
84
|
*/
|
85
|
public static KeyStatement NewInstance(Language language, String label){
|
86
|
assert language != null : "Language for KeyStatement must not be null";
|
87
|
KeyStatement result = new KeyStatement();
|
88
|
result.putLabel(language, label);
|
89
|
return result;
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
*
|
94
|
*/
|
95
|
public KeyStatement() {
|
96
|
}
|
97
|
|
98
|
// ********************************* METHODS ***************************/
|
99
|
|
100
|
/**
|
101
|
* Returns the label with the content of <i>this</i> key statement.
|
102
|
* The different {@link LanguageString language strings} (texts) contained in the
|
103
|
* label should all have the same meaning.
|
104
|
*
|
105
|
* @see #getText(Language)
|
106
|
*/
|
107
|
public Map<Language, LanguageString> getLabel() {
|
108
|
return label;
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Returns the label with the content of <i>this</i> key statement for
|
113
|
* a specific language.
|
114
|
*
|
115
|
* @param language the language in which the label is formulated
|
116
|
* @return
|
117
|
*/
|
118
|
public LanguageString getLabel(Language language){
|
119
|
return label.get(language);
|
120
|
}
|
121
|
|
122
|
public void setLabel(Map<Language,LanguageString> label) {
|
123
|
this.label = label;
|
124
|
}
|
125
|
|
126
|
/**
|
127
|
* Returns the text string in the given {@link Language language} with the content
|
128
|
* of <i>this</i> key statement.
|
129
|
*
|
130
|
* @param language the language in which the label is formulated
|
131
|
* @see #getLabel(Language)
|
132
|
*/
|
133
|
public String getLabelText(Language language) {
|
134
|
LanguageString languageString = label.get(language);
|
135
|
if (languageString == null){
|
136
|
return null;
|
137
|
}else{
|
138
|
return languageString.getText();
|
139
|
}
|
140
|
}
|
141
|
|
142
|
/**
|
143
|
* Returns the LanguageString in the preferred language. Preferred languages
|
144
|
* are specified by the parameter languages, which receives a list of
|
145
|
* Language instances in the order of preference. If no representation in
|
146
|
* any preferred languages is found the method falls back to return the
|
147
|
* Representation in Language.DEFAULT() and if necessary further falls back
|
148
|
* to return the first element found if any.
|
149
|
*
|
150
|
* TODO think about this fall-back strategy &
|
151
|
* see also {@link TermBase#getPreferredRepresentation(List)}
|
152
|
*
|
153
|
* @param languages
|
154
|
* @return
|
155
|
*/
|
156
|
public LanguageString getPreferredLanguageString(List<Language> languages) {
|
157
|
return MultilanguageTextHelper.getPreferredLanguageString(label, languages);
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* Creates a {@link LanguageString language string} based on the given text string
|
162
|
* and the given {@link Language language}, returns it and adds it to the multilanguage
|
163
|
* text representing the content of <i>this</i> text data.
|
164
|
*
|
165
|
* @param text the string representing the content of the text data
|
166
|
* in a particular language
|
167
|
* @param language the language in which the text string is formulated
|
168
|
* @return the language string
|
169
|
* @see #getMultilanguageText()
|
170
|
* @see #putText(LanguageString)
|
171
|
* @deprecated should follow the put semantic of maps, this method will be removed in v4.0
|
172
|
* Use the {@link #putLabel(Language, String) putLabel} method
|
173
|
*/
|
174
|
public LanguageString putLabel(String label, Language language) {
|
175
|
return putLabel(language, label);
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* Creates a {@link LanguageString language string} based on the given text string
|
180
|
* and the given {@link Language language}, returns it and adds it to the multilanguage
|
181
|
* text representing the content of <i>this</i> text data.
|
182
|
*
|
183
|
* @param language the language in which the text string is formulated
|
184
|
* @param text the string representing the content of the text data
|
185
|
* in a particular language
|
186
|
*
|
187
|
* @return the language string
|
188
|
* @see #getLabel()
|
189
|
* @see #putLabel(LanguageString)
|
190
|
*
|
191
|
*/
|
192
|
public LanguageString putLabel(Language language, String label) {
|
193
|
LanguageString result = this.label.put(language , LanguageString.NewInstance(label, language));
|
194
|
return (result == null ? null : result);
|
195
|
}
|
196
|
/**
|
197
|
* Adds a translated {@link LanguageString text in a particular language}
|
198
|
* to the label.
|
199
|
* The given language string will be returned.
|
200
|
*
|
201
|
* @param languageString the language string representing the content of
|
202
|
* the text data in a particular language
|
203
|
* @return the language string
|
204
|
* @see #getLabel()
|
205
|
* @see #putLabel(String, Language)
|
206
|
*/
|
207
|
public LanguageString putLabel(LanguageString languageString) {
|
208
|
if (languageString == null){
|
209
|
return null;
|
210
|
}else{
|
211
|
Language language = languageString.getLanguage();
|
212
|
return this.label.put(language, languageString);
|
213
|
}
|
214
|
}
|
215
|
|
216
|
/**
|
217
|
* Adds a translated {@link LanguageString text in a particular language}
|
218
|
* to the label.
|
219
|
* The given language string will be returned.
|
220
|
*
|
221
|
* @param languageString the language string representing the content of
|
222
|
* the text data in a particular language
|
223
|
* @return the language string
|
224
|
* @see #getLabel()
|
225
|
* @see #putLabel(String, Language)
|
226
|
* @deprecated This method will be removed in v4.0
|
227
|
* Use the {@link #putLabel(LanguageString) putLabel} method instead
|
228
|
*/
|
229
|
public LanguageString putText(LanguageString languageString) {
|
230
|
return putLabel(languageString);
|
231
|
}
|
232
|
|
233
|
/**
|
234
|
* Removes from label the one {@link LanguageString language string}
|
235
|
* with the given {@link Language language}. Returns the removed
|
236
|
* language string.
|
237
|
*
|
238
|
* @param language the language in which the language string to be removed
|
239
|
* has been formulated
|
240
|
* @return the language string associated with the given language
|
241
|
* @see #getLabelText()
|
242
|
* @deprecated This method will be removed in v4.0
|
243
|
* Use the {@link #removeLabel(Language)} method instead
|
244
|
*/
|
245
|
public LanguageString removeText(Language language) {
|
246
|
return removeLabel(language);
|
247
|
}
|
248
|
|
249
|
/**
|
250
|
* Removes from label the one {@link LanguageString language string}
|
251
|
* with the given {@link Language language}. Returns the removed
|
252
|
* language string.
|
253
|
*
|
254
|
* @param language the language in which the language string to be removed
|
255
|
* has been formulated
|
256
|
* @return the language string associated with the given language
|
257
|
* @see #getLabelText()
|
258
|
*/
|
259
|
public LanguageString removeLabel(Language language) {
|
260
|
return this.label.remove(language);
|
261
|
}
|
262
|
|
263
|
|
264
|
/**
|
265
|
* Returns the number of {@link Language languages} in which the label
|
266
|
* of <i>this</i> key statement has been formulated.
|
267
|
*
|
268
|
* @see #getMultilanguageText()
|
269
|
*/
|
270
|
public int countLanguages(){
|
271
|
return label.size();
|
272
|
}
|
273
|
|
274
|
//*********************** CLONE ********************************************************/
|
275
|
|
276
|
/**
|
277
|
* Clones <i>this</i> KeyStatement. This is a shortcut that enables to create
|
278
|
* a new instance that differs only slightly from <i>this</i> KeyStatement by
|
279
|
* modifying only some of the attributes.
|
280
|
*
|
281
|
* @see eu.etaxonomy.cdm.model.common.VersionableEntitity#clone()
|
282
|
* @see java.lang.Object#clone()
|
283
|
*/
|
284
|
@Override
|
285
|
public Object clone() {
|
286
|
KeyStatement result;
|
287
|
try {
|
288
|
result = (KeyStatement) super.clone();
|
289
|
|
290
|
result.label = new HashMap<Language, LanguageString>();
|
291
|
|
292
|
|
293
|
for (Entry<Language,LanguageString> entry: this.label.entrySet()){
|
294
|
|
295
|
result.label.put(entry.getKey(), entry.getValue());
|
296
|
}
|
297
|
|
298
|
return result;
|
299
|
}catch (CloneNotSupportedException e) {
|
300
|
logger.warn("Object does not implement cloneable");
|
301
|
e.printStackTrace();
|
302
|
return null;
|
303
|
}
|
304
|
}
|
305
|
|
306
|
//********************* toString() *************************************/
|
307
|
|
308
|
@Override
|
309
|
public String toString(){
|
310
|
if (label != null && ! label.isEmpty()){
|
311
|
String result = label.values().iterator().next().getText();
|
312
|
return result;
|
313
|
}else{
|
314
|
return super.toString();
|
315
|
}
|
316
|
|
317
|
}
|
318
|
|
319
|
}
|