Project

General

Profile

Download (9.09 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.JoinColumn;
19
import javax.persistence.JoinTable;
20
import javax.persistence.ManyToOne;
21
import javax.persistence.MapKeyJoinColumn;
22
import javax.persistence.OneToMany;
23
import javax.xml.bind.annotation.XmlAccessType;
24
import javax.xml.bind.annotation.XmlAccessorType;
25
import javax.xml.bind.annotation.XmlElement;
26
import javax.xml.bind.annotation.XmlIDREF;
27
import javax.xml.bind.annotation.XmlRootElement;
28
import javax.xml.bind.annotation.XmlSchemaType;
29
import javax.xml.bind.annotation.XmlType;
30
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
31

    
32
import org.apache.log4j.Logger;
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(
79
            name = "TaxonInteraction_LanguageString",
80
            joinColumns = @JoinColumn(name="DescriptionElementBase_id")
81
	)
82
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.DELETE})
83
    private Map<Language,LanguageString> description = new HashMap<Language,LanguageString>();
84

    
85
	@XmlElement(name = "Taxon2")
86
	@XmlIDREF
87
	@XmlSchemaType(name = "IDREF")
88
	@ManyToOne(fetch = FetchType.LAZY)
89
	@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
90
	private Taxon taxon2;
91

    
92
	/**
93
	 * Class constructor: creates a new empty taxon interaction instance.
94
	 */
95
	public TaxonInteraction() {
96
		super(null);
97
	}
98

    
99
	/**
100
	 * Creates a new empty taxon interaction instance.
101
	 */
102
	public static TaxonInteraction NewInstance(){
103
		return new TaxonInteraction();
104
	}
105

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

    
120

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

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

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

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

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

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

    
235

    
236
//*********************************** CLONE *****************************************/
237

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

    
249
		try {
250
			TaxonInteraction result = (TaxonInteraction)super.clone();
251

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

    
260

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