Fauna Europaea Import
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / faunaEuropaea / FaunaEuropaeaImportBase.java
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
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.CdmIoBase;
25 import eu.etaxonomy.cdm.io.common.ICdmIO;
26 import eu.etaxonomy.cdm.io.common.ICdmImport;
27 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
28 import eu.etaxonomy.cdm.io.common.ImportHelper;
29 import eu.etaxonomy.cdm.io.common.MapWrapper;
30 import eu.etaxonomy.cdm.io.common.Source;
31 import eu.etaxonomy.cdm.model.common.Annotation;
32 import eu.etaxonomy.cdm.model.common.CdmBase;
33 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
34 import eu.etaxonomy.cdm.model.common.Language;
35 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
36
37 /**
38 * @author a.babadshanjan
39 * @created 11.05.2009
40 * @version 1.0
41 */
42 public abstract class FaunaEuropaeaImportBase extends CdmImportBase<FaunaEuropaeaImportConfigurator, FaunaEuropaeaImportState>
43 implements ICdmImport<FaunaEuropaeaImportConfigurator,FaunaEuropaeaImportState> {
44 private static final Logger logger = Logger.getLogger(FaunaEuropaeaImportBase.class);
45
46 // /* Max number of taxa to retrieve (for test purposes) */
47 // protected static final int maxTaxa = 1000;
48 // /* Max number of taxa to be saved with one service call */
49 // protected int limit = 20000; // TODO: Make configurable
50 // /* Interval for progress info message when retrieving taxa */
51 // protected static final int modCount = 10000;
52 // /* Highest taxon index in the FauEu database */
53 // protected int highestTaxonIndex = 0;
54
55 protected boolean resultSetHasColumn(ResultSet rs, String columnName){
56 try {
57 ResultSetMetaData metaData = rs.getMetaData();
58 for (int i = 0; i < metaData.getColumnCount(); i++){
59 if (metaData.getColumnName(i + 1).equalsIgnoreCase(columnName)){
60 return true;
61 }
62 }
63 return false;
64 } catch (SQLException e) {
65 logger.warn("Exception in resultSetHasColumn");
66 return false;
67 }
68 }
69
70 protected boolean checkSqlServerColumnExists(Source source, String tableName, String columnName){
71 String strQuery = "SELECT Count(t.id) as n " +
72 " FROM sysobjects AS t " +
73 " INNER JOIN syscolumns AS c ON t.id = c.id " +
74 " WHERE (t.xtype = 'U') AND " +
75 " (t.name = '" + tableName + "') AND " +
76 " (c.name = '" + columnName + "')";
77 ResultSet rs = source.getResultSet(strQuery) ;
78 int n;
79 try {
80 rs.next();
81 n = rs.getInt("n");
82 return n>0;
83 } catch (SQLException e) {
84 e.printStackTrace();
85 return false;
86 }
87
88 }
89
90 /**
91 * Returns a map that holds all values of a ResultSet. This is needed if a value needs to
92 * be accessed twice
93 * @param rs
94 * @return
95 * @throws SQLException
96 */
97 protected Map<String, Object> getValueMap(ResultSet rs) throws SQLException{
98 try{
99 Map<String, Object> valueMap = new HashMap<String, Object>();
100 int colCount = rs.getMetaData().getColumnCount();
101 for (int c = 0; c < colCount ; c++){
102 Object value = rs.getObject(c+1);
103 String label = rs.getMetaData().getColumnLabel(c+1).toLowerCase();
104 if (value != null && ! CdmUtils.Nz(value.toString()).trim().equals("")){
105 valueMap.put(label, value);
106 }
107 }
108 return valueMap;
109 }catch(SQLException e){
110 throw e;
111 }
112 }
113
114
115 protected boolean saveTaxa(Map<String, MapWrapper<? extends CdmBase>> stores,
116 int highestTaxonIndex, int limit) {
117
118 MapWrapper<TaxonBase> taxonStore = (MapWrapper<TaxonBase>)stores.get(ICdmIO.TAXON_STORE);
119
120 int n = 0;
121 int nbrOfTaxa = highestTaxonIndex;
122 // int nbrOfTaxa = taxonStore.size();
123 boolean success = true;
124
125 if(logger.isInfoEnabled()) { logger.info("Saving taxa ..."); }
126
127 if (nbrOfTaxa < limit) { // TODO: test with critical values
128 limit = nbrOfTaxa;
129 } else {
130 n = nbrOfTaxa / limit;
131 }
132
133 if(logger.isInfoEnabled()) {
134 logger.info("number of taxa = " + taxonStore.size()
135 + ", highest taxon index = " + highestTaxonIndex
136 + ", limit = " + limit
137 + ", n = " + n);
138 }
139
140 // save taxa in blocks of <=limit
141
142 for (int j = 1; j <= n + 1; j++)
143 {
144 int offset = j - 1;
145 int start = offset * limit;
146
147 if(logger.isInfoEnabled()) {
148 logger.info("Saving taxa: " + start + " - " + (start + limit - 1));
149 }
150
151 if(logger.isInfoEnabled()) {
152 logger.info("index = " + j
153 + ", offset = " + offset
154 + ", start = " + start);
155 }
156
157 if (j == n + 1) {
158 limit = nbrOfTaxa - n * limit;
159 if(logger.isInfoEnabled()) {
160 logger.info("n = " + n + ", limit = " + limit);
161 }
162 }
163
164 TransactionStatus txStatus = startTransaction();
165
166 Collection<TaxonBase> taxonMapPart = taxonStore.objects(start, limit);
167 getTaxonService().saveTaxonAll(taxonMapPart);
168 taxonMapPart = null;
169 taxonStore.removeObjects(start, limit);
170
171 commitTransaction(txStatus);
172
173 }
174
175 return success;
176 }
177 }