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
;
20 import org
.springframework
.transaction
.TransactionStatus
;
22 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
23 import eu
.etaxonomy
.cdm
.io
.common
.CdmImportBase
;
24 import eu
.etaxonomy
.cdm
.io
.common
.CdmIoBase
;
25 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
26 import eu
.etaxonomy
.cdm
.io
.common
.ICdmImport
;
27 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
28 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
29 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
30 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
31 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
32 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
33 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
34 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
38 * @author a.babadshanjan
42 public abstract class FaunaEuropaeaImportBase
extends CdmImportBase
<FaunaEuropaeaImportConfigurator
, FaunaEuropaeaImportState
>
43 implements ICdmImport
<FaunaEuropaeaImportConfigurator
,FaunaEuropaeaImportState
> {
44 private static final Logger logger
= Logger
.getLogger(FaunaEuropaeaImportBase
.class);
46 // /* Max number of taxa to retrieve (for test purposes) */
47 // protected static final int maxTaxa = 1000;
48 // /* Max number of taxa to be saved with one service call */
49 // protected int limit = 20000; // TODO: Make configurable
50 // /* Interval for progress info message when retrieving taxa */
51 // protected static final int modCount = 10000;
52 // /* Highest taxon index in the FauEu database */
53 // protected int highestTaxonIndex = 0;
55 protected boolean resultSetHasColumn(ResultSet rs
, String columnName
){
57 ResultSetMetaData metaData
= rs
.getMetaData();
58 for (int i
= 0; i
< metaData
.getColumnCount(); i
++){
59 if (metaData
.getColumnName(i
+ 1).equalsIgnoreCase(columnName
)){
64 } catch (SQLException e
) {
65 logger
.warn("Exception in resultSetHasColumn");
70 protected boolean checkSqlServerColumnExists(Source source
, String tableName
, String columnName
){
71 String strQuery
= "SELECT Count(t.id) as n " +
72 " FROM sysobjects AS t " +
73 " INNER JOIN syscolumns AS c ON t.id = c.id " +
74 " WHERE (t.xtype = 'U') AND " +
75 " (t.name = '" + tableName
+ "') AND " +
76 " (c.name = '" + columnName
+ "')";
77 ResultSet rs
= source
.getResultSet(strQuery
) ;
83 } catch (SQLException e
) {
91 * Returns a map that holds all values of a ResultSet. This is needed if a value needs to
95 * @throws SQLException
97 protected Map
<String
, Object
> getValueMap(ResultSet rs
) throws SQLException
{
99 Map
<String
, Object
> valueMap
= new HashMap
<String
, Object
>();
100 int colCount
= rs
.getMetaData().getColumnCount();
101 for (int c
= 0; c
< colCount
; c
++){
102 Object value
= rs
.getObject(c
+1);
103 String label
= rs
.getMetaData().getColumnLabel(c
+1).toLowerCase();
104 if (value
!= null && ! CdmUtils
.Nz(value
.toString()).trim().equals("")){
105 valueMap
.put(label
, value
);
109 }catch(SQLException e
){
115 protected boolean saveTaxa(Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
,
116 int highestTaxonIndex
, int limit
) {
118 MapWrapper
<TaxonBase
> taxonStore
= (MapWrapper
<TaxonBase
>)stores
.get(ICdmIO
.TAXON_STORE
);
121 int nbrOfTaxa
= highestTaxonIndex
;
122 // int nbrOfTaxa = taxonStore.size();
123 boolean success
= true;
125 if(logger
.isInfoEnabled()) { logger
.info("Saving taxa ..."); }
127 if (nbrOfTaxa
< limit
) { // TODO: test with critical values
130 n
= nbrOfTaxa
/ limit
;
133 if(logger
.isInfoEnabled()) {
134 logger
.info("number of taxa = " + taxonStore
.size()
135 + ", highest taxon index = " + highestTaxonIndex
136 + ", limit = " + limit
140 // save taxa in blocks of <=limit
142 for (int j
= 1; j
<= n
+ 1; j
++)
145 int start
= offset
* limit
;
147 if(logger
.isInfoEnabled()) {
148 logger
.info("Saving taxa: " + start
+ " - " + (start
+ limit
- 1));
151 if(logger
.isInfoEnabled()) {
152 logger
.info("index = " + j
153 + ", offset = " + offset
154 + ", start = " + start
);
158 limit
= nbrOfTaxa
- n
* limit
;
159 if(logger
.isInfoEnabled()) {
160 logger
.info("n = " + n
+ ", limit = " + limit
);
164 TransactionStatus txStatus
= startTransaction();
166 Collection
<TaxonBase
> taxonMapPart
= taxonStore
.objects(start
, limit
);
167 getTaxonService().saveTaxonAll(taxonMapPart
);
169 taxonStore
.removeObjects(start
, limit
);
171 commitTransaction(txStatus
);