2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.faunaEuropaea
;
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
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
22 import org
.springframework
.transaction
.TransactionStatus
;
24 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
25 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
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
.agent
.TeamOrPersonBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
33 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
34 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTermBase
;
35 import eu
.etaxonomy
.cdm
.model
.description
.PresenceTerm
;
36 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
37 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
38 import eu
.etaxonomy
.cdm
.model
.location
.TdwgArea
;
39 import eu
.etaxonomy
.cdm
.model
.reference
.Generic
;
40 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
41 import eu
.etaxonomy
.cdm
.model
.reference
.StrictReferenceBase
;
42 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
44 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
48 * @author a.babadshanjan
53 public class FaunaEuropaeaDistributionImport
extends FaunaEuropaeaImportBase
{
54 private static final Logger logger
= Logger
.getLogger(FaunaEuropaeaDistributionImport
.class);
56 private int modCount
= 10000;
57 /* Max number of references to be saved with one service call */
58 private int limit
= 20000; // TODO: Make configurable
61 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
64 protected boolean doCheck(FaunaEuropaeaImportState state
) {
65 boolean result
= true;
66 FaunaEuropaeaImportConfigurator fauEuConfig
= state
.getConfig();
67 logger
.warn("Checking for Distributions not yet fully implemented");
68 result
&= checkReferenceStatus(fauEuConfig
);
73 private boolean checkReferenceStatus(FaunaEuropaeaImportConfigurator fauEuConfig
) {
74 boolean result
= true;
76 Source source
= fauEuConfig
.getSource();
78 ResultSet rs
= source
.getResultSet(sqlStr
);
80 // } catch (SQLException e) {
81 // e.printStackTrace();
87 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
90 protected boolean doInvoke(FaunaEuropaeaImportState state
) {
92 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
= state
.getStores();
93 MapWrapper
<TaxonBase
> taxonStore
= (MapWrapper
<TaxonBase
>)stores
.get(ICdmIO
.TAXON_STORE
);
94 TransactionStatus txStatus
= null;
96 //make not needed maps empty
97 // MapWrapper<TeamOrPersonBase<?>> authorStore = (MapWrapper<TeamOrPersonBase<?>>)stores.get(ICdmIO.TEAM_STORE);
98 // authorMap.makeEmpty();
100 Set
<TaxonBase
> taxonMap
= new HashSet
<TaxonBase
>();
102 FaunaEuropaeaImportConfigurator fauEuConfig
= state
.getConfig();
103 Source source
= fauEuConfig
.getSource();
105 String namespace
= "Distribution";
106 boolean success
= true;
108 if(logger
.isInfoEnabled()) { logger
.info("Start making Distributions..."); }
112 " SELECT distribution.*, Area.* " +
113 " FROM distribution INNER JOIN Area ON distribution.dis_ara_id = Area.ara_id " +
116 ResultSet rs
= source
.getResultSet(strQuery
) ;
121 if ((i
++ % modCount
) == 0 && i
!= 1 ) {
122 if(logger
.isInfoEnabled()) {
123 logger
.info("Distributions handled: " + (i
-1));
127 int disId
= rs
.getInt("dis_id");
128 int taxonId
= rs
.getInt("dis_tax_id");
129 int occStatusId
= rs
.getInt("dis_present");
131 TaxonBase
<?
> taxonBase
= taxonStore
.get(taxonId
);
134 if (taxonBase
!= null) {
136 if (taxonBase
instanceof Taxon
) {
137 taxon
= (Taxon
)taxonBase
;
139 logger
.warn("TaxonBase (" + taxonId
+ " is not of type Taxon but: "
140 + taxonBase
.getClass().getSimpleName());
144 TaxonDescription taxonDescription
;
145 Set
<TaxonDescription
> descriptionSet
= taxon
.getDescriptions();
146 if (descriptionSet
.size() > 0) {
147 taxonDescription
= descriptionSet
.iterator().next();
149 taxonDescription
= TaxonDescription
.NewInstance();
150 taxon
.addDescription(taxonDescription
);
153 PresenceAbsenceTermBase
<?
> presenceAbsenceStatus
154 = FaunaEuropaeaTransformer
.occStatus2PresenceAbsence(occStatusId
);
155 NamedArea namedArea
= FaunaEuropaeaTransformer
.areaId2TdwgArea(rs
);
157 Distribution newDistribution
= Distribution
.NewInstance(namedArea
, presenceAbsenceStatus
);
158 taxonDescription
.addElement(newDistribution
);
160 //ImportHelper.setOriginalSource(reference, fauEuConfig.getSourceReference(), disId, namespace);
162 // if (!refStore.containsId(refId)) {
163 // if (reference == null) {
164 // logger.warn("Reference is null");
166 // refStore.put(refId, reference);
168 // logger.warn("Reference with duplicated ref_id (" + refId +
169 // ") not imported.");
174 } catch (Exception e
) {
175 logger
.warn("An exception occurred when creating distribution with id " + disId
+
176 ". Reference could not be saved.");
181 if(logger
.isInfoEnabled()) { logger
.info("Saving distributions ..."); }
183 if (state
.getConfig().isUseTransactions()) {
184 txStatus
= startTransaction();
187 success
= saveTaxa(state
, state
.getHighestTaxonIndex(), state
.getConfig().getLimitSave());
189 if (state
.getConfig().isUseTransactions()) {
190 commitTransaction(txStatus
);
193 if(logger
.isInfoEnabled()) { logger
.info("End making distributions..."); }
197 } catch (SQLException e
) {
198 logger
.error("SQLException:" + e
);
205 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
207 protected boolean isIgnore(FaunaEuropaeaImportState state
){
208 return !state
.getConfig().isDoOccurrence();