278cf53b0bb685e65677808c8cbdd2d3d7a27cd0
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / model / SynonymUtil.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.model;
12
13 import java.util.HashSet;
14 import java.util.Set;
15
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;
26
27 /**
28 * @author n.hoffmann
29 * @created 22.04.2009
30 * @version 1.0
31 */
32 public class SynonymUtil {
33
34 /**
35 * Creates a basionym relationship between basionymName and
36 * each name in its homotypic group.
37 *
38 * @param basionymName
39 * @deprecated TODO move this to cmdlib
40 */
41 public static void setGroupBasionym(TaxonNameBase<?, ?> basionymName) {
42
43
44 HomotypicalGroup homotypicalGroup = basionymName.getHomotypicalGroup();
45
46 if (homotypicalGroup == null) {
47 return;
48 }
49
50 Set<NameRelationship> relations = new HashSet<NameRelationship>();
51 Set<NameRelationship> removeRelations = new HashSet<NameRelationship>();
52
53 for(TaxonNameBase<?, ?> typifiedName : homotypicalGroup.getTypifiedNames()){
54
55 Set<NameRelationship> nameRelations = typifiedName.getRelationsFromThisName();
56
57 for(NameRelationship nameRelation : nameRelations){
58 relations.add(nameRelation);
59 }
60 }
61
62 for (NameRelationship relation : relations) {
63
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);
69 }
70 }
71
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);
77 }
78
79
80 for (TaxonNameBase<?, ?> name : homotypicalGroup.getTypifiedNames()) {
81 if (!name.equals(basionymName)) {
82
83 // First check whether the relationship already exists
84 if (!isNameBasionymOf(basionymName, name)) {
85
86 // Then create it
87 name.addRelationshipFromName(basionymName,
88 NameRelationshipType.BASIONYM(), null);
89 }
90 }
91 }
92 }
93
94 /**
95 * Creates a basionym relationship between basionymName and
96 * each name in its homotypic group.
97 *
98 * @param basionymName
99 * @deprecated TODO move this to cmdlib
100 */
101 public static void removeGroupBasionym(TaxonNameBase<?, ?> basionymName) {
102
103 HomotypicalGroup homotypicalGroup = basionymName.getHomotypicalGroup();
104
105 if (homotypicalGroup == null) {
106 return;
107 }
108
109 Set<NameRelationship> relations = new HashSet<NameRelationship>();
110 Set<NameRelationship> removeRelations = new HashSet<NameRelationship>();
111
112 for(TaxonNameBase<?, ?> typifiedName : homotypicalGroup.getTypifiedNames()){
113
114 Set<NameRelationship> nameRelations = typifiedName.getRelationsFromThisName();
115
116 for(NameRelationship nameRelation : nameRelations){
117 relations.add(nameRelation);
118 }
119 }
120
121 for (NameRelationship relation : relations) {
122
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);
129 }
130 }
131
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);
137 }
138 }
139
140 /**
141 * Checks whether synonym's name is the basionym for ALL names
142 * in its group.
143 *
144 * @param synonym
145 *
146 * @return
147 * @deprecated TODO move this to cmdlib
148 */
149 public static boolean isSynonymGroupBasionym(Synonym synonym) {
150
151 TaxonNameBase<?, ?> synonymName = synonym.getName();
152 return isNameGroupBasionym(synonymName);
153 }
154
155 /**
156 * Checks whether name is the basionym for ALL names
157 * in its group.
158 * @param name
159 *
160 * @return
161 * @deprecated TODO move this to cmdlib
162 */
163 public static boolean isNameGroupBasionym(TaxonNameBase<?, ?> name) {
164 if (name == null) {
165 return false;
166 }
167
168 HomotypicalGroup homotypicalGroup = name.getHomotypicalGroup();
169 if (homotypicalGroup == null) {
170 return false;
171 }
172
173 Set<TaxonNameBase> typifiedNames = homotypicalGroup.getTypifiedNames();
174
175 // Check whether there are any other names in the group
176 if (typifiedNames.size() == 1) {
177 return false;
178 }
179
180
181 for (TaxonNameBase taxonName : typifiedNames) {
182 if (!taxonName.equals(name)) {
183 if (!isNameBasionymOf(name, taxonName)) {
184 return false;
185 }
186 }
187 }
188 return true;
189 }
190
191 /**
192 * Checks whether a basionym relationship exists between fromName and toName.
193 *
194 * @param fromName
195 * @param toName
196 * @return
197 * @deprecated TODO move this to cmdlib
198 */
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)) {
204 return true;
205 }
206 }
207 return false;
208 }
209
210 /**
211 * TODO move this to cdmlib
212 *
213 * Basically we just want to swap the names. Unfortunately this is all super complicated.
214 *
215 * @param oldSynonym
216 * @param oldTaxon
217 * @return
218 *
219 * @deprecated
220 */
221 public static Taxon swapSynonymAndAccepted(Synonym oldSynonym, Taxon oldTaxon) {
222
223 // Create a new taxon from synonym
224 Taxon newTaxon = Taxon.NewInstance(oldSynonym.getName(), oldSynonym.getSec());
225
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());
228
229 // Remove synonym from taxon
230 oldTaxon.removeSynonym(oldSynonym);
231
232 // Swap parent
233 Taxon parentTaxon = oldTaxon.getTaxonomicParent();
234 newTaxon.setTaxonomicParent(parentTaxon, null, null);
235 oldTaxon.setTaxonomicParent(null, null, null);
236
237 // Swap children
238 Set<Taxon> childTaxa = oldTaxon.getTaxonomicChildren();
239 for (Taxon childTaxon : childTaxa) {
240 childTaxon.setTaxonomicParent(newTaxon, null, null);
241 }
242
243 // Swap taxon and synonym
244 if (NameUtil.isNameHomotypic(newSynonym.getName(), newTaxon)) {
245 newTaxon.addSynonym(newSynonym, SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF());
246 } else {
247 newTaxon.addSynonym(newSynonym, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
248 }
249
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();
255 }
256 newTaxon.addSynonym(synonym, synRelType);
257 oldTaxon.removeSynonym(synonym);
258 }
259
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());
266 }
267
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())) {
272 continue;
273 }
274 if (oldTaxon.equals(relation.getFromTaxon())) {
275 newTaxon.addTaxonRelation(relation.getToTaxon(), relation.getType(),
276 relation.getCitation(), relation.getCitationMicroReference());
277 } else {
278 if (relation.getToTaxon() != null) {
279 relation.getToTaxon().addTaxonRelation(newTaxon, relation.getType(),
280 relation.getCitation(), relation.getCitationMicroReference());
281 }
282 }
283 oldTaxon.removeTaxonRelation(relation);
284 }
285
286 // Move descriptions
287 Set<TaxonDescription> taxonDescriptions = new HashSet<TaxonDescription>();
288 for(TaxonDescription taxonDescription : oldTaxon.getDescriptions()){
289 taxonDescriptions.add(taxonDescription);
290 }
291 for(TaxonDescription taxonDescription : taxonDescriptions){
292 newTaxon.addDescription(taxonDescription);
293 oldTaxon.removeDescription(taxonDescription);
294 }
295
296 return newTaxon;
297 }
298 }