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.description;
|
11
|
|
12
|
|
13
|
import java.util.HashMap;
|
14
|
import java.util.Map;
|
15
|
|
16
|
import eu.etaxonomy.cdm.jaxb.MultilanguageTextAdapter;
|
17
|
import eu.etaxonomy.cdm.model.common.Language;
|
18
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
19
|
import eu.etaxonomy.cdm.model.common.MultilanguageText;
|
20
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
21
|
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.hibernate.annotations.Cascade;
|
24
|
import org.hibernate.annotations.CascadeType;
|
25
|
import javax.persistence.*;
|
26
|
import javax.xml.bind.annotation.XmlAccessType;
|
27
|
import javax.xml.bind.annotation.XmlAccessorType;
|
28
|
import javax.xml.bind.annotation.XmlElement;
|
29
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
30
|
import javax.xml.bind.annotation.XmlIDREF;
|
31
|
import javax.xml.bind.annotation.XmlRootElement;
|
32
|
import javax.xml.bind.annotation.XmlSchemaType;
|
33
|
import javax.xml.bind.annotation.XmlTransient;
|
34
|
import javax.xml.bind.annotation.XmlType;
|
35
|
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
36
|
|
37
|
/**
|
38
|
* This class represents information pieces expressed in one or several natural
|
39
|
* languages. A {@link TextFormat format} used for structuring the text may also be stated.
|
40
|
* <P>
|
41
|
* This class corresponds partially to NaturalLanguageDescription according to
|
42
|
* the SDD schema.
|
43
|
*
|
44
|
* @author m.doering
|
45
|
* @version 1.0
|
46
|
* @created 08-Nov-2007 13:06:59
|
47
|
*/
|
48
|
@XmlAccessorType(XmlAccessType.FIELD)
|
49
|
@XmlType(name = "TextData", propOrder = {
|
50
|
"multiLanguageText",
|
51
|
"format"
|
52
|
})
|
53
|
@XmlRootElement(name = "TextData")
|
54
|
@Entity
|
55
|
public class TextData extends DescriptionElementBase {
|
56
|
|
57
|
static Logger logger = Logger.getLogger(TextData.class);
|
58
|
|
59
|
//@XmlElement(name = "MultiLanguageText", type = MultilanguageText.class)
|
60
|
@XmlElement(name = "MultiLanguageText")
|
61
|
@XmlJavaTypeAdapter(MultilanguageTextAdapter.class)
|
62
|
//private MultilanguageText multiLanguageText;
|
63
|
private Map<Language, LanguageString> multiLanguageText;
|
64
|
|
65
|
@XmlElement(name = "Format")
|
66
|
@XmlIDREF
|
67
|
@XmlSchemaType(name = "IDREF")
|
68
|
private TextFormat format;
|
69
|
|
70
|
// ************* CONSTRUCTORS *************/
|
71
|
/**
|
72
|
* Class constructor: creates a new empty text data instance.
|
73
|
*
|
74
|
* @see #TextData(Feature)
|
75
|
*/
|
76
|
public TextData(){
|
77
|
this(null);
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Class constructor: creates a new text data instance with the {@link Feature feature}
|
82
|
* to be described.
|
83
|
*
|
84
|
* @param feature the feature the text data refer to
|
85
|
* @see #TextData()
|
86
|
*/
|
87
|
public TextData(Feature feature){
|
88
|
super(feature);
|
89
|
initTextSet();
|
90
|
}
|
91
|
|
92
|
//********* METHODS **************************************/
|
93
|
/**
|
94
|
* Creates a new empty text data instance.
|
95
|
*
|
96
|
* @see #NewInstance(Feature)
|
97
|
* @see #NewInstance(String, Language, TextFormat)
|
98
|
*/
|
99
|
public static TextData NewInstance(){
|
100
|
return new TextData();
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Creates a new text data instance with the {@link Feature feature}
|
105
|
* to be described.
|
106
|
*
|
107
|
* @param feature the feature the text data refer to
|
108
|
* @see #NewInstance()
|
109
|
* @see #NewInstance(String, Language, TextFormat)
|
110
|
*/
|
111
|
public static TextData NewInstance(Feature feature){
|
112
|
return new TextData(feature);
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Creates a new text data instance with a given text in a given particular
|
117
|
* {@link Language language} and with the given text format for structuring it.
|
118
|
*
|
119
|
* @param text the text string with the content of the description
|
120
|
* @param language the language in which the text string is formulated
|
121
|
* @param format the text format used to structure the text string
|
122
|
* @see #NewInstance()
|
123
|
* @see #NewInstance(Feature)
|
124
|
*/
|
125
|
public static TextData NewInstance(String text, Language language, TextFormat format){
|
126
|
TextData result = new TextData();
|
127
|
result.putText(text, language);
|
128
|
result.setFormat(format);
|
129
|
return result;
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* Returns the multilanguage text with the content of <i>this</i> text data.
|
134
|
* The different {@link LanguageString language strings} (texts) contained in the
|
135
|
* multilanguage text should all have the same meaning.
|
136
|
*
|
137
|
* @see #getText(Language)
|
138
|
*/
|
139
|
@OneToMany (fetch= FetchType.LAZY)
|
140
|
@MapKey(name="language")
|
141
|
@Cascade({CascadeType.SAVE_UPDATE})
|
142
|
public Map<Language, LanguageString> getMultilanguageText() {
|
143
|
//public MultilanguageText getMultilanguageText() {
|
144
|
initTextSet();
|
145
|
return multiLanguageText;
|
146
|
}
|
147
|
/**
|
148
|
* @see #getMultilanguageText()
|
149
|
*/
|
150
|
protected void setMultilanguageText(Map<Language, LanguageString> texts) {
|
151
|
//protected void setMultilanguageText(MultilanguageText texts) {
|
152
|
this.multiLanguageText = texts;
|
153
|
}
|
154
|
/**
|
155
|
* Returns the text string in the given {@link Language language} with the content
|
156
|
* of <i>this</i> text data.
|
157
|
*
|
158
|
* @param language the language in which the text string looked for is formulated
|
159
|
* @see #getMultilanguageText()
|
160
|
*/
|
161
|
@Transient
|
162
|
public String getText(Language language) {
|
163
|
initTextSet();
|
164
|
LanguageString languageString = multiLanguageText.get(language);
|
165
|
if (languageString == null){
|
166
|
return null;
|
167
|
}else{
|
168
|
return languageString.getText();
|
169
|
}
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* Creates a {@link LanguageString language string} based on the given text string
|
174
|
* and the given {@link Language language}, returns it and adds it to the multilanguage
|
175
|
* text representing the content of <i>this</i> text data.
|
176
|
*
|
177
|
* @param text the string representing the content of the text data
|
178
|
* in a particular language
|
179
|
* @param language the language in which the text string is formulated
|
180
|
* @return the language string
|
181
|
* @see #getMultilanguageText()
|
182
|
* @see #putText(LanguageString)
|
183
|
*/
|
184
|
@Transient
|
185
|
public LanguageString putText(String text, Language language) {
|
186
|
initTextSet();
|
187
|
LanguageString result = this.multiLanguageText.put(language , LanguageString.NewInstance(text, language));
|
188
|
return (result == null ? null : result);
|
189
|
}
|
190
|
/**
|
191
|
* Adds a translated {@link LanguageString text in a particular language}
|
192
|
* to the multilanguage text representing the content of <i>this</i> text data.
|
193
|
* The given language string will be returned.
|
194
|
*
|
195
|
* @param languageString the language string representing the content of
|
196
|
* the text data in a particular language
|
197
|
* @return the language string
|
198
|
* @see #getMultilanguageText()
|
199
|
* @see #putText(String, Language)
|
200
|
*/
|
201
|
@Transient
|
202
|
public LanguageString putText(LanguageString languageString) {
|
203
|
initTextSet();
|
204
|
|
205
|
if (languageString == null){
|
206
|
return null;
|
207
|
}else{
|
208
|
Language language = languageString.getLanguage();
|
209
|
return this.multiLanguageText.put(language, languageString);
|
210
|
}
|
211
|
}
|
212
|
/**
|
213
|
* Removes from the multilanguage representing the content of
|
214
|
* <i>this</i> text data the one {@link LanguageString language string}
|
215
|
* with the given {@link Language language}. Returns the removed
|
216
|
* language string.
|
217
|
*
|
218
|
* @param language the language in which the language string to be removed
|
219
|
* has been formulated
|
220
|
* @return the language string associated with the given language
|
221
|
* @see #getMultilanguageText()
|
222
|
*/
|
223
|
public LanguageString removeText(Language language) {
|
224
|
initTextSet();
|
225
|
return this.multiLanguageText.remove(language);
|
226
|
}
|
227
|
|
228
|
private void initTextSet(){
|
229
|
if (multiLanguageText == null){
|
230
|
multiLanguageText = MultilanguageText.NewInstance();
|
231
|
}
|
232
|
}
|
233
|
|
234
|
/**
|
235
|
* Returns the number of {@link Language languages} in which the content
|
236
|
* of <i>this</i> text data has been formulated.
|
237
|
*
|
238
|
* @see #getMultilanguageText()
|
239
|
*/
|
240
|
public int countLanguages(){
|
241
|
initTextSet();
|
242
|
return multiLanguageText.size();
|
243
|
}
|
244
|
|
245
|
|
246
|
/**
|
247
|
* Returns the {@link TextFormat format} used for structuring the text representing
|
248
|
* the content of <i>this</i> text data.
|
249
|
*
|
250
|
* @see #getMultilanguageText()
|
251
|
*/
|
252
|
@ManyToOne
|
253
|
public TextFormat getFormat() {
|
254
|
return format;
|
255
|
}
|
256
|
/**
|
257
|
* @see #getFormat()
|
258
|
*/
|
259
|
public void setFormat(TextFormat format) {
|
260
|
this.format = format;
|
261
|
}
|
262
|
|
263
|
}
|