Added "Save All" button which saves all open taxon editors, commits the current trans...
[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 import java.util.SortedSet;
15
16 import org.apache.log4j.Logger;
17 import org.springframework.transaction.TransactionStatus;
18
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;
41
42 /**
43 * @author p.ciardelli
44 * @created 26.05.2008
45 * @version 1.0
46 */
47 /**
48 * @author p.ciardelli
49 * @created 17.09.2008
50 * @version 1.0
51 */
52 public class CdmUtil {
53 private static final Logger logger = Logger.getLogger(CdmUtil.class);
54
55 private static INonViralNameParser nonViralNameParser;
56
57 private static TermVocabulary<Feature> features;
58 private static SortedSet<Rank> ranks;
59
60 /**
61 * Checks whether synonym's name is the basionym for ALL names
62 * in its group.
63 *
64 * @param synonym
65 *
66 * @return
67 */
68 public static boolean isSynonymGroupBasionym(Synonym synonym) {
69
70 TaxonNameBase synonymName = synonym.getName();
71 return isNameGroupBasionym(synonymName);
72 }
73
74 /**
75 * Checks whether name is the basionym for ALL names
76 * in its group.
77 * @param name
78 *
79 * @return
80 */
81 public static boolean isNameGroupBasionym(TaxonNameBase name) {
82 if (name == null) {
83 return false;
84 }
85
86 HomotypicalGroup homotypicalGroup = name.getHomotypicalGroup();
87 if (homotypicalGroup == null) {
88 return false;
89 }
90
91 Set<TaxonNameBase> typifiedNames = homotypicalGroup.getTypifiedNames();
92
93 // Check whether there are any other names in the group
94 if (typifiedNames.size() == 1) {
95 return false;
96 }
97
98 boolean isBasionymToAll = true;
99
100 for (TaxonNameBase taxonName : typifiedNames) {
101 if (!taxonName.equals(name)) {
102 if (!isNameBasionymOf(name, taxonName)) {
103 return false;
104 }
105 }
106 }
107 return true;
108 }
109
110 /**
111 * Checks whether a basionym relationship exists between fromName and toName.
112 *
113 * @param fromName
114 * @param toName
115 * @return
116 */
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)) {
122 return true;
123 }
124 }
125 return false;
126 }
127
128 /**
129 * Creates a basionym relationship between basionymName and
130 * each name in its homotypic group.
131 *
132 * @param basionymName
133 */
134 public static void setGroupBasionym(TaxonNameBase basionymName) {
135 HomotypicalGroup homotypicalGroup = basionymName.getHomotypicalGroup();
136 if (homotypicalGroup == null) {
137 return;
138 }
139 for (TaxonNameBase name : homotypicalGroup.getTypifiedNames()) {
140 if (!name.equals(basionymName)) {
141
142 // First check whether the relationship already exists
143 if (!isNameBasionymOf(basionymName, name)) {
144
145 // Then create it
146 name.addRelationshipFromName(basionymName,
147 NameRelationshipType.BASIONYM(), null);
148 }
149 }
150 }
151 }
152
153 /**
154 * Removes all basionym relationships between basionymName and
155 * the names in its homotypic group.
156 *
157 * @param basionymName
158 */
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>();
163
164 for (NameRelationship relation : relations) {
165
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);
171 }
172 }
173
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);
179 }
180 }
181
182 /**
183 * Checks whether name belongs to the same homotypic group as taxon's name.
184 *
185 * @param name
186 * @param taxon
187 * @return
188 */
189 public static boolean isNameHomotypic(TaxonNameBase name, Taxon taxon) {
190 TaxonNameBase taxonName = taxon.getName();
191 if (taxonName == null || name == null) {
192 return false;
193 }
194 HomotypicalGroup homotypicGroup = taxonName.getHomotypicalGroup();
195 if (homotypicGroup == null) {
196 return false;
197 }
198 if (homotypicGroup.equals(name.getHomotypicalGroup())) {
199 return true;
200 }
201 return false;
202 }
203
204 /**
205 * Returns whatever is currently considered the display name for Taxon
206 * objects. Currently titleCache.
207 *
208 * @param taxon
209 * @return
210 */
211 public static String getDisplayName(TaxonBase taxonBase) {
212 TaxonNameBase name = taxonBase.getName();
213 return getDisplayName(name);
214 }
215
216 /**
217 * @param name
218 * @return
219 */
220 public static String getDisplayName(TaxonNameBase name) {
221 if (name != null) {
222 // return name.getTitleCache();
223 return name.getFullTitleCache();
224 }
225 return "";
226 }
227
228 /**
229 * @return
230 */
231 private static INonViralNameParser getNonViralNameParser() {
232 if (nonViralNameParser == null) {
233 nonViralNameParser = NonViralNameParserImpl.NewInstance();
234 }
235 return nonViralNameParser;
236 }
237
238 /**
239 * Takes a raw string, returns a brand spanking new name object.
240 *
241 * @param fullReference
242 * @param nomCode
243 * @param rank
244 * @return
245 */
246 public static TaxonNameBase parseFullReference(String fullReference,
247 NomenclaturalCode nomCode, Rank rank) {
248
249 if (nomCode == null) {
250 nomCode = UiUtil.getPreferredNomenclaturalCode();
251 }
252
253 // logger.warn("Nomenclatural code: " + nomCode.toString());
254
255 TaxonNameBase name = getNonViralNameParser().parseFullReference(fullReference,
256 nomCode, rank);
257 return name;
258 }
259
260 /**
261 * Takes an existing name object and a string, returns the same name
262 * object with the fields parsed from the string.
263 *
264 * @param nameToBeFilled
265 * @param fullReference
266 * @param rank
267 * @param makeEmpty
268 */
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);
272 }
273
274 /**
275 * @param nameToBeFilled
276 * @param fullNameString
277 * @param rank
278 * @param makeEmpty
279 */
280 public static void parseFullName(NonViralName nameToBeFilled,
281 String fullNameString, Rank rank, boolean makeEmpty) {
282 getNonViralNameParser().parseFullName(nameToBeFilled, fullNameString,
283 rank, makeEmpty);
284 }
285
286
287
288 /**
289 * @return
290 */
291 public static ReferenceBase getSessionDefaultSec() {
292 return null;
293 // return TaxEditorPlugin.getDefault().getSec();
294 }
295
296 /**
297 * @param oldTaxon
298 * @param newAcceptedTaxon
299 * @param synonymType
300 * @param citation
301 * @param citationMicroReference
302 */
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);
309 }
310
311 /**
312 * @param searchText
313 * @return
314 */
315 public static Set<TaxonNameBase> getNameByName(String searchText) {
316 Set<TaxonNameBase> resultsSet = new HashSet<TaxonNameBase>();
317 resultsSet.addAll(getNameService()
318 .getNamesByName(searchText.replace("*", "%")));
319 return resultsSet;
320 }
321
322 public static INameService getNameService() {
323 return TaxEditorPlugin.getDefault().getNameService();
324 }
325
326 public static ITaxonService getTaxonService() {
327 return TaxEditorPlugin.getDefault().getTaxonService();
328 }
329
330 public static IDescriptionService getDescriptionService() {
331 return TaxEditorPlugin.getDefault().getDescriptionService();
332 }
333
334 public static TermVocabulary<Feature> getFeatures() {
335 if (features == null) {
336 features = CdmUtil.getDescriptionService().
337 getDefaultFeatureVocabulary();
338 }
339 return features;
340 }
341
342 public static SortedSet<Rank> getRanks() {
343 if (ranks == null) {
344 // TransactionStatus tx = startReadOnlyTransaction();
345 OrderedTermVocabulary<Rank> rankVocabulary = TaxEditorPlugin.getDefault().getRankVocabulary();
346 ranks = rankVocabulary.getOrderedTerms(null);
347 // commitTransaction(tx);
348 }
349 return ranks;
350 }
351
352 /**
353 * @param searchText
354 * @param taxon
355 * @return
356 */
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));
361 return resultsSet;
362 }
363
364 public static TransactionStatus startTransaction() {
365 return TaxEditorPlugin.getDefault().getCdmApp().startTransaction();
366 }
367
368 public static TransactionStatus startReadOnlyTransaction() {
369 return TaxEditorPlugin.getDefault().getCdmApp().startTransaction(true);
370 }
371
372 public static void commitTransaction(TransactionStatus tx) {
373 TaxEditorPlugin.getDefault().getCdmApp().commitTransaction(tx);
374 }
375 }