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
.faunaEuropaea
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.ResultSetMetaData
;
14 import java
.sql
.SQLException
;
15 import java
.util
.Collection
;
16 import java
.util
.HashMap
;
19 import org
.apache
.log4j
.Logger
;
21 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
22 import eu
.etaxonomy
.cdm
.io
.common
.CdmImportBase
;
23 import eu
.etaxonomy
.cdm
.io
.common
.CdmIoBase
;
24 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
25 import eu
.etaxonomy
.cdm
.io
.common
.ICdmImport
;
26 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
27 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
28 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
29 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
30 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
33 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
37 * @author a.babadshanjan
41 public abstract class FaunaEuropaeaImportBase
extends CdmImportBase
<FaunaEuropaeaImportConfigurator
, FaunaEuropaeaImportState
>
42 implements ICdmImport
<FaunaEuropaeaImportConfigurator
,FaunaEuropaeaImportState
> {
43 private static final Logger logger
= Logger
.getLogger(FaunaEuropaeaImportBase
.class);
45 // /* Max number of taxa to retrieve (for test purposes) */
46 // protected static final int maxTaxa = 1000;
47 // /* Max number of taxa to be saved with one service call */
48 // protected int limit = 20000; // TODO: Make configurable
49 // /* Interval for progress info message when retrieving taxa */
50 // protected static final int modCount = 10000;
51 // /* Highest taxon index in the FauEu database */
52 // protected int highestTaxonIndex = 0;
54 protected boolean resultSetHasColumn(ResultSet rs
, String columnName
){
56 ResultSetMetaData metaData
= rs
.getMetaData();
57 for (int i
= 0; i
< metaData
.getColumnCount(); i
++){
58 if (metaData
.getColumnName(i
+ 1).equalsIgnoreCase(columnName
)){
63 } catch (SQLException e
) {
64 logger
.warn("Exception in resultSetHasColumn");
69 protected boolean checkSqlServerColumnExists(Source source
, String tableName
, String columnName
){
70 String strQuery
= "SELECT Count(t.id) as n " +
71 " FROM sysobjects AS t " +
72 " INNER JOIN syscolumns AS c ON t.id = c.id " +
73 " WHERE (t.xtype = 'U') AND " +
74 " (t.name = '" + tableName
+ "') AND " +
75 " (c.name = '" + columnName
+ "')";
76 ResultSet rs
= source
.getResultSet(strQuery
) ;
82 } catch (SQLException e
) {
90 * Returns a map that holds all values of a ResultSet. This is needed if a value needs to
94 * @throws SQLException
96 protected Map
<String
, Object
> getValueMap(ResultSet rs
) throws SQLException
{
98 Map
<String
, Object
> valueMap
= new HashMap
<String
, Object
>();
99 int colCount
= rs
.getMetaData().getColumnCount();
100 for (int c
= 0; c
< colCount
; c
++){
101 Object value
= rs
.getObject(c
+1);
102 String label
= rs
.getMetaData().getColumnLabel(c
+1).toLowerCase();
103 if (value
!= null && ! CdmUtils
.Nz(value
.toString()).trim().equals("")){
104 valueMap
.put(label
, value
);
108 }catch(SQLException e
){
114 protected boolean saveTaxa(Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
,
115 int highestTaxonIndex
, int limit
) {
117 MapWrapper
<TaxonBase
> taxonStore
= (MapWrapper
<TaxonBase
>)stores
.get(ICdmIO
.TAXON_STORE
);
120 int nbrOfTaxa
= highestTaxonIndex
;
121 // int nbrOfTaxa = taxonStore.size();
122 boolean success
= true;
124 if(logger
.isInfoEnabled()) { logger
.info("Saving taxa ..."); }
126 if (nbrOfTaxa
< limit
) { // TODO: test with critical values
129 n
= nbrOfTaxa
/ limit
;
132 if(logger
.isInfoEnabled()) {
133 logger
.info("number of taxa = " + taxonStore
.size()
134 + ", highest taxon index = " + highestTaxonIndex
135 + ", limit = " + limit
139 // save taxa in chunks of <=limit
141 for (int j
= 1; j
<= n
+ 1; j
++)
144 int start
= offset
* limit
;
146 if(logger
.isInfoEnabled()) { logger
.info("Saving taxa: " + start
+ " - " + (start
+ limit
- 1)); }
148 if(logger
.isInfoEnabled()) {
149 logger
.info("index = " + j
150 + ", offset = " + offset
151 + ", start = " + start
);
155 limit
= nbrOfTaxa
- n
* limit
;
156 if(logger
.isInfoEnabled()) { logger
.info("n = " + n
+ ", limit = " + limit
); }
159 Collection
<TaxonBase
> taxonMapPart
= taxonStore
.objects(start
, limit
);
160 getTaxonService().saveTaxonAll(taxonMapPart
);
162 //taxonStore.removeObjects(start, limit);