Merge branch 'release/5.44.0'
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / tcsxml / in / TcsXmlTaxonRelationsImport.java
1 /**
2 * Copyright (C) 2009 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 package eu.etaxonomy.cdm.io.tcsxml.in;
10
11 import java.io.InputStream;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17
18 import org.apache.logging.log4j.LogManager;
19 import org.apache.logging.log4j.Logger;
20 import org.jdom.Element;
21 import org.jdom.Namespace;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.stereotype.Component;
24
25 import eu.etaxonomy.cdm.common.CdmUtils;
26 import eu.etaxonomy.cdm.common.ResultWrapper;
27 import eu.etaxonomy.cdm.common.XmlHelp;
28 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
29 import eu.etaxonomy.cdm.io.common.ICdmIO;
30 import eu.etaxonomy.cdm.io.common.MapWrapper;
31 import eu.etaxonomy.cdm.io.tcsrdf.TcsRdfImportConfigurator;
32 import eu.etaxonomy.cdm.io.tcsrdf.TcsRdfTaxonNameImport;
33 import eu.etaxonomy.cdm.io.tcsxml.TcsXmlTransformer;
34 import eu.etaxonomy.cdm.model.common.CdmBase;
35 import eu.etaxonomy.cdm.model.common.IRelationshipType;
36 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
37 import eu.etaxonomy.cdm.model.description.TaxonDescription;
38 import eu.etaxonomy.cdm.model.name.TaxonName;
39 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
40 import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
41 import eu.etaxonomy.cdm.model.reference.Reference;
42 import eu.etaxonomy.cdm.model.taxon.Classification;
43 import eu.etaxonomy.cdm.model.taxon.Synonym;
44 import eu.etaxonomy.cdm.model.taxon.SynonymType;
45 import eu.etaxonomy.cdm.model.taxon.Taxon;
46 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
47 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
48 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
49 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
50
51 /**
52 * @author a.mueller
53 */
54 @Component
55 public class TcsXmlTaxonRelationsImport extends TcsXmlImportBase implements ICdmIO<TcsXmlImportState> {
56 private static final long serialVersionUID = 6632990505515905663L;
57
58 private static final Logger logger = LogManager.getLogger();
59
60 private static int modCount = 30000;
61
62 @Autowired
63 private TcsRdfTaxonNameImport rdfNameImport;
64 private TcsRdfImportConfigurator rdfConfig = TcsRdfImportConfigurator.NewInstance(null, null);
65
66 public TcsXmlTaxonRelationsImport(){
67 super();
68 }
69
70 @Override
71 public boolean doCheck(TcsXmlImportState state){
72 boolean result = true;
73 logger.warn("Checking for TaxonRelations not yet implemented");
74 logger.warn("Creation of homotypic relations is still problematic");
75
76 return result;
77 }
78
79 @Override
80 public void doInvoke(TcsXmlImportState state){
81
82
83 logger.info("start make taxon relations ...");
84 MapWrapper<TaxonBase> taxonMap = (MapWrapper<TaxonBase>)state.getStore(ICdmIO.TAXON_STORE);
85 MapWrapper<TaxonName> taxonNameMap = (MapWrapper<TaxonName>)state.getStore(ICdmIO.TAXONNAME_STORE);
86 MapWrapper<Reference> referenceMap = (MapWrapper<Reference>)state.getStore(ICdmIO.REFERENCE_STORE);
87
88 Set<TaxonBase> taxonStore = new HashSet<TaxonBase>();
89
90 ResultWrapper<Boolean> success = ResultWrapper.NewInstance(true);
91 String childName;
92 boolean obligatory;
93 String idNamespace = "TaxonRelationDTO";
94
95 TcsXmlImportConfigurator config = state.getConfig();
96 Element elDataSet = super.getDataSetElement(config);
97 Namespace tcsNamespace = config.getTcsXmlNamespace();
98
99 childName = "TaxonConcepts";
100 obligatory = false;
101 Element elTaxonConcepts = XmlHelp.getSingleChildElement(success, elDataSet, childName, tcsNamespace, obligatory);
102
103 childName = "TaxonConcept";
104 List<Element> elTaxonConceptList = elTaxonConcepts == null ? new ArrayList<Element>() : elTaxonConcepts.getChildren(childName, tcsNamespace);
105
106 int i = 0;
107 int taxonRelCount = 0;
108
109 //for each taxonConcept
110 logger.info("NUmber of taxon concepts: " + elTaxonConceptList.size());
111 for (Element elTaxonConcept : elTaxonConceptList){
112 if ((i++ % modCount) == 0){ logger.info("Taxa handled: " + (i-1));}
113 taxonRelCount += makeTaxonConcept(state, taxonMap, taxonStore, elTaxonConcept, tcsNamespace, success);
114 }//elTaxonConcept
115 logger.info("Taxa handled: " + taxonStore.size());
116 //TaxonRelationshipAssertions
117 taxonRelCount += makeTaxonRelationshipAssertion(state, taxonMap, referenceMap, taxonStore, elDataSet, tcsNamespace, success);
118
119 logger.info("Taxa to save: " + taxonStore.size());
120 getTaxonService().save(taxonStore);
121
122 //do basionym relationships
123 childName = "TaxonNames";
124 obligatory = false;
125 Element elTaxonNames = XmlHelp.getSingleChildElement(success, elDataSet, childName, tcsNamespace, obligatory);
126
127 childName = "TaxonName";
128 List<Element> elTaxonNameList = elTaxonNames == null ? new ArrayList<Element>() : elTaxonNames.getChildren(childName, tcsNamespace);
129
130 logger.info("NUmber of taxon concepts: " + elTaxonNameList.size());
131 for (Element elTaxonName : elTaxonNameList){
132 if ((i++ % modCount) == 0){ logger.info("Taxa handled: " + (i-1));}
133 taxonRelCount += makeBasionymRelations(state, taxonMap, taxonNameMap, taxonStore, elTaxonName, tcsNamespace, success);
134 }//elTaxonConcept
135 logger.info("Taxa handled: " + taxonStore.size());
136
137
138 taxonStore = null;
139 logger.info("end make taxon relations ...");
140 if (!success.getValue()){
141 state.setUnsuccessfull();
142 }
143 return;
144 }
145
146 private int makeBasionymRelations(TcsXmlImportState state,
147 MapWrapper<TaxonBase> taxonMap, MapWrapper<TaxonName> taxonNameMap, Set<TaxonBase> taxonStore,
148 Element elTaxonName, Namespace tcsNamespace,
149 ResultWrapper<Boolean> success) {
150
151 /*
152 * <tcs:Basionym>
153 <tcs:RelatedName ref="urn:lsid:ipni.org:names:50000063-1">Caryophyllaceae</tcs:RelatedName>
154 </tcs:Basionym>
155 */
156
157
158 String childName = "Basionym";
159 boolean obligatory = false;
160 Element elBasionymRelationships = XmlHelp.getSingleChildElement(success, elTaxonName, childName, tcsNamespace, obligatory);
161 if (elBasionymRelationships != null){
162 childName = "RelatedName";
163 obligatory = false;
164 Element elBasionym = XmlHelp.getSingleChildElement(success, elBasionymRelationships, childName, tcsNamespace, obligatory);
165
166 String id = elTaxonName.getAttributeValue("id");
167 TaxonName name = taxonNameMap.get(removeVersionOfRef(id));
168
169 TaxonBase taxonBase = name.getTaxonBases().iterator().next();
170
171 String ref = elBasionym.getAttributeValue("ref");
172 TaxonName basionymName = taxonNameMap.get(removeVersionOfRef(ref));
173
174 if (basionymName != null){
175 basionymName = HibernateProxyHelper.deproxy(basionymName, TaxonName.class);
176 TaxonBase basionym;
177 if (basionymName.getTaxonBases().isEmpty()){
178 basionym = Synonym.NewInstance(basionymName, null);
179 }else{
180 basionym = basionymName.getTaxonBases().iterator().next();
181 }
182 //Synonym basionymSyn;
183 if (basionym instanceof Taxon){
184
185 if (!(taxonBase instanceof Synonym)){
186 logger.debug("The taxon " + basionymName.getTitleCache() + " is used in a concept and can not be a synonym " + name.getTitleCache() + "but will be added as basionym for the homotypical group");
187 name.addBasionym(basionymName);
188 //basionymName.getHomotypicalGroup().addTypifiedName(name);
189 } else if (taxonBase instanceof Synonym){
190 Synonym synonym = (Synonym) taxonBase;
191 ((Taxon)basionym).addSynonym(synonym, SynonymType.HOMOTYPIC_SYNONYM_OF);
192 basionym.getHomotypicGroup().setGroupBasionym(basionymName);
193 taxonStore.add(basionym);
194 }
195 }else{
196 if (taxonBase instanceof Taxon){
197 Synonym synonym = (Synonym) basionym;
198 ((Taxon)taxonBase).addSynonym(synonym, SynonymType.HOMOTYPIC_SYNONYM_OF);
199 taxonBase.getHomotypicGroup().setGroupBasionym(basionymName);
200 taxonStore.add(taxonBase);
201 } else{
202 Taxon acc = ((Synonym)taxonBase).getAcceptedTaxon();
203 if (acc != null){
204 acc.addHomotypicSynonym((Synonym)basionym);
205 basionymName.getHomotypicalGroup().setGroupBasionym(basionymName);
206 }
207 }
208 }
209
210
211 } else{
212 basionymName = TaxonNameFactory.NewNonViralInstance(name.getRank());
213 childName = "RelatedName";
214 obligatory = true;
215 Element elName = XmlHelp.getSingleChildElement(success, elBasionym, childName, tcsNamespace, obligatory);
216 String strName = (elName == null)? "" : elName.getTextNormalize();
217 basionymName.setTitleCache(strName, false);
218 Synonym basionymSyn = Synonym.NewInstance(basionymName, unknownSec());
219 if (taxonBase instanceof Taxon){
220 Taxon taxon = (Taxon)taxonBase;
221 taxon.addSynonym(basionymSyn, SynonymType.HOMOTYPIC_SYNONYM_OF);
222 taxon.getHomotypicGroup().setGroupBasionym(basionymName);
223 taxonStore.add(taxon);
224 } else{
225 Synonym syn = (Synonym) taxonBase;
226 if (syn.getAcceptedTaxon() != null){
227 Taxon accTaxon = syn.getAcceptedTaxon();
228 accTaxon.addSynonym(basionymSyn, SynonymType.HOMOTYPIC_SYNONYM_OF);
229 accTaxon.getHomotypicGroup().setGroupBasionym(basionymName);
230 taxonStore.add(accTaxon);
231 }
232 }
233 }
234 }
235 return 1;
236 }
237
238 private int makeTaxonConcept(TcsXmlImportState state, MapWrapper<TaxonBase> taxonMap, Set<TaxonBase> taxonStore, Element elTaxonConcept, Namespace tcsNamespace, ResultWrapper<Boolean> success){
239 int taxonRelCount = 0;
240
241 String childName = "TaxonRelationships";
242 boolean obligatory = false;
243 Element elTaxonRelationships = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
244
245 if (elTaxonRelationships != null){
246 //Relationships
247 String tcsElementName = "TaxonRelationship";
248 List<Element> elTaxonRelationshipList = elTaxonRelationships.getChildren(tcsElementName, tcsNamespace);
249 logger.info("Number of taxonrelationships: " + elTaxonRelationshipList.size());
250 for (Element elTaxonRelationship: elTaxonRelationshipList){
251
252 taxonRelCount++;
253 logger.debug("TaxonRelationship "+ taxonRelCount);
254
255 String strId = elTaxonConcept.getAttributeValue("id");
256 //TODO
257 // String strConceptType = elTaxonConcept.getAttributeValue("type"); //original, revision, incomplete, aggregate, nominal
258 // String strPrimary = elTaxonConcept.getAttributeValue("primary"); //If primary='true' the concept is the first level response to a query. If 'false' the concept may be a secondary concept linked directly or indirectly to the definition of a primary concept.
259 // String strForm = elTaxonConcept.getAttributeValue("form"); //anamorph, teleomorph, hybrid
260
261 TaxonBase fromTaxon = taxonMap.get(removeVersionOfRef(strId));
262 makeRelationshipType(state, elTaxonRelationship, taxonMap, taxonStore, fromTaxon, success);
263
264 if (fromTaxon instanceof Taxon){
265 makeHomotypicSynonymRelations((Taxon)fromTaxon);
266 }
267 }// end Relationship
268 }
269 return taxonRelCount;
270 }
271
272 private int makeTaxonRelationshipAssertion(
273 TcsXmlImportState state,
274 MapWrapper<TaxonBase> taxonMap,
275 MapWrapper<Reference> referenceMap,
276 Set<TaxonBase> taxonStore,
277 Element elDataSet,
278 Namespace tcsNamespace,
279 ResultWrapper<Boolean> success){
280
281 int i = 0;
282 String childName = "TaxonRelationshipAssertions";
283 boolean obligatory = false;
284 Element elTaxonRelationshipAssertions = XmlHelp.getSingleChildElement(success, elDataSet, childName, tcsNamespace, obligatory);
285 if(elTaxonRelationshipAssertions == null){
286 return 0;
287 }
288
289 childName = "TaxonRelationshipAssertion";
290 List<Element> elTaxonRelationshipAssertionList = elTaxonRelationshipAssertions.getChildren(childName, tcsNamespace);
291 //for each taxon relationship assertion
292 for (Element elTaxonRelationshipAssertion : elTaxonRelationshipAssertionList){
293 if ((i++ % modCount) == 0){ logger.info("TaxonRelationshipAssertions handled: " + (i-1));}
294 String strId = elTaxonRelationshipAssertion.getAttributeValue("id");
295 //TODO id
296
297 childName = "AccordingTo";
298 obligatory = true;
299 Element elAccordingTo = XmlHelp.getSingleChildElement(success, elTaxonRelationshipAssertion, childName, tcsNamespace, obligatory);
300 Reference ref = makeAccordingTo(elAccordingTo, referenceMap, success);
301
302 childName = "FromTaxonConcept";
303 obligatory = true;
304 Element elFromTaxonConcept = XmlHelp.getSingleChildElement(success, elTaxonRelationshipAssertion, childName, tcsNamespace, obligatory);
305
306 Class<? extends TaxonBase> clazz = Taxon.class;
307 //TODO if synonym
308 TaxonBase fromTaxon = makeReferenceType(elFromTaxonConcept, clazz, taxonMap, success);
309
310 makeRelationshipType(state, elTaxonRelationshipAssertion, taxonMap, taxonStore, fromTaxon, success);
311 }//elTaxonRelationshipAssertion
312
313 return i;
314 }
315
316
317 /**
318 * Handles the TCS RelationshipType element.
319 * @param tcsConfig
320 * @param elRelationship
321 * @param taxonMap
322 * @param taxonStore
323 * @param fromTaxon
324 * @param success
325 */
326 private void makeRelationshipType(
327 TcsXmlImportState state
328 , Element elRelationship
329 , MapWrapper<TaxonBase> taxonMap
330 , Set<TaxonBase> taxonStore
331 , TaxonBase fromTaxon
332 , ResultWrapper<Boolean> success
333 ){
334
335 if (elRelationship == null){
336 success.setValue(false);
337 }
338 String strRelType = elRelationship.getAttributeValue("type");
339
340
341 try {
342 ResultWrapper<Boolean> isInverse = new ResultWrapper<Boolean>();
343 isInverse.setValue(false);
344 if ("has vernacular".equalsIgnoreCase(strRelType)){
345 handleVernacular(success, state, elRelationship, fromTaxon);
346 }else{
347 IRelationshipType relType = TcsXmlTransformer.tcsRelationshipType2Relationship(strRelType, isInverse);
348
349 //toTaxon (should be part of relationshipType)
350 boolean isSynonym = (relType instanceof SynonymType);
351 TaxonBase toTaxon = getToTaxon(elRelationship, taxonMap, state.getMissingConceptLSIDs(), isSynonym, success, state);
352
353 if (toTaxon != null && fromTaxon != null){
354 //exchange taxa if relationship is inverse
355 if (isInverse.getValue() == true ){
356 TaxonBase tmp = toTaxon;
357 toTaxon = fromTaxon;
358 fromTaxon = tmp;
359 }
360
361 //Create relationship
362 if (! (toTaxon instanceof Taxon)){
363 logger.warn("TaxonBase toTaxon is not of Type 'Taxon'. Relationship is not added.");
364 success.setValue(false);
365 }else{
366 Taxon taxonTo = (Taxon)toTaxon;
367 if (relType instanceof SynonymType){
368 SynonymType synRelType = (SynonymType)relType;
369 if (! (fromTaxon instanceof Synonym )){
370 logger.warn("TaxonBase fromTaxon is not of Type 'Synonym'. Relationship is not added.");
371 success.setValue(false);
372 }else{
373 Synonym synonym = (Synonym)fromTaxon;
374 TaxonName synName = synonym.getName();
375 TaxonName accName = taxonTo.getName();
376 if (synName != null && accName != null && synName.isHomotypic(accName)
377 && ( synRelType.equals(SynonymType.SYNONYM_OF))){
378 synRelType = SynonymType.HOMOTYPIC_SYNONYM_OF;
379 }
380 if (! relationExists(taxonTo, synonym, synRelType)){
381 taxonTo.addSynonym(synonym, synRelType);
382 }else{
383 //TODO citation, microReference
384 //TODO different synRelTypes -> warning
385 success.setValue(false);
386 }
387 }
388 }else if (relType instanceof TaxonRelationshipType){
389 Reference citation = null;
390 String microReference = null;
391 makeTaxonRelationship(state, (TaxonRelationshipType)relType, fromTaxon, taxonTo, citation, microReference, success);
392 }else{
393 logger.warn("Unknown Relationshiptype");
394 success.setValue(false);
395 }
396 taxonStore.add(toTaxon);
397 }
398 }else{
399 if (toTaxon == null){
400 // logger.warn("toTaxon (" + /*strToTaxon + */ ") could not be found in taxonMap. Relationship of type " + strRelType + " was not added to CDM");
401 }
402 if (fromTaxon == null){
403 // logger.warn("fromTaxon (" + /*strTaxonAbout + */") could not be found in taxonMap. Relationship was not added to CDM");
404 }
405 success.setValue(false);
406 }
407 }
408 } catch (UnknownCdmTypeException e) {
409 //TODO
410 logger.warn("relationshipType " + strRelType + " not yet implemented");
411 success.setValue(false);
412 }
413 return;
414 }
415
416 private void handleVernacular(ResultWrapper<Boolean> success, TcsXmlImportState state, Element elRelationship, TaxonBase taxonBase) {
417 if (! taxonBase.isInstanceOf(Taxon.class)){
418 logger.warn("From Taxon is not of type Taxon but of type " + taxonBase.getClass().getSimpleName());
419 success.setValue(false);
420 return;
421 }else{
422 Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
423 Map<String, CommonTaxonName> commonNameMap = state.getCommonNameMap();
424 CommonTaxonName commonTaxonName = getCommonName(elRelationship, commonNameMap, success);
425 TaxonDescription description = getDescription(taxon);
426 description.addElement(commonTaxonName);
427 }
428 }
429
430 private TaxonDescription getDescription(Taxon taxon) {
431 if (taxon.getDescriptions().isEmpty()){
432 return TaxonDescription.NewInstance(taxon);
433 }else{
434 //TODO only if the description represents this TCS file
435 return taxon.getDescriptions().iterator().next();
436 }
437 }
438
439 private CommonTaxonName getCommonName(Element elTaxonRelationship, Map<String, CommonTaxonName> commonNameMap, ResultWrapper<Boolean> success){
440 CommonTaxonName result = null;
441 if (elTaxonRelationship == null || commonNameMap == null){
442 success.setValue(false);
443 }else{
444 String childName = "ToTaxonConcept";
445 boolean obligatory = true;
446 Element elToTaxonConcept = XmlHelp.getSingleChildElement(success, elTaxonRelationship, childName, elTaxonRelationship.getNamespace(), obligatory);
447
448 String linkType = elToTaxonConcept.getAttributeValue("linkType");
449 if (linkType == null || linkType.equals("local")){
450 String ref = elToTaxonConcept.getAttributeValue("ref");
451 if (ref != null){
452 result = commonNameMap.get(ref);
453 }else{
454 logger.warn("Non ref not yet implemented for vernacular name relationship");
455 }
456 }else{
457 logger.warn("External link types for vernacular name not yet implemented");
458 }
459 }
460 return result;
461 }
462
463 private void makeTaxonRelationship(TcsXmlImportState state, TaxonRelationshipType relType, TaxonBase fromTaxon, Taxon taxonTo, Reference citation, String microReference, ResultWrapper<Boolean> success){
464 TaxonRelationshipType taxRelType = relType;
465 if (! (fromTaxon instanceof Taxon )){
466 logger.warn("TaxonBase fromTaxon " + /*strTaxonAbout +*/ "is not of Type 'Taxon'. Relationship is not added.");
467 success.setValue(false);
468 }else{
469 Taxon taxonFrom = (Taxon)fromTaxon;
470 if (relType.equals(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN())){
471 makeTaxonomicallyIncluded(state, taxonTo, taxonFrom, citation, microReference);
472 }else{
473 taxonFrom.addTaxonRelation(taxonTo, taxRelType, citation, microReference);
474 }
475 }
476 }
477
478 private boolean makeTaxonomicallyIncluded(TcsXmlImportState state, Taxon toTaxon, Taxon fromTaxon, Reference citation, String microCitation){
479 Reference sec = toTaxon.getSec();
480 Classification tree = state.getTree(sec);
481 if (tree == null){
482 tree = makeTree(state, sec);
483 }
484 TaxonNode childNode = tree.addParentChild(toTaxon, fromTaxon, citation, microCitation);
485 return (childNode != null);
486 }
487
488
489 @SuppressWarnings("rawtypes")
490 private TaxonBase getToTaxon(Element elTaxonRelationship, MapWrapper<TaxonBase> map, List<String> missingNameID,boolean isSynonym, ResultWrapper<Boolean> success, TcsXmlImportState state){
491 TaxonBase result = null;
492 if (elTaxonRelationship == null || map == null){
493 success.setValue(false);
494 }else{
495 String childName = "ToTaxonConcept";
496 boolean obligatory = true;
497 Element elToTaxonConcept = XmlHelp.getSingleChildElement(success, elTaxonRelationship, childName, elTaxonRelationship.getNamespace(), obligatory);
498
499
500 String linkType = elToTaxonConcept.getAttributeValue("linkType");
501 if (linkType == null || linkType.equals("local")){
502 String ref = elToTaxonConcept.getAttributeValue("ref");
503 if (ref != null){
504 result = map.get(ref);
505 if (result == null && state.getConfig().isDoGetMissingNames()){
506
507
508 String[] split= ref.split(":");
509 String id = split[split.length-1];
510 logger.info("get name for id " + id);
511 if (missingNameID.contains(id)){
512 return null;
513 }
514 InputStream nameStream = service.getNamesById(id);
515
516
517 try{
518 String nameUri = "urn:lsid:ipni.org:names:"+ id;
519 TaxonName name = rdfNameImport.handleRdfElementFromStream(nameStream, rdfConfig, (MapWrapper<TaxonName>)state.getStore(ICdmIO.TAXONNAME_STORE), nameUri);
520 if (name != null){
521 if (name.getTaxa().isEmpty()){
522
523 result = Taxon.NewInstance(name, null);
524 }else{
525 result = name.getTaxa().iterator().next();
526 }
527 name.addSource(OriginalSourceType.Import, ref, "TaxonConcept", null, null);
528 result.addSource(OriginalSourceType.Import, ref, "TaxonConcept", null, null);
529
530 map.put(removeVersionOfRef(ref), result);
531 } else{
532 missingNameID.add(id);
533 }
534 }catch(Exception e){
535 logger.debug(e.getMessage());
536 e.printStackTrace();
537 }
538 }
539 }else{
540 String title = elToTaxonConcept.getTextNormalize();
541 //TODO synonym?
542 TaxonName taxonName = TaxonNameFactory.NewNonViralInstance(null);
543 taxonName.setTitleCache(title, true);
544 logger.warn("Free text related taxon seems to be bug in TCS");
545 if (isSynonym){
546 result = Synonym.NewInstance(taxonName, TcsXmlTaxonImport.unknownSec());
547 }else{
548 result = Taxon.NewInstance(taxonName, TcsXmlTaxonImport.unknownSec());
549 }
550 result.setTitleCache(title, true);
551 }
552 }else{
553 logger.warn("External link types for synonym not yet implemented");
554 }
555 }
556 return result;
557 }
558
559 private boolean relationExists(Taxon taxonTo, Synonym synonym, SynonymType synRelType){
560 if (synonym == null || taxonTo == null
561 || !taxonTo.equals(synonym.getAcceptedTaxon())){
562 return false;
563 }else{
564 return CdmUtils.nullSafeEqual(synonym.getType(),synRelType);
565 }
566 }
567
568 private boolean makeHomotypicSynonymRelations(Taxon aboutTaxon){
569 TaxonName aboutName = aboutTaxon.getName();
570 if (aboutName != null){
571 Set<TaxonName> typifiedNames = aboutName.getHomotypicalGroup().getTypifiedNames();
572 for (TaxonName typifiedName : typifiedNames){
573 //TODO check if name is part of this tcs file
574 if (typifiedName.equals(aboutName)){
575 continue;
576 }
577 Set<Synonym> syns = typifiedName.getSynonyms();
578 for(Synonym syn:syns){
579 aboutTaxon.addSynonym(syn, SynonymType.HOMOTYPIC_SYNONYM_OF);
580 }
581 }
582 }
583 return true;
584 }
585
586 @Override
587 protected boolean isIgnore(TcsXmlImportState state){
588 return ! state.getConfig().isDoRelTaxa();
589 }
590 }