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
.cdm
.io
.tcsrdf
;
12 import java
.util
.HashSet
;
13 import java
.util
.List
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.jdom
.Element
;
18 import org
.jdom
.Namespace
;
19 import org
.springframework
.stereotype
.Component
;
21 import eu
.etaxonomy
.cdm
.common
.XmlHelp
;
22 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
23 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
24 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipTermBase
;
25 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
26 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonomicTree
;
33 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
42 public class TcsRdfTaxonRelationsImport
extends TcsRdfImportBase
implements ICdmIO
<TcsRdfImportState
> {
43 private static final Logger logger
= Logger
.getLogger(TcsRdfTaxonRelationsImport
.class);
45 private static int modCount
= 30000;
47 public TcsRdfTaxonRelationsImport(){
52 public boolean doCheck(TcsRdfImportState state
){
53 boolean result
= true;
54 logger
.warn("Checking for TaxonRelations not yet implemented");
55 logger
.warn("Creation of homotypic relations is still problematic");
56 //result &= checkArticlesWithoutJournal(bmiConfig);
57 //result &= checkPartOfJournal(bmiConfig);
63 public boolean doInvoke(TcsRdfImportState state
){
65 MapWrapper
<TaxonBase
> taxonMap
= (MapWrapper
<TaxonBase
>)state
.getStore(ICdmIO
.TAXON_STORE
);
66 MapWrapper
<ReferenceBase
> referenceMap
= (MapWrapper
<ReferenceBase
>)state
.getStore(ICdmIO
.REFERENCE_STORE
);
67 logger
.info("start makeTaxonRelationships ...");
68 boolean success
=true;
70 String xmlElementName
;
71 Namespace elementNamespace
;
73 Set
<TaxonBase
> taxonStore
= new HashSet
<TaxonBase
>();
75 TcsRdfImportConfigurator config
= state
.getConfig();
76 Element root
= config
.getSourceRoot();
77 Namespace taxonConceptNamespace
= config
.getTcNamespace();
79 xmlElementName
= "TaxonConcept";
80 elementNamespace
= taxonConceptNamespace
;
81 List
<Element
> elTaxonConcepts
= root
.getChildren(xmlElementName
, elementNamespace
);
84 //for each taxonConcept
85 for (Element elTaxonConcept
: elTaxonConcepts
){
87 if ((i
++ % modCount
) == 0){ logger
.info("Taxa handled: " + (i
-1));}
90 xmlElementName
= "about";
91 elementNamespace
= config
.getRdfNamespace();
92 String strTaxonAbout
= elTaxonConcept
.getAttributeValue(xmlElementName
, elementNamespace
);
94 TaxonBase aboutTaxon
= taxonMap
.get(strTaxonAbout
);
96 if (aboutTaxon
instanceof Taxon
){
97 success
&= makeHomotypicSynonymRelations((Taxon
)aboutTaxon
);
100 xmlElementName
= "hasRelationship";
101 elementNamespace
= taxonConceptNamespace
;
102 List
<Element
> elHasRelationships
= elTaxonConcept
.getChildren(xmlElementName
, elementNamespace
);
105 for (Element elHasRelationship
: elHasRelationships
){
106 xmlElementName
= "relationship";
107 elementNamespace
= taxonConceptNamespace
;
108 List
<Element
> elRelationships
= elHasRelationship
.getChildren(xmlElementName
, elementNamespace
);
110 for (Element elRelationship
: elRelationships
){
111 success
&= makeRelationship(elRelationship
, strTaxonAbout
, taxonMap
, state
, taxonStore
);
114 } catch (Exception e
) {
116 logger
.error("Error. Taxon relationship could not be imported");
120 logger
.info("Taxa to save: " + taxonStore
.size());
121 getTaxonService().save(taxonStore
);
123 logger
.info("end makeRelTaxa ...");
128 private boolean makeRelationship(
129 Element elRelationship
,
130 String strTaxonAbout
,
131 MapWrapper
<TaxonBase
> taxonMap
,
132 TcsRdfImportState state
,
133 Set
<TaxonBase
> taxonStore
){
134 boolean success
= true;
135 String xmlElementName
;
136 String xmlAttributeName
;
137 Namespace elementNamespace
;
138 Namespace attributeNamespace
;
139 TcsRdfImportConfigurator tcsConfig
= state
.getConfig();
141 xmlElementName
= "relationshipCategory";
142 elementNamespace
= tcsConfig
.getTcNamespace();
143 xmlAttributeName
= "resource";
144 attributeNamespace
= tcsConfig
.getRdfNamespace();
145 String strRelCategory
= XmlHelp
.getChildAttributeValue(elRelationship
, xmlElementName
, elementNamespace
, xmlAttributeName
, attributeNamespace
);
147 RelationshipTermBase relType
= TcsRdfTransformer
.tcsRelationshipCategory2Relationship(strRelCategory
);
148 boolean isReverse
= TcsRdfTransformer
.isReverseRelationshipCategory(strRelCategory
);
150 xmlElementName
= "toTaxon";
151 elementNamespace
= tcsConfig
.getTcNamespace();
152 xmlAttributeName
= "resource";
153 attributeNamespace
= tcsConfig
.getRdfNamespace();
154 String strToTaxon
= XmlHelp
.getChildAttributeValue(elRelationship
, xmlElementName
, elementNamespace
, xmlAttributeName
, attributeNamespace
);
155 TaxonBase toTaxon
= taxonMap
.get(strToTaxon
);
156 TaxonBase fromTaxon
= taxonMap
.get(strTaxonAbout
);
157 if (toTaxon
!= null && fromTaxon
!= null){
159 if (isReverse
== true ){
160 TaxonBase tmp
= toTaxon
;
165 //Create relationship
166 if (! (toTaxon
instanceof Taxon
)){
167 logger
.warn("TaxonBase toTaxon is not of Type 'Taxon'. Relationship is not added.");
170 Taxon taxonTo
= (Taxon
)toTaxon
;
171 ReferenceBase citation
= null;
172 String microReference
= null;
173 if (relType
instanceof SynonymRelationshipType
){
174 success
&= makeSynRelType((SynonymRelationshipType
)relType
, taxonTo
, fromTaxon
, citation
, microReference
);
175 }else if (relType
instanceof TaxonRelationshipType
){
176 success
&= makeTaxonRelType((TaxonRelationshipType
)relType
, state
, taxonTo
, fromTaxon
, strTaxonAbout
, citation
, microReference
);
178 logger
.warn("Unknown Relationshiptype");
181 taxonStore
.add(toTaxon
);
184 if (toTaxon
== null){
185 logger
.warn("toTaxon (" + strToTaxon
+ ") could not be found in taxonMap. Relationship of type " + strRelCategory
+ " was not added to CDM");
187 if (fromTaxon
== null){
188 logger
.warn("fromTaxon (" + strTaxonAbout
+ ") could not be found in taxonMap. Relationship was not added to CDM");
193 } catch (UnknownCdmTypeException e
) {
195 logger
.warn("tc:relationshipCategory " + strRelCategory
+ " not yet implemented");
202 private boolean makeSynRelType(SynonymRelationshipType synRelType
, Taxon taxonTo
, TaxonBase fromTaxon
, ReferenceBase citation
, String microReference
){
203 boolean success
= true;
204 if (! (fromTaxon
instanceof Synonym
)){
205 logger
.warn("TaxonBase fromTaxon is not of Type 'Synonym'. Relationship is not added.");
208 Synonym synonym
= (Synonym
)fromTaxon
;
209 TaxonNameBase synName
= synonym
.getName();
210 TaxonNameBase accName
= taxonTo
.getName();
211 if (synName
!= null && accName
!= null && synName
.isHomotypic(accName
)
212 && ( synRelType
.equals(SynonymRelationshipType
.SYNONYM_OF()))){
213 synRelType
= SynonymRelationshipType
.HOMOTYPIC_SYNONYM_OF();
215 if (! relationExists(taxonTo
, synonym
, synRelType
)){
216 taxonTo
.addSynonym(synonym
, synRelType
, citation
, microReference
);
218 //TODO citation, microReference
219 //TODO different synRelTypes -> warning
226 private boolean makeTaxonRelType(TaxonRelationshipType relType
, TcsRdfImportState state
, Taxon taxonTo
, TaxonBase fromTaxon
, String strTaxonAbout
, ReferenceBase citation
, String microReference
){
227 boolean success
= true;
228 if (! (fromTaxon
instanceof Taxon
)){
229 logger
.warn("TaxonBase fromTaxon " + strTaxonAbout
+ " is not of Type 'Taxon'. Relationship is not added.");
232 Taxon taxonFrom
= (Taxon
)fromTaxon
;
233 if (state
.getConfig().isUseTaxonomicTree() && relType
.equals(TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN())){
234 success
&= makeTaxonomicallyIncluded(state
, taxonTo
, taxonFrom
, citation
, microReference
);
236 taxonFrom
.addTaxonRelation(taxonTo
, relType
, citation
, microReference
);
242 private boolean makeTaxonomicallyIncluded(TcsRdfImportState state
, Taxon toTaxon
, Taxon fromTaxon
, ReferenceBase citation
, String microCitation
){
243 boolean success
= true;
244 ReferenceBase sec
= toTaxon
.getSec();
245 TaxonomicTree tree
= state
.getTree(sec
);
247 tree
= makeTree(state
, sec
);
249 success
= tree
.addParentChild(toTaxon
, fromTaxon
, citation
, microCitation
);
253 private boolean relationExists(Taxon taxonTo
, Synonym synonym
, SynonymRelationshipType synRelType
){
254 if (synonym
== null){
257 if (synonym
.getRelationType(taxonTo
).size() > 0){
258 Set
<SynonymRelationshipType
> relTypeList
= synonym
.getRelationType(taxonTo
);
259 if (relTypeList
.contains(synRelType
)){
262 logger
.warn("Taxon-Synonym pair has 2 different SynonymRelationships. This is against the rules");
270 private boolean makeHomotypicSynonymRelations(Taxon aboutTaxon
){
271 if (true)return false;
272 TaxonNameBase aboutName
= aboutTaxon
.getName();
273 if (aboutName
!= null){
274 Set
<TaxonNameBase
> typifiedNames
= aboutName
.getHomotypicalGroup().getTypifiedNames();
275 for (TaxonNameBase typifiedName
: typifiedNames
){
276 //TODO check if name is part of this tcs file
277 if (typifiedName
.equals(aboutName
)){
280 Set
<Synonym
> syns
= typifiedName
.getSynonyms();
281 for(Synonym syn
:syns
){
282 aboutTaxon
.addSynonym(syn
, SynonymRelationshipType
.HOMOTYPIC_SYNONYM_OF());
292 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
294 protected boolean isIgnore(TcsRdfImportState state
){
295 return ! state
.getConfig().isDoRelTaxa();