Fauna Europaea Import - able to save > 200.000 taxa
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / faunaEuropaea / FaunaEuropaeaDistributionImport.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.SQLException;
14 import java.util.ArrayList;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.apache.log4j.Logger;
21 import org.springframework.stereotype.Component;
22
23 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
24 import eu.etaxonomy.cdm.io.common.ICdmIO;
25 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
26 import eu.etaxonomy.cdm.io.common.ImportHelper;
27 import eu.etaxonomy.cdm.io.common.MapWrapper;
28 import eu.etaxonomy.cdm.io.common.Source;
29 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
32 import eu.etaxonomy.cdm.model.description.Distribution;
33 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
34 import eu.etaxonomy.cdm.model.description.PresenceTerm;
35 import eu.etaxonomy.cdm.model.description.TaxonDescription;
36 import eu.etaxonomy.cdm.model.location.NamedArea;
37 import eu.etaxonomy.cdm.model.location.TdwgArea;
38 import eu.etaxonomy.cdm.model.reference.Generic;
39 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
40 import eu.etaxonomy.cdm.model.reference.StrictReferenceBase;
41 import eu.etaxonomy.cdm.model.taxon.Taxon;
42 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
43 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
44
45
46 /**
47 * @author a.babadshanjan
48 * @created 12.05.2009
49 * @version 1.0
50 */
51 @Component
52 public class FaunaEuropaeaDistributionImport extends FaunaEuropaeaImportBase {
53 private static final Logger logger = Logger.getLogger(FaunaEuropaeaDistributionImport.class);
54
55 private int modCount = 10000;
56 /* Max number of references to be saved with one service call */
57 private int limit = 20000; // TODO: Make configurable
58
59 /* (non-Javadoc)
60 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
61 */
62 @Override
63 protected boolean doCheck(IImportConfigurator config) {
64 boolean result = true;
65 FaunaEuropaeaImportConfigurator fauEuConfig = (FaunaEuropaeaImportConfigurator)config;
66 logger.warn("Checking for Distributions not yet fully implemented");
67 result &= checkReferenceStatus(fauEuConfig);
68
69 return result;
70 }
71
72 private boolean checkReferenceStatus(FaunaEuropaeaImportConfigurator fauEuConfig) {
73 boolean result = true;
74 // try {
75 Source source = fauEuConfig.getSource();
76 String sqlStr = "";
77 ResultSet rs = source.getResultSet(sqlStr);
78 return result;
79 // } catch (SQLException e) {
80 // e.printStackTrace();
81 // return false;
82 // }
83 }
84
85 /* (non-Javadoc)
86 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
87 */
88 @Override
89 protected boolean doInvoke(IImportConfigurator config,
90 Map<String, MapWrapper<? extends CdmBase>> stores) {
91
92 MapWrapper<TaxonBase> taxonStore = (MapWrapper<TaxonBase>)stores.get(ICdmIO.TAXON_STORE);
93
94 //make not needed maps empty
95 // MapWrapper<TeamOrPersonBase<?>> authorStore = (MapWrapper<TeamOrPersonBase<?>>)stores.get(ICdmIO.TEAM_STORE);
96 // authorMap.makeEmpty();
97
98 Set<TaxonBase> taxonMap = new HashSet<TaxonBase>();
99
100 FaunaEuropaeaImportConfigurator fauEuConfig = (FaunaEuropaeaImportConfigurator)config;
101 Source source = fauEuConfig.getSource();
102
103 String namespace = "Distribution";
104 boolean success = true;
105
106 if(logger.isInfoEnabled()) { logger.info("Start making Distributions..."); }
107
108 try {
109 String strQuery =
110 " SELECT distribution.*, Area.* " +
111 " FROM distribution INNER JOIN Area ON distribution.dis_ara_id = Area.ara_id " +
112 " WHERE (1=1)";
113
114 ResultSet rs = source.getResultSet(strQuery) ;
115
116 int i = 0;
117 while (rs.next()) {
118
119 if ((i++ % modCount) == 0 && i!= 1 ) {
120 if(logger.isInfoEnabled()) {
121 logger.info("Distributions handled: " + (i-1));
122 }
123 }
124
125 int disId = rs.getInt("dis_id");
126 int taxonId = rs.getInt("dis_tax_id");
127 int occStatusId = rs.getInt("ara_id");
128
129 TaxonBase<?> taxonBase = taxonStore.get(taxonId);
130
131 try {
132 if (taxonBase != null) {
133 Taxon taxon;
134 if (taxonBase instanceof Taxon) {
135 taxon = (Taxon)taxonBase;
136 } else {
137 logger.warn("TaxonBase (" + taxonId + " is not of type Taxon but: "
138 + taxonBase.getClass().getSimpleName());
139 continue;
140 }
141
142 TaxonDescription taxonDescription;
143 Set<TaxonDescription> descriptionSet= taxon.getDescriptions();
144 if (descriptionSet.size() > 0) {
145 taxonDescription = descriptionSet.iterator().next();
146 } else {
147 taxonDescription = TaxonDescription.NewInstance();
148 taxon.addDescription(taxonDescription);
149 }
150
151 PresenceAbsenceTermBase<?> presenceAbsenceStatus
152 = FaunaEuropaeaTransformer.occStatus2PresenceAbsence(occStatusId);
153 NamedArea namedArea = FaunaEuropaeaTransformer.areaId2TdwgArea(rs);
154
155 Distribution newDistribution = Distribution.NewInstance(namedArea, presenceAbsenceStatus);
156 taxonDescription.addElement(newDistribution);
157
158 //ImportHelper.setOriginalSource(reference, fauEuConfig.getSourceReference(), disId, namespace);
159
160 // if (!refStore.containsId(refId)) {
161 // if (reference == null) {
162 // logger.warn("Reference is null");
163 // }
164 // refStore.put(refId, reference);
165 // } else {
166 // logger.warn("Reference with duplicated ref_id (" + refId +
167 // ") not imported.");
168 // }
169 taxonMap.add(taxon);
170 }
171
172 } catch (Exception e) {
173 logger.warn("An exception occurred when creating distribution with id " + disId +
174 ". Reference could not be saved.");
175 }
176 }
177
178 if(logger.isInfoEnabled()) { logger.info("Saving distributions ..."); }
179
180 // save taxa
181 getTaxonService().saveTaxonAll(taxonMap);
182
183 if(logger.isInfoEnabled()) { logger.info("End making distributions..."); }
184
185 return true;
186
187 } catch (SQLException e) {
188 logger.error("SQLException:" + e);
189 return false;
190 }
191 }
192
193
194 /* (non-Javadoc)
195 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
196 */
197 protected boolean isIgnore(IImportConfigurator config){
198 return (config.getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
199 }
200 }