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
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
22 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
23 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
24 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
25 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
28 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceType
;
29 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
30 import eu
.etaxonomy
.cdm
.model
.description
.CommonTaxonName
;
31 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
32 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
33 import eu
.etaxonomy
.cdm
.model
.location
.Country
;
34 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
35 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
49 public class GlobisCommonNameImport
extends GlobisImportBase
<Taxon
> {
50 private static final Logger logger
= Logger
.getLogger(GlobisCommonNameImport
.class);
52 private int modCount
= 10000;
53 private static final String pluralString
= "common names";
54 private static final String dbTableName
= "species_language";
55 private static final Class
<?
> cdmTargetClass
= Taxon
.class; //not needed
57 public GlobisCommonNameImport(){
58 super(pluralString
, dbTableName
, cdmTargetClass
);
61 //dirty but acceptable for globis environment
62 private Map
<Integer
,Reference
> refMap
= new HashMap
<Integer
,Reference
>();
65 protected String
getIdQuery() {
66 String strRecordQuery
=
68 " FROM " + dbTableName
;
69 return strRecordQuery
;
74 protected String
getRecordQuery(GlobisImportConfigurator config
) {
75 String strRecordQuery
=
77 " FROM " + getTableName() + " sl " +
78 " WHERE ( sl.ID IN (" + ID_LIST_TOKEN
+ ") )";
79 return strRecordQuery
;
83 public boolean doPartition(ResultSetPartitioner partitioner
, GlobisImportState state
) {
84 boolean success
= true;
86 Set
<TaxonBase
> objectsToSave
= new HashSet
<TaxonBase
>();
88 Map
<String
, Taxon
> taxonMap
= (Map
<String
, Taxon
>) partitioner
.getObjectMap(TAXON_NAMESPACE
);
90 ResultSet rs
= partitioner
.getResultSet();
96 //for each common name
99 if ((i
++ % modCount
) == 0 && i
!= 1 ){ logger
.info(pluralString
+ " handled: " + (i
-1));}
101 Integer idTaxon
= nullSafeInt(rs
,"IDCurrentSpec");
106 Reference
<?
> sourceRef
= state
.getTransactionalSourceReference();
109 Integer id
= nullSafeInt(rs
,"ID");
110 String isoLang
= rs
.getString("ISO");
111 String strCommonName
= rs
.getString("commonname");
112 Integer refID
= nullSafeInt(rs
,"ReferenceID");
113 String strCountryCode
= rs
.getString("Code2");
116 Taxon taxon
= taxonMap
.get(String
.valueOf(idTaxon
));
118 logger
.warn("No taxon found for taxonId " + idTaxon
);
119 }else if (isBlank(strCommonName
)){
120 logger
.warn("No common name string defined for common name ID: " + id
);
122 Language language
= getLanguage(isoLang
);
123 if (language
== null){
124 logger
.warn("No language found for common name ID: " + id
);
126 NamedArea area
= Country
.getCountryByIso3166A2(strCountryCode
);
128 logger
.warn("No country found for common name ID: " + id
);
131 TaxonDescription taxonDescription
= getTaxonDescription(taxon
, sourceRef
, ! IMAGE_GALLERY
, CREATE
);
132 CommonTaxonName commonName
= CommonTaxonName
.NewInstance(strCommonName
, language
, area
);
133 taxonDescription
.addElement(commonName
);
135 Reference
<?
> ref
= handleReference(state
, refID
);
136 if (ref
== null && refID
!= null){
137 logger
.warn("No reference found for common name ID: " + id
);
139 commonName
.addSource(OriginalSourceType
.Import
, String
.valueOf(refID
), "reference", sourceRef
, null);
142 objectsToSave
.add(taxon
);
145 } catch (Exception e
) {
146 logger
.warn("Exception in current_species: IDcurrentspec " + idTaxon
+ ". " + e
.getMessage());
152 logger
.warn(pluralString
+ " to save: " + objectsToSave
.size());
153 getTaxonService().save(objectsToSave
);
156 } catch (SQLException e
) {
157 logger
.error("SQLException:" + e
);
163 private Map
<String
,Language
> languageMap
= new HashMap
<String
,Language
>();
164 private Language
getLanguage(String isoLang
) {
165 Language result
= languageMap
.get(isoLang
);
168 result
= getTermService().getLanguageByIso(isoLang
);
170 logger
.warn("No language found for iso code: " + isoLang
);
177 private Reference
<?
> handleReference(GlobisImportState state
, Integer refId
){
181 Reference
<?
> result
= refMap
.get(refId
);
185 String sql
= "SELECT * FROM [references] WHERE ReferenceID = " + refId
;
186 ResultSet rs
= state
.getConfig().getSource().getResultSet(sql
);
189 String authors
= rs
.getString("Author(s)");
190 String title
= rs
.getString("Title");
191 String details
= rs
.getString("Details");
192 Integer year
= nullSafeInt(rs
, "year");
193 result
= ReferenceFactory
.newGeneric();
194 result
.setTitleCache(details
, true);
195 result
.setTitle(title
);
196 result
.setDatePublished(TimePeriod
.NewInstance(year
));
198 TeamOrPersonBase
<?
> author
;
199 String
[] authorSplit
= authors
.split("&");
200 if (authorSplit
.length
> 1){
201 Team team
= Team
.NewInstance();
203 for (String singleAuthor
: authorSplit
){
204 Person person
= makeSingleAuthor(singleAuthor
);
205 team
.addTeamMember(person
);
208 author
= makeSingleAuthor(authors
);
211 result
.setAuthorTeam(author
);
212 refMap
.put(refId
,result
);
214 } catch (SQLException e
) {
224 private Person
makeSingleAuthor(String authors
) {
225 Person result
= Person
.NewTitledInstance(authors
);
230 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
, GlobisImportState state
) {
234 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<Object
, Map
<String
, ?
extends CdmBase
>>();
236 Set
<String
> taxonIdSet
= new HashSet
<String
>();
239 handleForeignKey(rs
, taxonIdSet
, "IDCurrentSpec");
243 nameSpace
= TAXON_NAMESPACE
;
244 cdmClass
= Taxon
.class;
246 Map
<String
, Taxon
> objectMap
= (Map
<String
, Taxon
>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
247 result
.put(nameSpace
, objectMap
);
250 } catch (SQLException e
) {
251 throw new RuntimeException(e
);
258 protected boolean doCheck(GlobisImportState state
){
259 // IOValidator<GlobisImportState> validator = new GlobisCurrentSpeciesImportValidator();
264 protected boolean isIgnore(GlobisImportState state
){
265 return ! state
.getConfig().isDoCommonNames();