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
.pesi
.faunaEuropaea
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
21 import org
.springframework
.transaction
.TransactionStatus
;
23 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
24 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
25 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
27 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
33 * @author a.babadshanjan
38 public class FaunaEuropaeaAdditionalTaxonDataImport
extends FaunaEuropaeaImportBase
{
40 private static final Logger logger
= Logger
.getLogger(FaunaEuropaeaAdditionalTaxonDataImport
.class);
41 private static final String parentPluralString
= "Synonyms";
42 private static final String pluralString
= "InfraGenericEpithets";
43 private static final String acceptedTaxonUUID
= "A9C24E42-69F5-4681-9399-041E652CF338"; // any accepted taxon uuid, taken from original fauna europaea database
46 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
49 protected boolean doCheck(FaunaEuropaeaImportState state
) {
50 boolean result
= true;
51 FaunaEuropaeaImportConfigurator fauEuConfig
= state
.getConfig();
52 logger
.warn("Checking for Taxa not yet fully implemented");
53 result
&= checkTaxonStatus(fauEuConfig
);
59 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
61 protected boolean isIgnore(FaunaEuropaeaImportState state
) {
62 return ! state
.getConfig().isDoTaxa();
65 private boolean checkTaxonStatus(FaunaEuropaeaImportConfigurator fauEuConfig
) {
66 boolean result
= true;
68 Source source
= fauEuConfig
.getSource();
70 ResultSet rs
= source
.getResultSet(sqlStr
);
72 // } catch (SQLException e) {
73 // e.printStackTrace();
79 * Import taxa from FauEU DB
81 protected void doInvoke(FaunaEuropaeaImportState state
) {
83 if(logger
.isInfoEnabled()) {
84 logger
.info("Started creating " + pluralString
+ "...");
87 processAdditionalInfraGenericEpithets(state
);
89 logger
.info("The End is Nigh... " + pluralString
+ "...");
98 private void processAdditionalInfraGenericEpithets(FaunaEuropaeaImportState state
) {
101 Set
<UUID
> uuidSet
= new HashSet
<UUID
>();
102 FaunaEuropaeaImportConfigurator fauEuConfig
= state
.getConfig();
103 ICdmDataSource destination
= fauEuConfig
.getDestination();
104 TransactionStatus txStatus
= null;
105 List
<TaxonNameBase
> taxonNames
= null;
106 txStatus
= startTransaction(false);
108 String selectQuery
= "SELECT t.uuid from TaxonNameBase t INNER JOIN " +
109 "TaxonNameBase t2 ON t.GenusOrUninomial = t2.GenusOrUninomial AND t.SpecificEpithet = t2.SpecificEpithet " +
110 "WHERE t.InfraGenericEpithet IS NULL AND t.rank_id = 764 AND t2.rank_id = 766 AND t2.InfraGenericEpithet IS NOT NULL";
112 logger
.info("Retrieving TaxonNames...");
116 resultSet
= destination
.executeQuery(selectQuery
);
119 while (resultSet
.next()) {
120 uuidSet
.add(UUID
.fromString(resultSet
.getString("UUID")));
122 } catch (SQLException e
) {
123 logger
.error("An error occured: ", e
);
126 // Fetch TaxonName objects for UUIDs
127 taxonNames
= getNameService().find(uuidSet
);
129 for (TaxonNameBase taxonName
: taxonNames
) {
131 // Check whether its taxonName has an infraGenericEpithet
132 if (taxonName
!= null && (taxonName
.isInstanceOf(NonViralName
.class))) {
133 NonViralName targetNonViralName
= CdmBase
.deproxy(taxonName
, NonViralName
.class);
134 String infraGenericEpithet
= targetNonViralName
.getInfraGenericEpithet();
135 if (infraGenericEpithet
== null) {
136 String genusOrUninomial
= targetNonViralName
.getGenusOrUninomial();
137 String specificEpithet
= targetNonViralName
.getSpecificEpithet();
138 List
<TaxonBase
> foundTaxa
= getTaxonService().listTaxaByName(Taxon
.class, genusOrUninomial
, "*", specificEpithet
,
139 "*", null, pageSize
, 1);
140 if (foundTaxa
.size() == 1) {
141 // one matching Taxon found
142 TaxonBase taxon
= foundTaxa
.iterator().next();
144 TaxonNameBase name
= taxon
.getName();
145 if (name
!= null && name
.isInstanceOf(NonViralName
.class)) {
146 NonViralName nonViralName
= CdmBase
.deproxy(name
, NonViralName
.class);
147 infraGenericEpithet
= nonViralName
.getInfraGenericEpithet();
149 // set infraGenericEpithet
150 // targetNonViralName.setInfraGenericEpithet(infraGenericEpithet);
151 logger
.info("Added an InfraGenericEpithet to this TaxonName: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
155 } else if (foundTaxa
.size() > 1) {
156 logger
.warn("Multiple taxa match search criteria: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
157 for (TaxonBase foundTaxon
: foundTaxa
) {
158 logger
.warn(foundTaxon
.getUuid() + ", " + foundTaxon
.getTitleCache());
160 } else if (foundTaxa
.size() == 0) {
161 // logger.error("No matches for search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
168 // Commit transaction
169 commitTransaction(txStatus
);
170 logger
.info("Committed transaction.");