Project

General

Profile

Download (9.11 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.occurrence.SpecimenOrObservationBase;
43

    
44
/**
45
 * This class represents associations between the described
46
 * {@link SpecimenOrObservationBase specimen or observation}
47
 * and a second one (for instance a host).
48
 * Only {@link SpecimenDescription specimen descriptions} may contain individuals association.
49
 * The association itself is described by a {@link MultilanguageText multilanguage text}.
50
 * <P>
51
 * This class corresponds (partially) to NaturalLanguageDescriptionType
52
 * according to the SDD schema.
53
 *
54
 * @author m.doering
55
 * @version 1.0
56
 * @created 08-Nov-2007 13:06:28
57
 */
58
@XmlAccessorType(XmlAccessType.FIELD)
59
@XmlType(name = "IndividualsAssociation", propOrder = {
60
    "description",
61
    "associatedSpecimenOrObservation"
62
})
63
@XmlRootElement(name = "IndividualsAssociation")
64
@Entity
65
@Audited
66
@Indexed(index = "eu.etaxonomy.cdm.model.description.DescriptionElementBase")
67
public class IndividualsAssociation extends DescriptionElementBase implements IMultiLanguageTextHolder, Cloneable{
68
	private static final long serialVersionUID = -4117554860254531809L;
69
	private static final Logger logger = Logger.getLogger(IndividualsAssociation.class);
70

    
71
	@XmlElement(name = "Description")
72
	@XmlJavaTypeAdapter(MultilanguageTextAdapter.class)
73
	@OneToMany(fetch = FetchType.LAZY, orphanRemoval=true)
74
	@MapKeyJoinColumn(name="description_mapkey_id")
75
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.DELETE })
76
	@JoinTable(name = "IndividualsAssociation_LanguageString")  //to distinguish from other DescriptionElementBase_LanguageString cases
77
	private Map<Language,LanguageString> description = new HashMap<Language,LanguageString>();
78

    
79
	@XmlElement(name = "AssociatedSpecimenOrObservation")
80
	@XmlIDREF
81
	@XmlSchemaType(name = "IDREF")
82
	@ManyToOne(fetch = FetchType.LAZY)
83
	@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
84
	private SpecimenOrObservationBase associatedSpecimenOrObservation;
85

    
86
	/**
87
	 * Class constructor: creates a new empty individuals association instance.
88
	 */
89
	protected IndividualsAssociation(){
90
		super(null);
91
	}
92

    
93
	/**
94
	 * Creates a new empty individuals association instance.
95
	 */
96
	public static IndividualsAssociation NewInstance(){
97
		return NewInstance(null);
98
	}
99

    
100
	/**
101
	 * Creates a new empty individuals association instance.
102
	 */
103
	public static IndividualsAssociation NewInstance(SpecimenOrObservationBase specimen){
104
		IndividualsAssociation result =  new IndividualsAssociation();
105
		result.setFeature(Feature.INDIVIDUALS_ASSOCIATION());
106
		result.setAssociatedSpecimenOrObservation(specimen);
107
		return result;
108
	}
109

    
110

    
111
	/**
112
	 * Returns the second {@link SpecimenOrObservationBase specimen or observation}
113
	 * involved in <i>this</i> individuals association.
114
	 * The first specimen or observation is the specimen or observation
115
	 * described in the corresponding {@link SpecimenDescription specimen description}.
116
	 */
117
	public SpecimenOrObservationBase getAssociatedSpecimenOrObservation() {
118
		return associatedSpecimenOrObservation;
119
	}
120
	/**
121
	 * @see	#getAssociatedSpecimenOrObservation()
122
	 */
123
	public void setAssociatedSpecimenOrObservation(
124
			SpecimenOrObservationBase associatedSpecimenOrObservation) {
125
		this.associatedSpecimenOrObservation = associatedSpecimenOrObservation;
126
	}
127

    
128

    
129
	/**
130
	 * Returns the {@link MultilanguageText multilanguage text} used to describe
131
	 * <i>this</i> individuals association. The different {@link LanguageString language strings}
132
	 * contained in the multilanguage text should all have the same meaning.
133
	 */
134
	public Map<Language,LanguageString> getDescription(){
135
		return this.description;
136
	}
137

    
138
	/**
139
	 * Adds a translated {@link LanguageString text in a particular language}
140
	 * to the {@link MultilanguageText multilanguage text} used to describe
141
	 * <i>this</i> individuals association.
142
	 *
143
	 * @param description	the language string describing the individuals association
144
	 * 						in a particular language
145
	 * @see    	   			#getDescription()
146
	 * @see    	   			#putDescription(Language, String)
147
	 * @deprecated 			should follow the put semantic of maps, this method will be removed in v4.0
148
	 * 						Use the {@link #putDescription(LanguageString) putDescription} method instead
149
	 */
150
	@Deprecated
151
    public void addDescription(LanguageString description){
152
		this.putDescription(description);
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> individuals association.
159
	 *
160
	 * @param description	the language string describing the individuals association
161
	 * 						in a particular language
162
	 * @see    	   			#getDescription()
163
	 * @see    	   			#putDescription(Language, String)
164
	 *
165
	 */
166
	public void putDescription(LanguageString description){
167
		this.description.put(description.getLanguage(),description);
168
	}
169
	/**
170
	 * Creates a {@link LanguageString language string} based on the given text string
171
	 * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
172
	 * used to describe <i>this</i> individuals association.
173
	 *
174
	 * @param text		the string describing the individuals association
175
	 * 					in a particular language
176
	 * @param language	the language in which the text string is formulated
177
	 * @see    	   		#getDescription()
178
	 * @see    	   		#putDescription(LanguageString)
179
	 */
180
	public void putDescription(Language language, String text){
181
		this.description.put(language, LanguageString.NewInstance(text, language));
182
	}
183

    
184
	/**
185
	 * Creates a {@link LanguageString language string} based on the given text string
186
	 * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
187
	 * used to describe <i>this</i> individuals association.
188
	 *
189
	 * @param text		the string describing the individuals association
190
	 * 					in a particular language
191
	 * @param language	the language in which the text string is formulated
192
	 * @see    	   		#getDescription()
193
	 * @see    	   		#putDescription(LanguageString)
194
	 * @deprecated		should follow the put semantic of maps, this method will be removed in v4.0
195
	 * 					Use the {@link #putDescription(Language, String) putDescription} method instead
196
	 */
197
	@Deprecated
198
    public void addDescription(String text, Language language){
199
		this.putDescription(language, text);
200
	}
201
	/**
202
	 * Removes from the {@link MultilanguageText multilanguage text} used to describe
203
	 * <i>this</i> individuals association the one {@link LanguageString language string}
204
	 * with the given {@link Language language}.
205
	 *
206
	 * @param  language	the language in which the language string to be removed
207
	 * 					has been formulated
208
	 * @see     		#getDescription()
209
	 */
210
	public void removeDescription(Language language){
211
		this.description.remove(language);
212
	}
213

    
214

    
215
//*********************************** CLONE *****************************************/
216

    
217
	/**
218
	 * Clones <i>this</i> individuals association. This is a shortcut that enables to create
219
	 * a new instance that differs only slightly from <i>this</i> individuals association by
220
	 * modifying only some of the attributes.
221
	 *
222
	 * @see eu.etaxonomy.cdm.model.description.DescriptionElementBase#clone()
223
	 * @see java.lang.Object#clone()
224
	 */
225
	@Override
226
	public Object clone() {
227

    
228
		try {
229
			IndividualsAssociation result = (IndividualsAssociation)super.clone();
230

    
231
			//description
232
			result.description = new HashMap<Language, LanguageString>();
233
			for (Language language : getDescription().keySet()){
234
				//TODO clone needed?
235
				LanguageString newLanguageString = (LanguageString)getDescription().get(language).clone();
236
				result.description.put(language, newLanguageString);
237
			}
238

    
239

    
240
			return result;
241
			//no changes to: associatedSpecimenOrObservation
242
		} catch (CloneNotSupportedException e) {
243
			logger.warn("Object does not implement cloneable");
244
			e.printStackTrace();
245
			return null;
246
		}
247
	}
248

    
249
}
(14-14/36)