4 package eu
.etaxonomy
.cdm
.io
.tcs
;
6 import java
.util
.HashSet
;
11 import org
.apache
.log4j
.Logger
;
12 import org
.jdom
.Element
;
13 import org
.jdom
.Namespace
;
15 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
16 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
17 import eu
.etaxonomy
.cdm
.common
.XmlHelp
;
18 import eu
.etaxonomy
.cdm
.io
.common
.CdmIoBase
;
19 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
20 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
21 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
22 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
23 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipTermBase
;
24 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
25 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
31 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
38 public class TcsTaxonRelationsIO
extends CdmIoBase
implements ICdmIO
{
39 private static final Logger logger
= Logger
.getLogger(TcsTaxonRelationsIO
.class);
41 private static int modCount
= 30000;
43 public TcsTaxonRelationsIO(){
48 public boolean doCheck(IImportConfigurator config
){
49 boolean result
= true;
50 logger
.warn("Checking for TaxonRelations not yet implemented");
51 logger
.warn("Creation of homotypic relations is still problematic");
52 //result &= checkArticlesWithoutJournal(bmiConfig);
53 //result &= checkPartOfJournal(bmiConfig);
59 public boolean doInvoke(IImportConfigurator config
, CdmApplicationController cdmApp
, Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
){
61 MapWrapper
<TaxonBase
> taxonMap
= (MapWrapper
<TaxonBase
>)stores
.get(ICdmIO
.TAXON_STORE
);
62 MapWrapper
<ReferenceBase
> referenceMap
= (MapWrapper
<ReferenceBase
>)stores
.get(ICdmIO
.REFERENCE_STORE
);
63 logger
.info("start makeTaxonRelationships ...");
64 boolean success
=true;
66 String xmlElementName
;
67 Namespace elementNamespace
;
69 Set
<TaxonBase
> taxonStore
= new HashSet
<TaxonBase
>();
71 TcsImportConfigurator tcsConfig
= (TcsImportConfigurator
)config
;
72 Element root
= tcsConfig
.getSourceRoot();
73 Namespace taxonConceptNamespace
= tcsConfig
.getTcNamespace();
75 ITaxonService taxonService
= cdmApp
.getTaxonService();
77 xmlElementName
= "TaxonConcept";
78 elementNamespace
= taxonConceptNamespace
;
79 List
<Element
> elTaxonConcepts
= root
.getChildren(xmlElementName
, elementNamespace
);
82 //for each taxonConcept
83 for (Element elTaxonConcept
: elTaxonConcepts
){
84 if ((i
++ % modCount
) == 0){ logger
.info("Taxa handled: " + (i
-1));}
87 xmlElementName
= "about";
88 elementNamespace
= tcsConfig
.getRdfNamespace();
89 String strTaxonAbout
= elTaxonConcept
.getAttributeValue(xmlElementName
, elementNamespace
);
91 TaxonBase aboutTaxon
= taxonMap
.get(strTaxonAbout
);
93 if (aboutTaxon
instanceof Taxon
){
94 makeHomotypicSynonymRelations((Taxon
)aboutTaxon
);
97 xmlElementName
= "hasRelationship";
98 elementNamespace
= taxonConceptNamespace
;
99 List
<Element
> elHasRelationships
= elTaxonConcept
.getChildren(xmlElementName
, elementNamespace
);
102 for (Element elHasRelationship
: elHasRelationships
){
103 xmlElementName
= "relationship";
104 elementNamespace
= taxonConceptNamespace
;
105 List
<Element
> elRelationships
= elHasRelationship
.getChildren(xmlElementName
, elementNamespace
);
107 for (Element elRelationship
: elRelationships
){
108 makeRelationship(elRelationship
, strTaxonAbout
, taxonMap
, tcsConfig
, taxonStore
);
112 logger
.info("Taxa to save: " + taxonStore
.size());
113 taxonService
.saveTaxonAll(taxonStore
);
115 logger
.info("end makeRelTaxa ...");
120 private boolean makeRelationship(
121 Element elRelationship
,
122 String strTaxonAbout
,
123 MapWrapper
<TaxonBase
> taxonMap
,
124 TcsImportConfigurator tcsConfig
,
125 Set
<TaxonBase
> taxonStore
){
126 boolean result
= true;
127 String xmlElementName
;
128 String xmlAttributeName
;
129 Namespace elementNamespace
;
130 Namespace attributeNamespace
;
132 xmlElementName
= "relationshipCategory";
133 elementNamespace
= tcsConfig
.getTcNamespace();
134 xmlAttributeName
= "resource";
135 attributeNamespace
= tcsConfig
.getRdfNamespace();
136 String strRelCategory
= XmlHelp
.getChildAttributeValue(elRelationship
, xmlElementName
, elementNamespace
, xmlAttributeName
, attributeNamespace
);
138 RelationshipTermBase relType
= TcsTransformer
.tcsRelationshipCategory2Relationship(strRelCategory
);
139 boolean isReverse
= TcsTransformer
.isReverseRelationshipCategory(strRelCategory
);
141 xmlElementName
= "toTaxon";
142 elementNamespace
= tcsConfig
.getTcNamespace();
143 xmlAttributeName
= "resource";
144 attributeNamespace
= tcsConfig
.getRdfNamespace();
145 String strToTaxon
= XmlHelp
.getChildAttributeValue(elRelationship
, xmlElementName
, elementNamespace
, xmlAttributeName
, attributeNamespace
);
146 TaxonBase toTaxon
= taxonMap
.get(strToTaxon
);
147 TaxonBase fromTaxon
= taxonMap
.get(strTaxonAbout
);
148 if (toTaxon
!= null && fromTaxon
!= null){
150 if (isReverse
== true ){
151 TaxonBase tmp
= toTaxon
;
156 //Create relationship
157 if (! (toTaxon
instanceof Taxon
)){
158 logger
.warn("TaxonBase toTaxon is not of Type 'Taxon'. Relationship is not added.");
161 Taxon taxonTo
= (Taxon
)toTaxon
;
162 ReferenceBase citation
= null;
163 String microReference
= null;
164 if (relType
instanceof SynonymRelationshipType
){
165 SynonymRelationshipType synRelType
= (SynonymRelationshipType
)relType
;
166 if (! (fromTaxon
instanceof Synonym
)){
167 logger
.warn("TaxonBase fromTaxon is not of Type 'Synonym'. Relationship is not added.");
170 Synonym synonym
= (Synonym
)fromTaxon
;
171 TaxonNameBase synName
= synonym
.getName();
172 TaxonNameBase accName
= taxonTo
.getName();
173 if (synName
!= null && accName
!= null && synName
.isHomotypic(accName
)
174 && ( synRelType
.equals(SynonymRelationshipType
.SYNONYM_OF()))){
175 synRelType
= SynonymRelationshipType
.HOMOTYPIC_SYNONYM_OF();
177 if (! relationExists(taxonTo
, synonym
, synRelType
)){
178 taxonTo
.addSynonym(synonym
, synRelType
, citation
, microReference
);
180 //TODO citation, microReference
181 //TODO different synRelTypes -> warning
185 }else if (relType
instanceof TaxonRelationshipType
){
186 TaxonRelationshipType taxRelType
= (TaxonRelationshipType
)relType
;
187 if (! (fromTaxon
instanceof Taxon
)){
188 logger
.warn("TaxonBase fromTaxon " + strTaxonAbout
+ "is not of Type 'Taxon'. Relationship is not added.");
191 Taxon taxonFrom
= (Taxon
)fromTaxon
;
192 taxonFrom
.addTaxonRelation(taxonTo
, taxRelType
, citation
, microReference
);
195 logger
.warn("Unknown Relationshiptype");
198 taxonStore
.add(toTaxon
);
201 if (toTaxon
== null){
202 logger
.warn("toTaxon (" + strToTaxon
+ ") could not be found in taxonMap. Relationship of type " + strRelCategory
+ " was not added to CDM");
204 if (fromTaxon
== null){
205 logger
.warn("fromTaxon (" + strTaxonAbout
+ ") could not be found in taxonMap. Relationship was not added to CDM");
210 } catch (UnknownCdmTypeException e
) {
212 logger
.warn("tc:relationshipCategory " + strRelCategory
+ " not yet implemented");
220 private boolean relationExists(Taxon taxonTo
, Synonym synonym
, SynonymRelationshipType synRelType
){
221 if (synonym
== null){
224 if (synonym
.getRelationType(taxonTo
).size() > 0){
225 Set
<SynonymRelationshipType
> relTypeList
= synonym
.getRelationType(taxonTo
);
226 if (relTypeList
.contains(synRelType
)){
229 logger
.warn("Taxon-Synonym pair has 2 different SynonymRelationships. This is against the rules");
237 private boolean makeHomotypicSynonymRelations(Taxon aboutTaxon
){
238 if (true)return false;
239 TaxonNameBase aboutName
= aboutTaxon
.getName();
240 if (aboutName
!= null){
241 Set
<TaxonNameBase
> typifiedNames
= aboutName
.getHomotypicalGroup().getTypifiedNames();
242 for (TaxonNameBase typifiedName
: typifiedNames
){
243 //TODO check if name is part of this tcs file
244 if (typifiedName
.equals(aboutName
)){
247 Set
<Synonym
> syns
= typifiedName
.getSynonyms();
248 for(Synonym syn
:syns
){
249 aboutTaxon
.addSynonym(syn
, SynonymRelationshipType
.HOMOTYPIC_SYNONYM_OF());
259 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
261 protected boolean isIgnore(IImportConfigurator config
){
262 return ! config
.isDoRelTaxa();