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
|
import java.util.regex.Matcher;
|
19
|
import java.util.regex.Pattern;
|
20
|
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.springframework.transaction.TransactionStatus;
|
23
|
|
24
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
25
|
import eu.etaxonomy.cdm.io.common.CdmImportBase;
|
26
|
import eu.etaxonomy.cdm.io.common.ICdmImport;
|
27
|
import eu.etaxonomy.cdm.io.common.Source;
|
28
|
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
|
29
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
30
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
31
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
32
|
|
33
|
/**
|
34
|
* @author a.babadshanjan
|
35
|
* @created 11.05.2009
|
36
|
* @version 1.0
|
37
|
*/
|
38
|
public abstract class FaunaEuropaeaImportBase extends CdmImportBase<FaunaEuropaeaImportConfigurator, FaunaEuropaeaImportState>
|
39
|
implements ICdmImport<FaunaEuropaeaImportConfigurator,FaunaEuropaeaImportState> {
|
40
|
private static final Logger logger = Logger.getLogger(FaunaEuropaeaImportBase.class);
|
41
|
|
42
|
// /* Max number of taxa to retrieve (for test purposes) */
|
43
|
// protected static final int maxTaxa = 1000;
|
44
|
// /* Max number of taxa to be saved with one service call */
|
45
|
// protected int limit = 20000; // TODO: Make configurable
|
46
|
// /* Interval for progress info message when retrieving taxa */
|
47
|
// protected static final int modCount = 10000;
|
48
|
// /* Highest taxon index in the FauEu database */
|
49
|
// protected int highestTaxonIndex = 0;
|
50
|
|
51
|
protected boolean resultSetHasColumn(ResultSet rs, String columnName){
|
52
|
try {
|
53
|
ResultSetMetaData metaData = rs.getMetaData();
|
54
|
for (int i = 0; i < metaData.getColumnCount(); i++){
|
55
|
if (metaData.getColumnName(i + 1).equalsIgnoreCase(columnName)){
|
56
|
return true;
|
57
|
}
|
58
|
}
|
59
|
return false;
|
60
|
} catch (SQLException e) {
|
61
|
logger.warn("Exception in resultSetHasColumn");
|
62
|
return false;
|
63
|
}
|
64
|
}
|
65
|
|
66
|
protected boolean checkSqlServerColumnExists(Source source, String tableName, String columnName){
|
67
|
String strQuery = "SELECT Count(t.id) as n " +
|
68
|
" FROM sysobjects AS t " +
|
69
|
" INNER JOIN syscolumns AS c ON t.id = c.id " +
|
70
|
" WHERE (t.xtype = 'U') AND " +
|
71
|
" (t.name = '" + tableName + "') AND " +
|
72
|
" (c.name = '" + columnName + "')";
|
73
|
ResultSet rs = source.getResultSet(strQuery) ;
|
74
|
int n;
|
75
|
try {
|
76
|
rs.next();
|
77
|
n = rs.getInt("n");
|
78
|
return n>0;
|
79
|
} catch (SQLException e) {
|
80
|
e.printStackTrace();
|
81
|
return false;
|
82
|
}
|
83
|
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* Returns a map that holds all values of a ResultSet. This is needed if a value needs to
|
88
|
* be accessed twice
|
89
|
* @param rs
|
90
|
* @return
|
91
|
* @throws SQLException
|
92
|
*/
|
93
|
protected Map<String, Object> getValueMap(ResultSet rs) throws SQLException{
|
94
|
try{
|
95
|
Map<String, Object> valueMap = new HashMap<String, Object>();
|
96
|
int colCount = rs.getMetaData().getColumnCount();
|
97
|
for (int c = 0; c < colCount ; c++){
|
98
|
Object value = rs.getObject(c+1);
|
99
|
String label = rs.getMetaData().getColumnLabel(c+1).toLowerCase();
|
100
|
if (value != null && ! CdmUtils.Nz(value.toString()).trim().equals("")){
|
101
|
valueMap.put(label, value);
|
102
|
}
|
103
|
}
|
104
|
return valueMap;
|
105
|
}catch(SQLException e){
|
106
|
throw e;
|
107
|
}
|
108
|
}
|
109
|
|
110
|
|
111
|
/**
|
112
|
* @param state
|
113
|
* @param sourceRef
|
114
|
*/
|
115
|
protected Classification getClassificationFor(FaunaEuropaeaImportState state, Reference<?> sourceRef) {
|
116
|
|
117
|
Classification tree;
|
118
|
UUID treeUuid = state.getTreeUuid(sourceRef);
|
119
|
if (treeUuid == null){
|
120
|
if(logger.isInfoEnabled()) { logger.info(".. creating new classification"); }
|
121
|
|
122
|
TransactionStatus txStatus = startTransaction();
|
123
|
tree = makeTreeMemSave(state, sourceRef);
|
124
|
commitTransaction(txStatus);
|
125
|
|
126
|
} else {
|
127
|
tree = getClassificationService().find(treeUuid);
|
128
|
}
|
129
|
return tree;
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* Returns whether a regular expression is found in a given target string.
|
134
|
* @param regEx
|
135
|
* @param targetString
|
136
|
* @return
|
137
|
*/
|
138
|
protected static boolean expressionMatches(String regEx, String targetString) {
|
139
|
|
140
|
Matcher matcher = createMatcher(regEx, targetString);
|
141
|
if (matcher == null) {
|
142
|
return false;
|
143
|
}
|
144
|
if (matcher.find()) {
|
145
|
return true;
|
146
|
} else {
|
147
|
return false;
|
148
|
}
|
149
|
}
|
150
|
|
151
|
protected static int expressionEnd(String regEx, String targetString){
|
152
|
Matcher matcher = createMatcher(regEx, targetString);
|
153
|
if (matcher == null) {
|
154
|
return -1;
|
155
|
}
|
156
|
if (matcher.find()){
|
157
|
return matcher.end();
|
158
|
} else {
|
159
|
return -1;
|
160
|
}
|
161
|
}
|
162
|
|
163
|
private static Matcher createMatcher (String regEx, String targetString){
|
164
|
if (targetString == null) {
|
165
|
return null;
|
166
|
}
|
167
|
Pattern pattern = Pattern.compile(regEx);
|
168
|
Matcher matcher = pattern.matcher(targetString);
|
169
|
return matcher;
|
170
|
}
|
171
|
|
172
|
protected MarkerType getUuidMarkerType(UUID uuid, FaunaEuropaeaImportState state){
|
173
|
if (uuid == null){
|
174
|
uuid = UUID.randomUUID();
|
175
|
}
|
176
|
|
177
|
MarkerType markerType = state.getMarkerType(uuid);
|
178
|
if (markerType == null){
|
179
|
markerType = (MarkerType)getTermService().find(uuid);
|
180
|
if (markerType == null){
|
181
|
if (uuid.equals(PesiTransformer.uuidMarkerGuidIsMissing)){
|
182
|
markerType = MarkerType.NewInstance("Uuid is Missing", "Uuid is missing", null);
|
183
|
markerType.setUuid(uuid);
|
184
|
} else if (uuid.equals(PesiTransformer.uuidMarkerTypeHasNoLastAction)){
|
185
|
markerType = MarkerType.NewInstance("Has no last Action", "Has no last action", null);
|
186
|
markerType.setUuid(uuid);
|
187
|
}
|
188
|
markerType = (MarkerType)getTermService().save(markerType);
|
189
|
}
|
190
|
}
|
191
|
|
192
|
state.putMarkerType(markerType);
|
193
|
return markerType;
|
194
|
}
|
195
|
|
196
|
}
|