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
.List
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.jdom
.Attribute
;
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
.ImportHelper
;
24 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
25 import eu
.etaxonomy
.cdm
.model
.agent
.INomenclaturalAuthor
;
26 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
27 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
29 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
30 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
31 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
32 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
33 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
34 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
35 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
36 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
37 import eu
.etaxonomy
.cdm
.model
.reference
.Generic
;
38 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
39 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
47 public class TcsRdfTaxonNameImport
extends TcsRdfImportBase
implements ICdmIO
<TcsRdfImportState
> {
48 private static final Logger logger
= Logger
.getLogger(TcsRdfTaxonNameImport
.class);
50 private static int modCount
= 5000;
52 public TcsRdfTaxonNameImport(){
57 public boolean doCheck(TcsRdfImportState config
){
58 boolean result
= true;
59 logger
.warn("BasionymRelations not yet implemented");
60 logger
.warn("Checking for TaxonNames not yet implemented");
61 //result &= checkArticlesWithoutJournal(tcsConfig);
62 //result &= checkPartOfJournal(tcsConfig);
67 protected static CdmSingleAttributeXmlMapperBase
[] standardMappers
= new CdmSingleAttributeXmlMapperBase
[]{
68 new CdmTextElementMapper("genusPart", "genusOrUninomial")
69 , new CdmTextElementMapper("uninomial", "genusOrUninomial") //TODO make it a more specific Mapper for both attributes
70 , new CdmTextElementMapper("specificEpithet", "specificEpithet")
71 , new CdmTextElementMapper("infraspecificEpithet", "infraSpecificEpithet")
72 , new CdmTextElementMapper("infragenericEpithet", "infraGenericEpithet")
73 , new CdmTextElementMapper("microReference", nsTcom
, "nomenclaturalMicroReference")
77 protected static CdmSingleAttributeXmlMapperBase
[] operationalMappers
= new CdmSingleAttributeXmlMapperBase
[]{
78 new CdmUnclearMapper("basionymAuthorship")
79 , new CdmUnclearMapper("combinationAuthorship")
80 , new CdmUnclearMapper("hasAnnotation")
81 , new CdmUnclearMapper("rank")
82 , new CdmUnclearMapper("nomenclaturalCode")
83 , new CdmUnclearMapper("publishedIn", nsTcom
)
84 , new CdmUnclearMapper("year")
87 protected static CdmSingleAttributeXmlMapperBase
[] unclearMappers
= new CdmSingleAttributeXmlMapperBase
[]{
88 new CdmUnclearMapper("authorship")
89 , new CdmUnclearMapper("rankString")
90 , new CdmUnclearMapper("nameComplete")
91 , new CdmUnclearMapper("hasBasionym")
92 , new CdmUnclearMapper("dateOfEntry", nsTpalm
)
96 protected boolean doInvoke(TcsRdfImportState state
){
98 MapWrapper
<TaxonNameBase
> taxonNameMap
= (MapWrapper
<TaxonNameBase
>)state
.getStore(ICdmIO
.TAXONNAME_STORE
);
99 MapWrapper
<ReferenceBase
> referenceMap
= (MapWrapper
<ReferenceBase
>)state
.getStore(ICdmIO
.REFERENCE_STORE
);
100 MapWrapper
<TeamOrPersonBase
> authorMap
= (MapWrapper
<TeamOrPersonBase
>)state
.getStore(ICdmIO
.TEAM_STORE
);
102 String tcsElementName
;
103 Namespace tcsNamespace
;
106 logger
.info("start makeTaxonNames ...");
107 TcsRdfImportConfigurator config
= state
.getConfig();
108 Element root
= config
.getSourceRoot();
109 boolean success
=true;
111 Namespace rdfNamespace
= config
.getRdfNamespace();
112 Namespace taxonNameNamespace
= config
.getTnNamespace();
114 String idNamespace
= "TaxonName";
116 List
<Element
> elTaxonNames
= root
.getChildren("TaxonName", taxonNameNamespace
);
120 for (Element elTaxonName
: elTaxonNames
){
122 if ((++i
% modCount
) == 0){ logger
.info("Names handled: " + (i
-1));}
124 Attribute about
= elTaxonName
.getAttribute("about", rdfNamespace
);
126 //create TaxonName element
127 String nameAbout
= elTaxonName
.getAttributeValue("about", rdfNamespace
);
128 String strRank
= XmlHelp
.getChildAttributeValue(elTaxonName
, "rank", taxonNameNamespace
, "resource", rdfNamespace
);
129 String strNomenclaturalCode
= XmlHelp
.getChildAttributeValue(elTaxonName
, "nomenclaturalCode", taxonNameNamespace
, "resource", rdfNamespace
);
132 Rank rank
= TcsRdfTransformer
.rankString2Rank(strRank
);
133 NomenclaturalCode nomCode
= TcsRdfTransformer
.nomCodeString2NomCode(strNomenclaturalCode
);
134 TaxonNameBase nameBase
= nomCode
.getNewTaxonNameInstance(rank
);
136 Set
<String
> omitAttributes
= null;
137 makeStandardMapper(elTaxonName
, nameBase
, omitAttributes
, standardMappers
);
141 tcsElementName
= "publishedIn";
142 tcsNamespace
= config
.getCommonNamespace();
143 value
= (String
)ImportHelper
.getXmlInputValue(elTaxonName
, tcsElementName
, tcsNamespace
);
145 Generic nomRef
= Generic
.NewInstance(); //TODO
146 nomRef
.setTitleCache(value
);
147 nameBase
.setNomenclaturalReference(nomRef
);
150 tcsElementName
= "year";
151 tcsNamespace
= taxonNameNamespace
;
153 value
= (String
)ImportHelper
.getXmlInputValue(elTaxonName
, tcsElementName
, tcsNamespace
);
156 year
= Integer
.valueOf(value
);
157 TimePeriod timeP
= TimePeriod
.NewInstance(year
);
158 nomRef
.setDatePublished(timeP
);
159 } catch (RuntimeException e
) {
160 logger
.warn("year could not be parsed");
163 if (state
.getConfig().isPublishReferences()){
164 nomRef
.addMarker(Marker
.NewInstance(MarkerType
.PUBLISH(), false));
169 tcsNamespace
= taxonNameNamespace
;
170 Element elAnnotation
= elTaxonName
.getChild("hasAnnotation", tcsNamespace
);
171 if (elAnnotation
!= null){
172 Element elNomenclaturalNote
= elAnnotation
.getChild("NomenclaturalNote", tcsNamespace
);
173 if (elNomenclaturalNote
!= null){
174 String statusValue
= (String
)ImportHelper
.getXmlInputValue(elNomenclaturalNote
, "note", tcsNamespace
);
175 String type
= XmlHelp
.getChildAttributeValue(elNomenclaturalNote
, "type", tcsNamespace
, "resource", rdfNamespace
);
176 String tdwgType
= "http://rs.tdwg.org/ontology/voc/TaxonName#PublicationStatus";
177 if (tdwgType
.equalsIgnoreCase(type
)){
179 NomenclaturalStatusType statusType
= TcsRdfTransformer
.nomStatusString2NomStatus(statusValue
);
180 //NomenclaturalStatusType statusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(statusValue);
181 if (statusType
!= null){
182 nameBase
.addStatus(NomenclaturalStatus
.NewInstance(statusType
));
184 } catch (UnknownCdmTypeException e
) {
185 if (! statusValue
.equals("valid")){
186 logger
.warn("Unknown NomenclaturalStatusType: " + statusValue
);
193 if (nameBase
instanceof NonViralName
){
194 NonViralName
<?
> nonViralName
= (NonViralName
<?
>)nameBase
;
198 tcsElementName
= "basionymAuthorship";
199 String basionymAuthorValue
= (String
)ImportHelper
.getXmlInputValue(elTaxonName
, tcsElementName
, taxonNameNamespace
);
200 if (basionymAuthorValue
!= null){
201 INomenclaturalAuthor basionymAuthor
= Team
.NewInstance();
202 basionymAuthor
.setNomenclaturalTitle(basionymAuthorValue
);
203 nonViralName
.setBasionymAuthorTeam(basionymAuthor
);
207 tcsElementName
= "combinationAuthorship";
208 String combinationAuthorValue
= (String
)ImportHelper
.getXmlInputValue(elTaxonName
, tcsElementName
, taxonNameNamespace
);
209 if (combinationAuthorValue
!= null){
210 INomenclaturalAuthor combinationAuthor
= Team
.NewInstance();
211 combinationAuthor
.setNomenclaturalTitle(combinationAuthorValue
);
212 nonViralName
.setCombinationAuthorTeam(combinationAuthor
);
215 //set the authorshipCache
216 tcsElementName
= "authorship";
217 String authorValue
= (String
)ImportHelper
.getXmlInputValue(elTaxonName
, tcsElementName
, taxonNameNamespace
);
218 String cache
= nonViralName
.getAuthorshipCache();
219 if ( authorValue
!= null){
220 //compare existing authorship cache with new one and check if it is necessary to
221 //make cache protected //TODO refinement
223 nonViralName
.setAuthorshipCache(authorValue
);
225 cache
= basionymAuthorValue
== null ? cache
: cache
.replace(basionymAuthorValue
, "");
226 cache
= combinationAuthorValue
== null ? cache
: cache
.replace(combinationAuthorValue
, "");
227 cache
= cache
.replace("\\(|\\)", "");
228 cache
= cache
.trim();
229 if (! cache
.equals("")){
230 nonViralName
.setAuthorshipCache(authorValue
);
235 ImportHelper
.setOriginalSource(nameBase
, config
.getSourceReference(), nameAbout
, idNamespace
);
237 checkAdditionalContents(elTaxonName
, standardMappers
, operationalMappers
, unclearMappers
);
241 //ImportHelper.setOriginalSource(nameBase, tcsConfig.getSourceReference(), nameId);
242 taxonNameMap
.put(nameAbout
, nameBase
);
244 } catch (UnknownCdmTypeException e
) {
246 logger
.warn("Name with id " + nameAbout
+ " has unknown rank " + strRank
+ " and could not be saved.");
250 logger
.info(i
+ " names handled");
251 getNameService().save(taxonNameMap
.objects());
252 // makeNameSpecificData(nameMap);
253 logger
.info("end makeTaxonNames ...");
258 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
260 protected boolean isIgnore(TcsRdfImportState state
){
261 return ! state
.getConfig().isDoTaxonNames();