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
;
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
;
47 * @author a.babadshanjan
52 public class FaunaEuropaeaDistributionImport
extends FaunaEuropaeaImportBase
{
53 private static final Logger logger
= Logger
.getLogger(FaunaEuropaeaDistributionImport
.class);
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
60 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
63 protected boolean doCheck(FaunaEuropaeaImportState state
) {
64 boolean result
= true;
65 FaunaEuropaeaImportConfigurator fauEuConfig
= state
.getConfig();
66 logger
.warn("Checking for Distributions not yet fully implemented");
67 result
&= checkReferenceStatus(fauEuConfig
);
72 private boolean checkReferenceStatus(FaunaEuropaeaImportConfigurator fauEuConfig
) {
73 boolean result
= true;
75 Source source
= fauEuConfig
.getSource();
77 ResultSet rs
= source
.getResultSet(sqlStr
);
79 // } catch (SQLException e) {
80 // e.printStackTrace();
86 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
89 protected boolean doInvoke(FaunaEuropaeaImportState state
) {
91 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
= state
.getStores();
92 MapWrapper
<TaxonBase
> taxonStore
= (MapWrapper
<TaxonBase
>)stores
.get(ICdmIO
.TAXON_STORE
);
94 //make not needed maps empty
95 // MapWrapper<TeamOrPersonBase<?>> authorStore = (MapWrapper<TeamOrPersonBase<?>>)stores.get(ICdmIO.TEAM_STORE);
96 // authorMap.makeEmpty();
98 Set
<TaxonBase
> taxonMap
= new HashSet
<TaxonBase
>();
100 FaunaEuropaeaImportConfigurator fauEuConfig
= state
.getConfig();
101 Source source
= fauEuConfig
.getSource();
103 String namespace
= "Distribution";
104 boolean success
= true;
106 if(logger
.isInfoEnabled()) { logger
.info("Start making Distributions..."); }
110 " SELECT distribution.*, Area.* " +
111 " FROM distribution INNER JOIN Area ON distribution.dis_ara_id = Area.ara_id " +
114 ResultSet rs
= source
.getResultSet(strQuery
) ;
119 if ((i
++ % modCount
) == 0 && i
!= 1 ) {
120 if(logger
.isInfoEnabled()) {
121 logger
.info("Distributions handled: " + (i
-1));
125 int disId
= rs
.getInt("dis_id");
126 int taxonId
= rs
.getInt("dis_tax_id");
127 int occStatusId
= rs
.getInt("ara_id");
129 TaxonBase
<?
> taxonBase
= taxonStore
.get(taxonId
);
132 if (taxonBase
!= null) {
134 if (taxonBase
instanceof Taxon
) {
135 taxon
= (Taxon
)taxonBase
;
137 logger
.warn("TaxonBase (" + taxonId
+ " is not of type Taxon but: "
138 + taxonBase
.getClass().getSimpleName());
142 TaxonDescription taxonDescription
;
143 Set
<TaxonDescription
> descriptionSet
= taxon
.getDescriptions();
144 if (descriptionSet
.size() > 0) {
145 taxonDescription
= descriptionSet
.iterator().next();
147 taxonDescription
= TaxonDescription
.NewInstance();
148 taxon
.addDescription(taxonDescription
);
151 PresenceAbsenceTermBase
<?
> presenceAbsenceStatus
152 = FaunaEuropaeaTransformer
.occStatus2PresenceAbsence(occStatusId
);
153 NamedArea namedArea
= FaunaEuropaeaTransformer
.areaId2TdwgArea(rs
);
155 Distribution newDistribution
= Distribution
.NewInstance(namedArea
, presenceAbsenceStatus
);
156 taxonDescription
.addElement(newDistribution
);
158 //ImportHelper.setOriginalSource(reference, fauEuConfig.getSourceReference(), disId, namespace);
160 // if (!refStore.containsId(refId)) {
161 // if (reference == null) {
162 // logger.warn("Reference is null");
164 // refStore.put(refId, reference);
166 // logger.warn("Reference with duplicated ref_id (" + refId +
167 // ") not imported.");
172 } catch (Exception e
) {
173 logger
.warn("An exception occurred when creating distribution with id " + disId
+
174 ". Reference could not be saved.");
178 if(logger
.isInfoEnabled()) { logger
.info("Saving distributions ..."); }
181 getTaxonService().saveTaxonAll(taxonMap
);
183 if(logger
.isInfoEnabled()) { logger
.info("End making distributions..."); }
187 } catch (SQLException e
) {
188 logger
.error("SQLException:" + e
);
195 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
197 protected boolean isIgnore(FaunaEuropaeaImportState state
){
198 return !state
.getConfig().isDoOccurrence();