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
;
14 import java
.util
.SortedSet
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.springframework
.transaction
.TransactionStatus
;
19 import eu
.etaxonomy
.cdm
.api
.service
.IDescriptionService
;
20 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
21 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
22 import eu
.etaxonomy
.cdm
.model
.common
.OrderedTermVocabulary
;
23 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
24 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
25 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
27 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
28 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
29 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
30 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
31 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
32 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
37 import eu
.etaxonomy
.cdm
.strategy
.parser
.INonViralNameParser
;
38 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
39 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
40 import eu
.etaxonomy
.taxeditor
.UiUtil
;
52 public class CdmUtil
{
53 private static final Logger logger
= Logger
.getLogger(CdmUtil
.class);
55 private static INonViralNameParser nonViralNameParser
;
57 private static TermVocabulary
<Feature
> features
;
58 private static SortedSet
<Rank
> ranks
;
61 * Checks whether synonym's name is the basionym for ALL names
68 public static boolean isSynonymGroupBasionym(Synonym synonym
) {
70 TaxonNameBase synonymName
= synonym
.getName();
71 return isNameGroupBasionym(synonymName
);
75 * Checks whether name is the basionym for ALL names
81 public static boolean isNameGroupBasionym(TaxonNameBase name
) {
86 HomotypicalGroup homotypicalGroup
= name
.getHomotypicalGroup();
87 if (homotypicalGroup
== null) {
91 Set
<TaxonNameBase
> typifiedNames
= homotypicalGroup
.getTypifiedNames();
93 // Check whether there are any other names in the group
94 if (typifiedNames
.size() == 1) {
98 boolean isBasionymToAll
= true;
100 for (TaxonNameBase taxonName
: typifiedNames
) {
101 if (!taxonName
.equals(name
)) {
102 if (!isNameBasionymOf(name
, taxonName
)) {
111 * Checks whether a basionym relationship exists between fromName and toName.
117 public static boolean isNameBasionymOf(TaxonNameBase fromName
, TaxonNameBase toName
) {
118 Set
<NameRelationship
> relations
= toName
.getRelationsToThisName();
119 for (NameRelationship relation
: relations
) {
120 if (relation
.getType().equals(NameRelationshipType
.BASIONYM()) &&
121 relation
.getFromName().equals(fromName
)) {
129 * Creates a basionym relationship between basionymName and
130 * each name in its homotypic group.
132 * @param basionymName
134 public static void setGroupBasionym(TaxonNameBase basionymName
) {
135 HomotypicalGroup homotypicalGroup
= basionymName
.getHomotypicalGroup();
136 if (homotypicalGroup
== null) {
139 for (TaxonNameBase name
: homotypicalGroup
.getTypifiedNames()) {
140 if (!name
.equals(basionymName
)) {
142 // First check whether the relationship already exists
143 if (!isNameBasionymOf(basionymName
, name
)) {
146 name
.addRelationshipFromName(basionymName
,
147 NameRelationshipType
.BASIONYM(), null);
154 * Removes all basionym relationships between basionymName and
155 * the names in its homotypic group.
157 * @param basionymName
159 public static void removeGroupBasionym(TaxonNameBase basionymName
) {
160 HomotypicalGroup homotypicalGroup
= basionymName
.getHomotypicalGroup();
161 Set
<NameRelationship
> relations
= basionymName
.getRelationsFromThisName();
162 Set
<NameRelationship
> removeRelations
= new HashSet
<NameRelationship
>();
164 for (NameRelationship relation
: relations
) {
166 // If this is a basionym relation, and toName is in the homotypical group,
167 // remove the relationship.
168 if (relation
.getType().equals(NameRelationshipType
.BASIONYM()) &&
169 relation
.getToName().getHomotypicalGroup().equals(homotypicalGroup
)) {
170 removeRelations
.add(relation
);
174 // Removing relations from a set through which we are iterating causes a
175 // ConcurrentModificationException. Therefore, we delete the targeted
176 // relations in a second step.
177 for (NameRelationship relation
: removeRelations
) {
178 basionymName
.removeNameRelationship(relation
);
183 * Checks whether name belongs to the same homotypic group as taxon's name.
189 public static boolean isNameHomotypic(TaxonNameBase name
, Taxon taxon
) {
190 TaxonNameBase taxonName
= taxon
.getName();
191 if (taxonName
== null || name
== null) {
194 HomotypicalGroup homotypicGroup
= taxonName
.getHomotypicalGroup();
195 if (homotypicGroup
== null) {
198 if (homotypicGroup
.equals(name
.getHomotypicalGroup())) {
205 * Returns whatever is currently considered the display name for Taxon
206 * objects. Currently titleCache.
211 public static String
getDisplayName(TaxonBase taxonBase
) {
212 TaxonNameBase name
= taxonBase
.getName();
213 return getDisplayName(name
);
220 public static String
getDisplayName(TaxonNameBase name
) {
222 // return name.getTitleCache();
223 return name
.getFullTitleCache();
231 private static INonViralNameParser
getNonViralNameParser() {
232 if (nonViralNameParser
== null) {
233 nonViralNameParser
= NonViralNameParserImpl
.NewInstance();
235 return nonViralNameParser
;
239 * Takes a raw string, returns a brand spanking new name object.
241 * @param fullReference
246 public static TaxonNameBase
parseFullReference(String fullReference
,
247 NomenclaturalCode nomCode
, Rank rank
) {
249 if (nomCode
== null) {
250 nomCode
= UiUtil
.getPreferredNomenclaturalCode();
253 // logger.warn("Nomenclatural code: " + nomCode.toString());
255 TaxonNameBase name
= getNonViralNameParser().parseFullReference(fullReference
,
261 * Takes an existing name object and a string, returns the same name
262 * object with the fields parsed from the string.
264 * @param nameToBeFilled
265 * @param fullReference
269 public static void parseFullReference(NonViralName nameToBeFilled
, String fullReference
, Rank rank
, boolean makeEmpty
) {
270 getNonViralNameParser().parseFullReference(nameToBeFilled
, fullReference
, rank
, makeEmpty
);
271 nameToBeFilled
.setFullTitleCache(nameToBeFilled
.generateFullTitle(), false);
275 * @param nameToBeFilled
276 * @param fullNameString
280 public static void parseFullName(NonViralName nameToBeFilled
,
281 String fullNameString
, Rank rank
, boolean makeEmpty
) {
282 getNonViralNameParser().parseFullName(nameToBeFilled
, fullNameString
,
291 public static ReferenceBase
getSessionDefaultSec() {
293 // return TaxEditorPlugin.getDefault().getSec();
298 * @param newAcceptedTaxon
301 * @param citationMicroReference
303 public static void makeTaxonSynonym(Taxon oldTaxon
, Taxon newAcceptedTaxon
,
304 SynonymRelationshipType synonymType
, ReferenceBase citation
,
305 String citationMicroReference
) {
306 ITaxonService taxonService
= getTaxonService();
307 taxonService
.makeTaxonSynonym(oldTaxon
, newAcceptedTaxon
, synonymType
,
308 citation
, citationMicroReference
);
315 public static Set
<TaxonNameBase
> getNameByName(String searchText
) {
316 Set
<TaxonNameBase
> resultsSet
= new HashSet
<TaxonNameBase
>();
317 resultsSet
.addAll(getNameService()
318 .getNamesByName(searchText
.replace("*", "%")));
322 public static INameService
getNameService() {
323 return TaxEditorPlugin
.getDefault().getNameService();
326 public static ITaxonService
getTaxonService() {
327 return TaxEditorPlugin
.getDefault().getTaxonService();
330 public static IDescriptionService
getDescriptionService() {
331 return TaxEditorPlugin
.getDefault().getDescriptionService();
334 public static TermVocabulary
<Feature
> getFeatures() {
335 if (features
== null) {
336 features
= CdmUtil
.getDescriptionService().
337 getDefaultFeatureVocabulary();
342 public static SortedSet
<Rank
> getRanks() {
344 // TransactionStatus tx = startReadOnlyTransaction();
345 OrderedTermVocabulary
<Rank
> rankVocabulary
= TaxEditorPlugin
.getDefault().getRankVocabulary();
346 ranks
= rankVocabulary
.getOrderedTerms(null);
347 // commitTransaction(tx);
357 public static Set
<TaxonNameBase
> getNameByNameForTaxonContext(String searchText
, Taxon taxon
) {
358 Set
<TaxonNameBase
> resultsSet
= new HashSet
<TaxonNameBase
>();
359 resultsSet
.addAll(getNameService().getNamesByName
360 (searchText
.replace("*", "%"), taxon
));
364 public static TransactionStatus
startTransaction() {
365 return TaxEditorPlugin
.getDefault().getCdmApp().startTransaction();
368 public static TransactionStatus
startReadOnlyTransaction() {
369 return TaxEditorPlugin
.getDefault().getCdmApp().startTransaction(true);
372 public static void commitTransaction(TransactionStatus tx
) {
373 TaxEditorPlugin
.getDefault().getCdmApp().commitTransaction(tx
);