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.pesi.faunaEuropaea;
|
11
|
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.ResultSetMetaData;
|
14
|
import java.sql.SQLException;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.Map;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.springframework.transaction.TransactionStatus;
|
21
|
|
22
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
23
|
import eu.etaxonomy.cdm.io.common.CdmImportBase;
|
24
|
import eu.etaxonomy.cdm.io.common.ICdmImport;
|
25
|
import eu.etaxonomy.cdm.io.common.Source;
|
26
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
27
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
28
|
|
29
|
/**
|
30
|
* @author a.babadshanjan
|
31
|
* @created 11.05.2009
|
32
|
* @version 1.0
|
33
|
*/
|
34
|
public abstract class FaunaEuropaeaImportBase extends CdmImportBase<FaunaEuropaeaImportConfigurator, FaunaEuropaeaImportState>
|
35
|
implements ICdmImport<FaunaEuropaeaImportConfigurator,FaunaEuropaeaImportState> {
|
36
|
private static final Logger logger = Logger.getLogger(FaunaEuropaeaImportBase.class);
|
37
|
|
38
|
// /* Max number of taxa to retrieve (for test purposes) */
|
39
|
// protected static final int maxTaxa = 1000;
|
40
|
// /* Max number of taxa to be saved with one service call */
|
41
|
// protected int limit = 20000; // TODO: Make configurable
|
42
|
// /* Interval for progress info message when retrieving taxa */
|
43
|
// protected static final int modCount = 10000;
|
44
|
// /* Highest taxon index in the FauEu database */
|
45
|
// protected int highestTaxonIndex = 0;
|
46
|
|
47
|
protected boolean resultSetHasColumn(ResultSet rs, String columnName){
|
48
|
try {
|
49
|
ResultSetMetaData metaData = rs.getMetaData();
|
50
|
for (int i = 0; i < metaData.getColumnCount(); i++){
|
51
|
if (metaData.getColumnName(i + 1).equalsIgnoreCase(columnName)){
|
52
|
return true;
|
53
|
}
|
54
|
}
|
55
|
return false;
|
56
|
} catch (SQLException e) {
|
57
|
logger.warn("Exception in resultSetHasColumn");
|
58
|
return false;
|
59
|
}
|
60
|
}
|
61
|
|
62
|
protected boolean checkSqlServerColumnExists(Source source, String tableName, String columnName){
|
63
|
String strQuery = "SELECT Count(t.id) as n " +
|
64
|
" FROM sysobjects AS t " +
|
65
|
" INNER JOIN syscolumns AS c ON t.id = c.id " +
|
66
|
" WHERE (t.xtype = 'U') AND " +
|
67
|
" (t.name = '" + tableName + "') AND " +
|
68
|
" (c.name = '" + columnName + "')";
|
69
|
ResultSet rs = source.getResultSet(strQuery) ;
|
70
|
int n;
|
71
|
try {
|
72
|
rs.next();
|
73
|
n = rs.getInt("n");
|
74
|
return n>0;
|
75
|
} catch (SQLException e) {
|
76
|
e.printStackTrace();
|
77
|
return false;
|
78
|
}
|
79
|
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Returns a map that holds all values of a ResultSet. This is needed if a value needs to
|
84
|
* be accessed twice
|
85
|
* @param rs
|
86
|
* @return
|
87
|
* @throws SQLException
|
88
|
*/
|
89
|
protected Map<String, Object> getValueMap(ResultSet rs) throws SQLException{
|
90
|
try{
|
91
|
Map<String, Object> valueMap = new HashMap<String, Object>();
|
92
|
int colCount = rs.getMetaData().getColumnCount();
|
93
|
for (int c = 0; c < colCount ; c++){
|
94
|
Object value = rs.getObject(c+1);
|
95
|
String label = rs.getMetaData().getColumnLabel(c+1).toLowerCase();
|
96
|
if (value != null && ! CdmUtils.Nz(value.toString()).trim().equals("")){
|
97
|
valueMap.put(label, value);
|
98
|
}
|
99
|
}
|
100
|
return valueMap;
|
101
|
}catch(SQLException e){
|
102
|
throw e;
|
103
|
}
|
104
|
}
|
105
|
|
106
|
|
107
|
/**
|
108
|
* @param state
|
109
|
* @param sourceRef
|
110
|
*/
|
111
|
protected Classification getClassificationFor(FaunaEuropaeaImportState state, Reference<?> sourceRef) {
|
112
|
|
113
|
Classification tree;
|
114
|
UUID treeUuid = state.getTreeUuid(sourceRef);
|
115
|
if (treeUuid == null){
|
116
|
if(logger.isInfoEnabled()) { logger.info(".. creating new classification"); }
|
117
|
|
118
|
TransactionStatus txStatus = startTransaction();
|
119
|
tree = makeTreeMemSave(state, sourceRef);
|
120
|
commitTransaction(txStatus);
|
121
|
|
122
|
} else {
|
123
|
tree = getClassificationService().find(treeUuid);
|
124
|
}
|
125
|
return tree;
|
126
|
}
|
127
|
|
128
|
}
|