(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / taxon / Synonym.java
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
13 import org.apache.log4j.Logger;
14 import org.hibernate.annotations.Cascade;
15 import org.hibernate.annotations.CascadeType;
16
17 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
18 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
19
20 import java.util.*;
21
22 import javax.persistence.*;
23
24 /**
25 * @author m.doering
26 * @version 1.0
27 * @created 08-Nov-2007 13:06:55
28 */
29 @Entity
30 public class Synonym extends TaxonBase {
31 static Logger logger = Logger.getLogger(Synonym.class);
32
33 private Set<SynonymRelationship> synonymRelations = new HashSet<SynonymRelationship>();
34
35
36 public static Synonym NewInstance(TaxonNameBase taxonName, ReferenceBase sec){
37 Synonym result = new Synonym();
38 result.setName(taxonName);
39 result.setSec(sec);
40 return result;
41 }
42
43 //TODO should be private, but still produces Spring init errors
44 public Synonym(){
45 }
46
47
48 @OneToMany(mappedBy="synonym")
49 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
50 public Set<SynonymRelationship> getSynonymRelations() {
51 return synonymRelations;
52 }
53 protected void setSynonymRelations(Set<SynonymRelationship> synonymRelations) {
54 this.synonymRelations = synonymRelations;
55 }
56 protected void addSynonymRelation(SynonymRelationship synonymRelation) {
57 this.synonymRelations.add(synonymRelation);
58 }
59 protected void removeSynonymRelation(SynonymRelationship synonymRelation) {
60 synonymRelation.setSynonym(null);
61 Taxon taxon = synonymRelation.getAcceptedTaxon();
62 if (taxon != null){
63 synonymRelation.setAcceptedTaxon(null);
64 taxon.removeSynonymRelation(synonymRelation);
65 }
66 this.synonymRelations.remove(synonymRelation);
67 }
68
69
70 @Transient
71 public Set<Taxon> getAcceptedTaxa() {
72 Set<Taxon>taxa=new HashSet<Taxon>();
73 for (SynonymRelationship rel:getSynonymRelations()){
74 taxa.add(rel.getAcceptedTaxon());
75 }
76 return taxa;
77 }
78
79 /**
80 * Return the synonymy relationship type for the relation to a given accepted taxon.
81 * If taxon is null or no relation exists to that taxon null is returned.
82 * @param taxon
83 * @return
84 */
85 @Transient
86 public SynonymRelationshipType getRelationType(Taxon taxon){
87 if (taxon == null ){
88 return null;
89 }
90 for (SynonymRelationship rel:getSynonymRelations()){
91 Taxon acceptedTaxon = rel.getAcceptedTaxon();
92 if (taxon.equals(acceptedTaxon)){
93 return rel.getType();
94 }
95 }
96 return null;
97 }
98 }