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
.store
.model
;
13 import java
.util
.HashSet
;
16 import org
.apache
.log4j
.Logger
;
18 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
19 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
20 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
21 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
22 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
23 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
24 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
34 public class SynonymUtil
{
35 private static final Logger logger
= Logger
.getLogger(SynonymUtil
.class);
39 * Creates a basionym relationship between basionymName and
40 * each name in its homotypic group.
43 * @deprecated TODO move this to cmdlib
45 public static void setGroupBasionym(TaxonNameBase basionymName
) {
48 HomotypicalGroup homotypicalGroup
= basionymName
.getHomotypicalGroup();
50 if (homotypicalGroup
== null) {
54 Set
<NameRelationship
> relations
= new HashSet
<NameRelationship
>();
55 Set
<NameRelationship
> removeRelations
= new HashSet
<NameRelationship
>();
57 for(TaxonNameBase typifiedName
: homotypicalGroup
.getTypifiedNames()){
59 Set
<NameRelationship
> nameRelations
= typifiedName
.getRelationsFromThisName();
61 for(NameRelationship nameRelation
: nameRelations
){
62 relations
.add(nameRelation
);
66 for (NameRelationship relation
: relations
) {
68 // If this is a basionym relation, and toName is in the homotypical group,
69 // remove the relationship.
70 if (relation
.getType().equals(NameRelationshipType
.BASIONYM()) &&
71 relation
.getToName().getHomotypicalGroup().equals(homotypicalGroup
)) {
72 removeRelations
.add(relation
);
76 // Removing relations from a set through which we are iterating causes a
77 // ConcurrentModificationException. Therefore, we delete the targeted
78 // relations in a second step.
79 for (NameRelationship relation
: removeRelations
) {
80 basionymName
.removeNameRelationship(relation
);
84 for (TaxonNameBase name
: homotypicalGroup
.getTypifiedNames()) {
85 if (!name
.equals(basionymName
)) {
87 // First check whether the relationship already exists
88 if (!isNameBasionymOf(basionymName
, name
)) {
91 name
.addRelationshipFromName(basionymName
,
92 NameRelationshipType
.BASIONYM(), null);
99 * Creates a basionym relationship between basionymName and
100 * each name in its homotypic group.
102 * @param basionymName
103 * @deprecated TODO move this to cmdlib
105 public static void removeGroupBasionym(TaxonNameBase basionymName
) {
107 HomotypicalGroup homotypicalGroup
= basionymName
.getHomotypicalGroup();
109 if (homotypicalGroup
== null) {
113 Set
<NameRelationship
> relations
= new HashSet
<NameRelationship
>();
114 Set
<NameRelationship
> removeRelations
= new HashSet
<NameRelationship
>();
116 for(TaxonNameBase typifiedName
: homotypicalGroup
.getTypifiedNames()){
118 Set
<NameRelationship
> nameRelations
= typifiedName
.getRelationsFromThisName();
120 for(NameRelationship nameRelation
: nameRelations
){
121 relations
.add(nameRelation
);
125 for (NameRelationship relation
: relations
) {
127 // If this is a basionym relation, and toName is in the homotypical group,
128 // and fromName is basionymName, remove the relationship.
129 if (relation
.getType().equals(NameRelationshipType
.BASIONYM()) &&
130 relation
.getFromName().equals(basionymName
) &&
131 relation
.getToName().getHomotypicalGroup().equals(homotypicalGroup
)) {
132 removeRelations
.add(relation
);
136 // Removing relations from a set through which we are iterating causes a
137 // ConcurrentModificationException. Therefore, we delete the targeted
138 // relations in a second step.
139 for (NameRelationship relation
: removeRelations
) {
140 basionymName
.removeNameRelationship(relation
);
145 * Checks whether synonym's name is the basionym for ALL names
151 * @deprecated TODO move this to cmdlib
153 public static boolean isSynonymGroupBasionym(Synonym synonym
) {
155 TaxonNameBase synonymName
= synonym
.getName();
156 return isNameGroupBasionym(synonymName
);
160 * Checks whether name is the basionym for ALL names
165 * @deprecated TODO move this to cmdlib
167 public static boolean isNameGroupBasionym(TaxonNameBase name
) {
172 HomotypicalGroup homotypicalGroup
= name
.getHomotypicalGroup();
173 if (homotypicalGroup
== null) {
177 Set
<TaxonNameBase
> typifiedNames
= homotypicalGroup
.getTypifiedNames();
179 // Check whether there are any other names in the group
180 if (typifiedNames
.size() == 1) {
184 boolean isBasionymToAll
= true;
186 for (TaxonNameBase taxonName
: typifiedNames
) {
187 if (!taxonName
.equals(name
)) {
188 if (!isNameBasionymOf(name
, taxonName
)) {
197 * Checks whether a basionym relationship exists between fromName and toName.
202 * @deprecated TODO move this to cmdlib
204 public static boolean isNameBasionymOf(TaxonNameBase fromName
, TaxonNameBase toName
) {
205 Set
<NameRelationship
> relations
= toName
.getRelationsToThisName();
206 for (NameRelationship relation
: relations
) {
207 if (relation
.getType().equals(NameRelationshipType
.BASIONYM()) &&
208 relation
.getFromName().equals(fromName
)) {
216 * TODO move this to cdmlib
218 * Basically we just want to swap the names. Unfortunately this is all super complicated.
226 public static Taxon
swapSynonymAndAccepted(Synonym oldSynonym
, Taxon oldTaxon
) {
228 // Create a new taxon from synonym
229 Taxon newTaxon
= Taxon
.NewInstance(oldSynonym
.getName(), oldSynonym
.getSec());
231 // Create a new synonym from the taxon and add it to the newly created taxon
232 Synonym newSynonym
= Synonym
.NewInstance(oldTaxon
.getName(), oldTaxon
.getSec());
234 // Remove synonym from taxon
235 oldTaxon
.removeSynonym(oldSynonym
);
238 Taxon parentTaxon
= oldTaxon
.getTaxonomicParent();
239 newTaxon
.setTaxonomicParent(parentTaxon
, null, null);
240 oldTaxon
.setTaxonomicParent(null, null, null);
243 Set
<Taxon
> childTaxa
= oldTaxon
.getTaxonomicChildren();
244 for (Taxon childTaxon
: childTaxa
) {
245 childTaxon
.setTaxonomicParent(newTaxon
, null, null);
248 // Swap taxon and synonym
249 if (NameUtil
.isNameHomotypic(newSynonym
.getName(), newTaxon
)) {
250 newTaxon
.addSynonym(newSynonym
, SynonymRelationshipType
.HOMOTYPIC_SYNONYM_OF());
252 newTaxon
.addSynonym(newSynonym
, SynonymRelationshipType
.HETEROTYPIC_SYNONYM_OF());
255 // Move the other synonyms from old accepted taxon to new accepted taxon
256 for (Synonym synonym
: oldTaxon
.getSynonyms()) {
257 SynonymRelationshipType synRelType
= SynonymRelationshipType
.HOMOTYPIC_SYNONYM_OF();
258 if (NameUtil
.isNameHomotypic(synonym
.getName(), newTaxon
) != true) {
259 synRelType
= SynonymRelationshipType
.HETEROTYPIC_SYNONYM_OF();
261 newTaxon
.addSynonym(synonym
, synRelType
);
262 oldTaxon
.removeSynonym(synonym
);
265 // Move misapplied names
266 for (Taxon misappliedNameTaxon
: oldTaxon
.getMisappliedNames()) {
267 newTaxon
.addMisappliedName(misappliedNameTaxon
,
268 misappliedNameTaxon
.getName().getCitation(),
269 ""); //TODO: Set the microcitation
270 oldTaxon
.removeTaxon(misappliedNameTaxon
, TaxonRelationshipType
.MISAPPLIED_NAME_FOR());
273 // Move concept relations
274 for (TaxonRelationship relation
: oldTaxon
.getTaxonRelations()) {
275 if (relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR()) ||
276 relation
.getType().equals(TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN())) {
279 if (oldTaxon
.equals(relation
.getFromTaxon())) {
280 newTaxon
.addTaxonRelation(relation
.getToTaxon(), relation
.getType(),
281 relation
.getCitation(), relation
.getCitationMicroReference());
283 if (relation
.getToTaxon() != null) {
284 relation
.getToTaxon().addTaxonRelation(newTaxon
, relation
.getType(),
285 relation
.getCitation(), relation
.getCitationMicroReference());
288 oldTaxon
.removeTaxonRelation(relation
);
292 Set
<TaxonDescription
> taxonDescriptions
= new HashSet
<TaxonDescription
>();
293 for(TaxonDescription taxonDescription
: oldTaxon
.getDescriptions()){
294 taxonDescriptions
.add(taxonDescription
);
296 for(TaxonDescription taxonDescription
: taxonDescriptions
){
297 newTaxon
.addDescription(taxonDescription
);
298 oldTaxon
.removeDescription(taxonDescription
);