Project

General

Profile

Download (7.08 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.taxon;
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.XmlRootElement;
21
import javax.xml.bind.annotation.XmlSchemaType;
22
import javax.xml.bind.annotation.XmlType;
23

    
24
import org.apache.log4j.Logger;
25
import org.hibernate.annotations.Cascade;
26
import org.hibernate.annotations.CascadeType;
27
import org.hibernate.envers.Audited;
28
import org.hibernate.search.annotations.ContainedIn;
29
import org.hibernate.search.annotations.Indexed;
30
import org.springframework.beans.factory.annotation.Configurable;
31

    
32
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
33
import eu.etaxonomy.cdm.model.name.ITaxonNameBase;
34
import eu.etaxonomy.cdm.model.name.TaxonName;
35
import eu.etaxonomy.cdm.model.reference.Reference;
36
import eu.etaxonomy.cdm.strategy.cache.taxon.ITaxonCacheStrategy;
37
import eu.etaxonomy.cdm.strategy.cache.taxon.TaxonBaseDefaultCacheStrategy;
38
import eu.etaxonomy.cdm.validation.Level3;
39
import eu.etaxonomy.cdm.validation.annotation.HomotypicSynonymsShouldBelongToGroup;
40

    
41
/**
42
 * The class for synonyms: these are {@link TaxonBase taxa} the {@link name.TaxonName taxon names}
43
 * of which are not used by the {@link TaxonBase#getSec() reference} to designate a real
44
 * taxon but are mentioned as taxon names that were oder are used by some other
45
 * unspecified references to designate (at least to some extent) the same
46
 * particular real taxon. Synonyms that are {@link #getAcceptedTaxon() attached} to an accepted {@link Taxon taxon}
47
 * are actually meaningless.<BR>
48
 * Splitting taxa in "accepted/valid" and "synonyms"
49
 * makes it easier to handle particular relationships between
50
 * ("accepted/valid") {@link Taxon taxa} on the one hand and ("synonym") taxa
51
 *  on the other.
52
 *
53
 * @author m.doering
54
 * @since 08-Nov-2007 13:06:55
55
 */
56
@XmlAccessorType(XmlAccessType.FIELD)
57
@XmlType(name = "Synonym", propOrder = {
58
    "acceptedTaxon",
59
    "type",
60
})
61
@XmlRootElement(name = "Synonym")
62
@Entity
63
@Indexed(index = "eu.etaxonomy.cdm.model.taxon.TaxonBase")
64
@Audited
65
@Configurable
66
@HomotypicSynonymsShouldBelongToGroup(groups = Level3.class)
67
public class Synonym extends TaxonBase<ITaxonCacheStrategy<Synonym>> {
68

    
69
    private static final long serialVersionUID = 6977221584815363620L;
70

    
71
    @SuppressWarnings("unused")
72
	private static final Logger logger = Logger.getLogger(Synonym.class);
73

    
74
    @XmlElement(name = "acceptedTaxon")
75
    @XmlIDREF
76
    @XmlSchemaType(name = "IDREF")
77
    @ManyToOne(fetch = FetchType.LAZY)
78
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
79
    @ContainedIn
80
//  @NotEmpty(groups = Level2.class,message="{eu.etaxonomy.cdm.model.taxon.Synonym.noOrphanedSynonyms.message}")
81
//    @NotNull(groups = Level2.class)
82
    private Taxon acceptedTaxon;
83

    
84
    @XmlElement(name = "Type")
85
    @XmlIDREF
86
    @XmlSchemaType(name = "IDREF")
87
    @ManyToOne(fetch=FetchType.EAGER)
88
    private SynonymType type;
89

    
90
//************************************* FACTORY ****************************/
91
    /**
92
     * @see #NewInstance(TaxonName, Reference)
93
     * @param taxonName
94
     * @param sec
95
     * @return
96
     */
97
    public static Synonym NewInstance(ITaxonNameBase taxonName, Reference sec){
98
        return NewInstance(TaxonName.castAndDeproxy(taxonName), sec);
99
    }
100

    
101
    /**
102
     * Creates a new synonym instance with
103
     * the {@link eu.etaxonomy.cdm.model.name.TaxonName taxon name} used and the {@link eu.etaxonomy.cdm.model.reference.Reference reference}
104
     * using it as a synonym and not as an ("accepted/correct") {@link Taxon taxon}.
105
     *
106
     * @param  TaxonName    the taxon name used
107
     * @param  sec          the reference using the taxon name
108
     * @see  #Synonym(TaxonName, Reference)
109
     */
110
    public static Synonym NewInstance(TaxonName taxonName, Reference sec){
111
        Synonym result = new Synonym(taxonName, sec, null);
112
        return result;
113
    }
114

    
115
    public static Synonym NewInstance(TaxonName taxonName, Reference sec, String secDetail){
116
        Synonym result = new Synonym(taxonName, sec, secDetail);
117
        return result;
118
    }
119

    
120
// ************* CONSTRUCTORS *************/
121
	/**
122
	 * Class constructor: creates a new empty synonym instance.
123
	 *
124
	 * @see 	#Synonym(TaxonName, Reference)
125
	 */
126
	//TODO should be private, but still produces Spring init errors
127
	public Synonym(){
128
		this.cacheStrategy = new TaxonBaseDefaultCacheStrategy<>();
129
	}
130

    
131
	private Synonym(TaxonName taxonName, Reference sec, String secDetail){
132
		super(taxonName, sec, secDetail);
133
		this.cacheStrategy = new TaxonBaseDefaultCacheStrategy<>();
134
	}
135

    
136
//********************** GETTER/SETTER ******************************/
137

    
138
	/**
139
	 * Returns the "accepted/valid" {@link Taxon taxon}
140
	 *
141
	 */
142
	public Taxon getAcceptedTaxon() {
143
		return this.acceptedTaxon;
144
	}
145
    protected void setAcceptedTaxon(Taxon acceptedTaxon) {
146
        if (acceptedTaxon == null){
147
            Taxon oldTaxon = this.acceptedTaxon;
148
            this.acceptedTaxon = null;
149
            oldTaxon.removeSynonym(this);
150
        }else{
151
            if (this.acceptedTaxon != null){
152
                this.acceptedTaxon.removeSynonym(this, false);
153
            }
154
            this.acceptedTaxon = acceptedTaxon;
155
            this.acceptedTaxon.addSynonym(this);
156
            checkHomotypic();
157
        }
158
    }
159

    
160
    public SynonymType getType() {
161
        return type;
162
    }
163
    public void setType(SynonymType type) {
164
        this.type = type;
165
        checkHomotypic();
166
    }
167

    
168
//***************** METHODS **************************/
169
	/**
170
	 * Returns true if <i>this</i> is a synonym of the given taxon.
171
	 *
172
	 * @param taxon	the taxon to check synonym for
173
	 * @return	true if <i>this</i> is a synonm of the given taxon
174
	 *
175
	 * @see #getAcceptedTaxon()
176
	 */
177
	@Transient
178
	public boolean isSynonymOf(Taxon taxon){
179
		return taxon != null && taxon.equals(this.acceptedTaxon);
180
	}
181

    
182
	@Override
183
    @Transient
184
	public boolean isOrphaned() {
185
	    return this.acceptedTaxon == null || this.acceptedTaxon.isOrphaned();
186
	}
187

    
188
    /**
189
     * Checks if the synonym type is homotypic. If it is
190
     * the name of <code>this</code> synonym is added to the {@link HomotypicalGroup
191
     * homotypic group} of the {@link Taxon accepted taxon}.
192
     */
193
    private void checkHomotypic() {
194
        if (type != null && type.equals(SynonymType.HOMOTYPIC_SYNONYM_OF())
195
                && acceptedTaxon != null && acceptedTaxon.getName() != null){
196
                acceptedTaxon.getName().getHomotypicalGroup().addTypifiedName(this.getName());
197
        }
198
    }
199

    
200
//*********************** CLONE ********************************************************/
201

    
202
	@Override
203
	public Object clone() {
204
		Synonym result;
205
		result = (Synonym)super.clone();
206

    
207
		//no changes to accepted taxon, type, partial, proParte
208

    
209
		return result;
210

    
211
	}
212

    
213

    
214
}
(7-7/21)