Project

General

Profile

Download (4.95 KB) Statistics
| Branch: | Revision:
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.model.reference.Reference;
29
import eu.etaxonomy.cdm.model.taxon.Classification;
30

    
31
/**
32
 * @author a.babadshanjan
33
 * @created 11.05.2009
34
 * @version 1.0
35
 */
36
public abstract class FaunaEuropaeaImportBase extends CdmImportBase<FaunaEuropaeaImportConfigurator, FaunaEuropaeaImportState>
37
implements ICdmImport<FaunaEuropaeaImportConfigurator,FaunaEuropaeaImportState> {
38
	private static final Logger logger = Logger.getLogger(FaunaEuropaeaImportBase.class);
39

    
40
//	/* Max number of taxa to retrieve (for test purposes) */
41
//	protected static final int maxTaxa = 1000;
42
//	/* Max number of taxa to be saved with one service call */
43
//	protected int limit = 20000; // TODO: Make configurable
44
//	/* Interval for progress info message when retrieving taxa */
45
//	protected static final int modCount = 10000;
46
//	/* Highest taxon index in the FauEu database */
47
//	protected int highestTaxonIndex = 0;
48

    
49
	protected boolean resultSetHasColumn(ResultSet rs, String columnName){
50
		try {
51
			ResultSetMetaData metaData = rs.getMetaData();
52
			for (int i = 0; i < metaData.getColumnCount(); i++){
53
				if (metaData.getColumnName(i + 1).equalsIgnoreCase(columnName)){
54
					return true;
55
				}
56
			}
57
			return false;
58
		} catch (SQLException e) {
59
            logger.warn("Exception in resultSetHasColumn");
60
            return false;
61
		}
62
	}
63

    
64
	protected boolean checkSqlServerColumnExists(Source source, String tableName, String columnName){
65
		String strQuery = "SELECT  Count(t.id) as n " +
66
				" FROM sysobjects AS t " +
67
				" INNER JOIN syscolumns AS c ON t.id = c.id " +
68
				" WHERE (t.xtype = 'U') AND " +
69
				" (t.name = '" + tableName + "') AND " +
70
				" (c.name = '" + columnName + "')";
71
		ResultSet rs = source.getResultSet(strQuery) ;
72
		int n;
73
		try {
74
			rs.next();
75
			n = rs.getInt("n");
76
			return n>0;
77
		} catch (SQLException e) {
78
			e.printStackTrace();
79
			return false;
80
		}
81

    
82
	}
83

    
84
	/**
85
	 * Returns a map that holds all values of a ResultSet. This is needed if a value needs to
86
	 * be accessed twice
87
	 * @param rs
88
	 * @return
89
	 * @throws SQLException
90
	 */
91
	protected Map<String, Object> getValueMap(ResultSet rs) throws SQLException{
92
		try{
93
			Map<String, Object> valueMap = new HashMap<String, Object>();
94
			int colCount = rs.getMetaData().getColumnCount();
95
			for (int c = 0; c < colCount ; c++){
96
				Object value = rs.getObject(c+1);
97
				String label = rs.getMetaData().getColumnLabel(c+1).toLowerCase();
98
				if (value != null && ! CdmUtils.Nz(value.toString()).trim().equals("")){
99
					valueMap.put(label, value);
100
				}
101
			}
102
			return valueMap;
103
		}catch(SQLException e){
104
			throw e;
105
		}
106
	}
107

    
108

    
109
	/**
110
	 * @param state
111
	 * @param sourceRef
112
	 */
113
	protected Classification getClassificationFor(FaunaEuropaeaImportState state, Reference<?> sourceRef) {
114

    
115
		Classification tree;
116
		UUID treeUuid = state.getTreeUuid(sourceRef);
117
		if (treeUuid == null){
118
			if(logger.isInfoEnabled()) { logger.info(".. creating new classification"); }
119

    
120
			TransactionStatus txStatus = startTransaction();
121
			tree = makeTreeMemSave(state, sourceRef);
122
			commitTransaction(txStatus);
123

    
124
		} else {
125
			tree = getClassificationService().find(treeUuid);
126
		}
127
		return tree;
128
	}
129

    
130
	/**
131
	 * Returns whether a regular expression is found in a given target string.
132
	 * @param regEx
133
	 * @param targetString
134
	 * @return
135
	 */
136
	protected static boolean expressionMatches(String regEx, String targetString) {
137

    
138
		Matcher matcher = createMatcher(regEx, targetString);
139
		if (matcher == null) {
140
            return false;
141
        }
142
		if (matcher.find()) {
143
			return true;
144
		} else {
145
			return false;
146
		}
147
	}
148

    
149
	protected static int expressionEnd(String regEx, String targetString){
150
		Matcher matcher = createMatcher(regEx, targetString);
151
		if (matcher == null) {
152
            return -1;
153
        }
154
		if (matcher.find()){
155
			return matcher.end();
156
		} else {
157
            return -1;
158
        }
159
	}
160

    
161
	private static Matcher createMatcher (String regEx, String targetString){
162
		if (targetString == null) {
163
			return null;
164
		}
165
		Pattern pattern = Pattern.compile(regEx);
166
		Matcher matcher = pattern.matcher(targetString);
167
		return matcher;
168
	}
169

    
170

    
171

    
172
}
(7-7/20)