Project

General

Profile

Download (10 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.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
24
import org.hibernate.envers.Audited;
25

    
26
import eu.etaxonomy.cdm.model.common.RelationshipBase;
27
import eu.etaxonomy.cdm.model.reference.Reference;
28

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

    
59
    private static final long serialVersionUID = -78515930138896939L;
60
    private static final Logger logger = LogManager.getLogger(HybridRelationship.class);
61

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

    
67
    private RuleConsidered ruleConsidered;
68

    
69
	@XmlElement(name = "RelatedFrom")
70
    @XmlIDREF
71
    @XmlSchemaType(name = "IDREF")
72
    @ManyToOne(fetch=FetchType.LAZY)
73
//    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
74
	private TaxonName relatedFrom;
75

    
76
	@XmlElement(name = "RelatedTo")
77
    @XmlIDREF
78
    @XmlSchemaType(name = "IDREF")
79
    @ManyToOne(fetch=FetchType.LAZY)
80
//    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
81
	private TaxonName relatedTo;
82

    
83
    @XmlElement(name = "Type")
84
    @XmlIDREF
85
    @XmlSchemaType(name = "IDREF")
86
    @ManyToOne(fetch=FetchType.LAZY)
87
	private HybridRelationshipType type;
88

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

    
97

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

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

    
136
	//********* METHODS **************************************/
137

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

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

    
174
	/**
175
	 * Returns the ICBN rule considered (that is the
176
	 * article/note/recommendation in the nomenclatural code) for building
177
	 * the string representing the (child) hybrid {@link BotanicalName taxon name}
178
	 * within <i>this</i> hybrid relationship.
179
	 */
180
	@Override
181
    public String getRuleConsidered(){
182
		return this.ruleConsidered().getText();
183
	}
184
	/**
185
	 * @see  #getRuleConsidered()
186
	 */
187
	@Override
188
    public void setRuleConsidered(String ruleConsidered){
189
		this.ruleConsidered().setText(ruleConsidered);
190
	}
191
	/**
192
     * The {@link NomenclaturalCodeEdition code edition} for the {@link #getRuleConsidered() rule considered}.
193
     */
194
    @Override
195
    public NomenclaturalCodeEdition getCodeEdition() {
196
        return ruleConsidered().getCodeEdition();
197
    }
198
    @Override
199
    public void setCodeEdition(NomenclaturalCodeEdition codeEdition) {
200
        ruleConsidered().setCodeEdition(codeEdition);
201
    }
202
	private RuleConsidered ruleConsidered(){
203
	    if(this.ruleConsidered==null){
204
	        ruleConsidered = new RuleConsidered();
205
	    }
206
	    return ruleConsidered;
207
	}
208

    
209
	@Override
210
    protected TaxonName getRelatedFrom() {
211
		return relatedFrom;
212
	}
213

    
214
	@Override
215
    protected TaxonName getRelatedTo() {
216
		return relatedTo;
217
	}
218

    
219
	@Override
220
    public HybridRelationshipType getType() {
221
		return type;
222
	}
223

    
224
	@Override
225
    protected void setRelatedFrom(INonViralName relatedFrom) {
226
		this.relatedFrom = TaxonName.castAndDeproxy(relatedFrom);
227
	}
228

    
229
	@Override
230
    protected void setRelatedTo(INonViralName relatedTo) {
231
		this.relatedTo = TaxonName.castAndDeproxy(relatedTo);
232
	}
233

    
234
	@Override
235
    public void setType(HybridRelationshipType type) {
236
		this.type = type;
237
	}
238

    
239
// ************************ compareTo *************************************************
240

    
241
	@Override
242
    public int compareTo(HybridRelationship rel2) {
243
		HybridRelationshipType type1 = this.getType();
244
		HybridRelationshipType type2 = rel2.getType();
245
		int compareType = type1.compareTo(type2);
246
		if (compareType != 0){
247
			return compareType;
248
		}else{
249
		    TaxonName related1 = this.getRelatedFrom();
250
		    TaxonName related2 = rel2.getRelatedFrom();
251
			if (related1 != related2){
252
				related1 = this.getRelatedTo();
253
				related2 = rel2.getRelatedTo();
254
			}
255
			if (related1.equals(related2)){
256
			    return 0;
257
			}
258

    
259
			String title1 = related1.getTitleCache();
260
			String title2 = related2.getTitleCache();
261
			return title1.compareTo(title2);
262
		}
263
	}
264

    
265
//*********************** CLONE ********************************************************/
266

    
267
	/**
268
	 * Clones <i>this</i> hybrid relationship. This is a shortcut that enables to create
269
	 * a new instance that differs only slightly from <i>this</i> hybrid relationship by
270
	 * modifying only some of the attributes.<BR>
271
	 * CAUTION: Cloning a relationship will not add the relationship to the according
272
	 * {@link #relatedFrom} and {@link #relatedTo} objects. The method is meant to be used
273
	 * mainly for internal purposes (e.g. used within {@link TaxonName#clone()}
274
	 *
275
	 * @see eu.etaxonomy.cdm.model.common.RelationshipBase#clone()
276
	 * @see java.lang.Object#clone()
277
	 */
278
	@Override
279
	public HybridRelationship clone() {
280
		HybridRelationship result;
281
		try {
282
			result = (HybridRelationship)super.clone();
283
			result.ruleConsidered = this.ruleConsidered == null? null : this.ruleConsidered.clone();
284
			//no changes to: relatedFrom, relatedTo, type
285
			return result;
286
		} catch (CloneNotSupportedException e) {
287
			logger.warn("Object does not implement cloneable");
288
			e.printStackTrace();
289
			return null;
290
		}
291
	}
292
}
(2-2/39)