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
.logging
.log4j
.LogManager
;
20 import org
.apache
.logging
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
23 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
24 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
25 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
26 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
29 import eu
.etaxonomy
.cdm
.model
.common
.VerbatimTimePeriod
;
30 import eu
.etaxonomy
.cdm
.model
.description
.CommonTaxonName
;
31 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
32 import eu
.etaxonomy
.cdm
.model
.location
.Country
;
33 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
34 import eu
.etaxonomy
.cdm
.model
.reference
.OriginalSourceType
;
35 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
36 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
50 public class GlobisCommonNameImport
extends GlobisImportBase
<Taxon
> {
52 private static final long serialVersionUID
= 2462539699442149049L;
53 private static final Logger logger
= LogManager
.getLogger();
55 private int modCount
= 10000;
56 private static final String pluralString
= "common names";
57 private static final String dbTableName
= "species_language";
58 private static final Class
<?
> cdmTargetClass
= Taxon
.class; //not needed
60 public GlobisCommonNameImport(){
61 super(pluralString
, dbTableName
, cdmTargetClass
);
64 //dirty but acceptable for globis environment
65 private Map
<Integer
,Reference
> refMap
= new HashMap
<Integer
,Reference
>();
68 protected String
getIdQuery() {
69 String strRecordQuery
=
71 " FROM " + dbTableName
;
72 return strRecordQuery
;
77 protected String
getRecordQuery(GlobisImportConfigurator config
) {
78 String strRecordQuery
=
80 " FROM " + getTableName() + " sl " +
81 " WHERE ( sl.ID IN (" + ID_LIST_TOKEN
+ ") )";
82 return strRecordQuery
;
86 public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner
, GlobisImportState state
) {
87 boolean success
= true;
89 @SuppressWarnings("rawtypes")
90 Set
<TaxonBase
> objectsToSave
= new HashSet
<>();
92 @SuppressWarnings("unchecked")
93 Map
<String
, Taxon
> taxonMap
= partitioner
.getObjectMap(TAXON_NAMESPACE
);
95 ResultSet rs
= partitioner
.getResultSet();
101 //for each common name
104 if ((i
++ % modCount
) == 0 && i
!= 1 ){ logger
.info(pluralString
+ " handled: " + (i
-1));}
106 Integer idTaxon
= nullSafeInt(rs
,"IDCurrentSpec");
111 Reference sourceRef
= state
.getTransactionalSourceReference();
114 Integer id
= nullSafeInt(rs
,"ID");
115 String isoLang
= rs
.getString("ISO");
116 String strCommonName
= rs
.getString("commonname");
117 Integer refID
= nullSafeInt(rs
,"ReferenceID");
118 String strCountryCode
= rs
.getString("Code2");
121 Taxon taxon
= taxonMap
.get(String
.valueOf(idTaxon
));
123 logger
.warn("No taxon found for taxonId " + idTaxon
);
124 }else if (isBlank(strCommonName
)){
125 logger
.warn("No common name string defined for common name ID: " + id
);
127 Language language
= getLanguage(isoLang
);
128 if (language
== null){
129 logger
.warn("No language found for common name ID: " + id
);
131 NamedArea area
= Country
.getCountryByIso3166A2(strCountryCode
);
133 logger
.warn("No country found for common name ID: " + id
);
136 TaxonDescription taxonDescription
= getTaxonDescription(taxon
, sourceRef
, ! IMAGE_GALLERY
, CREATE
);
137 CommonTaxonName commonName
= CommonTaxonName
.NewInstance(strCommonName
, language
, area
);
138 taxonDescription
.addElement(commonName
);
140 Reference ref
= handleReference(state
, refID
);
141 if (ref
== null && refID
!= null){
142 logger
.warn("No reference found for common name ID: " + id
);
144 commonName
.addSource(OriginalSourceType
.Import
, String
.valueOf(refID
), "reference", sourceRef
, null);
147 objectsToSave
.add(taxon
);
150 } catch (Exception e
) {
151 logger
.warn("Exception in current_species: IDcurrentspec " + idTaxon
+ ". " + e
.getMessage());
157 logger
.warn(pluralString
+ " to save: " + objectsToSave
.size());
158 getTaxonService().save(objectsToSave
);
161 } catch (SQLException e
) {
162 logger
.error("SQLException:" + e
);
167 private Map
<String
,Language
> languageMap
= new HashMap
<>();
168 private Language
getLanguage(String isoLang
) {
169 Language result
= languageMap
.get(isoLang
);
172 result
= getTermService().getLanguageByIso(isoLang
);
174 logger
.warn("No language found for iso code: " + isoLang
);
181 private Reference
handleReference(GlobisImportState state
, Integer refId
){
185 Reference result
= refMap
.get(refId
);
189 String sql
= "SELECT * FROM [references] WHERE ReferenceID = " + refId
;
190 ResultSet rs
= state
.getConfig().getSource().getResultSet(sql
);
193 String authors
= rs
.getString("Author(s)");
194 String title
= rs
.getString("Title");
195 String details
= rs
.getString("Details");
196 Integer year
= nullSafeInt(rs
, "year");
197 result
= ReferenceFactory
.newGeneric();
198 result
.setTitleCache(details
, true);
199 result
.setTitle(title
);
200 result
.setDatePublished(VerbatimTimePeriod
.NewVerbatimInstance(year
));
202 TeamOrPersonBase
<?
> author
;
203 String
[] authorSplit
= authors
.split("&");
204 if (authorSplit
.length
> 1){
205 Team team
= Team
.NewInstance();
207 for (String singleAuthor
: authorSplit
){
208 Person person
= makeSingleAuthor(singleAuthor
);
209 team
.addTeamMember(person
);
212 author
= makeSingleAuthor(authors
);
215 result
.setAuthorship(author
);
216 refMap
.put(refId
,result
);
218 } catch (SQLException e
) {
228 private Person
makeSingleAuthor(String authors
) {
229 Person result
= Person
.NewTitledInstance(authors
);
234 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
, GlobisImportState state
) {
237 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
239 Set
<String
> taxonIdSet
= new HashSet
<>();
242 handleForeignKey(rs
, taxonIdSet
, "IDCurrentSpec");
246 nameSpace
= TAXON_NAMESPACE
;
248 Map
<String
, Taxon
> objectMap
= getCommonService().getSourcedObjectsByIdInSourceC(Taxon
.class, idSet
, nameSpace
);
249 result
.put(nameSpace
, objectMap
);
252 } catch (SQLException e
) {
253 throw new RuntimeException(e
);
259 protected boolean doCheck(GlobisImportState state
){
260 // IOValidator<GlobisImportState> validator = new GlobisCurrentSpeciesImportValidator();
265 protected boolean isIgnore(GlobisImportState state
){
266 return ! state
.getConfig().isDoCommonNames();