(no commit message)
[cdmlib-apps.git] / cdm-pesi / src / main / java / eu / etaxonomy / cdm / io / pesi / 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.pesi.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 void doInvoke(FaunaEuropaeaImportState state) {
82
83 if(logger.isInfoEnabled()) {
84 logger.info("Started creating " + pluralString + "...");
85 }
86
87 processAdditionalInfraGenericEpithets(state);
88
89 logger.info("The End is Nigh... " + pluralString + "...");
90 return;
91 }
92
93 /**
94 *
95 * @param state
96 * @return
97 */
98 private void processAdditionalInfraGenericEpithets(FaunaEuropaeaImportState state) {
99 int count = 0;
100 int pageSize = 1000;
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);
107
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";
111
112 logger.info("Retrieving TaxonNames...");
113
114 ResultSet resultSet;
115 try {
116 resultSet = destination.executeQuery(selectQuery);
117
118 // Collect UUIDs
119 while (resultSet.next()) {
120 uuidSet.add(UUID.fromString(resultSet.getString("UUID")));
121 }
122 } catch (SQLException e) {
123 logger.error("An error occured: ", e);
124 }
125
126 // Fetch TaxonName objects for UUIDs
127 taxonNames = getNameService().find(uuidSet);
128
129 for (TaxonNameBase taxonName : taxonNames) {
130
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();
143 if (taxon != null) {
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();
148
149 // set infraGenericEpithet
150 // targetNonViralName.setInfraGenericEpithet(infraGenericEpithet);
151 logger.info("Added an InfraGenericEpithet to this TaxonName: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
152 count++;
153 }
154 }
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());
159 }
160 } else if (foundTaxa.size() == 0) {
161 // logger.error("No matches for search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
162 }
163 }
164
165 }
166 }
167
168 // Commit transaction
169 commitTransaction(txStatus);
170 logger.info("Committed transaction.");
171
172 return;
173 }
174
175 }