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 static eu
.etaxonomy
.cdm
.io
.faunaEuropaea
.FaunaEuropaeaTransformer
.T_STATUS_ACCEPTED
;
13 import static eu
.etaxonomy
.cdm
.io
.faunaEuropaea
.FaunaEuropaeaTransformer
.T_STATUS_NOT_ACCEPTED
;
15 import java
.sql
.ResultSet
;
16 import java
.sql
.SQLException
;
17 import java
.util
.HashMap
;
20 import java
.util
.UUID
;
22 import org
.apache
.log4j
.Logger
;
23 import org
.springframework
.stereotype
.Component
;
24 import org
.springframework
.transaction
.TransactionStatus
;
26 import eu
.etaxonomy
.cdm
.io
.berlinModel
.CdmOneToManyMapper
;
27 import eu
.etaxonomy
.cdm
.io
.berlinModel
.CdmStringMapper
;
28 import eu
.etaxonomy
.cdm
.io
.berlinModel
.in
.BerlinModelImportConfigurator
;
29 import eu
.etaxonomy
.cdm
.io
.common
.CdmAttributeMapperBase
;
30 import eu
.etaxonomy
.cdm
.io
.common
.CdmSingleAttributeMapperBase
;
31 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
32 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
33 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
34 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
35 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
36 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
37 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
38 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
39 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
40 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
41 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
42 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
43 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
44 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
45 import eu
.etaxonomy
.cdm
.model
.reference
.Generic
;
46 import eu
.etaxonomy
.cdm
.model
.reference
.PublicationBase
;
47 import eu
.etaxonomy
.cdm
.model
.reference
.Publisher
;
48 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
49 import eu
.etaxonomy
.cdm
.model
.reference
.StrictReferenceBase
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
52 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
53 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
57 * @author a.babadshanjan
62 public class FaunaEuropaeaRefImport
extends FaunaEuropaeaImportBase
{
63 private static final Logger logger
= Logger
.getLogger(FaunaEuropaeaRefImport
.class);
65 /* Interval for progress info message when retrieving taxa */
66 private int modCount
= 10000;
70 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
73 protected boolean doCheck(FaunaEuropaeaImportState state
) {
74 boolean result
= true;
75 FaunaEuropaeaImportConfigurator fauEuConfig
= state
.getConfig();
76 logger
.warn("Checking for References not yet fully implemented");
77 result
&= checkReferenceStatus(fauEuConfig
);
82 private boolean checkReferenceStatus(FaunaEuropaeaImportConfigurator fauEuConfig
) {
83 boolean result
= true;
85 Source source
= fauEuConfig
.getSource();
87 ResultSet rs
= source
.getResultSet(sqlStr
);
89 // } catch (SQLException e) {
90 // e.printStackTrace();
96 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
99 protected boolean doInvoke(FaunaEuropaeaImportState state
) {
101 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
= state
.getStores();
102 MapWrapper
<TeamOrPersonBase
> authorStore
= (MapWrapper
<TeamOrPersonBase
>)stores
.get(ICdmIO
.TEAM_STORE
);
103 MapWrapper
<TaxonBase
> taxonStore
= (MapWrapper
<TaxonBase
>)stores
.get(ICdmIO
.TAXON_STORE
);
104 MapWrapper
<ReferenceBase
> refStore
= (MapWrapper
<ReferenceBase
>)stores
.get(ICdmIO
.REFERENCE_STORE
);
105 TransactionStatus txStatus
= null;
107 FaunaEuropaeaImportConfigurator fauEuConfig
= state
.getConfig();
108 Source source
= fauEuConfig
.getSource();
110 String namespace
= "Reference";
111 boolean success
= true;
113 if(logger
.isInfoEnabled()) { logger
.info("Start making References..."); }
117 " SELECT MAX(TAX_ID) AS TAX_ID FROM dbo.Taxon ";
119 ResultSet rs
= source
.getResultSet(strQuery
);
121 int maxTaxonId
= rs
.getInt("TAX_ID");
122 // highestTaxonIndex = maxTaxonId;
126 " SELECT Reference.*, TaxRefs.* " +
127 " FROM Reference INNER JOIN TaxRefs ON Reference.ref_id = TaxRefs.trf_ref_id " +
129 " ORDER BY TaxRefs.trf_tax_id";
131 rs
= source
.getResultSet(strQuery
) ;
136 if ((i
++ % modCount
) == 0 && i
!= 1 ) {
137 if(logger
.isInfoEnabled()) {
138 logger
.info("References handled: " + (i
-1));
142 int taxonId
= rs
.getInt("trf_tax_id");
143 int refId
= rs
.getInt("ref_id");
144 String refAuthor
= rs
.getString("ref_author");
145 String year
= rs
.getString("ref_year");
146 String title
= rs
.getString("ref_title");
147 String refSource
= rs
.getString("ref_source");
148 String page
= rs
.getString("trf_page");
150 StrictReferenceBase
<?
> reference
= null;
151 TeamOrPersonBase
<Team
> author
= null;
154 reference
= Generic
.NewInstance();
155 reference
.setTitleCache(title
);
156 reference
.setDatePublished(ImportHelper
.getDatePublished(year
));
157 author
= Team
.NewInstance();
158 author
.setTitleCache(refAuthor
);
160 ImportHelper
.setOriginalSource(reference
, fauEuConfig
.getSourceReference(), refId
, namespace
);
161 ImportHelper
.setOriginalSource(author
, fauEuConfig
.getSourceReference(), refId
, namespace
);
165 if (!refStore
.containsId(refId
)) {
166 if (reference
== null) {
167 logger
.warn("Reference is null");
169 refStore
.put(refId
, reference
);
170 if (logger
.isDebugEnabled()) {
171 logger
.debug("Stored reference (" + refId
+ ") " + refAuthor
);
174 if (logger
.isDebugEnabled()) {
175 logger
.debug("Not imported reference with duplicated ref_id (" + refId
+
183 if (!authorStore
.containsId(refId
)) { // TODO: Don't insert identical author names
184 if (refAuthor
== null) {
185 logger
.warn("Reference author is null");
187 authorStore
.put(refId
, author
);
188 if (logger
.isDebugEnabled()) {
189 logger
.debug("Stored author (" + refId
+ ") " + refAuthor
);
192 if (logger
.isDebugEnabled()) {
193 logger
.debug("Not imported author with duplicated aut_id (" + refId
+
198 } catch (Exception e
) {
199 logger
.warn("An exception occurred when creating reference with id " + refId
+
200 ". Reference could not be saved.");
204 // Create descriptions
207 TaxonBase taxonBase
= taxonStore
.get(taxonId
);
208 if (taxonBase
== null) { continue; }
209 boolean isSynonym
= taxonBase
.isInstanceOf(Synonym
.class);
211 Synonym syn
= CdmBase
.deproxy(taxonBase
, Synonym
.class);
212 taxon
= syn
.getAcceptedTaxa().iterator().next();
214 taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
217 TaxonDescription taxonDescription
= null;
218 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
219 if (descriptions
.size() > 0) {
220 taxonDescription
= descriptions
.iterator().next();
222 taxonDescription
= TaxonDescription
.NewInstance();
223 taxon
.addDescription(taxonDescription
);
226 TextData textData
= TextData
.NewInstance(Feature
.CITATION());
228 Synonym syn
= CdmBase
.deproxy(taxonBase
, Synonym
.class);
229 textData
.setNameUsedInReference(syn
.getName());
231 textData
.setCitation(reference
);
232 textData
.setCitationMicroReference(page
);
233 taxonDescription
.addElement(textData
);
235 } catch (Exception e
) {
236 logger
.warn("An exception occurred when creating description for reference " + refId
+
237 ". Taxon description could not be saved.");
243 if(logger
.isInfoEnabled()) { logger
.info("Saving references ..."); }
245 if (state
.getConfig().isUseTransactions()) {
246 txStatus
= startTransaction();
249 // save taxa, references, and authors
250 success
= saveTaxa(state
, state
.getHighestTaxonIndex(), state
.getConfig().getLimitSave());
251 getReferenceService().saveReferenceAll(refStore
.objects());
252 getAgentService().saveAgentAll(authorStore
.objects());
254 if (state
.getConfig().isUseTransactions()) {
255 commitTransaction(txStatus
);
258 if(logger
.isInfoEnabled()) { logger
.info("End making references ..."); }
260 } catch (SQLException e
) {
261 logger
.error("SQLException:" + e
);
269 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
271 protected boolean isIgnore(FaunaEuropaeaImportState state
){
272 return (state
.getConfig().getDoReferences() == IImportConfigurator
.DO_REFERENCES
.NONE
);