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