| 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.validation.Valid; |
|---|
| 17 | import javax.validation.constraints.NotNull; |
|---|
| 18 | import javax.xml.bind.annotation.XmlAccessType; |
|---|
| 19 | import javax.xml.bind.annotation.XmlAccessorType; |
|---|
| 20 | import javax.xml.bind.annotation.XmlElement; |
|---|
| 21 | import javax.xml.bind.annotation.XmlIDREF; |
|---|
| 22 | import javax.xml.bind.annotation.XmlRootElement; |
|---|
| 23 | import javax.xml.bind.annotation.XmlSchemaType; |
|---|
| 24 | import javax.xml.bind.annotation.XmlType; |
|---|
| 25 | |
|---|
| 26 | import org.apache.log4j.Logger; |
|---|
| 27 | import org.hibernate.annotations.Cascade; |
|---|
| 28 | import org.hibernate.annotations.CascadeType; |
|---|
| 29 | import org.hibernate.envers.Audited; |
|---|
| 30 | |
|---|
| 31 | import eu.etaxonomy.cdm.model.common.IRelated; |
|---|
| 32 | import eu.etaxonomy.cdm.model.common.RelationshipBase; |
|---|
| 33 | import eu.etaxonomy.cdm.model.common.RelationshipTermBase; |
|---|
| 34 | import eu.etaxonomy.cdm.model.reference.Reference; |
|---|
| 35 | import eu.etaxonomy.cdm.validation.Level2; |
|---|
| 36 | import eu.etaxonomy.cdm.validation.Level3; |
|---|
| 37 | import eu.etaxonomy.cdm.validation.annotation.HomotypicSynonymsShouldBelongToGroup; |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * The class representing the assignation of a {@link Synonym synonym} to an |
|---|
| 41 | * ("accepted/correct") {@link Taxon taxon}. This includes a {@link SynonymRelationshipType synonym relationship type} |
|---|
| 42 | * (for instance "heterotypic synonym of"). Within a synonym relationship the |
|---|
| 43 | * synonym plays the source role and the taxon the target role. Between a |
|---|
| 44 | * synonym and an ("accepted/correct") taxon there should exist at most one |
|---|
| 45 | * synonym relationship.<BR> |
|---|
| 46 | * Both, synonym and ("accepted/correct") taxon, must have the same |
|---|
| 47 | * {@link TaxonBase#getSec() concept reference}. |
|---|
| 48 | * <P> |
|---|
| 49 | * This class corresponds in part to: <ul> |
|---|
| 50 | * <li> Relationship according to the TDWG ontology |
|---|
| 51 | * <li> TaxonRelationship according to the TCS |
|---|
| 52 | * </ul> |
|---|
| 53 | * |
|---|
| 54 | * @author m.doering |
|---|
| 55 | * @version 1.0 |
|---|
| 56 | * @created 08-Nov-2007 13:06:55 |
|---|
| 57 | */ |
|---|
| 58 | @XmlAccessorType(XmlAccessType.FIELD) |
|---|
| 59 | @XmlType(name = "SynonymRelationship", propOrder = { |
|---|
| 60 | "relatedFrom", |
|---|
| 61 | "relatedTo", |
|---|
| 62 | "type", |
|---|
| 63 | "proParte", |
|---|
| 64 | "partial" |
|---|
| 65 | }) |
|---|
| 66 | @XmlRootElement(name = "SynonymRelationship") |
|---|
| 67 | @Entity |
|---|
| 68 | @Audited |
|---|
| 69 | @HomotypicSynonymsShouldBelongToGroup(groups = Level3.class) |
|---|
| 70 | public class SynonymRelationship extends RelationshipBase<Synonym, Taxon, SynonymRelationshipType> { |
|---|
| 71 | private static final Logger logger = Logger.getLogger(SynonymRelationship.class); |
|---|
| 72 | |
|---|
| 73 | @XmlElement(name = "IsProParte") |
|---|
| 74 | private boolean proParte = false; |
|---|
| 75 | |
|---|
| 76 | @XmlElement(name = "IsPartial") |
|---|
| 77 | private boolean partial = false; |
|---|
| 78 | |
|---|
| 79 | @XmlElement(name = "RelatedFrom") |
|---|
| 80 | @XmlIDREF |
|---|
| 81 | @XmlSchemaType(name = "IDREF") |
|---|
| 82 | @ManyToOne(fetch=FetchType.EAGER) |
|---|
| 83 | @Cascade(CascadeType.SAVE_UPDATE) |
|---|
| 84 | @NotNull(groups = Level2.class) |
|---|
| 85 | @Valid |
|---|
| 86 | private Synonym relatedFrom; |
|---|
| 87 | |
|---|
| 88 | @XmlElement(name = "RelatedTo") |
|---|
| 89 | @XmlIDREF |
|---|
| 90 | @XmlSchemaType(name = "IDREF") |
|---|
| 91 | @ManyToOne(fetch=FetchType.EAGER) |
|---|
| 92 | @Cascade(CascadeType.SAVE_UPDATE) |
|---|
| 93 | @NotNull(groups = Level2.class) |
|---|
| 94 | @Valid |
|---|
| 95 | private Taxon relatedTo; |
|---|
| 96 | |
|---|
| 97 | @XmlElement(name = "Type") |
|---|
| 98 | @XmlIDREF |
|---|
| 99 | @XmlSchemaType(name = "IDREF") |
|---|
| 100 | @ManyToOne(fetch=FetchType.EAGER) |
|---|
| 101 | private SynonymRelationshipType type; |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * @deprecated for hibernate only, don't use |
|---|
| 105 | */ |
|---|
| 106 | @Deprecated |
|---|
| 107 | private SynonymRelationship(){ |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | * Class constructor: creates a new synonym relationship instance (with the |
|---|
| 112 | * given {@link Synonym synonym}, the given "accepted/correct" {@link Taxon taxon}, |
|---|
| 113 | * the given {@link SynonymRelationshipType synonym relationship type} and with the |
|---|
| 114 | * {@link eu.etaxonomy.cdm.model.reference.Reference reference source} on which the relationship assertion is based). |
|---|
| 115 | * Moreover the new synonym relationship will be added to the respective |
|---|
| 116 | * sets of synonym relationships assigned to the synonym and to the |
|---|
| 117 | * "accepted/correct" taxon. |
|---|
| 118 | * |
|---|
| 119 | * @param synonym the synonym instance involved in the new synonym relationship |
|---|
| 120 | * @param taxon the taxon instance involved in the new synonym relationship |
|---|
| 121 | * @param type the synonym relationship type of the new synonym relationship |
|---|
| 122 | * @param citation the reference source for the new synonym relationship |
|---|
| 123 | * @param citationMicroReference the string with the details describing the exact localisation within the reference |
|---|
| 124 | * @see eu.etaxonomy.cdm.model.common.RelationshipBase#RelationshipBase(IRelated, IRelated, RelationshipTermBase, Reference, String) |
|---|
| 125 | */ |
|---|
| 126 | protected SynonymRelationship(Synonym synonym, Taxon taxon, SynonymRelationshipType type, Reference citation, String citationMicroReference) { |
|---|
| 127 | super(synonym, taxon, type, citation, citationMicroReference); |
|---|
| 128 | if (type != null && type.equals(SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF()) && taxon != null && taxon.getName() != null && synonym != null && synonym != null){ |
|---|
| 129 | taxon.getName().getHomotypicalGroup().addTypifiedName(synonym.getName()); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * Returns "true" if the ProParte flag is set. This indicates that, within |
|---|
| 136 | * <i>this</i> synonym relationship, the {@link name.TaxonNameBase taxon name} used as a |
|---|
| 137 | * {@link Synonym synonym} designated originally a real taxon which later has |
|---|
| 138 | * been split. In this case the synonym is therefore the synonym of at least |
|---|
| 139 | * two different ("accepted/correct") {@link Taxon taxa} and at least one |
|---|
| 140 | * more synonym relationship with the same synonym should exist. |
|---|
| 141 | */ |
|---|
| 142 | public boolean isProParte() { |
|---|
| 143 | return proParte; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * @see #isProParte() |
|---|
| 148 | */ |
|---|
| 149 | public void setProParte(boolean proParte) { |
|---|
| 150 | this.proParte = proParte; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | /** |
|---|
| 154 | * Returns "true" if the ProParte flag is set. This indicates that, within |
|---|
| 155 | * <i>this</i> synonym relationship, the {@link name.TaxonNameBase taxon name} used as a |
|---|
| 156 | * {@link Synonym synonym} designated originally a real taxon which later has |
|---|
| 157 | * been lumped together with another one. In this case the |
|---|
| 158 | * ("accepted/correct") {@link Taxon taxon} has therefore at least |
|---|
| 159 | * two different synonyms (for the two lumped real taxa) and at least one |
|---|
| 160 | * more synonym relationship with the same ("accepted/correct") taxon should |
|---|
| 161 | * exist. |
|---|
| 162 | */ |
|---|
| 163 | public boolean isPartial() { |
|---|
| 164 | return partial; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /** |
|---|
| 168 | * @see #isPartial() |
|---|
| 169 | */ |
|---|
| 170 | public void setPartial(boolean partial) { |
|---|
| 171 | this.partial = partial; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /** |
|---|
| 175 | * Returns the ("accepted/correct") {@link Taxon taxon} involved in <i>this</i> |
|---|
| 176 | * synonym relationship. The taxon plays the target role in the relationship. |
|---|
| 177 | * |
|---|
| 178 | * @see #getSynonym() |
|---|
| 179 | * @see Synonym#getAcceptedTaxa() |
|---|
| 180 | * @see eu.etaxonomy.cdm.model.common.RelationshipBase#getRelatedTo() |
|---|
| 181 | * @see eu.etaxonomy.cdm.model.common.RelationshipBase#getType() |
|---|
| 182 | */ |
|---|
| 183 | @Transient |
|---|
| 184 | public Taxon getAcceptedTaxon(){ |
|---|
| 185 | return this.getRelatedTo(); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /** |
|---|
| 189 | * Sets the given ("accepted/valid") {@link Taxon taxon} to <i>this</i> |
|---|
| 190 | * synonym relationship. Therefore <i>this</i> synonym relationship will be |
|---|
| 191 | * added to the corresponding set of synonym relationships assigned to the |
|---|
| 192 | * given taxon. |
|---|
| 193 | * |
|---|
| 194 | * @param acceptedTaxon the taxon instance to be set in <i>this</i> synonym relationship |
|---|
| 195 | * @see #getAcceptedTaxon() |
|---|
| 196 | * @see Taxon#getSynonymRelations() |
|---|
| 197 | */ |
|---|
| 198 | public void setAcceptedTaxon(Taxon acceptedTaxon){ |
|---|
| 199 | this.setRelatedTo(acceptedTaxon); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | /** |
|---|
| 203 | * Returns the {@link Synonym synonym} involved in <i>this</i> synonym |
|---|
| 204 | * relationship. The synonym plays the source role in the relationship. |
|---|
| 205 | * |
|---|
| 206 | * @see #getAcceptedTaxon() |
|---|
| 207 | * @see Taxon#getSynonyms() |
|---|
| 208 | * @see eu.etaxonomy.cdm.model.common.RelationshipBase#getRelatedFrom() |
|---|
| 209 | * @see eu.etaxonomy.cdm.model.common.RelationshipBase#getType() |
|---|
| 210 | */ |
|---|
| 211 | @Transient |
|---|
| 212 | public Synonym getSynonym(){ |
|---|
| 213 | return this.getRelatedFrom(); |
|---|
| 214 | } |
|---|
| 215 | /** |
|---|
| 216 | * Sets the given {@link Synonym synonym} to <i>this</i> synonym relationship. |
|---|
| 217 | * Therefore <i>this</i> synonym relationship will be |
|---|
| 218 | * added to the corresponding set of synonym relationships assigned to the |
|---|
| 219 | * given synonym. Furthermore if the given synonym replaces an "old" one |
|---|
| 220 | * <i>this</i> synonym relationship will be removed from the set of synonym |
|---|
| 221 | * relationships assigned to the "old" synonym. |
|---|
| 222 | * |
|---|
| 223 | * @param synonym the synonym instance to be set in <i>this</i> synonym relationship |
|---|
| 224 | * @see #getSynonym() |
|---|
| 225 | * @see Synonym#getSynonymRelations() |
|---|
| 226 | */ |
|---|
| 227 | public void setSynonym(Synonym synonym){ |
|---|
| 228 | this.setRelatedFrom(synonym); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | protected Synonym getRelatedFrom() { |
|---|
| 232 | return relatedFrom; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | protected Taxon getRelatedTo() { |
|---|
| 236 | return relatedTo; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | public SynonymRelationshipType getType() { |
|---|
| 240 | return type; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | protected void setRelatedFrom(Synonym relatedFrom) { |
|---|
| 244 | if (relatedFrom == null){ |
|---|
| 245 | this.deletedObjects.add(this.relatedFrom); |
|---|
| 246 | } |
|---|
| 247 | this.relatedFrom = relatedFrom; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | protected void setRelatedTo(Taxon relatedTo) { |
|---|
| 251 | if (relatedTo == null){ |
|---|
| 252 | this.deletedObjects.add(this.relatedTo); |
|---|
| 253 | } |
|---|
| 254 | this.relatedTo = relatedTo; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | public void setType(SynonymRelationshipType type) { |
|---|
| 258 | this.type = type; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | //*********************************** CLONE *****************************************/ |
|---|
| 262 | |
|---|
| 263 | /** |
|---|
| 264 | * Clones <i>this</i> SynonymRelationship. This is a shortcut that enables to create |
|---|
| 265 | * a new instance that differs only slightly from <i>this</i> SynonymRelationship by |
|---|
| 266 | * modifying only some of the attributes. |
|---|
| 267 | * |
|---|
| 268 | * @see eu.etaxonomy.cdm.model.common.RelationshipBase#clone() |
|---|
| 269 | * @see java.lang.Object#clone() |
|---|
| 270 | */ |
|---|
| 271 | @Override |
|---|
| 272 | public Object clone() { |
|---|
| 273 | SynonymRelationship result; |
|---|
| 274 | |
|---|
| 275 | try{ |
|---|
| 276 | result = (SynonymRelationship) super.clone(); |
|---|
| 277 | //no changes to relatedFrom, relatedTo, type, partial, proParte |
|---|
| 278 | |
|---|
| 279 | return result; |
|---|
| 280 | } catch (CloneNotSupportedException e) { |
|---|
| 281 | logger.warn("Object does not implement cloneable"); |
|---|
| 282 | e.printStackTrace(); |
|---|
| 283 | return null; |
|---|
| 284 | } |
|---|
| 285 | } |
|---|
| 286 | } |
|---|