Project

General

Profile

Download (5.8 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.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
	protected void doInvoke(FaunaEuropaeaImportState state) {				
62
		
63
		if(logger.isInfoEnabled()) {
64
			logger.info("Started creating " + pluralString + "...");
65
		}
66
		
67
		processAdditionalInfraGenericEpithets(state);
68
		
69
		logger.info("The End is Nigh... " + pluralString + "...");
70
		return;
71
	}
72

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

    
88
		String selectQuery = "SELECT t.uuid from TaxonNameBase t INNER JOIN " +
89
				"TaxonNameBase t2 ON t.GenusOrUninomial = t2.GenusOrUninomial AND t.SpecificEpithet = t2.SpecificEpithet " +
90
				"WHERE t.InfraGenericEpithet IS NULL AND t.rank_id = 764 AND t2.rank_id = 766 AND t2.InfraGenericEpithet IS NOT NULL";
91
		
92
		logger.info("Retrieving TaxonNames...");
93
		
94
		ResultSet resultSet;
95
		try {
96
			resultSet = destination.executeQuery(selectQuery);
97
		
98
			// Collect UUIDs
99
			while (resultSet.next()) {
100
				uuidSet.add(UUID.fromString(resultSet.getString("UUID")));
101
			}
102
		} catch (SQLException e) {
103
			logger.error("An error occured: ", e);
104
		}
105

    
106
		// Fetch TaxonName objects for UUIDs
107
		if (!uuidSet.isEmpty()){
108
			taxonNames = getNameService().find(uuidSet);
109
			
110
			for (TaxonNameBase taxonName : taxonNames) {
111
				
112
				// Check whether its taxonName has an infraGenericEpithet
113
				if (taxonName != null && (taxonName.isInstanceOf(NonViralName.class))) {
114
					NonViralName targetNonViralName = CdmBase.deproxy(taxonName, NonViralName.class);
115
					String infraGenericEpithet = targetNonViralName.getInfraGenericEpithet();
116
					if (infraGenericEpithet == null) {
117
						String genusOrUninomial = targetNonViralName.getGenusOrUninomial();
118
						String specificEpithet = targetNonViralName.getSpecificEpithet();
119
						List<TaxonBase> foundTaxa = getTaxonService().listTaxaByName(Taxon.class, genusOrUninomial, "*", specificEpithet, 
120
								"*", null, pageSize, 1);
121
						if (foundTaxa.size() == 1) {
122
							// one matching Taxon found
123
							TaxonBase taxon = foundTaxa.iterator().next();
124
							if (taxon != null) {
125
								TaxonNameBase name = taxon.getName();
126
								if (name != null && name.isInstanceOf(NonViralName.class)) {
127
									NonViralName nonViralName = CdmBase.deproxy(name, NonViralName.class);
128
									infraGenericEpithet = nonViralName.getInfraGenericEpithet();
129
									
130
									// set infraGenericEpithet
131
	//									targetNonViralName.setInfraGenericEpithet(infraGenericEpithet);
132
									logger.debug("Added an InfraGenericEpithet to this TaxonName: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
133
									count++;
134
								}
135
							}
136
						} else if (foundTaxa.size() > 1) {
137
							logger.warn("Multiple taxa match search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
138
							for (TaxonBase foundTaxon : foundTaxa) {
139
								logger.warn(foundTaxon.getUuid() + ", " + foundTaxon.getTitleCache());
140
							}
141
						} else if (foundTaxa.size() == 0) {
142
	//							logger.error("No matches for search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
143
						}
144
					}
145
					
146
				}
147
			}
148
		}else {
149
			logger.debug("There are no additional infrageneric epithets!");
150
		}
151

    
152
		// Commit transaction
153
		commitTransaction(txStatus);
154
		logger.info("Committed transaction.");
155
		
156
		return;
157
	}
158
	
159
	
160
	/* (non-Javadoc)
161
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
162
	 */
163
	protected boolean isIgnore(FaunaEuropaeaImportState state) {
164
		return ! state.getConfig().isDoTaxa();
165
	}
166

    
167
}
(1-1/20)