more tests and features for #476: Implement free-text search methods for TaxonBase...
[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.HashSet;
15 import java.util.Map;
16
17 import javax.persistence.Entity;
18 import javax.persistence.FetchType;
19 import javax.persistence.JoinTable;
20 import javax.persistence.ManyToOne;
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.annotations.Cascade;
33 import org.hibernate.annotations.CascadeType;
34 import org.hibernate.envers.Audited;
35 import org.hibernate.search.annotations.Indexed;
36
37 import eu.etaxonomy.cdm.jaxb.MultilanguageTextAdapter;
38 import eu.etaxonomy.cdm.model.common.IMultiLanguageTextHolder;
39 import eu.etaxonomy.cdm.model.common.Language;
40 import eu.etaxonomy.cdm.model.common.LanguageString;
41 import eu.etaxonomy.cdm.model.common.MultilanguageText;
42 import eu.etaxonomy.cdm.model.taxon.Taxon;
43
44 // FIXME
45 /**
46 * This class represents interactions between the described {@link Taxon taxon}
47 * and a second one (for instance a parasite, a prey or a hybrid parent).
48 * Only {@link TaxonDescription taxon descriptions} may contain taxon interactions.
49 * The interaction itself is described by a {@link MultilanguageText multilanguage text}.
50 * <P>
51 * This class corresponds to: <ul>
52 * <li> NaturalLanguageDescriptionType (partially) according to the SDD schema
53 * <li> Associations according to the TDWG ontology
54 * </ul>
55 *
56 * @author m.doering
57 * @version 1.0
58 * @created 08-Nov-2007 13:06:57
59 */
60 @XmlAccessorType(XmlAccessType.FIELD)
61 @XmlType(name = "TaxonInteraction", propOrder = {
62 "description",
63 "taxon2"
64 })
65 @XmlRootElement(name = "TaxonInteraction")
66 @Entity
67 @Audited
68 @Indexed(index = "eu.etaxonomy.cdm.model.description.DescriptionElementBase")
69 public class TaxonInteraction extends DescriptionElementBase implements IMultiLanguageTextHolder, Cloneable{
70 private static final long serialVersionUID = -5014025677925668627L;
71 private static final Logger logger = Logger.getLogger(TaxonInteraction.class);
72
73 @XmlElement(name = "Description")
74 @XmlJavaTypeAdapter(MultilanguageTextAdapter.class)
75 @OneToMany(fetch = FetchType.LAZY)
76 @JoinTable(name = "TaxonInteraction_LanguageString")
77 @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.DELETE, CascadeType.DELETE_ORPHAN })
78 private Map<Language,LanguageString> description = new HashMap<Language,LanguageString>();
79
80 @XmlElement(name = "Taxon2")
81 @XmlIDREF
82 @XmlSchemaType(name = "IDREF")
83 @ManyToOne(fetch = FetchType.LAZY)
84 @Cascade(CascadeType.SAVE_UPDATE)
85 private Taxon taxon2;
86
87 /**
88 * Class constructor: creates a new empty taxon interaction instance.
89 */
90 public TaxonInteraction() {
91 super(null);
92 }
93
94 /**
95 * Creates a new empty taxon interaction instance.
96 */
97 public static TaxonInteraction NewInstance(){
98 return new TaxonInteraction();
99 }
100
101 /**
102 * Creates a new empty taxon interaction instance and also sets the feature
103 *
104 * @param feature
105 * @return
106 */
107 public static TaxonInteraction NewInstance(Feature feature){
108 TaxonInteraction taxonInteraction = new TaxonInteraction();
109 if(feature.isSupportsTaxonInteraction()){
110 taxonInteraction.setFeature(feature);
111 }
112 return taxonInteraction;
113 }
114
115
116 /**
117 * Returns the second {@link Taxon taxon} involved in <i>this</i> taxon interaction.
118 * The first taxon is the taxon described in the corresponding
119 * {@link TaxonDescription taxon description}.
120 */
121 public Taxon getTaxon2(){
122 return this.taxon2;
123 }
124 /**
125 * @see #getTaxon2()
126 */
127 public void setTaxon2(Taxon taxon2){
128 this.taxon2 = taxon2;
129 }
130
131 /**
132 * Returns the {@link MultilanguageText multilanguage text} used to describe
133 * <i>this</i> taxon interaction. The different {@link LanguageString language strings}
134 * contained in the multilanguage text should all have the same meaning.
135 */
136 public Map<Language,LanguageString> getDescriptions(){
137 return this.description;
138 }
139
140 /**
141 * Returns the description string in the given {@link Language language}
142 *
143 * @param language the language in which the description string looked for is formulated
144 * @see #getDescriptions()
145 */
146 public String getDescription(Language language){
147 LanguageString languageString = description.get(language);
148 if (languageString == null){
149 return null;
150 }else{
151 return languageString.getText();
152 }
153 }
154
155 /**
156 * Adds a translated {@link LanguageString text in a particular language}
157 * to the {@link MultilanguageText multilanguage text} used to describe
158 * <i>this</i> taxon interaction.
159 *
160 * @param description the language string describing the taxon interaction
161 * in a particular language
162 * @see #getDescription()
163 * @see #putDescription(String, Language)
164 * @deprecated should follow the put semantic of maps, this method will be removed in v4.0
165 * Use the {@link #putDescription(LanguageString) putDescription} method instead
166 */
167 @Deprecated
168 public void addDescription(LanguageString description){
169 this.putDescription(description);
170 }
171
172 /**
173 * Adds a translated {@link LanguageString text in a particular language}
174 * to the {@link MultilanguageText multilanguage text} used to describe
175 * <i>this</i> taxon interaction.
176 *
177 * @param description the language string describing the taxon interaction
178 * in a particular language
179 * @see #getDescription()
180 * @see #putDescription(String, Language)
181 */
182 public void putDescription(LanguageString description){
183 this.description.put(description.getLanguage(),description);
184 }
185 /**
186 * Creates a {@link LanguageString language string} based on the given text string
187 * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
188 * used to describe <i>this</i> taxon interaction.
189 *
190 * @param text the string describing the taxon interaction
191 * in a particular language
192 * @param language the language in which the text string is formulated
193 * @see #getDescription()
194 * @see #putDescription(LanguageString)
195 */
196 public void putDescription(Language language, String text){
197 this.description.put(language, LanguageString.NewInstance(text, language));
198 }
199
200 /**
201 * Creates a {@link LanguageString language string} based on the given text string
202 * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
203 * used to describe <i>this</i> taxon interaction.
204 *
205 * @param text the string describing the taxon interaction
206 * in a particular language
207 * @param language the language in which the text string is formulated
208 * @see #getDescription()
209 * @see #addDescription(LanguageString)
210 * @deprecated should follow the put semantic of maps, this method will be removed in v4.0
211 * Use the {@link #putDescription(Language, String) putDescription} method instead
212 */
213 @Deprecated
214 public void addDescription(String text, Language language){
215 this.putDescription(language, text);
216 }
217 /**
218 * Removes from the {@link MultilanguageText multilanguage text} used to describe
219 * <i>this</i> taxon interaction the one {@link LanguageString language string}
220 * with the given {@link Language language}.
221 *
222 * @param lang the language in which the language string to be removed
223 * has been formulated
224 * @see #getDescription()
225 */
226 public void removeDescription(Language lang){
227 this.description.remove(lang);
228 }
229
230
231 //*********************************** CLONE *****************************************/
232
233 /**
234 * Clones <i>this</i> taxon interaction. This is a shortcut that enables to create
235 * a new instance that differs only slightly from <i>this</i> taxon interaction by
236 * modifying only some of the attributes.
237 *
238 * @see eu.etaxonomy.cdm.model.description.DescriptionElementBase#clone()
239 * @see java.lang.Object#clone()
240 */
241 @Override
242 public Object clone() {
243
244 try {
245 TaxonInteraction result = (TaxonInteraction)super.clone();
246
247 //description
248 result.description = new HashMap<Language, LanguageString>();
249 for (Language language : getDescriptions().keySet()){
250 //TODO clone needed? See also IndividualsAssociation
251 LanguageString newLanguageString = (LanguageString)getDescriptions().get(language).clone();
252 result.description.put(language, newLanguageString);
253 }
254
255
256 return result;
257 //no changes to: taxon2
258 } catch (CloneNotSupportedException e) {
259 logger.warn("Object does not implement cloneable");
260 e.printStackTrace();
261 return null;
262 }
263 }
264 }