1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.io.faunaEuropaea;
|
11
|
|
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;
|
17
|
import java.util.Map;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
|
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;
|
35
|
|
36
|
/**
|
37
|
* @author a.babadshanjan
|
38
|
* @created 11.05.2009
|
39
|
* @version 1.0
|
40
|
*/
|
41
|
public abstract class FaunaEuropaeaImportBase extends CdmImportBase<FaunaEuropaeaImportConfigurator, FaunaEuropaeaImportState>
|
42
|
implements ICdmImport<FaunaEuropaeaImportConfigurator,FaunaEuropaeaImportState> {
|
43
|
private static final Logger logger = Logger.getLogger(FaunaEuropaeaImportBase.class);
|
44
|
|
45
|
/* Highest taxon index in the FauEu database */
|
46
|
// protected int highestTaxonIndex = 0;
|
47
|
|
48
|
protected boolean resultSetHasColumn(ResultSet rs, String columnName){
|
49
|
try {
|
50
|
ResultSetMetaData metaData = rs.getMetaData();
|
51
|
for (int i = 0; i < metaData.getColumnCount(); i++){
|
52
|
if (metaData.getColumnName(i + 1).equalsIgnoreCase(columnName)){
|
53
|
return true;
|
54
|
}
|
55
|
}
|
56
|
return false;
|
57
|
} catch (SQLException e) {
|
58
|
logger.warn("Exception in resultSetHasColumn");
|
59
|
return false;
|
60
|
}
|
61
|
}
|
62
|
|
63
|
protected boolean checkSqlServerColumnExists(Source source, String tableName, String columnName){
|
64
|
String strQuery = "SELECT Count(t.id) as n " +
|
65
|
" FROM sysobjects AS t " +
|
66
|
" INNER JOIN syscolumns AS c ON t.id = c.id " +
|
67
|
" WHERE (t.xtype = 'U') AND " +
|
68
|
" (t.name = '" + tableName + "') AND " +
|
69
|
" (c.name = '" + columnName + "')";
|
70
|
ResultSet rs = source.getResultSet(strQuery) ;
|
71
|
int n;
|
72
|
try {
|
73
|
rs.next();
|
74
|
n = rs.getInt("n");
|
75
|
return n>0;
|
76
|
} catch (SQLException e) {
|
77
|
e.printStackTrace();
|
78
|
return false;
|
79
|
}
|
80
|
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* Returns a map that holds all values of a ResultSet. This is needed if a value needs to
|
85
|
* be accessed twice
|
86
|
* @param rs
|
87
|
* @return
|
88
|
* @throws SQLException
|
89
|
*/
|
90
|
protected Map<String, Object> getValueMap(ResultSet rs) throws SQLException{
|
91
|
try{
|
92
|
Map<String, Object> valueMap = new HashMap<String, Object>();
|
93
|
int colCount = rs.getMetaData().getColumnCount();
|
94
|
for (int c = 0; c < colCount ; c++){
|
95
|
Object value = rs.getObject(c+1);
|
96
|
String label = rs.getMetaData().getColumnLabel(c+1).toLowerCase();
|
97
|
if (value != null && ! CdmUtils.Nz(value.toString()).trim().equals("")){
|
98
|
valueMap.put(label, value);
|
99
|
}
|
100
|
}
|
101
|
return valueMap;
|
102
|
}catch(SQLException e){
|
103
|
throw e;
|
104
|
}
|
105
|
}
|
106
|
|
107
|
|
108
|
protected boolean saveTaxa(Map<String, MapWrapper<? extends CdmBase>> stores,
|
109
|
int highestTaxonIndex, int limit) {
|
110
|
|
111
|
MapWrapper<TaxonBase> taxonStore = (MapWrapper<TaxonBase>)stores.get(ICdmIO.TAXON_STORE);
|
112
|
|
113
|
int n = 0;
|
114
|
int nbrOfTaxa = highestTaxonIndex;
|
115
|
// int nbrOfTaxa = taxonStore.size();
|
116
|
boolean success = true;
|
117
|
|
118
|
if(logger.isInfoEnabled()) { logger.info("Saving taxa ..."); }
|
119
|
|
120
|
if (nbrOfTaxa < limit) { // TODO: test with critical values
|
121
|
limit = nbrOfTaxa;
|
122
|
} else {
|
123
|
n = nbrOfTaxa / limit;
|
124
|
}
|
125
|
|
126
|
if(logger.isInfoEnabled()) {
|
127
|
logger.info("number of taxa = " + taxonStore.size()
|
128
|
+ ", highest taxon index = " + highestTaxonIndex
|
129
|
+ ", limit = " + limit
|
130
|
+ ", n = " + n);
|
131
|
}
|
132
|
|
133
|
// save taxa in chunks of <=limit
|
134
|
|
135
|
for (int j = 1; j <= n + 1; j++)
|
136
|
{
|
137
|
int offset = j - 1;
|
138
|
int start = offset * limit;
|
139
|
|
140
|
if(logger.isInfoEnabled()) { logger.info("Saving taxa: " + start + " - " + (start + limit - 1)); }
|
141
|
|
142
|
if(logger.isInfoEnabled()) {
|
143
|
logger.info("index = " + j
|
144
|
+ ", offset = " + offset
|
145
|
+ ", start = " + start);
|
146
|
}
|
147
|
|
148
|
if (j == n + 1) {
|
149
|
limit = nbrOfTaxa - n * limit;
|
150
|
if(logger.isInfoEnabled()) { logger.info("n = " + n + ", limit = " + limit); }
|
151
|
}
|
152
|
|
153
|
Collection<TaxonBase> taxonMapPart = taxonStore.objects(start, limit);
|
154
|
getTaxonService().saveTaxonAll(taxonMapPart);
|
155
|
taxonMapPart = null;
|
156
|
//taxonStore.removeObjects(start, limit);
|
157
|
}
|
158
|
|
159
|
return success;
|
160
|
}
|
161
|
}
|