Project

General

Profile

Download (9.2 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.name;
11

    
12
import javax.persistence.Entity;
13
import javax.persistence.FetchType;
14
import javax.persistence.ManyToOne;
15
import javax.persistence.Transient;
16
import javax.xml.bind.annotation.XmlAccessType;
17
import javax.xml.bind.annotation.XmlAccessorType;
18
import javax.xml.bind.annotation.XmlElement;
19
import javax.xml.bind.annotation.XmlIDREF;
20
import javax.xml.bind.annotation.XmlSchemaType;
21
import javax.xml.bind.annotation.XmlType;
22

    
23
import org.apache.log4j.Logger;
24
import org.hibernate.annotations.Cascade;
25
import org.hibernate.annotations.CascadeType;
26
import org.hibernate.envers.Audited;
27

    
28
import eu.etaxonomy.cdm.model.common.RelationshipBase;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30

    
31
/**
32
 * The class representing a hybrid relationship between one of the {@link BotanicalName parents}
33
 * of a hybrid taxon name and the hybrid taxon name itself. A hybrid taxon name
34
 * is a {@link BotanicalName botanical taxon name} assigned to a hybrid plant following
35
 * the {@link NomenclaturalCode#ICBN() ICBN} (Appendix I). A hybrid taxon name must have one
36
 * of the hybrid flags set. The hybrid relationship includes a {@link HybridRelationshipType hybrid relationship type}
37
 * (for instance "first parent" or "female parent") and the article of the ICBN
38
 * on which the hybrid taxon name relies.
39
 * <P>
40
 * This class corresponds partially to: <ul>
41
 * <li> Relationship according to the TDWG ontology
42
 * <li> TaxonRelationship according to the TCS
43
 * </ul>
44
 *
45
 * @author m.doering
46
 * @version 1.0
47
 * @created 08-Nov-2007 13:06:26
48
 */
49
@XmlAccessorType(XmlAccessType.FIELD)
50
@XmlType(name = "HybridRelationship", propOrder = {
51
	"relatedFrom",
52
	"relatedTo",
53
	"type",
54
    "ruleConsidered"
55
})
56
@Entity
57
@Audited
58
public class HybridRelationship extends RelationshipBase<INonViralName, INonViralName, HybridRelationshipType>  implements Cloneable, Comparable<HybridRelationship>{
59
	private static final Logger logger = Logger.getLogger(HybridRelationship.class);
60

    
61
	//The nomenclatural code rule considered. The article/note/recommendation in the code in question that is commented on in
62
	//the note property.
63
	@XmlElement(name = "RuleConsidered")
64
	private String ruleConsidered;
65

    
66
	@XmlElement(name = "RelatedFrom")
67
    @XmlIDREF
68
    @XmlSchemaType(name = "IDREF")
69
    @ManyToOne(fetch=FetchType.LAZY)
70
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
71
	private TaxonNameBase<?,?> relatedFrom;
72

    
73
	@XmlElement(name = "RelatedTo")
74
    @XmlIDREF
75
    @XmlSchemaType(name = "IDREF")
76
    @ManyToOne(fetch=FetchType.LAZY)
77
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
78
	private TaxonNameBase<?,?> relatedTo;
79

    
80
    @XmlElement(name = "Type")
81
    @XmlIDREF
82
    @XmlSchemaType(name = "IDREF")
83
    @ManyToOne(fetch=FetchType.LAZY)
84
	private HybridRelationshipType type;
85

    
86
	/**
87
	 * @deprecated for hibernate use only, don't use
88
	 */
89
	@Deprecated
90
	private HybridRelationship(){
91
		super();
92
	}
93

    
94

    
95
	// ************* CONSTRUCTORS *************/
96
	/**
97
	 * Class constructor: creates a new hybrid relationship instance with no
98
	 * reference and adds it to the respective
99
	 * {@link BotanicalName#getHybridRelationships() botanical taxon name relation sets} of both involved names.
100
	 *
101
	 * @param toName			the taxon name to be set as target for the new hybrid relationship
102
	 * @param fromName			the taxon name to be set as source for the new hybrid relationship
103
	 * @param type				the relationship type to be assigned to the new hybrid relationship
104
	 * @param ruleConsidered	the string indicating the article of the ICBN for the hybrid taxon name
105
	 * @see						#HybridRelationship(BotanicalName, BotanicalName, HybridRelationshipType, Reference, String, String)
106
	 * @see						TaxonNameBase#addHybridRelationship(HybridRelationship)
107
	 */
108
	protected HybridRelationship(INonViralName hybridName, INonViralName parentName, HybridRelationshipType type, String ruleConsidered) {
109
		this(hybridName, parentName, type, null, null, ruleConsidered);
110
	}
111

    
112
	/**
113
	 * Class constructor: creates a new hybrid relationship instance including
114
	 * its {@link eu.etaxonomy.cdm.model.reference.Reference reference source} and adds it to the respective
115
	 *{@link BotanicalName#getHybridRelationships() botanical taxon name relation sets} of both involved names.
116
	 *
117
	 * @param toName				the taxon name to be set as target for the new hybrid relationship
118
	 * @param fromName				the taxon name to be set as source for the new hybrid relationship
119
	 * @param type					the relationship type to be assigned to the new hybrid relationship
120
	 * @param citation				the reference source for the new hybrid relationship
121
	 * @param citationMicroReference	the string with the details describing the exact localisation within the reference
122
	 * @param ruleConsidered		the string indicating the article of the ICBN for the hybrid taxon name
123
	 * @see							#HybridRelationship(BotanicalName, BotanicalName, HybridRelationshipType, String)
124
	 * @see							TaxonNameBase#addHybridRelationship(HybridRelationship)
125
	 */
126
	protected HybridRelationship(INonViralName  hybridName, INonViralName parentName, HybridRelationshipType type, Reference citation, String citationMicroReference, String ruleConsidered) {
127
		super(parentName, hybridName, type, citation, citationMicroReference);
128
		this.setRuleConsidered(ruleConsidered);
129
	}
130

    
131
	//********* METHODS **************************************/
132

    
133
	/**
134
	 * Returns the {@link BotanicalName botanical taxon name} that plays the parent role
135
	 * in <i>this</i> hybrid relationship.
136
	 *
137
	 * @see   #getHybridName()
138
	 * @see   eu.etaxonomy.cdm.model.common.RelationshipBase#getRelatedFrom()
139
	 */
140
	@Transient
141
	public TaxonNameBase getParentName(){
142
		return this.getRelatedFrom();
143
	}
144
	/**
145
	 * @see  #getParentName()
146
	 */
147
	public void setParentName(NonViralName parentName){
148
		this.setRelatedFrom(parentName);
149
	}
150

    
151
	/**
152
	 * Returns the {@link BotanicalName botanical taxon name} that plays the child role
153
	 * (the child is actually the hybrid taxon name) in <i>this</i> hybrid relationship.
154
	 *
155
	 * @see   #getParentName()
156
	 * @see   eu.etaxonomy.cdm.model.common.RelationshipBase#getRelatedTo()
157
	 */
158
	@Transient
159
	public TaxonNameBase getHybridName(){
160
		return this.getRelatedTo();
161
	}
162
	/**
163
	 * @see  #getHybridName()
164
	 */
165
	public void setHybridName(NonViralName hybridName){
166
		this.setRelatedTo(hybridName);
167
	}
168

    
169
	/**
170
	 * Returns the ICBN rule considered (that is the
171
	 * article/note/recommendation in the nomenclatural code) for building
172
	 * the string representing the (child) hybrid {@link BotanicalName taxon name}
173
	 * within <i>this</i> hybrid relationship.
174
	 */
175
	public String getRuleConsidered(){
176
		return this.ruleConsidered;
177
	}
178
	/**
179
	 * @see  #getRuleConsidered()
180
	 */
181
	public void setRuleConsidered(String ruleConsidered){
182
		this.ruleConsidered = ruleConsidered;
183
	}
184

    
185
	@Override
186
    protected TaxonNameBase getRelatedFrom() {
187
		return relatedFrom;
188
	}
189

    
190
	@Override
191
    protected TaxonNameBase getRelatedTo() {
192
		return relatedTo;
193
	}
194

    
195
	@Override
196
    public HybridRelationshipType getType() {
197
		return type;
198
	}
199

    
200
	@Override
201
    protected void setRelatedFrom(INonViralName relatedFrom) {
202
		this.relatedFrom = TaxonNameBase.castAndDeproxy(relatedFrom);
203
	}
204

    
205
	@Override
206
    protected void setRelatedTo(INonViralName relatedTo) {
207
		this.relatedTo = TaxonNameBase.castAndDeproxy(relatedTo);
208
	}
209

    
210
	@Override
211
    public void setType(HybridRelationshipType type) {
212
		this.type = type;
213
	}
214

    
215
// ************************ compareTo *************************************************
216

    
217
	@Override
218
    public int compareTo(HybridRelationship rel2) {
219
		HybridRelationshipType type1 = this.getType();
220
		HybridRelationshipType type2 = rel2.getType();
221
		int compareType = type1.compareTo(type2);
222
		if (compareType != 0){
223
			return compareType;
224
		}else{
225
		    TaxonNameBase<?,?> related1 = this.getRelatedFrom();
226
		    TaxonNameBase<?,?> related2 = rel2.getRelatedFrom();
227
			if (related1 != related2){
228
				related1 = this.getRelatedTo();
229
				related2 = rel2.getRelatedTo();
230
			}
231
			if (related1.equals(related2)){
232
			    return 0;
233
			}
234

    
235
			String title1 = related1.getTitleCache();
236
			String title2 = related2.getTitleCache();
237
			return title1.compareTo(title2);
238
		}
239
	}
240

    
241

    
242
//*********************** CLONE ********************************************************/
243

    
244
	/**
245
	 * Clones <i>this</i> hybrid relationship. This is a shortcut that enables to create
246
	 * a new instance that differs only slightly from <i>this</i> hybrid relationship by
247
	 * modifying only some of the attributes.<BR>
248
	 * CAUTION: Cloning a relationship will not add the relationship to the according
249
	 * {@link #relatedFrom} and {@link #relatedTo} objects. The method is meant to be used
250
	 * mainly for internal purposes (e.g. used within {@link TaxonNameBase#clone()}
251
	 *
252
	 * @see eu.etaxonomy.cdm.model.common.RelationshipBase#clone()
253
	 * @see java.lang.Object#clone()
254
	 */
255
	@Override
256
	public Object clone() {
257
		HybridRelationship result;
258
		try {
259
			result = (HybridRelationship)super.clone();
260
			//no changes to: relatedFrom, relatedTo, type
261
			return result;
262
		} catch (CloneNotSupportedException e) {
263
			logger.warn("Object does not implement cloneable");
264
			e.printStackTrace();
265
			return null;
266
		}
267
	}
268

    
269
}
(6-6/37)