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
.globis
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
19 import org
.apache
.commons
.lang
.StringUtils
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
23 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
24 import eu
.etaxonomy
.cdm
.io
.common
.IOValidator
;
25 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
26 import eu
.etaxonomy
.cdm
.io
.globis
.validation
.GlobisCurrentSpeciesImportValidator
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
29 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
30 import eu
.etaxonomy
.cdm
.model
.description
.PresenceTerm
;
31 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
32 import eu
.etaxonomy
.cdm
.model
.location
.WaterbodyOrCountry
;
33 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
34 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
35 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
48 public class GlobisCommonNameImport
extends GlobisImportBase
<Taxon
> {
49 private static final Logger logger
= Logger
.getLogger(GlobisCommonNameImport
.class);
51 private int modCount
= 10000;
52 private static final String pluralString
= "common names";
53 private static final String dbTableName
= "species_language";
54 private static final Class cdmTargetClass
= Taxon
.class; //not needed
56 public GlobisCommonNameImport(){
57 super(pluralString
, dbTableName
, cdmTargetClass
);
64 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#getIdQuery()
67 protected String
getIdQuery() {
68 String strRecordQuery
=
70 " FROM " + dbTableName
;
71 return strRecordQuery
;
78 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
81 protected String
getRecordQuery(GlobisImportConfigurator config
) {
82 String strRecordQuery
=
84 " FROM " + getTableName() + " sl " +
85 " WHERE ( sl.ID IN (" + ID_LIST_TOKEN
+ ") )";
86 return strRecordQuery
;
92 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#doPartition(eu.etaxonomy.cdm.io.common.ResultSetPartitioner, eu.etaxonomy.cdm.io.globis.GlobisImportState)
95 public boolean doPartition(ResultSetPartitioner partitioner
, GlobisImportState state
) {
96 boolean success
= true;
98 Set
<TaxonBase
> objectsToSave
= new HashSet
<TaxonBase
>();
100 Map
<String
, Taxon
> taxonMap
= (Map
<String
, Taxon
>) partitioner
.getObjectMap(TAXON_NAMESPACE
);
101 // Map<String, DerivedUnit> ecoFactDerivedUnitMap = (Map<String, DerivedUnit>) partitioner.getObjectMap(ECO_FACT_DERIVED_UNIT_NAMESPACE);
103 ResultSet rs
= partitioner
.getResultSet();
109 //for each common name
112 if ((i
++ % modCount
) == 0 && i
!= 1 ){ logger
.info(pluralString
+ " handled: " + (i
-1));}
114 Integer taxonId
= rs
.getInt("IdCrrentSpec");
120 Reference
<?
> sourceRef
= state
.getTransactionalSourceReference();
123 Taxon species
= createObject(rs
, state
);
126 handleCountries(state
, rs
, species
);
128 this.doIdCreatedUpdatedNotes(state
, species
, rs
, taxonId
, TAXON_NAMESPACE
);
130 objectsToSave
.add(species
);
133 } catch (Exception e
) {
134 logger
.warn("Exception in current_species: IDcurrentspec " + taxonId
+ ". " + e
.getMessage());
135 // e.printStackTrace();
140 // logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
142 logger
.warn(pluralString
+ " to save: " + objectsToSave
.size());
143 getTaxonService().save(objectsToSave
);
146 } catch (SQLException e
) {
147 logger
.error("SQLException:" + e
);
152 private void handleCountries(GlobisImportState state
, ResultSet rs
, Taxon species
) throws SQLException
{
153 String countriesStr
= rs
.getString("dtSpcCountries");
154 if (isBlank(countriesStr
)){
157 String
[] countriesSplit
= countriesStr
.split(";");
158 for (String countryStr
: countriesSplit
){
159 if (isBlank(countryStr
)){
162 countryStr
= countryStr
.trim();
164 //TODO use isComplete
165 boolean isComplete
= countryStr
.endsWith(".");
167 countryStr
= countryStr
.substring(0,countryStr
.length() - 1).trim();
169 boolean isDoubtful
= countryStr
.endsWith("[?]");
171 countryStr
= countryStr
.substring(0,countryStr
.length() - 3).trim();
173 if (countryStr
.startsWith("?")){
175 countryStr
= countryStr
.substring(1).trim();
180 countryStr
= normalizeCountry(countryStr
);
182 WaterbodyOrCountry country
= getCountry(state
, countryStr
);
186 status
= PresenceTerm
.PRESENT_DOUBTFULLY();
188 status
= PresenceTerm
.PRESENT();
191 if (country
!= null){
192 TaxonDescription desc
= getTaxonDescription(species
, state
.getTransactionalSourceReference(), false, true);
193 Distribution distribution
= Distribution
.NewInstance(country
, status
);
194 desc
.addElement(distribution
);
196 logger
.warn("Country string not recognized: " + countryStr
);
207 private String
normalizeCountry(String countryStr
) {
208 String result
= countryStr
.trim();
209 if (result
.endsWith(".")){
210 result
= result
.substring(0,result
.length() - 1);
217 * @see eu.etaxonomy.cdm.io.common.mapping.IMappingImport#createObject(java.sql.ResultSet, eu.etaxonomy.cdm.io.common.ImportStateBase)
219 public Taxon
createObject(ResultSet rs
, GlobisImportState state
)
220 throws SQLException
{
221 String speciesEpi
= rs
.getString("dtSpcSpcakt");
222 String subGenusEpi
= rs
.getString("dtSpcSubgenakt");
223 String genusEpi
= rs
.getString("dtSpcGenusakt");
224 String author
= rs
.getString("dtSpcAutor");
227 ZoologicalName zooName
= ZoologicalName
.NewInstance(Rank
.SPECIES());
228 zooName
.setSpecificEpithet(speciesEpi
);
229 if (StringUtils
.isNotBlank(subGenusEpi
)){
230 zooName
.setInfraGenericEpithet(subGenusEpi
);
232 zooName
.setGenusOrUninomial(genusEpi
);
233 handleAuthorAndYear(author
, zooName
);
235 Taxon taxon
= Taxon
.NewInstance(zooName
, state
.getTransactionalSourceReference());
245 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
247 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
) {
251 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<Object
, Map
<String
, ?
extends CdmBase
>>();
253 Set
<String
> taxonIdSet
= new HashSet
<String
>();
256 // handleForeignKey(rs, taxonIdSet, "taxonId");
260 nameSpace
= TAXON_NAMESPACE
;
261 cdmClass
= Taxon
.class;
263 Map
<String
, Taxon
> objectMap
= (Map
<String
, Taxon
>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
264 result
.put(nameSpace
, objectMap
);
267 } catch (SQLException e
) {
268 throw new RuntimeException(e
);
274 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
277 protected boolean doCheck(GlobisImportState state
){
278 IOValidator
<GlobisImportState
> validator
= new GlobisCurrentSpeciesImportValidator();
279 return validator
.validate(state
);
284 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
286 protected boolean isIgnore(GlobisImportState state
){
287 return ! state
.getConfig().isDoCurrentTaxa();