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
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.jdom
.Attribute
;
18 import org
.jdom
.Element
;
19 import org
.jdom
.Namespace
;
20 import org
.springframework
.stereotype
.Component
;
22 import eu
.etaxonomy
.cdm
.common
.XmlHelp
;
23 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
24 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
25 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
26 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
27 import eu
.etaxonomy
.cdm
.model
.agent
.INomenclaturalAuthor
;
28 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
29 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
32 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
33 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
34 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
35 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
36 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
37 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
38 import eu
.etaxonomy
.cdm
.model
.reference
.Generic
;
39 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
40 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
48 public class TcsRdfTaxonNameImport
extends TcsRdfImportBase
implements ICdmIO
<IImportConfigurator
> {
49 private static final Logger logger
= Logger
.getLogger(TcsRdfTaxonNameImport
.class);
51 private static int modCount
= 5000;
53 public TcsRdfTaxonNameImport(){
58 public boolean doCheck(IImportConfigurator config
){
59 boolean result
= true;
60 logger
.warn("BasionymRelations not yet implemented");
61 logger
.warn("Checking for TaxonNames not yet implemented");
62 //result &= checkArticlesWithoutJournal(tcsConfig);
63 //result &= checkPartOfJournal(tcsConfig);
68 protected static CdmIoXmlMapperBase
[] standardMappers
= new CdmIoXmlMapperBase
[]{
69 new CdmTextElementMapper("genusPart", "genusOrUninomial")
70 , new CdmTextElementMapper("uninomial", "genusOrUninomial") //TODO make it a more specific Mapper for both attributes
71 , new CdmTextElementMapper("specificEpithet", "specificEpithet")
72 , new CdmTextElementMapper("infraspecificEpithet", "infraSpecificEpithet")
73 , new CdmTextElementMapper("infragenericEpithet", "infraGenericEpithet")
74 , new CdmTextElementMapper("microReference", nsTcom
, "nomenclaturalMicroReference")
78 protected static CdmIoXmlMapperBase
[] operationalMappers
= new CdmIoXmlMapperBase
[]{
79 new CdmUnclearMapper("basionymAuthorship")
80 , new CdmUnclearMapper("combinationAuthorship")
81 , new CdmUnclearMapper("hasAnnotation")
82 , new CdmUnclearMapper("rank")
83 , new CdmUnclearMapper("nomenclaturalCode")
84 , new CdmUnclearMapper("publishedIn", nsTcom
)
85 , new CdmUnclearMapper("year")
88 protected static CdmIoXmlMapperBase
[] unclearMappers
= new CdmIoXmlMapperBase
[]{
89 new CdmUnclearMapper("authorship")
90 , new CdmUnclearMapper("rankString")
91 , new CdmUnclearMapper("nameComplete")
92 , new CdmUnclearMapper("hasBasionym")
93 , new CdmUnclearMapper("dateOfEntry", nsTpalm
)
97 public boolean doInvoke(IImportConfigurator config
, Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
){
99 MapWrapper
<TaxonNameBase
> taxonNameMap
= (MapWrapper
<TaxonNameBase
>)stores
.get(ICdmIO
.TAXONNAME_STORE
);
100 MapWrapper
<ReferenceBase
> referenceMap
= (MapWrapper
<ReferenceBase
>)stores
.get(ICdmIO
.REFERENCE_STORE
);
101 MapWrapper
<TeamOrPersonBase
> authorMap
= (MapWrapper
<TeamOrPersonBase
>)stores
.get(ICdmIO
.AUTHOR_STORE
);
103 String tcsElementName
;
104 Namespace tcsNamespace
;
107 logger
.info("start makeTaxonNames ...");
108 TcsRdfImportConfigurator tcsConfig
= (TcsRdfImportConfigurator
)config
;
109 Element root
= tcsConfig
.getSourceRoot();
110 boolean success
=true;
112 Namespace rdfNamespace
= tcsConfig
.getRdfNamespace();
113 Namespace taxonNameNamespace
= tcsConfig
.getTnNamespace();
115 String idNamespace
= "TaxonName";
117 List
<Element
> elTaxonNames
= root
.getChildren("TaxonName", taxonNameNamespace
);
121 for (Element elTaxonName
: elTaxonNames
){
123 if ((++i
% modCount
) == 0){ logger
.info("Names handled: " + (i
-1));}
125 Attribute about
= elTaxonName
.getAttribute("about", rdfNamespace
);
127 //create TaxonName element
128 String nameAbout
= elTaxonName
.getAttributeValue("about", rdfNamespace
);
129 String strRank
= XmlHelp
.getChildAttributeValue(elTaxonName
, "rank", taxonNameNamespace
, "resource", rdfNamespace
);
130 String strNomenclaturalCode
= XmlHelp
.getChildAttributeValue(elTaxonName
, "nomenclaturalCode", taxonNameNamespace
, "resource", rdfNamespace
);
133 Rank rank
= TcsRdfTransformer
.rankString2Rank(strRank
);
134 NomenclaturalCode nomCode
= TcsRdfTransformer
.nomCodeString2NomCode(strNomenclaturalCode
);
135 TaxonNameBase nameBase
= nomCode
.getNewTaxonNameInstance(rank
);
137 Set
<String
> omitAttributes
= null;
138 makeStandardMapper(elTaxonName
, nameBase
, omitAttributes
, standardMappers
);
142 tcsElementName
= "publishedIn";
143 tcsNamespace
= tcsConfig
.getCommonNamespace();
144 value
= (String
)ImportHelper
.getXmlInputValue(elTaxonName
, tcsElementName
, tcsNamespace
);
146 Generic nomRef
= Generic
.NewInstance(); //TODO
147 nomRef
.setTitleCache(value
);
148 nameBase
.setNomenclaturalReference(nomRef
);
151 tcsElementName
= "year";
152 tcsNamespace
= taxonNameNamespace
;
154 value
= (String
)ImportHelper
.getXmlInputValue(elTaxonName
, tcsElementName
, tcsNamespace
);
157 year
= Integer
.valueOf(value
);
158 TimePeriod timeP
= TimePeriod
.NewInstance(year
);
159 nomRef
.setDatePublished(timeP
);
160 } catch (RuntimeException e
) {
161 logger
.warn("year could not be parsed");
167 tcsNamespace
= taxonNameNamespace
;
168 Element elAnnotation
= elTaxonName
.getChild("hasAnnotation", tcsNamespace
);
169 if (elAnnotation
!= null){
170 Element elNomenclaturalNote
= elAnnotation
.getChild("NomenclaturalNote", tcsNamespace
);
171 if (elNomenclaturalNote
!= null){
172 String statusValue
= (String
)ImportHelper
.getXmlInputValue(elNomenclaturalNote
, "note", tcsNamespace
);
173 String type
= XmlHelp
.getChildAttributeValue(elNomenclaturalNote
, "type", tcsNamespace
, "resource", rdfNamespace
);
174 String tdwgType
= "http://rs.tdwg.org/ontology/voc/TaxonName#PublicationStatus";
175 if (tdwgType
.equalsIgnoreCase(type
)){
177 NomenclaturalStatusType statusType
= TcsRdfTransformer
.nomStatusString2NomStatus(statusValue
);
178 //NomenclaturalStatusType statusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(statusValue);
179 if (statusType
!= null){
180 nameBase
.addStatus(NomenclaturalStatus
.NewInstance(statusType
));
182 } catch (UnknownCdmTypeException e
) {
183 if (! statusValue
.equals("valid")){
184 logger
.warn("Unknown NomenclaturalStatusType: " + statusValue
);
191 if (nameBase
instanceof NonViralName
){
192 NonViralName
<?
> nonViralName
= (NonViralName
<?
>)nameBase
;
196 tcsElementName
= "basionymAuthorship";
197 String basionymAuthorValue
= (String
)ImportHelper
.getXmlInputValue(elTaxonName
, tcsElementName
, taxonNameNamespace
);
198 if (basionymAuthorValue
!= null){
199 INomenclaturalAuthor basionymAuthor
= Team
.NewInstance();
200 basionymAuthor
.setNomenclaturalTitle(basionymAuthorValue
);
201 nonViralName
.setBasionymAuthorTeam(basionymAuthor
);
205 tcsElementName
= "combinationAuthorship";
206 String combinationAuthorValue
= (String
)ImportHelper
.getXmlInputValue(elTaxonName
, tcsElementName
, taxonNameNamespace
);
207 if (combinationAuthorValue
!= null){
208 INomenclaturalAuthor combinationAuthor
= Team
.NewInstance();
209 combinationAuthor
.setNomenclaturalTitle(combinationAuthorValue
);
210 nonViralName
.setCombinationAuthorTeam(combinationAuthor
);
213 //set the authorshipCache
214 tcsElementName
= "authorship";
215 String authorValue
= (String
)ImportHelper
.getXmlInputValue(elTaxonName
, tcsElementName
, taxonNameNamespace
);
216 String cache
= nonViralName
.getAuthorshipCache();
217 if ( authorValue
!= null){
218 //compare existing authorship cache with new one and check if it is necessary to
219 //make cache protected //TODO refinement
221 nonViralName
.setAuthorshipCache(authorValue
);
223 cache
= basionymAuthorValue
== null ? cache
: cache
.replace(basionymAuthorValue
, "");
224 cache
= combinationAuthorValue
== null ? cache
: cache
.replace(combinationAuthorValue
, "");
225 cache
= cache
.replace("\\(|\\)", "");
226 cache
= cache
.trim();
227 if (! cache
.equals("")){
228 nonViralName
.setAuthorshipCache(authorValue
);
233 ImportHelper
.setOriginalSource(nameBase
, config
.getSourceReference(), nameAbout
, idNamespace
);
235 checkAdditionalContents(elTaxonName
, standardMappers
, operationalMappers
, unclearMappers
);
239 //ImportHelper.setOriginalSource(nameBase, tcsConfig.getSourceReference(), nameId);
240 taxonNameMap
.put(nameAbout
, nameBase
);
242 } catch (UnknownCdmTypeException e
) {
244 logger
.warn("Name with id " + nameAbout
+ " has unknown rank " + strRank
+ " and could not be saved.");
248 logger
.info(i
+ " names handled");
249 getNameService().saveTaxonNameAll(taxonNameMap
.objects());
250 // makeNameSpecificData(nameMap);
251 logger
.info("end makeTaxonNames ...");
256 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
258 protected boolean isIgnore(IImportConfigurator config
){
259 return ! config
.isDoTaxonNames();