Project

General

Profile

Download (9.03 KB) Statistics
| Branch: | Tag: | Revision:
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
 * @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, orphanRemoval=true)
76
	@MapKeyJoinColumn(name="description_mapkey_id")
77
    @JoinTable(name = "TaxonInteraction_LanguageString")  //to distinguish from other DescriptionElementBase_LanguageString
78
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.DELETE})
79
    private Map<Language,LanguageString> description = new HashMap<Language,LanguageString>();
80

    
81
	@XmlElement(name = "Taxon2")
82
	@XmlIDREF
83
	@XmlSchemaType(name = "IDREF")
84
	@ManyToOne(fetch = FetchType.LAZY)
85
	@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
86
	private Taxon taxon2;
87

    
88
	/**
89
	 * Class constructor: creates a new empty taxon interaction instance.
90
	 */
91
	public TaxonInteraction() {
92
		super(null);
93
	}
94

    
95
	/**
96
	 * Creates a new empty taxon interaction instance.
97
	 */
98
	public static TaxonInteraction NewInstance(){
99
		return new TaxonInteraction();
100
	}
101

    
102
	/**
103
	 * Creates a new empty taxon interaction instance and also sets the feature
104
	 *
105
	 * @param feature
106
	 * @return
107
	 */
108
	public static TaxonInteraction NewInstance(Feature feature){
109
		TaxonInteraction taxonInteraction = new TaxonInteraction();
110
		if(feature.isSupportsTaxonInteraction()){
111
			taxonInteraction.setFeature(feature);
112
		}
113
		return taxonInteraction;
114
	}
115

    
116

    
117
	/**
118
	 * Returns the second {@link Taxon taxon} involved in <i>this</i> taxon interaction.
119
	 * The first taxon is the taxon described in the corresponding
120
	 * {@link TaxonDescription taxon description}.
121
	 */
122
	public Taxon getTaxon2(){
123
		return this.taxon2;
124
	}
125
	/**
126
	 * @see	#getTaxon2()
127
	 */
128
	public void setTaxon2(Taxon taxon2){
129
		this.taxon2 = taxon2;
130
	}
131

    
132
	/**
133
	 * Returns the {@link MultilanguageText multilanguage text} used to describe
134
	 * <i>this</i> taxon interaction. The different {@link LanguageString language strings}
135
	 * contained in the multilanguage text should all have the same meaning.
136
	 */
137
	public Map<Language,LanguageString> getDescriptions(){
138
		return this.description;
139
	}
140

    
141
	/**
142
	 * Returns the description string in the given {@link Language language}
143
	 *
144
	 * @param language	the language in which the description string looked for is formulated
145
	 * @see				#getDescriptions()
146
	 */
147
	public String getDescription(Language language){
148
		LanguageString languageString = description.get(language);
149
		if (languageString == null){
150
			return null;
151
		}else{
152
			return languageString.getText();
153
		}
154
	}
155

    
156
	/**
157
	 * Adds a translated {@link LanguageString text in a particular language}
158
	 * to the {@link MultilanguageText multilanguage text} used to describe
159
	 * <i>this</i> taxon interaction.
160
	 *
161
	 * @param description	the language string describing the taxon interaction
162
	 * 						in a particular language
163
	 * @see    	   			#getDescription()
164
	 * @see    	   			#putDescription(String, Language)
165
	 * @deprecated			should follow the put semantic of maps, this method will be removed in v4.0
166
	 * 						Use the {@link #putDescription(LanguageString) putDescription} method instead
167
	 */
168
	@Deprecated
169
	public void addDescription(LanguageString description){
170
		this.putDescription(description);
171
	}
172

    
173
	/**
174
	 * Adds a translated {@link LanguageString text in a particular language}
175
	 * to the {@link MultilanguageText multilanguage text} used to describe
176
	 * <i>this</i> taxon interaction.
177
	 *
178
	 * @param description	the language string describing the taxon interaction
179
	 * 						in a particular language
180
	 * @see    	   			#getDescription()
181
	 * @see    	   			#putDescription(String, Language)
182
	 */
183
	public void putDescription(LanguageString description){
184
		this.description.put(description.getLanguage(),description);
185
	}
186
	/**
187
	 * Creates a {@link LanguageString language string} based on the given text string
188
	 * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
189
	 * used to describe <i>this</i> taxon interaction.
190
	 *
191
	 * @param text		the string describing the taxon interaction
192
	 * 					in a particular language
193
	 * @param language	the language in which the text string is formulated
194
	 * @see    	   		#getDescription()
195
	 * @see    	   		#putDescription(LanguageString)
196
	 */
197
	public void putDescription(Language language, String text){
198
		this.description.put(language, LanguageString.NewInstance(text, language));
199
	}
200

    
201
	/**
202
	 * Creates a {@link LanguageString language string} based on the given text string
203
	 * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
204
	 * used to describe <i>this</i> taxon interaction.
205
	 *
206
	 * @param text		the string describing the taxon interaction
207
	 * 					in a particular language
208
	 * @param language	the language in which the text string is formulated
209
	 * @see    	   		#getDescription()
210
	 * @see    	   		#addDescription(LanguageString)
211
	 * @deprecated 		should follow the put semantic of maps, this method will be removed in v4.0
212
	 * 					Use the {@link #putDescription(Language, String) putDescription} method instead
213
	 */
214
	@Deprecated
215
	public void addDescription(String text, Language language){
216
		this.putDescription(language, text);
217
	}
218
	/**
219
	 * Removes from the {@link MultilanguageText multilanguage text} used to describe
220
	 * <i>this</i> taxon interaction the one {@link LanguageString language string}
221
	 * with the given {@link Language language}.
222
	 *
223
	 * @param  lang	the language in which the language string to be removed
224
	 * 				has been formulated
225
	 * @see     	#getDescription()
226
	 */
227
	public void removeDescription(Language lang){
228
		this.description.remove(lang);
229
	}
230

    
231

    
232
//*********************************** CLONE *****************************************/
233

    
234
	/**
235
	 * Clones <i>this</i> taxon interaction. This is a shortcut that enables to create
236
	 * a new instance that differs only slightly from <i>this</i> taxon interaction by
237
	 * modifying only some of the attributes.
238
	 *
239
	 * @see eu.etaxonomy.cdm.model.description.DescriptionElementBase#clone()
240
	 * @see java.lang.Object#clone()
241
	 */
242
	@Override
243
	public Object clone() {
244

    
245
		try {
246
			TaxonInteraction result = (TaxonInteraction)super.clone();
247

    
248
			//description
249
			result.description = new HashMap<Language, LanguageString>();
250
			for (Language language : getDescriptions().keySet()){
251
				//TODO clone needed? See also IndividualsAssociation
252
				LanguageString newLanguageString = (LanguageString)getDescriptions().get(language).clone();
253
				result.description.put(language, newLanguageString);
254
			}
255

    
256

    
257
			return result;
258
			//no changes to: taxon2
259
		} catch (CloneNotSupportedException e) {
260
			logger.warn("Object does not implement cloneable");
261
			e.printStackTrace();
262
			return null;
263
		}
264
	}
265
}
(30-30/36)