Removed exception handling for exceptions that are never thrown
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / faunaEuropaea / FaunaEuropaeaAdditionalTaxonDataImport.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.io.faunaEuropaea;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.UUID;
18
19 import org.apache.log4j.Logger;
20 import org.springframework.stereotype.Component;
21 import org.springframework.transaction.TransactionStatus;
22
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;
30
31
32 /**
33 * @author a.babadshanjan
34 * @created 21.08.2010
35 * @version 1.0
36 */
37 @Component
38 public class FaunaEuropaeaAdditionalTaxonDataImport extends FaunaEuropaeaImportBase {
39
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
44
45 /* (non-Javadoc)
46 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
47 */
48 @Override
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);
54
55 return result;
56 }
57
58 /* (non-Javadoc)
59 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
60 */
61 protected boolean isIgnore(FaunaEuropaeaImportState state) {
62 return ! state.getConfig().isDoTaxa();
63 }
64
65 private boolean checkTaxonStatus(FaunaEuropaeaImportConfigurator fauEuConfig) {
66 boolean result = true;
67 // try {
68 Source source = fauEuConfig.getSource();
69 String sqlStr = "";
70 ResultSet rs = source.getResultSet(sqlStr);
71 return result;
72 // } catch (SQLException e) {
73 // e.printStackTrace();
74 // return false;
75 // }
76 }
77
78 /**
79 * Import taxa from FauEU DB
80 */
81 protected boolean doInvoke(FaunaEuropaeaImportState state) {
82
83 boolean success = true;
84 if(logger.isInfoEnabled()) {
85 logger.info("Started creating " + pluralString + "...");
86 }
87
88 success = processAdditionalInfraGenericEpithets(state);
89
90 logger.info("The End is Nigh... " + pluralString + "...");
91 return success;
92 }
93
94 /**
95 *
96 * @param state
97 * @return
98 */
99 private boolean processAdditionalInfraGenericEpithets(FaunaEuropaeaImportState state) {
100 boolean success = true;
101 int count = 0;
102 int pageSize = 1000;
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);
109
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";
113
114 logger.info("Retrieving TaxonNames...");
115
116 ResultSet resultSet;
117 try {
118 resultSet = destination.executeQuery(selectQuery);
119
120 // Collect UUIDs
121 while (resultSet.next()) {
122 uuidSet.add(UUID.fromString(resultSet.getString("UUID")));
123 }
124 } catch (SQLException e) {
125 logger.error("An error occured: ", e);
126 }
127
128 // Fetch TaxonName objects for UUIDs
129 taxonNames = getNameService().find(uuidSet);
130
131 for (TaxonNameBase taxonName : taxonNames) {
132
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();
145 if (taxon != null) {
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();
150
151 // set infraGenericEpithet
152 // targetNonViralName.setInfraGenericEpithet(infraGenericEpithet);
153 logger.info("Added an InfraGenericEpithet to this TaxonName: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
154 count++;
155 }
156 }
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());
161 }
162 } else if (foundTaxa.size() == 0) {
163 // logger.error("No matches for search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
164 }
165 }
166
167 }
168 }
169
170 // Commit transaction
171 commitTransaction(txStatus);
172 logger.info("Committed transaction.");
173
174 return success;
175 }
176
177 }