2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.model
.description
;
12 import java
.util
.HashMap
;
13 import java
.util
.Iterator
;
14 import java
.util
.List
;
17 import javax
.persistence
.Entity
;
18 import javax
.persistence
.FetchType
;
19 import javax
.persistence
.ManyToOne
;
20 import javax
.persistence
.OneToMany
;
21 import javax
.validation
.constraints
.NotNull
;
22 import javax
.xml
.bind
.annotation
.XmlAccessType
;
23 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
24 import javax
.xml
.bind
.annotation
.XmlElement
;
25 import javax
.xml
.bind
.annotation
.XmlIDREF
;
26 import javax
.xml
.bind
.annotation
.XmlRootElement
;
27 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
28 import javax
.xml
.bind
.annotation
.XmlType
;
29 import javax
.xml
.bind
.annotation
.adapters
.XmlJavaTypeAdapter
;
31 import org
.apache
.log4j
.Logger
;
32 import org
.hibernate
.annotations
.Cascade
;
33 import org
.hibernate
.annotations
.CascadeType
;
34 import org
.hibernate
.envers
.Audited
;
35 import org
.hibernate
.search
.annotations
.Indexed
;
36 import org
.hibernate
.search
.annotations
.IndexedEmbedded
;
38 import eu
.etaxonomy
.cdm
.jaxb
.MultilanguageTextAdapter
;
39 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
40 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
41 import eu
.etaxonomy
.cdm
.model
.common
.MultilanguageTextHelper
;
42 import eu
.etaxonomy
.cdm
.model
.common
.TermBase
;
46 * This class represents information pieces expressed in one or several natural
47 * languages (for the {@link Feature feature} "medical use" for instance).
48 * A {@link TextFormat format} used for structuring the text may also be stated.
50 * This class corresponds partially to NaturalLanguageDescriptionType according
55 * @created 08-Nov-2007 13:06:59
57 @XmlAccessorType(XmlAccessType
.FIELD
)
58 @XmlType(name
= "TextData", propOrder
= {
62 @XmlRootElement(name
= "TextData")
65 @Indexed(index
= "eu.etaxonomy.cdm.model.description.DescriptionElementBase")
66 public class TextData
extends DescriptionElementBase
{
67 private static final long serialVersionUID
= -2165015581278282615L;
68 @SuppressWarnings("unused")
69 private static final Logger logger
= Logger
.getLogger(TextData
.class);
71 //@XmlElement(name = "MultiLanguageText", type = MultilanguageText.class)
72 @XmlElement(name
= "MultiLanguageText")
73 @XmlJavaTypeAdapter(MultilanguageTextAdapter
.class)
74 @OneToMany (fetch
= FetchType
.LAZY
)
75 @Cascade({CascadeType
.SAVE_UPDATE
,CascadeType
.MERGE
, CascadeType
.DELETE
, CascadeType
.DELETE_ORPHAN
})
78 private Map
<Language
, LanguageString
> multilanguageText
= new HashMap
<Language
,LanguageString
>();
80 @XmlElement(name
= "Format")
82 @XmlSchemaType(name
= "IDREF")
83 @ManyToOne(fetch
= FetchType
.LAZY
)
84 private TextFormat format
;
86 // ************* CONSTRUCTORS *************/
88 * Class constructor: creates a new empty text data instance.
90 * @see #TextData(Feature)
97 * Class constructor: creates a new text data instance with the {@link Feature feature}
100 * @param feature the feature the text data refer to
103 public TextData(Feature feature
){
107 //********* METHODS **************************************/
109 * Creates a new empty text data instance.
111 * @see #NewInstance(Feature)
112 * @see #NewInstance(String, Language, TextFormat)
114 public static TextData
NewInstance(){
115 return new TextData();
119 * Creates a new text data instance with the {@link Feature feature}
122 * @param feature the feature the text data refer to
123 * @see #NewInstance()
124 * @see #NewInstance(String, Language, TextFormat)
126 public static TextData
NewInstance(Feature feature
){
127 return new TextData(feature
);
131 * Creates a new text data instance with a given text in a given particular
132 * {@link Language language} and with the given text format for structuring it.
134 * @param text the text string with the content of the description
135 * @param language the language in which the text string is formulated
136 * @param format the text format used to structure the text string
137 * @see #NewInstance()
138 * @see #NewInstance(Feature)
140 public static TextData
NewInstance(String text
, Language language
, TextFormat format
){
141 TextData result
= new TextData();
142 result
.putText(text
, language
);
143 result
.setFormat(format
);
148 * Returns the multilanguage text with the content of <i>this</i> text data.
149 * The different {@link LanguageString language strings} (texts) contained in the
150 * multilanguage text should all have the same meaning.
152 * @see #getText(Language)
154 public Map
<Language
, LanguageString
> getMultilanguageText() {
155 return multilanguageText
;
159 * Returns the multilanguage text with the content of <i>this</i> text data for
160 * a specific language.
162 * @param language the language in which the text string looked for is formulated
165 public LanguageString
getLanguageText(Language language
){
166 return multilanguageText
.get(language
);
170 * @deprecated Method name is misleading. Use {@link #getLanguageText(Language)} instead.
173 public LanguageString
getMultilanguageText(Language language
){
174 return multilanguageText
.get(language
);
176 public void setMultilanguageText(Map
<Language
,LanguageString
> multilanguageText
) {
177 this.multilanguageText
= multilanguageText
;
181 * Returns the text string in the given {@link Language language} with the content
182 * of <i>this</i> text data.
184 * @param language the language in which the text string looked for is formulated
185 * @see #getMultilanguageText(Language)
187 public String
getText(Language language
) {
188 LanguageString languageString
= multilanguageText
.get(language
);
189 if (languageString
== null){
192 return languageString
.getText();
197 * Returns the LanguageString in the preferred language. Preferred languages
198 * are specified by the parameter languages, which receives a list of
199 * Language instances in the order of preference. If no representation in
200 * any preferred languages is found the method falls back to return the
201 * Representation in Language.DEFAULT() and if neccesary further falls back
202 * to return the first element found if any.
204 * TODO think about this fall-back strategy &
205 * see also {@link TermBase#getPreferredRepresentation(List)}
209 * @deprecated replaced by static method {@link MultilanguageTextHelper#getPreferredLanguageString(Map, List)}
212 public LanguageString
getPreferredLanguageString(List
<Language
> languages
) {
214 LanguageString languageString
= null;
215 if(languages
!= null){
216 for(Language language
: languages
) {
217 languageString
= multilanguageText
.get(language
);
218 if(languageString
!= null){
219 return languageString
;
223 languageString
= multilanguageText
.get(Language
.DEFAULT());
225 if(languageString
== null && multilanguageText
.size() > 0){
226 Iterator
<LanguageString
> it
= multilanguageText
.values().iterator();
228 languageString
= it
.next();
231 return languageString
;
235 * Creates a {@link LanguageString language string} based on the given text string
236 * and the given {@link Language language}, returns it and adds it to the multilanguage
237 * text representing the content of <i>this</i> text data.
239 * @param text the string representing the content of the text data
240 * in a particular language
241 * @param language the language in which the text string is formulated
242 * @return the language string
243 * @see #getMultilanguageText()
244 * @see #putText(LanguageString)
246 public LanguageString
putText(String text
, Language language
) {
247 LanguageString result
= this.multilanguageText
.put(language
, LanguageString
.NewInstance(text
, language
));
248 return (result
== null ?
null : result
);
251 * Adds a translated {@link LanguageString text in a particular language}
252 * to the multilanguage text representing the content of <i>this</i> text data.
253 * The given language string will be returned.
255 * @param languageString the language string representing the content of
256 * the text data in a particular language
257 * @return the language string
258 * @see #getMultilanguageText()
259 * @see #putText(String, Language)
261 public LanguageString
putText(LanguageString languageString
) {
263 if (languageString
== null){
266 Language language
= languageString
.getLanguage();
267 return this.multilanguageText
.put(language
, languageString
);
271 * Removes from the multilanguage representing the content of
272 * <i>this</i> text data the one {@link LanguageString language string}
273 * with the given {@link Language language}. Returns the removed
276 * @param language the language in which the language string to be removed
277 * has been formulated
278 * @return the language string associated with the given language
279 * @see #getMultilanguageText()
281 public LanguageString
removeText(Language language
) {
282 return this.multilanguageText
.remove(language
);
286 * Returns the number of {@link Language languages} in which the content
287 * of <i>this</i> text data has been formulated.
289 * @see #getMultilanguageText()
291 public int countLanguages(){
292 return multilanguageText
.size();
297 * Returns the {@link TextFormat format} used for structuring the text representing
298 * the content of <i>this</i> text data.
300 * @see #getMultilanguageText()
302 public TextFormat
getFormat() {
308 public void setFormat(TextFormat format
) {
309 this.format
= format
;