(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / name / HomotypicalGroup.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.name;
11
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16
17 import javax.persistence.Entity;
18 import javax.persistence.FetchType;
19 import javax.persistence.OneToMany;
20 import javax.persistence.Transient;
21
22 import org.apache.log4j.Logger;
23 import org.hibernate.annotations.Cascade;
24 import org.hibernate.annotations.CascadeType;
25
26 import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
27 import eu.etaxonomy.cdm.model.occurrence.Specimen;
28 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
29 import eu.etaxonomy.cdm.model.taxon.Synonym;
30 import eu.etaxonomy.cdm.strategy.cache.INameCacheStrategy;
31
32
33 /**
34 * A homotypical group represents all taxon names that share the same type
35 * specimens. This also includes suprageneric names like genera or families
36 * which usually have a name type designation that finally (a name type
37 * designation can also point to another suprageneric name) points to a species
38 * name, which in turn has a (set of) physical type specimen(s). This class
39 * allows to define the type designation only once for the homotypical group
40 * instead of defining a type designation for each one of the taxon names
41 * subsumed under one homotypical group.
42 *
43 * @author m.doering
44 *
45 */
46 @Entity
47 public class HomotypicalGroup extends AnnotatableEntity {
48 static Logger logger = Logger.getLogger(HomotypicalGroup.class);
49
50 protected Set<TaxonNameBase> typifiedNames = new HashSet();
51 protected Set<SpecimenTypeDesignation> typeDesignations = new HashSet();
52
53 public static HomotypicalGroup NewInstance(){
54 return new HomotypicalGroup();
55 }
56
57
58 public HomotypicalGroup() {
59 super();
60 }
61
62
63 @OneToMany(mappedBy="homotypicalGroup", fetch=FetchType.EAGER)
64 public Set<TaxonNameBase> getTypifiedNames() {
65 return typifiedNames;
66 }
67 protected void setTypifiedNames(Set<TaxonNameBase> typifiedNames) {
68 this.typifiedNames = typifiedNames;
69 }
70 public void addTypifiedName(TaxonNameBase typifiedName) {
71 if (typifiedName != null){
72 typifiedName.setHomotypicalGroup(this);
73 typifiedNames.add(typifiedName);
74 }
75 }
76 public void removeTypifiedName(TaxonNameBase typifiedName) {
77 typifiedName.setHomotypicalGroup(null);
78 typifiedNames.remove(typifiedName);
79 }
80
81
82 @OneToMany
83 @Cascade({CascadeType.SAVE_UPDATE})
84 public Set<SpecimenTypeDesignation> getTypeDesignations() {
85 return typeDesignations;
86 }
87 protected void setTypeDesignations(Set<SpecimenTypeDesignation> typeDesignations) {
88 this.typeDesignations = typeDesignations;
89 }
90 public void addTypeDesignation(SpecimenTypeDesignation typeDesignation, boolean addToAllNames) {
91 if (typeDesignation != null){
92 typeDesignation.setHomotypicalGroup(this);
93 typeDesignations.add(typeDesignation);
94 }
95 if (addToAllNames){
96 for (TaxonNameBase taxonNameBase : this.typifiedNames){
97 taxonNameBase.addSpecimenTypeDesignation(typeDesignation);
98 }
99 }
100 }
101 public void removeTypeDesignation(SpecimenTypeDesignation typeDesignation) {
102 if (typeDesignation != null){
103 typeDesignation.setHomotypicalGroup(null);
104 typeDesignations.remove(typeDesignation);
105 }
106 for (TaxonNameBase taxonNameBase : this.typifiedNames){
107 taxonNameBase.removeSpecimenTypeDesignation(typeDesignation);
108 }
109 }
110
111
112 /**
113 * Retrieves the synonyms of reference sec that are part of this homotypical group.
114 * If other names are part of this group that are not considered synonyms in the respective sec-reference,
115 * then they will not be included in the resultset.
116 * @param sec
117 * @return
118 */
119 @Transient
120 public List<Synonym> getSynonymsInGroup(ReferenceBase sec){
121 List<Synonym> result = new ArrayList();
122 for (TaxonNameBase<TaxonNameBase, INameCacheStrategy> n:this.getTypifiedNames()){
123 for (Synonym s:n.getSynonyms()){
124 if ( (s.getSec() == null && sec == null) ||
125 s.getSec().equals(sec)){
126 result.add(s);
127 }
128 }
129 }
130 // TODO: sort result list according to date first published, see nomenclatural reference
131 return result;
132 }
133 }