Project

General

Profile

Download (6.07 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.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
 * @since 11.05.2009
36
 */
37
public abstract class FaunaEuropaeaImportBase
38
        extends CdmImportBase<FaunaEuropaeaImportConfigurator, FaunaEuropaeaImportState>
39
        implements ICdmImport<FaunaEuropaeaImportConfigurator,FaunaEuropaeaImportState> {
40

    
41
    private static final Logger logger = Logger.getLogger(FaunaEuropaeaImportBase.class);
42

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

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

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

    
85
	}
86

    
87
	/**
88
	 * Returns a map that holds all values of a ResultSet. This is needed if a value needs to
89
	 * be accessed twice
90
	 * @param rs
91
	 * @return
92
	 * @throws SQLException
93
	 */
94
	protected Map<String, Object> getValueMap(ResultSet rs) throws SQLException{
95
		try{
96
			Map<String, Object> valueMap = new HashMap<String, Object>();
97
			int colCount = rs.getMetaData().getColumnCount();
98
			for (int c = 0; c < colCount ; c++){
99
				Object value = rs.getObject(c+1);
100
				String label = rs.getMetaData().getColumnLabel(c+1).toLowerCase();
101
				if (value != null && ! CdmUtils.Nz(value.toString()).trim().equals("")){
102
					valueMap.put(label, value);
103
				}
104
			}
105
			return valueMap;
106
		}catch(SQLException e){
107
			throw e;
108
		}
109
	}
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
	/**
129
	 * Returns whether a regular expression is found in a given target string.
130
	 * @param regEx
131
	 * @param targetString
132
	 * @return
133
	 */
134
	protected static boolean expressionMatches(String regEx, String targetString) {
135

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

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

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

    
168
	protected MarkerType getUuidMarkerType(UUID uuid, FaunaEuropaeaImportState state){
169
        if (uuid == null){
170
            uuid = UUID.randomUUID();
171
        }
172

    
173
        MarkerType markerType = state.getMarkerType(uuid);
174
            if (markerType == null){
175
                markerType = (MarkerType)getTermService().find(uuid);
176
                if (markerType == null){
177
                    if (uuid.equals(PesiTransformer.uuidMarkerGuidIsMissing)){
178
                        markerType = MarkerType.NewInstance("Uuid is Missing", "Uuid is missing", null);
179
                        markerType.setUuid(uuid);
180
                    } else if (uuid.equals(PesiTransformer.uuidMarkerTypeHasNoLastAction)){
181
                        markerType = MarkerType.NewInstance("Has no last Action", "Has no last action", null);
182
                        markerType.setUuid(uuid);
183
                    }
184
                    markerType = (MarkerType)getTermService().save(markerType);
185
                }
186
            }
187

    
188
            state.putMarkerType(markerType);
189
            return markerType;
190
        }
191

    
192
}
(7-7/20)