Project

General

Profile

Download (5.23 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.TaxonName;
28
import eu.etaxonomy.cdm.model.taxon.Taxon;
29
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
30

    
31

    
32
/**
33
 * @author a.babadshanjan
34
 * @since 21.08.2010
35
 */
36
@Component
37
public class FaunaEuropaeaAdditionalTaxonDataImport extends FaunaEuropaeaImportBase  {
38
    private static final long serialVersionUID = -6734273038256432559L;
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
	@Override
46
	protected boolean doCheck(FaunaEuropaeaImportState state) {
47
		boolean result = true;
48
		logger.warn("Checking for Taxa not yet fully implemented");
49
		return result;
50
	}
51

    
52

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

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

    
63
		processAdditionalInfraGenericEpithets(state);
64

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

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

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

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

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

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

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

    
105
			for (TaxonName taxonName : taxonNames) {
106

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

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

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

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

    
150
		return;
151
	}
152

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