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
.eflora
.centralAfrica
.ferns
;
12 import java
.lang
.reflect
.Method
;
13 import java
.sql
.ResultSet
;
14 import java
.sql
.ResultSetMetaData
;
15 import java
.sql
.SQLException
;
16 import java
.sql
.Timestamp
;
17 import java
.util
.HashMap
;
18 import java
.util
.HashSet
;
21 import java
.util
.UUID
;
23 import org
.apache
.commons
.lang
.StringUtils
;
24 import org
.apache
.log4j
.Logger
;
25 import org
.joda
.time
.DateTime
;
27 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
28 import eu
.etaxonomy
.cdm
.io
.common
.CdmImportBase
;
29 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
30 import eu
.etaxonomy
.cdm
.io
.common
.IPartitionedIO
;
31 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
32 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
33 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
34 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
.EDITOR
;
35 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportMapping
;
36 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
37 import eu
.etaxonomy
.cdm
.model
.common
.AnnotatableEntity
;
38 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
39 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
40 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
41 import eu
.etaxonomy
.cdm
.model
.common
.ExtensionType
;
42 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
43 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
44 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
45 import eu
.etaxonomy
.cdm
.model
.common
.User
;
46 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
47 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
48 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
55 public abstract class CentralAfricaFernsImportBase
<CDM_BASE
extends CdmBase
> extends CdmImportBase
<CentralAfricaFernsImportConfigurator
, CentralAfricaFernsImportState
> implements ICdmIO
<CentralAfricaFernsImportState
>, IPartitionedIO
<CentralAfricaFernsImportState
> {
56 private static final Logger logger
= Logger
.getLogger(CentralAfricaFernsImportBase
.class);
58 public static final UUID ID_IN_SOURCE_EXT_UUID
= UUID
.fromString("23dac094-e793-40a4-bad9-649fc4fcfd44");
60 protected static final String TAXON_NAMESPACE
= "African_pteridophytes_Taxon";
61 protected static final String NAME_NAMESPACE
= "African_pteridophytes_Name";
62 protected static final String HIGHER_TAXON_NAMESPACE
= "African_pteridophytes_Higher_Taxon";
64 private NonViralNameParserImpl parser
= NonViralNameParserImpl
.NewInstance();
67 private String pluralString
;
68 private String dbTableName
;
70 private Class cdmTargetClass
;
79 public CentralAfricaFernsImportBase(String pluralString
, String dbTableName
, Class cdmTargetClass
) {
80 this.pluralString
= pluralString
;
81 this.dbTableName
= dbTableName
;
82 this.cdmTargetClass
= cdmTargetClass
;
85 protected void doInvoke(CentralAfricaFernsImportState state
){
86 logger
.info("start make " + getPluralString() + " ...");
87 CentralAfricaFernsImportConfigurator config
= state
.getConfig();
88 Source source
= config
.getSource();
90 String strIdQuery
= getIdQuery();
91 String strRecordQuery
= getRecordQuery(config
);
93 int recordsPerTransaction
= config
.getRecordsPerTransaction();
95 ResultSetPartitioner partitioner
= ResultSetPartitioner
.NewInstance(source
, strIdQuery
, strRecordQuery
, recordsPerTransaction
);
96 while (partitioner
.nextPartition()){
97 partitioner
.doPartition(this, state
);
99 } catch (SQLException e
) {
100 logger
.error("SQLException:" + e
);
101 state
.setUnsuccessfull();
105 logger
.info("end make " + getPluralString() + " ... " + getSuccessString(true));
109 public boolean doPartition(ResultSetPartitioner partitioner
, CentralAfricaFernsImportState state
) {
110 boolean success
= true ;
111 Set objectsToSave
= new HashSet();
113 DbImportMapping
<?
, ?
> mapping
= getMapping();
114 mapping
.initialize(state
, cdmTargetClass
);
116 ResultSet rs
= partitioner
.getResultSet();
119 success
&= mapping
.invoke(rs
,objectsToSave
);
121 } catch (SQLException e
) {
122 logger
.error("SQLException:" + e
);
126 partitioner
.startDoSave();
127 getCommonService().save(objectsToSave
);
136 protected abstract DbImportMapping
<?
, ?
> getMapping();
141 protected abstract String
getRecordQuery(CentralAfricaFernsImportConfigurator config
);
146 protected String
getIdQuery(){
147 String result
= " SELECT id FROM " + getTableName();
152 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getPluralString()
154 public String
getPluralString(){
161 protected String
getTableName(){
162 return this.dbTableName
;
167 * Reads a foreign key field from the result set and adds its value to the idSet.
170 * @throws SQLException
172 protected void handleForeignKey(ResultSet rs
, Set
<String
> idSet
, String attributeName
)
173 throws SQLException
{
174 Object idObj
= rs
.getObject(attributeName
);
176 String id
= String
.valueOf(idObj
);
182 * Returns true if i is a multiple of recordsPerTransaction
184 * @param recordsPerTransaction
187 protected boolean loopNeedsHandling(int i
, int recordsPerLoop
) {
189 return (i
% recordsPerLoop
) == 0;
192 protected void doLogPerLoop(int count
, int recordsPerLog
, String pluralString
){
193 if ((count
% recordsPerLog
) == 0 && count
!= 0 ){ logger
.info(pluralString
+ " handled: " + (count
));}
198 protected void setAuthor(BotanicalName taxonName
, ResultSet rs
, String taxonNumber
, boolean isHigherTaxon
) throws SQLException
{
200 String authorsFull
= null;
201 String authorsAbbrev
= null;
202 if (! isHigherTaxon
){
203 authorsFull
= rs
.getString("Author/s - full");
204 authorsAbbrev
= rs
.getString("Author/s - abbreviated");
207 Rank rank
= taxonName
.getRank();
208 String authorString
= null;
210 if (rank
.equals(Rank
.ORDER())){
211 authorString
= rs
.getString("Order name author");
212 }else if (rank
.equals(Rank
.SUBORDER())){
213 authorString
= rs
.getString("Suborder name author");
214 }else if (rank
.equals(Rank
.FAMILY())){
215 authorString
= rs
.getString("Family name author");
216 }else if (rank
.equals(Rank
.SUBFAMILY())){
217 authorString
= rs
.getString("Subfamily name author");
218 }else if (rank
.equals(Rank
.TRIBE())){
219 authorString
= rs
.getString("Tribus author");
220 }else if (rank
.equals(Rank
.SUBTRIBE())){
221 authorString
= rs
.getString("Subtribus author");
222 }else if (rank
.equals(Rank
.SECTION_BOTANY())){
223 authorString
= rs
.getString("Section name author");
224 }else if (rank
.equals(Rank
.SUBSECTION_BOTANY())){
225 authorString
= rs
.getString("Subsection author");
226 }else if (rank
.equals(Rank
.GENUS())){
227 authorString
= rs
.getString("Genus name author");
228 }else if (rank
.equals(Rank
.SUBGENUS())){
229 authorString
= rs
.getString("Subgenus name author");
230 }else if (rank
.equals(Rank
.SERIES())){
231 authorString
= rs
.getString("Series name author");
232 }else if (rank
.equals(Rank
.SPECIES())){
233 authorString
= rs
.getString("Specific epithet author");
234 }else if (rank
.equals(Rank
.SUBSPECIES())){
235 authorString
= rs
.getString("Subspecies author");
236 }else if (rank
.equals(Rank
.VARIETY())){
237 authorString
= rs
.getString("Variety name author");
238 }else if (rank
.equals(Rank
.SUBVARIETY())){
239 authorString
= rs
.getString("Subvariety author");
240 }else if (rank
.equals(Rank
.FORM())){
241 authorString
= rs
.getString("Forma name author");
242 }else if (rank
.equals(Rank
.SUBFORM())){
243 authorString
= rs
.getString("Subforma author");
245 logger
.warn("Author string could not be defined");
246 if (! isHigherTaxon
){
247 authorString
= authorsAbbrev
;
248 if (StringUtils
.isBlank(authorString
)){
249 logger
.warn("Authors abbrev string could not be defined");
250 authorString
= authorsFull
;
255 logger
.warn(taxonNumber
+ ": Rank is null");
256 authorString
= authorsAbbrev
;
257 if (StringUtils
.isBlank(authorString
)){
258 logger
.warn(taxonNumber
+ ": Authors abbrev string could not be defined");
259 authorString
= authorsFull
;
263 if (StringUtils
.isNotBlank(authorString
)){
264 parser
.handleAuthors(taxonName
, taxonName
.getNameCache().trim() + " " + authorString
, authorString
);
266 if (! isHigherTaxon
){
267 String combinationAuthor
= taxonName
.getCombinationAuthorTeam()==null ?
"" :taxonName
.getCombinationAuthorTeam().getNomenclaturalTitle();
268 if (StringUtils
.isNotBlank(authorsAbbrev
) && ! authorsAbbrev
.equalsIgnoreCase(combinationAuthor
)){
269 //it is expected that the fullAuthor and the abbrevAuthor are the combination authors but very often it is not
270 logger
.warn(taxonNumber
+ ": Rank author and abbrev author are not equal: " + authorString
+ "\t<-> " + combinationAuthor
+ "\t<-> " + authorsAbbrev
);
272 // if (StringUtils.isNotBlank(authorsFull) && ! authorsFull.equalsIgnoreCase(authorString)){
273 // logger.warn("Rank author and full author are not equal Rankauthor: " + authorString + ", full author " + authorsFull);