minor
[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.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 * @since 08-Nov-2007 13:06:57
58 */
59 @XmlAccessorType(XmlAccessType.FIELD)
60 @XmlType(name = "TaxonInteraction", propOrder = {
61 "description",
62 "taxon2"
63 })
64 @XmlRootElement(name = "TaxonInteraction")
65 @Entity
66 @Audited
67 @Indexed(index = "eu.etaxonomy.cdm.model.description.DescriptionElementBase")
68 public class TaxonInteraction extends DescriptionElementBase implements IMultiLanguageTextHolder, Cloneable{
69 private static final long serialVersionUID = -5014025677925668627L;
70 private static final Logger logger = Logger.getLogger(TaxonInteraction.class);
71
72 @XmlElement(name = "Description")
73 @XmlJavaTypeAdapter(MultilanguageTextAdapter.class)
74 @OneToMany(fetch = FetchType.LAZY, orphanRemoval=true)
75 @MapKeyJoinColumn(name="description_mapkey_id")
76 @JoinTable(name = "TaxonInteraction_LanguageString") //to distinguish from other DescriptionElementBase_LanguageString
77 @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.DELETE})
78 private Map<Language,LanguageString> description = new HashMap<>();
79
80 @XmlElement(name = "Taxon2")
81 @XmlIDREF
82 @XmlSchemaType(name = "IDREF")
83 @ManyToOne(fetch = FetchType.LAZY)
84 @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
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> getDescription(){
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 */
165 public void putDescription(LanguageString description){
166 this.description.put(description.getLanguage(),description);
167 }
168 /**
169 * Creates a {@link LanguageString language string} based on the given text string
170 * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
171 * used to describe <i>this</i> taxon interaction.
172 *
173 * @param text the string describing the taxon interaction
174 * in a particular language
175 * @param language the language in which the text string is formulated
176 * @see #getDescription()
177 * @see #putDescription(LanguageString)
178 */
179 public void putDescription(Language language, String text){
180 this.description.put(language, LanguageString.NewInstance(text, language));
181 }
182
183 /**
184 * Removes from the {@link MultilanguageText multilanguage text} used to describe
185 * <i>this</i> taxon interaction the one {@link LanguageString language string}
186 * with the given {@link Language language}.
187 *
188 * @param lang the language in which the language string to be removed
189 * has been formulated
190 * @see #getDescription()
191 */
192 public void removeDescription(Language lang){
193 this.description.remove(lang);
194 }
195
196
197 //*********************************** CLONE *****************************************/
198
199 /**
200 * Clones <i>this</i> taxon interaction. This is a shortcut that enables to create
201 * a new instance that differs only slightly from <i>this</i> taxon interaction by
202 * modifying only some of the attributes.
203 *
204 * @see eu.etaxonomy.cdm.model.description.DescriptionElementBase#clone()
205 * @see java.lang.Object#clone()
206 */
207 @Override
208 public Object clone() {
209
210 try {
211 TaxonInteraction result = (TaxonInteraction)super.clone();
212
213 //description
214 result.description = cloneLanguageString(getDescription());
215
216 return result;
217 //no changes to: taxon2
218 } catch (CloneNotSupportedException e) {
219 logger.warn("Object does not implement cloneable");
220 e.printStackTrace();
221 return null;
222 }
223 }
224
225 }