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
;
13 import java
.util
.HashMap
;
16 import javax
.persistence
.Entity
;
17 import javax
.persistence
.FetchType
;
18 import javax
.persistence
.JoinTable
;
19 import javax
.persistence
.ManyToOne
;
20 import javax
.persistence
.MapKeyJoinColumn
;
21 import javax
.persistence
.OneToMany
;
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
.Hibernate
;
33 import org
.hibernate
.annotations
.Cascade
;
34 import org
.hibernate
.annotations
.CascadeType
;
35 import org
.hibernate
.envers
.Audited
;
36 import org
.hibernate
.search
.annotations
.Indexed
;
38 import eu
.etaxonomy
.cdm
.jaxb
.MultilanguageTextAdapter
;
39 import eu
.etaxonomy
.cdm
.model
.common
.IMultiLanguageTextHolder
;
40 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
41 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
42 import eu
.etaxonomy
.cdm
.model
.common
.MultilanguageText
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
47 * This class represents interactions between the described {@link Taxon taxon}
48 * and a second one (for instance a parasite, a prey or a hybrid parent).
49 * Only {@link TaxonDescription taxon descriptions} may contain taxon interactions.
50 * The interaction itself is described by a {@link MultilanguageText multilanguage text}.
52 * This class corresponds to: <ul>
53 * <li> NaturalLanguageDescriptionType (partially) according to the SDD schema
54 * <li> Associations according to the TDWG ontology
59 * @created 08-Nov-2007 13:06:57
61 @XmlAccessorType(XmlAccessType
.FIELD
)
62 @XmlType(name
= "TaxonInteraction", propOrder
= {
66 @XmlRootElement(name
= "TaxonInteraction")
69 @Indexed(index
= "eu.etaxonomy.cdm.model.description.DescriptionElementBase")
70 public class TaxonInteraction
extends DescriptionElementBase
implements IMultiLanguageTextHolder
, Cloneable
{
71 private static final long serialVersionUID
= -5014025677925668627L;
72 private static final Logger logger
= Logger
.getLogger(TaxonInteraction
.class);
74 @XmlElement(name
= "Description")
75 @XmlJavaTypeAdapter(MultilanguageTextAdapter
.class)
76 @OneToMany(fetch
= FetchType
.LAZY
, orphanRemoval
=true)
77 @MapKeyJoinColumn(name
="description_mapkey_id")
78 @JoinTable(name
= "TaxonInteraction_LanguageString") //to distinguish from other DescriptionElementBase_LanguageString
79 @Cascade({CascadeType
.SAVE_UPDATE
,CascadeType
.MERGE
, CascadeType
.DELETE
})
80 private Map
<Language
,LanguageString
> description
= new HashMap
<Language
,LanguageString
>();
82 @XmlElement(name
= "Taxon2")
84 @XmlSchemaType(name
= "IDREF")
85 @ManyToOne(fetch
= FetchType
.LAZY
)
86 @Cascade({CascadeType
.SAVE_UPDATE
,CascadeType
.MERGE
})
90 * Class constructor: creates a new empty taxon interaction instance.
92 public TaxonInteraction() {
97 * Creates a new empty taxon interaction instance.
99 public static TaxonInteraction
NewInstance(){
100 return new TaxonInteraction();
104 * Creates a new empty taxon interaction instance and also sets the feature
109 public static TaxonInteraction
NewInstance(Feature feature
){
110 TaxonInteraction taxonInteraction
= new TaxonInteraction();
111 if(feature
.isSupportsTaxonInteraction()){
112 taxonInteraction
.setFeature(feature
);
114 return taxonInteraction
;
119 * Returns the second {@link Taxon taxon} involved in <i>this</i> taxon interaction.
120 * The first taxon is the taxon described in the corresponding
121 * {@link TaxonDescription taxon description}.
123 public Taxon
getTaxon2(){
129 public void setTaxon2(Taxon taxon2
){
130 this.taxon2
= taxon2
;
134 * Returns the {@link MultilanguageText multilanguage text} used to describe
135 * <i>this</i> taxon interaction. The different {@link LanguageString language strings}
136 * contained in the multilanguage text should all have the same meaning.
138 public Map
<Language
,LanguageString
> getDescriptions(){
139 return this.description
;
143 * Returns the description string in the given {@link Language language}
145 * @param language the language in which the description string looked for is formulated
146 * @see #getDescriptions()
148 public String
getDescription(Language language
){
149 Hibernate
.initialize(description
);
150 LanguageString languageString
= description
.get(language
);
151 if (languageString
== null){
154 return languageString
.getText();
159 * Adds a translated {@link LanguageString text in a particular language}
160 * to the {@link MultilanguageText multilanguage text} used to describe
161 * <i>this</i> taxon interaction.
163 * @param description the language string describing the taxon interaction
164 * in a particular language
165 * @see #getDescription()
166 * @see #putDescription(String, Language)
167 * @deprecated should follow the put semantic of maps, this method will be removed in v4.0
168 * Use the {@link #putDescription(LanguageString) putDescription} method instead
171 public void addDescription(LanguageString description
){
172 this.putDescription(description
);
176 * Adds a translated {@link LanguageString text in a particular language}
177 * to the {@link MultilanguageText multilanguage text} used to describe
178 * <i>this</i> taxon interaction.
180 * @param description the language string describing the taxon interaction
181 * in a particular language
182 * @see #getDescription()
183 * @see #putDescription(String, Language)
185 public void putDescription(LanguageString description
){
186 this.description
.put(description
.getLanguage(),description
);
189 * Creates a {@link LanguageString language string} based on the given text string
190 * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
191 * used to describe <i>this</i> taxon interaction.
193 * @param text the string describing the taxon interaction
194 * in a particular language
195 * @param language the language in which the text string is formulated
196 * @see #getDescription()
197 * @see #putDescription(LanguageString)
199 public void putDescription(Language language
, String text
){
200 this.description
.put(language
, LanguageString
.NewInstance(text
, language
));
204 * Creates a {@link LanguageString language string} based on the given text string
205 * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
206 * used to describe <i>this</i> taxon interaction.
208 * @param text the string describing the taxon interaction
209 * in a particular language
210 * @param language the language in which the text string is formulated
211 * @see #getDescription()
212 * @see #addDescription(LanguageString)
213 * @deprecated should follow the put semantic of maps, this method will be removed in v4.0
214 * Use the {@link #putDescription(Language, String) putDescription} method instead
217 public void addDescription(String text
, Language language
){
218 this.putDescription(language
, text
);
221 * Removes from the {@link MultilanguageText multilanguage text} used to describe
222 * <i>this</i> taxon interaction the one {@link LanguageString language string}
223 * with the given {@link Language language}.
225 * @param lang the language in which the language string to be removed
226 * has been formulated
227 * @see #getDescription()
229 public void removeDescription(Language lang
){
230 this.description
.remove(lang
);
234 //*********************************** CLONE *****************************************/
237 * Clones <i>this</i> taxon interaction. This is a shortcut that enables to create
238 * a new instance that differs only slightly from <i>this</i> taxon interaction by
239 * modifying only some of the attributes.
241 * @see eu.etaxonomy.cdm.model.description.DescriptionElementBase#clone()
242 * @see java.lang.Object#clone()
245 public Object
clone() {
248 TaxonInteraction result
= (TaxonInteraction
)super.clone();
251 result
.description
= new HashMap
<Language
, LanguageString
>();
252 for (Language language
: getDescriptions().keySet()){
253 //TODO clone needed? See also IndividualsAssociation
254 LanguageString newLanguageString
= (LanguageString
)getDescriptions().get(language
).clone();
255 result
.description
.put(language
, newLanguageString
);
260 //no changes to: taxon2
261 } catch (CloneNotSupportedException e
) {
262 logger
.warn("Object does not implement cloneable");