Project

General

Profile

Download (5.4 KB) Statistics
| Branch: | Revision:
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.INonViralName;
26
import eu.etaxonomy.cdm.model.name.ITaxonNameBase;
27
import eu.etaxonomy.cdm.model.name.NonViralName;
28
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31

    
32

    
33
/**
34
 * @author a.babadshanjan
35
 * @created 21.08.2010
36
 */
37
@Component
38
public class FaunaEuropaeaAdditionalTaxonDataImport extends FaunaEuropaeaImportBase  {
39
    private static final long serialVersionUID = -6734273038256432559L;
40

    
41
    private static final Logger logger = Logger.getLogger(FaunaEuropaeaAdditionalTaxonDataImport.class);
42
//	private static final String parentPluralString = "Synonyms";
43
	private static final String pluralString = "InfraGenericEpithets";
44
	//private static final String acceptedTaxonUUID = "A9C24E42-69F5-4681-9399-041E652CF338"; // any accepted taxon uuid, taken from original fauna europaea database
45

    
46
	@Override
47
	protected boolean doCheck(FaunaEuropaeaImportState state) {
48
		boolean result = true;
49
		logger.warn("Checking for Taxa not yet fully implemented");
50
		return result;
51
	}
52

    
53

    
54
	/**
55
	 * Import taxa from FauEU DB
56
	 */
57
	@Override
58
    protected void doInvoke(FaunaEuropaeaImportState state) {
59

    
60
		if(logger.isInfoEnabled()) {
61
			logger.info("Started creating " + pluralString + "...");
62
		}
63

    
64
		processAdditionalInfraGenericEpithets(state);
65

    
66
		logger.info("The End is Nigh... " + pluralString + "...");
67
		return;
68
	}
69

    
70
	/**
71
	 *
72
	 * @param state
73
	 * @return
74
	 */
75
	private void processAdditionalInfraGenericEpithets(FaunaEuropaeaImportState state) {
76
		int pageSize = 1000;
77
		Set<UUID> uuidSet = new HashSet<UUID>();
78
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
79
		ICdmDataSource destination = fauEuConfig.getDestination();
80
		TransactionStatus txStatus = null;
81
		List<TaxonNameBase> taxonNames = null;
82
		txStatus = startTransaction(false);
83

    
84
		String selectQuery = "SELECT t.uuid from TaxonNameBase t INNER JOIN " +
85
				"TaxonNameBase t2 ON t.GenusOrUninomial = t2.GenusOrUninomial AND t.SpecificEpithet = t2.SpecificEpithet " +
86
				"WHERE t.InfraGenericEpithet IS NULL AND t.rank_id = 764 AND t2.rank_id = 766 AND t2.InfraGenericEpithet IS NOT NULL";
87

    
88
		logger.info("Retrieving TaxonNames...");
89

    
90
		ResultSet resultSet;
91
		try {
92
			resultSet = destination.executeQuery(selectQuery);
93

    
94
			// Collect UUIDs
95
			while (resultSet.next()) {
96
				uuidSet.add(UUID.fromString(resultSet.getString("UUID")));
97
			}
98
		} catch (SQLException e) {
99
			logger.error("An error occured: ", e);
100
		}
101

    
102
		// Fetch TaxonName objects for UUIDs
103
		if (!uuidSet.isEmpty()){
104
			taxonNames = getNameService().find(uuidSet);
105

    
106
			for (TaxonNameBase<?,?> taxonName : taxonNames) {
107

    
108
				// Check whether its taxonName has an infraGenericEpithet
109
				if (taxonName != null && (taxonName.isInstanceOf(NonViralName.class))) {
110
					INonViralName targetNonViralName = CdmBase.deproxy(taxonName, NonViralName.class);
111
					String infraGenericEpithet = targetNonViralName.getInfraGenericEpithet();
112
					if (infraGenericEpithet == null) {
113
						String genusOrUninomial = targetNonViralName.getGenusOrUninomial();
114
						String specificEpithet = targetNonViralName.getSpecificEpithet();
115
						List<TaxonBase> foundTaxa = getTaxonService().listTaxaByName(Taxon.class, genusOrUninomial, "*", specificEpithet,
116
								"*", "*", null, pageSize, 1);
117
						if (foundTaxa.size() == 1) {
118
							// one matching Taxon found
119
							TaxonBase<?> taxon = foundTaxa.iterator().next();
120
							if (taxon != null) {
121
								ITaxonNameBase name = taxon.getName();
122
								if (name != null && name.isInstanceOf(NonViralName.class)) {
123
									INonViralName nonViralName = CdmBase.deproxy(name, NonViralName.class);
124
									infraGenericEpithet = nonViralName.getInfraGenericEpithet();
125

    
126
									// set infraGenericEpithet
127
	//									targetNonViralName.setInfraGenericEpithet(infraGenericEpithet);
128
									logger.debug("Added an InfraGenericEpithet to this TaxonName: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
129
								}
130
							}
131
						} else if (foundTaxa.size() > 1) {
132
							logger.warn("Multiple taxa match search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
133
							for (TaxonBase<?> foundTaxon : foundTaxa) {
134
								logger.warn(foundTaxon.getUuid() + ", " + foundTaxon.getTitleCache());
135
							}
136
						} else if (foundTaxa.size() == 0) {
137
	//							logger.error("No matches for search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
138
						}
139
					}
140

    
141
				}
142
			}
143
		}else {
144
			logger.debug("There are no additional infrageneric epithets!");
145
		}
146

    
147
		// Commit transaction
148
		commitTransaction(txStatus);
149
		logger.info("Committed transaction.");
150

    
151
		return;
152
	}
153

    
154
	@Override
155
    protected boolean isIgnore(FaunaEuropaeaImportState state) {
156
		return ! state.getConfig().isDoTaxa();
157
	}
158
}
(1-1/20)