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
.taxeditor
.model
;
12 import java
.util
.HashSet
;
15 import org
.apache
.log4j
.Logger
;
17 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
18 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
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
.NomenclaturalCode
;
23 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
24 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
25 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
26 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
27 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
32 import eu
.etaxonomy
.cdm
.strategy
.parser
.INonViralNameParser
;
33 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
34 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
35 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
42 public class CdmUtil
{
43 private static final Logger logger
= Logger
.getLogger(CdmUtil
.class);
45 static INonViralNameParser nonViralNameParser
;
48 * Checks whether synonym's name is the basionym for ALL names
55 public static boolean isSynonymGroupBasionym(Synonym synonym
) {
57 TaxonNameBase synonymName
= synonym
.getName();
58 return isNameGroupBasionym(synonymName
);
62 * Checks whether name is the basionym for ALL names
68 public static boolean isNameGroupBasionym(TaxonNameBase name
) {
73 HomotypicalGroup homotypicalGroup
= name
.getHomotypicalGroup();
74 if (homotypicalGroup
== null) {
78 Set
<TaxonNameBase
> typifiedNames
= homotypicalGroup
.getTypifiedNames();
80 // Check whether there are any other names in the group
81 if (typifiedNames
.size() == 1) {
85 boolean isBasionymToAll
= true;
87 for (TaxonNameBase taxonName
: typifiedNames
) {
88 if (!taxonName
.equals(name
)) {
89 if (!isNameBasionymOf(name
, taxonName
)) {
98 * Checks whether a basionym relationship exists between fromName and toName.
104 public static boolean isNameBasionymOf(TaxonNameBase fromName
, TaxonNameBase toName
) {
105 Set
<NameRelationship
> relations
= toName
.getRelationsToThisName();
106 for (NameRelationship relation
: relations
) {
107 if (relation
.getType().equals(NameRelationshipType
.BASIONYM()) &&
108 relation
.getFromName().equals(fromName
)) {
116 * Creates a basionym relationship between basionymName and
117 * each name in its homotypic group.
119 * @param basionymName
121 public static void setGroupBasionym(TaxonNameBase basionymName
) {
122 HomotypicalGroup homotypicalGroup
= basionymName
.getHomotypicalGroup();
123 if (homotypicalGroup
== null) {
126 for (TaxonNameBase name
: homotypicalGroup
.getTypifiedNames()) {
127 if (!name
.equals(basionymName
)) {
129 // First check whether the relationship already exists
130 if (!isNameBasionymOf(basionymName
, name
)) {
133 name
.addRelationshipFromName(basionymName
,
134 NameRelationshipType
.BASIONYM(), null);
141 * Removes all basionym relationships between basionymName and
142 * the names in its homotypic group.
144 * @param basionymName
146 public static void removeGroupBasionym(TaxonNameBase basionymName
) {
147 HomotypicalGroup homotypicalGroup
= basionymName
.getHomotypicalGroup();
148 Set
<NameRelationship
> relations
= basionymName
.getRelationsFromThisName();
149 Set
<NameRelationship
> removeRelations
= new HashSet
<NameRelationship
>();
151 for (NameRelationship relation
: relations
) {
153 // If this is a basionym relation, and toName is in the homotypical group,
154 // remove the relationship.
155 if (relation
.getType().equals(NameRelationshipType
.BASIONYM()) &&
156 relation
.getToName().getHomotypicalGroup().equals(homotypicalGroup
)) {
157 removeRelations
.add(relation
);
161 // Removing relations from a set through which we are iterating causes a
162 // ConcurrentModificationException. Therefore, we delete the targeted
163 // relations in a second step.
164 for (NameRelationship relation
: removeRelations
) {
165 basionymName
.removeNameRelationship(relation
);
170 * Checks whether name belongs to the same homotypic group as taxon's name.
176 public static boolean isNameHomotypic(TaxonNameBase name
, Taxon taxon
) {
177 TaxonNameBase taxonName
= taxon
.getName();
178 if (taxonName
== null || name
== null) {
181 HomotypicalGroup homotypicGroup
= taxonName
.getHomotypicalGroup();
182 if (homotypicGroup
== null) {
185 if (homotypicGroup
.equals(name
.getHomotypicalGroup())) {
192 * Returns whatever is currently considered the display name for Taxon
193 * objects. Currently titleCache.
198 public static String
getDisplayName(TaxonBase taxonBase
) {
199 TaxonNameBase name
= taxonBase
.getName();
200 return getDisplayName(name
);
203 public static String
getDisplayName(TaxonNameBase name
) {
205 return name
.getTitleCache();
210 private static INonViralNameParser
getNonViralNameParser() {
211 if (nonViralNameParser
== null) {
212 nonViralNameParser
= NonViralNameParserImpl
.NewInstance();
214 return nonViralNameParser
;
218 * Takes a raw string, returns a brand spanking new name object.
220 * @param fullReference
225 public static TaxonNameBase
parseFullReference(String fullReference
,
226 NomenclaturalCode nomCode
, Rank rank
) {
228 if (nomCode
== null) {
229 nomCode
= getPreferredNomenclaturalCode();
232 // logger.warn("Nomenclatural code: " + nomCode.toString());
234 TaxonNameBase name
= getNonViralNameParser().parseFullReference(fullReference
,
240 * Takes an existing name object and a string, returns the same name
241 * object with the fields parsed from the string.
243 * @param nameToBeFilled
244 * @param fullReference
248 public static void parseFullReference(NonViralName nameToBeFilled
, String fullReference
, Rank rank
, boolean makeEmpty
) {
249 getNonViralNameParser().parseFullReference(nameToBeFilled
, fullReference
, rank
, makeEmpty
);
252 public static void parseFullName(NonViralName nameToBeFilled
,
253 String fullNameString
, Rank rank
, boolean makeEmpty
) {
254 getNonViralNameParser().parseFullName(nameToBeFilled
, fullNameString
,
258 public static NomenclaturalCode
getPreferredNomenclaturalCode() {
260 String nameCodePreference
= TaxEditorPlugin
.getDefault().getPreferenceStore().getString(ITaxEditorConstants
.CODE_PREFERENCE
);
262 // Check whether name code preference needs to be initialized
263 if (nameCodePreference
== null || nameCodePreference
.equals("")) {
264 nameCodePreference
= ITaxEditorConstants
.DEFAULT_CODE_PREFERENCE
;
265 TaxEditorPlugin
.getDefault().getPreferenceStore().setValue(ITaxEditorConstants
.CODE_PREFERENCE
,
269 if (nameCodePreference
.equals(ITaxEditorConstants
.CODE_PREFERENCE_ICBN
)) {
270 return NomenclaturalCode
.ICBN();
271 } else if (nameCodePreference
.equals(ITaxEditorConstants
.CODE_PREFERENCE_ICZN
)) {
272 return NomenclaturalCode
.ICZN();
277 public static NonViralName
getPreferredNameClassInstance() {
278 String nameCodePreference
= TaxEditorPlugin
.getDefault().getPreferenceStore().getString(ITaxEditorConstants
.CODE_PREFERENCE
);
280 // Check whether name code preference needs to be initialized
281 if (nameCodePreference
== null || nameCodePreference
.equals("")) {
282 nameCodePreference
= ITaxEditorConstants
.DEFAULT_CODE_PREFERENCE
;
283 TaxEditorPlugin
.getDefault().getPreferenceStore().setValue(ITaxEditorConstants
.CODE_PREFERENCE
,
287 if (nameCodePreference
.equals(ITaxEditorConstants
.CODE_PREFERENCE_ICBN
)) {
288 return BotanicalName
.NewInstance(null);
289 } else if (nameCodePreference
.equals(ITaxEditorConstants
.CODE_PREFERENCE_ICZN
)) {
290 return ZoologicalName
.NewInstance(null);
292 return NonViralName
.NewInstance(null);
295 public static ReferenceBase
getSessionDefaultSec() {
297 // return TaxEditorPlugin.getDefault().getSec();
300 public static void makeTaxonSynonym(Taxon oldTaxon
, Taxon newAcceptedTaxon
,
301 SynonymRelationshipType synonymType
, ReferenceBase citation
,
302 String citationMicroReference
) {
303 ITaxonService taxonService
= TaxEditorPlugin
.getDefault()
305 taxonService
.makeTaxonSynonym(oldTaxon
, newAcceptedTaxon
, synonymType
,
306 citation
, citationMicroReference
);
309 public static Set
<TaxonNameBase
> getNameByName(String searchText
) {
310 Set
<TaxonNameBase
> resultsSet
= new HashSet
<TaxonNameBase
>();
311 resultsSet
.addAll(TaxEditorPlugin
.getDefault().getNameService()
312 .getNamesByName(searchText
.replace("*", "%")));
316 public static Set
<TaxonNameBase
> getNameByNameForTaxonContext(String searchText
, Taxon taxon
) {
317 Set
<TaxonNameBase
> resultsSet
= new HashSet
<TaxonNameBase
>();
318 // resultsSet.addAll(TaxEditorPlugin.getDefault().getNameService()
319 // .getNamesByName(searchText.replace("*", "%")));
320 resultsSet
.addAll(TaxEditorPlugin
.getDefault().getNameService().
321 getNamesByName(searchText
.replace("*", "%"), taxon
));