Project

General

Profile

Download (4.52 KB) Statistics
| Branch: | Tag: | 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.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
import java.util.UUID;
19

    
20
import org.apache.log4j.Logger;
21
import org.springframework.transaction.TransactionStatus;
22

    
23
import eu.etaxonomy.cdm.common.CdmUtils;
24
import eu.etaxonomy.cdm.io.common.CdmImportBase;
25
import eu.etaxonomy.cdm.io.common.CdmIoBase;
26
import eu.etaxonomy.cdm.io.common.ICdmIO;
27
import eu.etaxonomy.cdm.io.common.ICdmImport;
28
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
29
import eu.etaxonomy.cdm.io.common.ImportHelper;
30
import eu.etaxonomy.cdm.io.common.MapWrapper;
31
import eu.etaxonomy.cdm.io.common.Source;
32
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
33
import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
34
import eu.etaxonomy.cdm.model.common.Annotation;
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
37
import eu.etaxonomy.cdm.model.common.Language;
38
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39
import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
40

    
41
/**
42
 * @author a.babadshanjan
43
 * @created 11.05.2009
44
 * @version 1.0
45
 */
46
public abstract class FaunaEuropaeaImportBase extends CdmImportBase<FaunaEuropaeaImportConfigurator, FaunaEuropaeaImportState> 
47
implements ICdmImport<FaunaEuropaeaImportConfigurator,FaunaEuropaeaImportState> {
48
	private static final Logger logger = Logger.getLogger(FaunaEuropaeaImportBase.class);
49
	
50
//	/* Max number of taxa to retrieve (for test purposes) */
51
//	protected static final int maxTaxa = 1000;
52
//	/* Max number of taxa to be saved with one service call */
53
//	protected int limit = 20000; // TODO: Make configurable
54
//	/* Interval for progress info message when retrieving taxa */
55
//	protected static final int modCount = 10000;
56
//	/* Highest taxon index in the FauEu database */
57
//	protected int highestTaxonIndex = 0;
58
	
59
	protected boolean resultSetHasColumn(ResultSet rs, String columnName){
60
		try {
61
			ResultSetMetaData metaData = rs.getMetaData();
62
			for (int i = 0; i < metaData.getColumnCount(); i++){
63
				if (metaData.getColumnName(i + 1).equalsIgnoreCase(columnName)){
64
					return true;
65
				}
66
			}
67
			return false;
68
		} catch (SQLException e) {
69
            logger.warn("Exception in resultSetHasColumn");
70
            return false;
71
		}
72
	}
73
	
74
	protected boolean checkSqlServerColumnExists(Source source, String tableName, String columnName){
75
		String strQuery = "SELECT  Count(t.id) as n " +
76
				" FROM sysobjects AS t " +
77
				" INNER JOIN syscolumns AS c ON t.id = c.id " +
78
				" WHERE (t.xtype = 'U') AND " + 
79
				" (t.name = '" + tableName + "') AND " + 
80
				" (c.name = '" + columnName + "')";
81
		ResultSet rs = source.getResultSet(strQuery) ;		
82
		int n;
83
		try {
84
			rs.next();
85
			n = rs.getInt("n");
86
			return n>0;
87
		} catch (SQLException e) {
88
			e.printStackTrace();
89
			return false;
90
		}
91
		
92
	}
93
	
94
	/**
95
	 * Returns a map that holds all values of a ResultSet. This is needed if a value needs to
96
	 * be accessed twice
97
	 * @param rs
98
	 * @return
99
	 * @throws SQLException
100
	 */
101
	protected Map<String, Object> getValueMap(ResultSet rs) throws SQLException{
102
		try{
103
			Map<String, Object> valueMap = new HashMap<String, Object>();
104
			int colCount = rs.getMetaData().getColumnCount();
105
			for (int c = 0; c < colCount ; c++){
106
				Object value = rs.getObject(c+1);
107
				String label = rs.getMetaData().getColumnLabel(c+1).toLowerCase();
108
				if (value != null && ! CdmUtils.Nz(value.toString()).trim().equals("")){
109
					valueMap.put(label, value);
110
				}
111
			}
112
			return valueMap;
113
		}catch(SQLException e){
114
			throw e;
115
		}
116
	}
117
	
118

    
119
	/**
120
	 * @param state
121
	 * @param sourceRef
122
	 */
123
	protected TaxonomicTree getTaxonomicTreeFor(FaunaEuropaeaImportState state, ReferenceBase<?> sourceRef) {
124
		
125
		TaxonomicTree tree;
126
		UUID treeUuid = state.getTreeUuid(sourceRef);
127
		if (treeUuid == null){
128
			if(logger.isInfoEnabled()) { logger.info(".. creating new taxonomic tree"); }
129
			
130
			TransactionStatus txStatus = startTransaction();
131
			tree = makeTreeMemSave(state, sourceRef);
132
			commitTransaction(txStatus);
133
			
134
		} else {
135
			tree = getTaxonTreeService().getTaxonomicTreeByUuid(treeUuid);
136
		}
137
		return tree;
138
	}
139

    
140
}
(6-6/15)