3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.model
;
13 import java
.util
.HashSet
;
16 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
17 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
18 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
19 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
20 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
21 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
22 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
23 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
24 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
32 public class SynonymUtil
{
35 * Creates a basionym relationship between basionymName and
36 * each name in its homotypic group.
39 * @deprecated TODO move this to cmdlib
41 public static void setGroupBasionym(TaxonNameBase
<?
, ?
> basionymName
) {
44 HomotypicalGroup homotypicalGroup
= basionymName
.getHomotypicalGroup();
46 if (homotypicalGroup
== null) {
50 Set
<NameRelationship
> relations
= new HashSet
<NameRelationship
>();
51 Set
<NameRelationship
> removeRelations
= new HashSet
<NameRelationship
>();
53 for(TaxonNameBase
<?
, ?
> typifiedName
: homotypicalGroup
.getTypifiedNames()){
55 Set
<NameRelationship
> nameRelations
= typifiedName
.getRelationsFromThisName();
57 for(NameRelationship nameRelation
: nameRelations
){
58 relations
.add(nameRelation
);
62 for (NameRelationship relation
: relations
) {
64 // If this is a basionym relation, and toName is in the homotypical group,
65 // remove the relationship.
66 if (relation
.getType().equals(NameRelationshipType
.BASIONYM()) &&
67 relation
.getToName().getHomotypicalGroup().equals(homotypicalGroup
)) {
68 removeRelations
.add(relation
);
72 // Removing relations from a set through which we are iterating causes a
73 // ConcurrentModificationException. Therefore, we delete the targeted
74 // relations in a second step.
75 for (NameRelationship relation
: removeRelations
) {
76 basionymName
.removeNameRelationship(relation
);
80 for (TaxonNameBase
<?
, ?
> name
: homotypicalGroup
.getTypifiedNames()) {
81 if (!name
.equals(basionymName
)) {
83 // First check whether the relationship already exists
84 if (!isNameBasionymOf(basionymName
, name
)) {
87 name
.addRelationshipFromName(basionymName
,
88 NameRelationshipType
.BASIONYM(), null);
95 * Creates a basionym relationship between basionymName and
96 * each name in its homotypic group.
99 * @deprecated TODO move this to cmdlib
101 public static void removeGroupBasionym(TaxonNameBase
<?
, ?
> basionymName
) {
103 HomotypicalGroup homotypicalGroup
= basionymName
.getHomotypicalGroup();
105 if (homotypicalGroup
== null) {
109 Set
<NameRelationship
> relations
= new HashSet
<NameRelationship
>();
110 Set
<NameRelationship
> removeRelations
= new HashSet
<NameRelationship
>();
112 for(TaxonNameBase
<?
, ?
> typifiedName
: homotypicalGroup
.getTypifiedNames()){
114 Set
<NameRelationship
> nameRelations
= typifiedName
.getRelationsFromThisName();
116 for(NameRelationship nameRelation
: nameRelations
){
117 relations
.add(nameRelation
);
121 for (NameRelationship relation
: relations
) {
123 // If this is a basionym relation, and toName is in the homotypical group,
124 // and fromName is basionymName, remove the relationship.
125 if (relation
.getType().equals(NameRelationshipType
.BASIONYM()) &&
126 relation
.getFromName().equals(basionymName
) &&
127 relation
.getToName().getHomotypicalGroup().equals(homotypicalGroup
)) {
128 removeRelations
.add(relation
);
132 // Removing relations from a set through which we are iterating causes a
133 // ConcurrentModificationException. Therefore, we delete the targeted
134 // relations in a second step.
135 for (NameRelationship relation
: removeRelations
) {
136 basionymName
.removeNameRelationship(relation
);
141 * Checks whether synonym's name is the basionym for ALL names
147 * @deprecated TODO move this to cmdlib
149 public static boolean isSynonymGroupBasionym(Synonym synonym
) {
151 TaxonNameBase
<?
, ?
> synonymName
= synonym
.getName();
152 return isNameGroupBasionym(synonymName
);
156 * Checks whether name is the basionym for ALL names
161 * @deprecated TODO move this to cmdlib
163 public static boolean isNameGroupBasionym(TaxonNameBase
<?
, ?
> name
) {
168 HomotypicalGroup homotypicalGroup
= name
.getHomotypicalGroup();
169 if (homotypicalGroup
== null) {
173 Set
<TaxonNameBase
> typifiedNames
= homotypicalGroup
.getTypifiedNames();
175 // Check whether there are any other names in the group
176 if (typifiedNames
.size() == 1) {
181 for (TaxonNameBase taxonName
: typifiedNames
) {
182 if (!taxonName
.equals(name
)) {
183 if (!isNameBasionymOf(name
, taxonName
)) {
192 * Checks whether a basionym relationship exists between fromName and toName.
197 * @deprecated TODO move this to cmdlib
199 public static boolean isNameBasionymOf(TaxonNameBase
<?
, ?
> fromName
, TaxonNameBase toName
) {
200 Set
<NameRelationship
> relations
= toName
.getRelationsToThisName();
201 for (NameRelationship relation
: relations
) {
202 if (relation
.getType().equals(NameRelationshipType
.BASIONYM()) &&
203 relation
.getFromName().equals(fromName
)) {
211 * TODO move this to cdmlib
213 * Basically we just want to swap the names. Unfortunately this is all super complicated.
221 public static Taxon
swapSynonymAndAccepted(Synonym oldSynonym
, Taxon oldTaxon
) {
223 // Create a new taxon from synonym
224 Taxon newTaxon
= Taxon
.NewInstance(oldSynonym
.getName(), oldSynonym
.getSec());
226 // Create a new synonym from the taxon and add it to the newly created taxon
227 Synonym newSynonym
= Synonym
.NewInstance(oldTaxon
.getName(), oldTaxon
.getSec());
229 // Remove synonym from taxon
230 oldTaxon
.removeSynonym(oldSynonym
);
233 Taxon parentTaxon
= oldTaxon
.getTaxonomicParent();
234 newTaxon
.setTaxonomicParent(parentTaxon
, null, null);
235 oldTaxon
.setTaxonomicParent(null, null, null);
238 Set
<Taxon
> childTaxa
= oldTaxon
.getTaxonomicChildren();
239 for (Taxon childTaxon
: childTaxa
) {
240 childTaxon
.setTaxonomicParent(newTaxon
, null, null);
243 // Swap taxon and synonym
244 if (NameUtil
.isNameHomotypic(newSynonym
.getName(), newTaxon
)) {
245 newTaxon
.addSynonym(newSynonym
, SynonymRelationshipType
.HOMOTYPIC_SYNONYM_OF());
247 newTaxon
.addSynonym(newSynonym
, SynonymRelationshipType
.HETEROTYPIC_SYNONYM_OF());
250 // Move the other synonyms from old accepted taxon to new accepted taxon
251 for (Synonym synonym
: oldTaxon
.getSynonyms()) {
252 SynonymRelationshipType synRelType
= SynonymRelationshipType
.HOMOTYPIC_SYNONYM_OF();
253 if (NameUtil
.isNameHomotypic(synonym
.getName(), newTaxon
) != true) {
254 synRelType
= SynonymRelationshipType
.HETEROTYPIC_SYNONYM_OF();
256 newTaxon
.addSynonym(synonym
, synRelType
);
257 oldTaxon
.removeSynonym(synonym
);
260 // Move misapplied names
261 for (Taxon misappliedNameTaxon
: oldTaxon
.getMisappliedNames()) {
262 newTaxon
.addMisappliedName(misappliedNameTaxon
,
263 misappliedNameTaxon
.getName().getCitation(),
264 ""); //TODO: Set the microcitation
265 oldTaxon
.removeTaxon(misappliedNameTaxon
, TaxonRelationshipType
.MISAPPLIED_NAME_FOR());
268 // Move concept relations
269 for (TaxonRelationship relation
: oldTaxon
.getTaxonRelations()) {
270 if (relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR()) ||
271 relation
.getType().equals(TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN())) {
274 if (oldTaxon
.equals(relation
.getFromTaxon())) {
275 newTaxon
.addTaxonRelation(relation
.getToTaxon(), relation
.getType(),
276 relation
.getCitation(), relation
.getCitationMicroReference());
278 if (relation
.getToTaxon() != null) {
279 relation
.getToTaxon().addTaxonRelation(newTaxon
, relation
.getType(),
280 relation
.getCitation(), relation
.getCitationMicroReference());
283 oldTaxon
.removeTaxonRelation(relation
);
287 Set
<TaxonDescription
> taxonDescriptions
= new HashSet
<TaxonDescription
>();
288 for(TaxonDescription taxonDescription
: oldTaxon
.getDescriptions()){
289 taxonDescriptions
.add(taxonDescription
);
291 for(TaxonDescription taxonDescription
: taxonDescriptions
){
292 newTaxon
.addDescription(taxonDescription
);
293 oldTaxon
.removeDescription(taxonDescription
);