d34e61cfc0b30fc66334ea2382f686bcd806ca5e
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / model / CdmUtil.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.model;
11
12 import java.util.HashSet;
13 import java.util.Set;
14
15 import org.apache.log4j.Logger;
16
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;
36
37 /**
38 * @author p.ciardelli
39 * @created 26.05.2008
40 * @version 1.0
41 */
42 public class CdmUtil {
43 private static final Logger logger = Logger.getLogger(CdmUtil.class);
44
45 static INonViralNameParser nonViralNameParser;
46
47 /**
48 * Checks whether synonym's name is the basionym for ALL names
49 * in its group.
50 *
51 * @param synonym
52 *
53 * @return
54 */
55 public static boolean isSynonymGroupBasionym(Synonym synonym) {
56
57 TaxonNameBase synonymName = synonym.getName();
58 return isNameGroupBasionym(synonymName);
59 }
60
61 /**
62 * Checks whether name is the basionym for ALL names
63 * in its group.
64 * @param name
65 *
66 * @return
67 */
68 public static boolean isNameGroupBasionym(TaxonNameBase name) {
69 if (name == null) {
70 return false;
71 }
72
73 HomotypicalGroup homotypicalGroup = name.getHomotypicalGroup();
74 if (homotypicalGroup == null) {
75 return false;
76 }
77
78 Set<TaxonNameBase> typifiedNames = homotypicalGroup.getTypifiedNames();
79
80 // Check whether there are any other names in the group
81 if (typifiedNames.size() == 1) {
82 return false;
83 }
84
85 boolean isBasionymToAll = true;
86
87 for (TaxonNameBase taxonName : typifiedNames) {
88 if (!taxonName.equals(name)) {
89 if (!isNameBasionymOf(name, taxonName)) {
90 return false;
91 }
92 }
93 }
94 return true;
95 }
96
97 /**
98 * Checks whether a basionym relationship exists between fromName and toName.
99 *
100 * @param fromName
101 * @param toName
102 * @return
103 */
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)) {
109 return true;
110 }
111 }
112 return false;
113 }
114
115 /**
116 * Creates a basionym relationship between basionymName and
117 * each name in its homotypic group.
118 *
119 * @param basionymName
120 */
121 public static void setGroupBasionym(TaxonNameBase basionymName) {
122 HomotypicalGroup homotypicalGroup = basionymName.getHomotypicalGroup();
123 if (homotypicalGroup == null) {
124 return;
125 }
126 for (TaxonNameBase name : homotypicalGroup.getTypifiedNames()) {
127 if (!name.equals(basionymName)) {
128
129 // First check whether the relationship already exists
130 if (!isNameBasionymOf(basionymName, name)) {
131
132 // Then create it
133 name.addRelationshipFromName(basionymName,
134 NameRelationshipType.BASIONYM(), null);
135 }
136 }
137 }
138 }
139
140 /**
141 * Removes all basionym relationships between basionymName and
142 * the names in its homotypic group.
143 *
144 * @param basionymName
145 */
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>();
150
151 for (NameRelationship relation : relations) {
152
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);
158 }
159 }
160
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);
166 }
167 }
168
169 /**
170 * Checks whether name belongs to the same homotypic group as taxon's name.
171 *
172 * @param name
173 * @param taxon
174 * @return
175 */
176 public static boolean isNameHomotypic(TaxonNameBase name, Taxon taxon) {
177 TaxonNameBase taxonName = taxon.getName();
178 if (taxonName == null || name == null) {
179 return false;
180 }
181 HomotypicalGroup homotypicGroup = taxonName.getHomotypicalGroup();
182 if (homotypicGroup == null) {
183 return false;
184 }
185 if (homotypicGroup.equals(name.getHomotypicalGroup())) {
186 return true;
187 }
188 return false;
189 }
190
191 /**
192 * Returns whatever is currently considered the display name for Taxon
193 * objects. Currently titleCache.
194 *
195 * @param taxon
196 * @return
197 */
198 public static String getDisplayName(TaxonBase taxonBase) {
199 TaxonNameBase name = taxonBase.getName();
200 return getDisplayName(name);
201 }
202
203 public static String getDisplayName(TaxonNameBase name) {
204 if (name != null) {
205 return name.getTitleCache();
206 }
207 return "";
208 }
209
210 private static INonViralNameParser getNonViralNameParser() {
211 if (nonViralNameParser == null) {
212 nonViralNameParser = NonViralNameParserImpl.NewInstance();
213 }
214 return nonViralNameParser;
215 }
216
217 /**
218 * Takes a raw string, returns a brand spanking new name object.
219 *
220 * @param fullReference
221 * @param nomCode
222 * @param rank
223 * @return
224 */
225 public static TaxonNameBase parseFullReference(String fullReference,
226 NomenclaturalCode nomCode, Rank rank) {
227
228 if (nomCode == null) {
229 nomCode = getPreferredNomenclaturalCode();
230 }
231
232 // logger.warn("Nomenclatural code: " + nomCode.toString());
233
234 TaxonNameBase name = getNonViralNameParser().parseFullReference(fullReference,
235 nomCode, rank);
236 return name;
237 }
238
239 /**
240 * Takes an existing name object and a string, returns the same name
241 * object with the fields parsed from the string.
242 *
243 * @param nameToBeFilled
244 * @param fullReference
245 * @param rank
246 * @param makeEmpty
247 */
248 public static void parseFullReference(NonViralName nameToBeFilled, String fullReference, Rank rank, boolean makeEmpty) {
249 getNonViralNameParser().parseFullReference(nameToBeFilled, fullReference, rank, makeEmpty);
250 }
251
252 public static void parseFullName(NonViralName nameToBeFilled,
253 String fullNameString, Rank rank, boolean makeEmpty) {
254 getNonViralNameParser().parseFullName(nameToBeFilled, fullNameString,
255 rank, makeEmpty);
256 }
257
258 public static NomenclaturalCode getPreferredNomenclaturalCode() {
259
260 String nameCodePreference = TaxEditorPlugin.getDefault().getPreferenceStore().getString(ITaxEditorConstants.CODE_PREFERENCE);
261
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,
266 nameCodePreference);
267 }
268
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();
273 }
274 return null;
275 }
276
277 public static NonViralName getPreferredNameClassInstance() {
278 String nameCodePreference = TaxEditorPlugin.getDefault().getPreferenceStore().getString(ITaxEditorConstants.CODE_PREFERENCE);
279
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,
284 nameCodePreference);
285 }
286
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);
291 }
292 return NonViralName.NewInstance(null);
293 }
294
295 public static ReferenceBase getSessionDefaultSec() {
296 return null;
297 // return TaxEditorPlugin.getDefault().getSec();
298 }
299
300 public static void makeTaxonSynonym(Taxon oldTaxon, Taxon newAcceptedTaxon,
301 SynonymRelationshipType synonymType, ReferenceBase citation,
302 String citationMicroReference) {
303 ITaxonService taxonService = TaxEditorPlugin.getDefault()
304 .getTaxonService();
305 taxonService.makeTaxonSynonym(oldTaxon, newAcceptedTaxon, synonymType,
306 citation, citationMicroReference);
307 }
308
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("*", "%")));
313 return resultsSet;
314 }
315
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));
322 return resultsSet;
323 }
324 }