329736f2a103b3e1d4dc6871800c0683330eccd1
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / description / TaxonInteraction.java
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 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;
30
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;
37
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;
44
45 // FIXME
46 /**
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}.
51 * <P>
52 * This class corresponds to: <ul>
53 * <li> NaturalLanguageDescriptionType (partially) according to the SDD schema
54 * <li> Associations according to the TDWG ontology
55 * </ul>
56 *
57 * @author m.doering
58 * @version 1.0
59 * @created 08-Nov-2007 13:06:57
60 */
61 @XmlAccessorType(XmlAccessType.FIELD)
62 @XmlType(name = "TaxonInteraction", propOrder = {
63 "description",
64 "taxon2"
65 })
66 @XmlRootElement(name = "TaxonInteraction")
67 @Entity
68 @Audited
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);
73
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>();
81
82 @XmlElement(name = "Taxon2")
83 @XmlIDREF
84 @XmlSchemaType(name = "IDREF")
85 @ManyToOne(fetch = FetchType.LAZY)
86 @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
87 private Taxon taxon2;
88
89 /**
90 * Class constructor: creates a new empty taxon interaction instance.
91 */
92 public TaxonInteraction() {
93 super(null);
94 }
95
96 /**
97 * Creates a new empty taxon interaction instance.
98 */
99 public static TaxonInteraction NewInstance(){
100 return new TaxonInteraction();
101 }
102
103 /**
104 * Creates a new empty taxon interaction instance and also sets the feature
105 *
106 * @param feature
107 * @return
108 */
109 public static TaxonInteraction NewInstance(Feature feature){
110 TaxonInteraction taxonInteraction = new TaxonInteraction();
111 if(feature.isSupportsTaxonInteraction()){
112 taxonInteraction.setFeature(feature);
113 }
114 return taxonInteraction;
115 }
116
117
118 /**
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}.
122 */
123 public Taxon getTaxon2(){
124 return this.taxon2;
125 }
126 /**
127 * @see #getTaxon2()
128 */
129 public void setTaxon2(Taxon taxon2){
130 this.taxon2 = taxon2;
131 }
132
133 /**
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.
137 */
138 public Map<Language,LanguageString> getDescriptions(){
139 return this.description;
140 }
141
142 /**
143 * Returns the description string in the given {@link Language language}
144 *
145 * @param language the language in which the description string looked for is formulated
146 * @see #getDescriptions()
147 */
148 public String getDescription(Language language){
149 Hibernate.initialize(description);
150 LanguageString languageString = description.get(language);
151 if (languageString == null){
152 return null;
153 }else{
154 return languageString.getText();
155 }
156 }
157
158 /**
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.
162 *
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
169 */
170 @Deprecated
171 public void addDescription(LanguageString description){
172 this.putDescription(description);
173 }
174
175 /**
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.
179 *
180 * @param description the language string describing the taxon interaction
181 * in a particular language
182 * @see #getDescription()
183 * @see #putDescription(String, Language)
184 */
185 public void putDescription(LanguageString description){
186 this.description.put(description.getLanguage(),description);
187 }
188 /**
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.
192 *
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)
198 */
199 public void putDescription(Language language, String text){
200 this.description.put(language, LanguageString.NewInstance(text, language));
201 }
202
203 /**
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.
207 *
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
215 */
216 @Deprecated
217 public void addDescription(String text, Language language){
218 this.putDescription(language, text);
219 }
220 /**
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}.
224 *
225 * @param lang the language in which the language string to be removed
226 * has been formulated
227 * @see #getDescription()
228 */
229 public void removeDescription(Language lang){
230 this.description.remove(lang);
231 }
232
233
234 //*********************************** CLONE *****************************************/
235
236 /**
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.
240 *
241 * @see eu.etaxonomy.cdm.model.description.DescriptionElementBase#clone()
242 * @see java.lang.Object#clone()
243 */
244 @Override
245 public Object clone() {
246
247 try {
248 TaxonInteraction result = (TaxonInteraction)super.clone();
249
250 //description
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);
256 }
257
258
259 return result;
260 //no changes to: taxon2
261 } catch (CloneNotSupportedException e) {
262 logger.warn("Object does not implement cloneable");
263 e.printStackTrace();
264 return null;
265 }
266 }
267 }