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.model.common.CdmBase;
|
25
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
26
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
27
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
28
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
29
|
|
30
|
|
31
|
/**
|
32
|
* @author a.babadshanjan
|
33
|
* @created 21.08.2010
|
34
|
* @version 1.0
|
35
|
*/
|
36
|
@Component
|
37
|
public class FaunaEuropaeaAdditionalTaxonDataImport extends FaunaEuropaeaImportBase {
|
38
|
|
39
|
private static final Logger logger = Logger.getLogger(FaunaEuropaeaAdditionalTaxonDataImport.class);
|
40
|
// private static final String parentPluralString = "Synonyms";
|
41
|
private static final String pluralString = "InfraGenericEpithets";
|
42
|
//private static final String acceptedTaxonUUID = "A9C24E42-69F5-4681-9399-041E652CF338"; // any accepted taxon uuid, taken from original fauna europaea database
|
43
|
|
44
|
/* (non-Javadoc)
|
45
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
46
|
*/
|
47
|
@Override
|
48
|
protected boolean doCheck(FaunaEuropaeaImportState state) {
|
49
|
boolean result = true;
|
50
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
51
|
logger.warn("Checking for Taxa not yet fully implemented");
|
52
|
// result &= checkTaxonStatus(fauEuConfig);
|
53
|
|
54
|
return result;
|
55
|
}
|
56
|
|
57
|
|
58
|
/**
|
59
|
* Import taxa from FauEU DB
|
60
|
*/
|
61
|
@Override
|
62
|
protected void doInvoke(FaunaEuropaeaImportState state) {
|
63
|
|
64
|
if(logger.isInfoEnabled()) {
|
65
|
logger.info("Started creating " + pluralString + "...");
|
66
|
}
|
67
|
|
68
|
processAdditionalInfraGenericEpithets(state);
|
69
|
|
70
|
logger.info("The End is Nigh... " + pluralString + "...");
|
71
|
return;
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
*
|
76
|
* @param state
|
77
|
* @return
|
78
|
*/
|
79
|
private void processAdditionalInfraGenericEpithets(FaunaEuropaeaImportState state) {
|
80
|
int count = 0;
|
81
|
int pageSize = 1000;
|
82
|
Set<UUID> uuidSet = new HashSet<UUID>();
|
83
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
84
|
ICdmDataSource destination = fauEuConfig.getDestination();
|
85
|
TransactionStatus txStatus = null;
|
86
|
List<TaxonNameBase> taxonNames = null;
|
87
|
txStatus = startTransaction(false);
|
88
|
|
89
|
String selectQuery = "SELECT t.uuid from TaxonNameBase t INNER JOIN " +
|
90
|
"TaxonNameBase t2 ON t.GenusOrUninomial = t2.GenusOrUninomial AND t.SpecificEpithet = t2.SpecificEpithet " +
|
91
|
"WHERE t.InfraGenericEpithet IS NULL AND t.rank_id = 764 AND t2.rank_id = 766 AND t2.InfraGenericEpithet IS NOT NULL";
|
92
|
|
93
|
logger.info("Retrieving TaxonNames...");
|
94
|
|
95
|
ResultSet resultSet;
|
96
|
try {
|
97
|
resultSet = destination.executeQuery(selectQuery);
|
98
|
|
99
|
// Collect UUIDs
|
100
|
while (resultSet.next()) {
|
101
|
uuidSet.add(UUID.fromString(resultSet.getString("UUID")));
|
102
|
}
|
103
|
} catch (SQLException e) {
|
104
|
logger.error("An error occured: ", e);
|
105
|
}
|
106
|
|
107
|
// Fetch TaxonName objects for UUIDs
|
108
|
if (!uuidSet.isEmpty()){
|
109
|
taxonNames = getNameService().find(uuidSet);
|
110
|
|
111
|
for (TaxonNameBase taxonName : taxonNames) {
|
112
|
|
113
|
// Check whether its taxonName has an infraGenericEpithet
|
114
|
if (taxonName != null && (taxonName.isInstanceOf(NonViralName.class))) {
|
115
|
NonViralName targetNonViralName = CdmBase.deproxy(taxonName, NonViralName.class);
|
116
|
String infraGenericEpithet = targetNonViralName.getInfraGenericEpithet();
|
117
|
if (infraGenericEpithet == null) {
|
118
|
String genusOrUninomial = targetNonViralName.getGenusOrUninomial();
|
119
|
String specificEpithet = targetNonViralName.getSpecificEpithet();
|
120
|
List<TaxonBase> foundTaxa = getTaxonService().listTaxaByName(Taxon.class, genusOrUninomial, "*", specificEpithet,
|
121
|
"*", "*", null, pageSize, 1);
|
122
|
if (foundTaxa.size() == 1) {
|
123
|
// one matching Taxon found
|
124
|
TaxonBase taxon = foundTaxa.iterator().next();
|
125
|
if (taxon != null) {
|
126
|
TaxonNameBase name = taxon.getName();
|
127
|
if (name != null && name.isInstanceOf(NonViralName.class)) {
|
128
|
NonViralName nonViralName = CdmBase.deproxy(name, NonViralName.class);
|
129
|
infraGenericEpithet = nonViralName.getInfraGenericEpithet();
|
130
|
|
131
|
// set infraGenericEpithet
|
132
|
// targetNonViralName.setInfraGenericEpithet(infraGenericEpithet);
|
133
|
logger.debug("Added an InfraGenericEpithet to this TaxonName: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
|
134
|
count++;
|
135
|
}
|
136
|
}
|
137
|
} else if (foundTaxa.size() > 1) {
|
138
|
logger.warn("Multiple taxa match search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
|
139
|
for (TaxonBase foundTaxon : foundTaxa) {
|
140
|
logger.warn(foundTaxon.getUuid() + ", " + foundTaxon.getTitleCache());
|
141
|
}
|
142
|
} else if (foundTaxa.size() == 0) {
|
143
|
// logger.error("No matches for search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
|
144
|
}
|
145
|
}
|
146
|
|
147
|
}
|
148
|
}
|
149
|
}else {
|
150
|
logger.debug("There are no additional infrageneric epithets!");
|
151
|
}
|
152
|
|
153
|
// Commit transaction
|
154
|
commitTransaction(txStatus);
|
155
|
logger.info("Committed transaction.");
|
156
|
|
157
|
return;
|
158
|
}
|
159
|
|
160
|
|
161
|
/* (non-Javadoc)
|
162
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
|
163
|
*/
|
164
|
@Override
|
165
|
protected boolean isIgnore(FaunaEuropaeaImportState state) {
|
166
|
return ! state.getConfig().isDoTaxa();
|
167
|
}
|
168
|
|
169
|
}
|