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
.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 boolean doInvoke(FaunaEuropaeaImportState state
) {
83 boolean success
= true;
84 if(logger
.isInfoEnabled()) {
85 logger
.info("Started creating " + pluralString
+ "...");
88 success
= processAdditionalInfraGenericEpithets(state
);
90 logger
.info("The End is Nigh... " + pluralString
+ "...");
99 private boolean processAdditionalInfraGenericEpithets(FaunaEuropaeaImportState state
) {
100 boolean success
= true;
103 Set
<UUID
> uuidSet
= new HashSet
<UUID
>();
104 FaunaEuropaeaImportConfigurator fauEuConfig
= state
.getConfig();
105 ICdmDataSource destination
= fauEuConfig
.getDestination();
106 TransactionStatus txStatus
= null;
107 List
<TaxonNameBase
> taxonNames
= null;
108 txStatus
= startTransaction(false);
110 String selectQuery
= "SELECT t.uuid from TaxonNameBase t INNER JOIN " +
111 "TaxonNameBase t2 ON t.GenusOrUninomial = t2.GenusOrUninomial AND t.SpecificEpithet = t2.SpecificEpithet " +
112 "WHERE t.InfraGenericEpithet IS NULL AND t.rank_id = 764 AND t2.rank_id = 766 AND t2.InfraGenericEpithet IS NOT NULL";
114 logger
.info("Retrieving TaxonNames...");
118 resultSet
= destination
.executeQuery(selectQuery
);
121 while (resultSet
.next()) {
122 uuidSet
.add(UUID
.fromString(resultSet
.getString("UUID")));
124 } catch (SQLException e
) {
125 logger
.error("An error occured: ", e
);
128 // Fetch TaxonName objects for UUIDs
129 taxonNames
= getNameService().find(uuidSet
);
131 for (TaxonNameBase taxonName
: taxonNames
) {
133 // Check whether its taxonName has an infraGenericEpithet
134 if (taxonName
!= null && (taxonName
.isInstanceOf(NonViralName
.class))) {
135 NonViralName targetNonViralName
= CdmBase
.deproxy(taxonName
, NonViralName
.class);
136 String infraGenericEpithet
= targetNonViralName
.getInfraGenericEpithet();
137 if (infraGenericEpithet
== null) {
138 String genusOrUninomial
= targetNonViralName
.getGenusOrUninomial();
139 String specificEpithet
= targetNonViralName
.getSpecificEpithet();
140 List
<TaxonBase
> foundTaxa
= getTaxonService().listTaxaByName(Taxon
.class, genusOrUninomial
, "*", specificEpithet
,
141 "*", null, pageSize
, 1);
142 if (foundTaxa
.size() == 1) {
143 // one matching Taxon found
144 TaxonBase taxon
= foundTaxa
.iterator().next();
146 TaxonNameBase name
= taxon
.getName();
147 if (name
!= null && name
.isInstanceOf(NonViralName
.class)) {
148 NonViralName nonViralName
= CdmBase
.deproxy(name
, NonViralName
.class);
149 infraGenericEpithet
= nonViralName
.getInfraGenericEpithet();
151 // set infraGenericEpithet
152 // targetNonViralName.setInfraGenericEpithet(infraGenericEpithet);
153 logger
.info("Added an InfraGenericEpithet to this TaxonName: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
157 } else if (foundTaxa
.size() > 1) {
158 logger
.warn("Multiple taxa match search criteria: " + taxonName
.getUuid() + " (" + taxonName
.getTitleCache() + ")");
159 for (TaxonBase foundTaxon
: foundTaxa
) {
160 logger
.warn(foundTaxon
.getUuid() + ", " + foundTaxon
.getTitleCache());
162 } else if (foundTaxa
.size() == 0) {
163 // logger.error("No matches for search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
170 // Commit transaction
171 commitTransaction(txStatus
);
172 logger
.info("Committed transaction.");