2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.model
.name
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collections
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import javax
.persistence
.Entity
;
19 import javax
.persistence
.FetchType
;
20 import javax
.persistence
.OneToMany
;
21 import javax
.persistence
.Transient
;
22 import javax
.xml
.bind
.annotation
.XmlAccessType
;
23 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
24 import javax
.xml
.bind
.annotation
.XmlElement
;
25 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
26 import javax
.xml
.bind
.annotation
.XmlIDREF
;
27 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
28 import javax
.xml
.bind
.annotation
.XmlType
;
30 import org
.apache
.log4j
.Logger
;
31 import org
.hibernate
.envers
.Audited
;
33 import eu
.etaxonomy
.cdm
.model
.common
.AnnotatableEntity
;
34 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonComparator
;
40 * The homotypical group class represents a set of {@link TaxonNameBase taxon names} associated
41 * on the base of their typifications. Since it can be asserted that two taxon
42 * names are typified by the same type without mentioning the type itself, even
43 * taxon names without explicit {@link TypeDesignationBase type designation} can belong
44 * to an homotypical group.<BR>
45 * Taxon names belonging to an homotypical group and the taxon names or
46 * {@link eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase specimens} used as types for their
47 * {@link TypeDesignationBase type designations} have the following properties: <ul>
48 * <li> A taxon name belongs exactly to one homotypical group
49 * <li> A type specimen or a type name can be used as a type only for taxon
50 * names belonging to the same homotypical group<BR>
51 * - therefore an homotypical group circumscribes a set of types<BR>
52 * - each taxon name shares a subset of these types<BR>
53 * - each type is used by a subset of these taxon names
54 * within the homotypical group
55 * <li> Names that share at least one common type must belong to the same
57 * <li> Names that share the same basionym or replaced synonym must belong to
58 * the same homotypical group
61 * @see TypeDesignationBase
62 * @see NameTypeDesignation
63 * @see SpecimenTypeDesignation
66 * @created 08-Nov-2007
68 @XmlAccessorType(XmlAccessType
.FIELD
)
69 @XmlType(name
= "HomotypicalGroup", propOrder
= {
74 public class HomotypicalGroup
extends AnnotatableEntity
{
75 private static final Logger logger
= Logger
.getLogger(HomotypicalGroup
.class);
77 @XmlElementWrapper(name
= "TypifiedNames")
78 @XmlElement(name
= "TypifiedName")
80 @XmlSchemaType(name
= "IDREF")
81 @OneToMany(mappedBy
="homotypicalGroup", fetch
=FetchType
.LAZY
)
82 protected Set
<TaxonNameBase
> typifiedNames
= new HashSet
<TaxonNameBase
>();
84 // ******************** static methods **************************************/
86 * Creates a new homotypical group instance with an empty set of typified
87 * {@link TaxonNameBase taxon names}.
89 * @see #HomotypicalGroup()
91 public static HomotypicalGroup
NewInstance(){
92 return new HomotypicalGroup();
96 //********************** CONSTRUCTOR ********************************************/
99 * Class constructor: creates a new homotypical group instance with an
100 * empty set of typified {@link TaxonNameBase taxon names}.
102 public HomotypicalGroup() {
106 // ********************** GETTER/SETTER/ADDER/REMOVER ********************************/
109 * Returns the set of {@link TaxonNameBase taxon names} that belong to <i>this</i> homotypical group.
111 * @see #getSpecimenTypeDesignations()
113 public Set
<TaxonNameBase
> getTypifiedNames() {
114 return typifiedNames
;
118 * Adds a new {@link TaxonNameBase taxon name} to the set of taxon names that belong
119 * to <i>this</i> homotypical group.
121 * @param typifiedName the taxon name to be added to <i>this</i> group
122 * @see #getTypifiedNames()
123 * @see #removeTypifiedName(TaxonNameBase)
125 public void addTypifiedName(TaxonNameBase typifiedName
) {
126 if (typifiedName
!= null){
127 typifiedName
.setHomotypicalGroup(this);
128 typifiedNames
.add(typifiedName
);
132 * Removes one element from the set of {@link TaxonNameBase taxon names}
133 * that belong to <i>this</i> homotypical group.
135 * @param taxonBase the taxon name which should be removed from the corresponding set
136 * @see #addTypifiedName(TaxonNameBase)
138 public void removeTypifiedName(TaxonNameBase typifiedName
) {
139 typifiedName
.setHomotypicalGroup(HomotypicalGroup
.NewInstance());
140 typifiedNames
.remove(typifiedName
);
144 * Merges the typified {@link TaxonNameBase taxon names} from one homotypical group into
145 * the set of typified taxon names of <i>this</i> homotypical group.
147 * @param homotypicalGroupToMerge the homotypical group the typified names of which
148 * are to be transferred to <i>this</i> homotypical group
150 public void merge(HomotypicalGroup homotypicalGroupToMerge
){
151 if (homotypicalGroupToMerge
!= null){
152 Set
<TaxonNameBase
> typifiedNames
= new HashSet
<TaxonNameBase
>();
153 typifiedNames
.addAll(homotypicalGroupToMerge
.getTypifiedNames());
154 for (TaxonNameBase typifiedName
: typifiedNames
){
155 this.addTypifiedName(typifiedName
);
162 * Returns the set of {@link SpecimenTypeDesignation specimen type designations} that
163 * typify the {@link TaxonNameBase taxon names} belonging to <i>this</i> homotypical group
164 * including the status of these designations.
166 * @see #getTypifiedNames()
167 * @see #getNameTypeDesignations()
168 * @see #getTypeDesignations()
169 * @see TaxonNameBase#getSpecimenTypeDesignations()
172 public Set
<SpecimenTypeDesignation
> getSpecimenTypeDesignations(){
173 Set
<SpecimenTypeDesignation
> result
= new HashSet
<SpecimenTypeDesignation
>();
174 for (TaxonNameBase taxonName
: typifiedNames
){
175 result
.addAll(taxonName
.getSpecimenTypeDesignations());
181 * Returns the set of {@link NameTypeDesignation name type designations} that
182 * typify the {@link TaxonNameBase taxon names} belonging to <i>this</i> homotypical group
183 * including the status of these designations.
185 * @see #getTypifiedNames()
186 * @see #getSpecimenTypeDesignations()
187 * @see #getTypeDesignations()
188 * @see TaxonNameBase#getNameTypeDesignations()
191 public Set
<NameTypeDesignation
> getNameTypeDesignations(){
192 Set
<NameTypeDesignation
> result
= new HashSet
<NameTypeDesignation
>();
193 for (TaxonNameBase taxonName
: typifiedNames
){
194 result
.addAll(taxonName
.getNameTypeDesignations());
201 * Returns the set of all {@link TypeDesignationBase type designations} that
202 * typify the {@link TaxonNameBase taxon names} belonging to <i>this</i> homotypical group
203 * (this includes either {@link NameTypeDesignation name type designations} or
204 * {@link SpecimenTypeDesignation specimen type designations}).
206 * @see #getTypifiedNames()
207 * @see #getNameTypeDesignations()
208 * @see #getSpecimenTypeDesignations()
209 * @see TaxonNameBase#getTypeDesignations()
212 public Set
<TypeDesignationBase
> getTypeDesignations(){
213 Set
<TypeDesignationBase
> result
= new HashSet
<TypeDesignationBase
>();
214 for (TaxonNameBase taxonName
: typifiedNames
){
215 result
.addAll(taxonName
.getTypeDesignations());
221 // * Returns the set of {@link SpecimenTypeDesignation specimen type designations} that
222 // * typify <i>this</i> homotypical group including the status of these designations.
224 // * @see #getTypifiedNames()
227 // @Cascade({CascadeType.SAVE_UPDATE})
228 // public Set<SpecimenTypeDesignation> getSpecimenTypeDesignations() {
229 // return specimenTypeDesignations;
232 // * @see #getSpecimenTypeDesignations()
234 // protected void setSpecimenTypeDesignations(Set<SpecimenTypeDesignation> specimenTypeDesignations) {
235 // this.specimenTypeDesignations = specimenTypeDesignations;
238 // * Adds a new {@link SpecimenTypeDesignation specimen type designation} to the set
239 // * of specimen type designations assigned to <i>this</i> homotypical group and eventually
240 // * (with a boolean parameter) also to the corresponding set of each of the
241 // * {@link TaxonNameBase taxon names} belonging to <i>this</i> homotypical group.
243 // * @param specimenTypeDesignation the specimen type designation to be added
244 // * @param addToAllNames the boolean flag indicating whether the addition will also
245 // * carried out for each taxon name
247 // * @see TaxonNameBase#getSpecimenTypeDesignations()
248 // * @see SpecimenTypeDesignation
250 // public void addSpecimenTypeDesignation(SpecimenTypeDesignation specimenTypeDesignation, boolean addToAllNames) {
251 // if (specimenTypeDesignation != null){
252 // specimenTypeDesignation.setHomotypicalGroup(this);
253 // specimenTypeDesignations.add(specimenTypeDesignation);
255 // if (addToAllNames){
256 // for (TaxonNameBase taxonNameBase : this.typifiedNames){
257 // taxonNameBase.addSpecimenTypeDesignation(specimenTypeDesignation);
262 // * Removes one element from the set of {@link SpecimenTypeDesignation specimen type designations} assigned to the
263 // * {@link HomotypicalGroup homotypical group} to which this {@link TaxonNameBase taxon name} belongs.
264 // * The same element will be removed from the corresponding set of each of
265 // * the taxon names belonging to <i>this</i> homotypical group. Furthermore the
266 // * homotypical group attribute of the specimen type designation will be
269 // * @param specimenTypeDesignation the specimen type designation which should be deleted
270 // * @see #getSpecimenTypeDesignations()
271 // * @see #addSpecimenTypeDesignation(SpecimenTypeDesignation, boolean)
272 // * @see TaxonNameBase#removeSpecimenTypeDesignation(SpecimenTypeDesignation)
273 // * @see SpecimenTypeDesignation#getHomotypicalGroup()
275 // public void removeSpecimenTypeDesignation(SpecimenTypeDesignation specimenTypeDesignation) {
276 // if (specimenTypeDesignation != null){
277 // specimenTypeDesignation.setHomotypicalGroup(null);
278 // specimenTypeDesignations.remove(specimenTypeDesignation);
280 // for (TaxonNameBase taxonNameBase : this.typifiedNames){
281 // taxonNameBase.removeSpecimenTypeDesignation(specimenTypeDesignation);
287 // * Returns the set of {@link NameTypeDesignation name type designations} that
288 // * typify <i>this</i> homotypical group including the status of these designations.
290 // * @see #getTypifiedNames()
293 // @Cascade({CascadeType.SAVE_UPDATE})
294 // public Set<NameTypeDesignation> getNameTypeDesignations() {
295 // return nameTypeDesignations;
298 // * @see #getNameTypeDesignations()
300 // protected void setNameTypeDesignations(Set<NameTypeDesignation> nameTypeDesignations) {
301 // this.nameTypeDesignations = nameTypeDesignations;
304 // * Adds a new {@link NameTypeDesignation name type designation} to the set
305 // * of name type designations assigned to <i>this</i> homotypical group and eventually
306 // * (with a boolean parameter) also to the corresponding set of each of the
307 // * {@link TaxonNameBase taxon names} belonging to <i>this</i> homotypical group.
309 // * @param nameTypeDesignation the name type designation to be added
310 // * @param addToAllNames the boolean flag indicating whether the addition will also
311 // * carried out for each taxon name
313 // * @see TaxonNameBase#getNameTypeDesignations()
314 // * @see NameTypeDesignation
316 // public void addNameTypeDesignation(NameTypeDesignation nameTypeDesignation, boolean addToAllNames) {
317 // if (nameTypeDesignation != null){
318 // nameTypeDesignation.setHomotypicalGroup(this);
319 // nameTypeDesignations.add(nameTypeDesignation);
321 // if (addToAllNames){
322 // for (TaxonNameBase taxonNameBase : this.typifiedNames){
323 // taxonNameBase.addNameTypeDesignation(nameTypeDesignation);
328 // * Removes one element from the set of {@link NameTypeDesignation name type designations} assigned to the
329 // * {@link HomotypicalGroup homotypical group} to which this {@link TaxonNameBase taxon name} belongs.
330 // * The same element will be removed from the corresponding set of each of
331 // * the taxon names belonging to <i>this</i> homotypical group. Furthermore the
332 // * homotypical group attribute of the name type designation will be
335 // * @param nameTypeDesignation the name type designation which should be deleted
336 // * @see #getNameTypeDesignations()
337 // * @see #addNameTypeDesignation(NameTypeDesignation, boolean)
338 // * @see TaxonNameBase#removeNameTypeDesignation(NameTypeDesignation)
339 // * @see NameTypeDesignation#getHomotypicalGroup()
341 // public void removeNameTypeDesignation(NameTypeDesignation nameTypeDesignation) {
342 // if (nameTypeDesignation != null){
343 // nameTypeDesignation.setHomotypicalGroup(null);
344 // nameTypeDesignations.remove(nameTypeDesignation);
346 // for (TaxonNameBase taxonNameBase : this.typifiedNames){
347 // taxonNameBase.removeNameTypeDesignation(nameTypeDesignation);
353 * Retrieves the ordered list (depending on the date of publication) of
354 * {@link taxon.Synonym synonyms} (according to a given reference)
355 * the {@link TaxonNameBase taxon names} of which belong to <i>this</i> homotypical group.
356 * If other names are part of <i>this</i> group that are not considered synonyms
357 * according to the respective reference, then they will not be included in
360 * @param sec the reference whose treatment is to be considered
361 * @return the ordered list of synonyms
362 * @see TaxonNameBase#getSynonyms()
363 * @see TaxonNameBase#getTaxa()
366 public List
<Synonym
> getSynonymsInGroup(Reference sec
){
367 List
<Synonym
> result
= new ArrayList();
368 for (TaxonNameBase
<?
, ?
>name
: this.getTypifiedNames()){
369 for (Synonym synonym
: name
.getSynonyms()){
370 if ( (synonym
.getSec() == null && sec
== null) ||
371 synonym
.getSec() != null && synonym
.getSec().equals(sec
)){
376 Collections
.sort(result
, new TaxonComparator());
382 * Creates a basionym relationship to all other names in this names homotypical
385 * @see HomotypicalGroup.setGroupBasionym(TaxonNameBase basionymName)
387 * @param basionymName
388 * @throws IllegalArgumentException if basionymName is not member in this homotypical group
390 public void setGroupBasionym(TaxonNameBase basionymName
) throws IllegalArgumentException
{
391 setGroupBasionym(basionymName
, null, null, null);
394 public void setGroupBasionym(TaxonNameBase basionymName
, Reference citation
, String microCitation
, String ruleConsidered
)
395 throws IllegalArgumentException
{
396 if (! typifiedNames
.contains(basionymName
)){
397 throw new IllegalArgumentException("Name to be set as basionym/original combination must be part of the homotypical group but is not");
399 if (typifiedNames
.size() < 2){return;}
402 for (TaxonNameBase name
: typifiedNames
) {
403 if (!name
.equals(basionymName
)) {
404 name
.addRelationshipFromName(basionymName
, NameRelationshipType
.BASIONYM(), citation
, microCitation
, ruleConsidered
);
410 * Removes all basionym relationships between basionymName and any other name
411 * in its homotypic group
413 * @param basionymName
415 public static void removeGroupBasionym(TaxonNameBase basionymName
) {
416 HomotypicalGroup homotypicalGroup
= basionymName
.getHomotypicalGroup();
417 Set
<NameRelationship
> relations
= basionymName
.getRelationsFromThisName();
418 Set
<NameRelationship
> removeRelations
= new HashSet
<NameRelationship
>();
420 for (NameRelationship relation
: relations
) {
422 // If this is a basionym relation, and toName is in the homotypical group,
423 // remove the relationship.
424 if (relation
.getType().isBasionymRelation() &&
425 relation
.getToName().getHomotypicalGroup().equals(homotypicalGroup
)) {
426 removeRelations
.add(relation
);
430 // Removing relations from a set through which we are iterating causes a
431 // ConcurrentModificationException. Therefore, we delete the targeted
432 // relations in a second step.
433 for (NameRelationship relation
: removeRelations
) {
434 basionymName
.removeNameRelationship(relation
);
440 * Returns all taxon names in the homotypical group that do not have an 'is_basionym_for' (zool.: 'is_original_combination_for')
441 * or a replaced synonym relationship.
445 public Set
<TaxonNameBase
> getUnrelatedNames(){
446 Set
<NameRelationship
> set
= getBasionymOrReplacedSynonymRelations(true, true);
447 Set
<TaxonNameBase
> result
= new HashSet
<TaxonNameBase
>();
448 result
.addAll(this.getTypifiedNames());
449 for (NameRelationship nameRelationship
: set
){
450 result
.remove(nameRelationship
.getFromName());
451 result
.remove(nameRelationship
.getToName());
457 * Returns all taxon names in the homotypical group that are new combinations (have a basionym/original combination
458 * or a replaced synonym).
462 public Set
<TaxonNameBase
> getNewCombinations(){
463 Set
<NameRelationship
> set
= getBasionymOrReplacedSynonymRelations(true, true);
464 Set
<TaxonNameBase
> result
= new HashSet
<TaxonNameBase
>();
465 for (NameRelationship nameRelationship
: set
){
466 result
.add(nameRelationship
.getToName());
474 * Returns all taxon names in the homotypical group that have an 'is_basionym_for' (zool.: 'is_original_combination_for')
475 * or a replaced synonym relationship.
479 public Set
<TaxonNameBase
> getBasionymsOrReplacedSynonyms(){
480 Set
<NameRelationship
> set
= getBasionymOrReplacedSynonymRelations(true, true);
481 Set
<TaxonNameBase
> result
= new HashSet
<TaxonNameBase
>();
482 for (NameRelationship nameRelationship
: set
){
483 result
.add(nameRelationship
.getFromName());
489 * Returns all taxon names in the homotypical group that have a 'is_basionym_for' (zool.: 'is_original_combination_for') relationship.
493 public Set
<TaxonNameBase
> getBasionyms(){
494 Set
<NameRelationship
> set
= getBasionymOrReplacedSynonymRelations(true, false);
495 Set
<TaxonNameBase
> result
= new HashSet
<TaxonNameBase
>();
496 for (NameRelationship nameRelationship
: set
){
497 result
.add(nameRelationship
.getFromName());
503 * Returns all taxon names in the homotypical group that have a 'is_replaced_synonym_for' relationship.
507 public Set
<TaxonNameBase
> getReplacedSynonym(){
508 Set
<NameRelationship
> set
= getBasionymOrReplacedSynonymRelations(false, true);
509 Set
<TaxonNameBase
> result
= new HashSet
<TaxonNameBase
>();
510 for (NameRelationship nameRelationship
: set
){
511 result
.add(nameRelationship
.getFromName());
517 * Returns the name relationships that represent either a basionym (original combination) relationship or
518 * a replaced synonym relationship.
522 public Set
<NameRelationship
> getBasionymAndReplacedSynonymRelations(){
523 return getBasionymOrReplacedSynonymRelations(true, true);
527 * Computes all basionym and replaced synonym relationships between names in this group.
528 * If <code>doBasionym</code> is <code>false</code> basionym relationships are excluded.
529 * If <code>doReplacedSynonym</code> is <code>false</code> replaced synonym relationships are excluded.
531 * @param doReplacedSynonym
535 private Set
<NameRelationship
> getBasionymOrReplacedSynonymRelations(boolean doBasionym
, boolean doReplacedSynonym
){
536 Set
<NameRelationship
> result
= new HashSet
<NameRelationship
>();
537 Set
<TaxonNameBase
> names
= this.getTypifiedNames();
538 if (names
.size() > 1){
539 for (TaxonNameBase name
: names
){
540 Set nameRels
= name
.getNameRelations();
541 //TODO make getNameRelations generic
542 for (Object obj
: nameRels
){
543 NameRelationship nameRel
= (NameRelationship
)obj
;
544 NameRelationshipType type
= nameRel
.getType();
545 if ( type
.isBasionymRelation() && doBasionym
){
546 if (testRelatedNameInThisGroup(nameRel
)){
549 logger
.warn("Name has basionym relation to a name that is not in the same homotypical group");
551 }else if (type
.isReplacedSynonymRelation() && doReplacedSynonym
) {
552 if (testRelatedNameInThisGroup(nameRel
)){
555 logger
.warn("Name has replaced synonym relation to a name that is not in the same homotypical group");
564 private boolean testRelatedNameInThisGroup(NameRelationship nameRel
){
565 TaxonNameBase toName
= nameRel
.getToName();
566 return (this.getTypifiedNames().contains(toName
));
569 private boolean isBasionymOrRepSynRel(NameRelationshipType relType
){
570 if (relType
== null){
571 throw new IllegalArgumentException("NameRelationshipType should never be null");
572 }else if (relType
.equals(NameRelationshipType
.BASIONYM())) {
574 }else if (relType
.equals(NameRelationshipType
.REPLACED_SYNONYM())){