Project

General

Profile

Download (7.07 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.indexFungorum;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Set;
19

    
20
import org.apache.commons.lang.StringUtils;
21
import org.apache.log4j.Logger;
22
import org.springframework.stereotype.Component;
23

    
24
import eu.etaxonomy.cdm.common.CdmUtils;
25
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
26
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.name.BotanicalName;
29
import eu.etaxonomy.cdm.model.name.NonViralName;
30
import eu.etaxonomy.cdm.model.name.Rank;
31
import eu.etaxonomy.cdm.model.reference.Reference;
32
import eu.etaxonomy.cdm.model.taxon.Classification;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
35

    
36

    
37
/**
38
 * @author a.mueller
39
 * @created 27.02.2012
40
 */
41
@Component
42
public class IndexFungorumGeneraImport  extends IndexFungorumImportBase {
43
	private static final Logger logger = Logger.getLogger(IndexFungorumGeneraImport.class);
44
	
45
	private static final String pluralString = "genera";
46
	private static final String dbTableName = "tblGenera";
47

    
48
	public IndexFungorumGeneraImport(){
49
		super(pluralString, dbTableName, null);
50
	}
51

    
52
	
53
	
54
	@Override
55
	protected String getIdQuery() {
56
		String result = " SELECT [RECORD NUMBER] FROM " + getTableName() +
57
				" ORDER BY [NAME OF FUNGUS] ";
58
		return result;
59
	}
60

    
61
	/* (non-Javadoc)
62
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
63
	 */
64
	@Override
65
	protected String getRecordQuery(IndexFungorumImportConfigurator config) {
66
		String strRecordQuery = 
67
				" SELECT DISTINCT c.[Family name], c.[Order name], c.[Subclass name], c.[Class name], c.[Subphylum name], c.[Phylum name], c.[Kingdom name], g.* " +
68
                " FROM tblGenera AS g LEFT OUTER JOIN  dbo.[tblPESIfungi-Classification] AS c ON g.[RECORD NUMBER] = c.PreferredNameFDCnumber " +
69
			" WHERE ( g.[RECORD NUMBER] IN (" + ID_LIST_TOKEN + ") )" + 
70
			"";
71
		return strRecordQuery;
72
	}
73

    
74
	
75
	
76
	
77
	@Override
78
	public boolean doPartition(ResultSetPartitioner partitioner, IndexFungorumImportState state) {
79
		boolean success =true;
80
		Reference<?> sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
81
		ResultSet rs = partitioner.getResultSet();
82
		Classification classification = getClassification(state);
83
		try {
84
			while (rs.next()){
85

    
86
				//TODO
87
				//DisplayName, NomRefCache
88

    
89
				Double id = (Double)rs.getObject("RECORD NUMBER");
90
				
91
				
92
				String preferredName = rs.getString("NAME OF FUNGUS");
93
				if (StringUtils.isBlank(preferredName)){
94
					logger.warn("Preferred name is blank. This case is not yet handled by IF import. RECORD NUMBER" + id);
95
				}
96
				
97
				Rank rank = Rank.GENUS();
98
				NonViralName<?> name = BotanicalName.NewInstance(rank);
99
				name.setGenusOrUninomial(preferredName);
100
				
101
				Taxon taxon = Taxon.NewInstance(name, sourceReference);
102
				Taxon parent = getParentTaxon(state, rs);
103
				classification.addParentChild(parent, taxon, null, null);
104
				
105
				//author + publication
106
				makeAuthorAndPublication(state, rs, name);
107
				//source
108
				//makeSource(state, taxon, id, NAMESPACE_GENERA );
109
				if (id != null){
110
					makeSource(state, taxon, id.intValue(), NAMESPACE_GENERA );
111
				} else{
112
					makeSource(state, taxon, null,NAMESPACE_GENERA);
113
				}
114
				getTaxonService().saveOrUpdate(taxon);
115
			}
116

    
117
			
118
		} catch (Exception e) {
119
			e.printStackTrace();
120
			logger.error(e.getMessage());
121
			state.setSuccess(false);
122
			success = false;
123
		}
124
		return success;
125
		
126
	}
127
	
128
	private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
129
		String parentName = getParentNameString(rs);
130
		if (parentName == null){
131
			logger.warn("Parent name not found for: " + rs.getString("NAME OF FUNGUS"));
132
		}
133
		Taxon taxon = state.getRelatedObject(NAMESPACE_SUPRAGENERIC_NAMES, parentName, Taxon.class);
134
		if (taxon == null){
135
			logger.warn("Taxon not found for " + parentName + " name of fungus: " +rs.getString("NAME OF FUNGUS") );
136
		}
137
		return taxon;
138
	}
139

    
140

    
141
	private String getParentNameString(ResultSet rs) throws SQLException {
142
		String parentName = rs.getString("Family name");
143
		if (parentName == null){
144
			logger.warn(rs.getObject("NAME OF FUNGUS") + " has no family name. ");
145
			return null;
146
		}
147
		if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
148
			parentName = rs.getString("Order name");
149
			if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
150
				parentName = rs.getString("Subclass name");
151
				if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
152
					parentName = rs.getString("Class name");
153
					if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
154
						parentName = rs.getString("Subphylum name");
155
						if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
156
							parentName = rs.getString("Phylum name");
157
							if (parentName.equalsIgnoreCase(INCERTAE_SEDIS) || parentName.equalsIgnoreCase(FOSSIL_FUNGI) ){
158
								parentName = rs.getString("Kingdom name");
159
							}
160
						}
161
					}
162
				}
163
			}	
164
		}
165
		return parentName;
166
	}
167

    
168

    
169
	@Override
170
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
171
		String nameSpace;
172
		Class<?> cdmClass;
173
		Set<String> idSet;
174
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
175
		
176
		try{
177
			Set<String> taxonNameSet = new HashSet<String>();
178
			while (rs.next()){
179
//				handleForeignKey(rs, taxonIdSet,"tu_acctaxon" );
180
			}
181
			
182
			//taxon map
183
			nameSpace = NAMESPACE_SUPRAGENERIC_NAMES ;
184
			cdmClass = TaxonBase.class;
185
//			idSet = taxonNameSet;
186
			Map<String, TaxonBase<?>> taxonMap = new HashMap<String, TaxonBase<?>>();
187
			List<TaxonBase> list = getTaxonService().listTaxaByName(Taxon.class, "*", null, null, null, null, 1000000, null);
188
			for (TaxonBase<?> taxon : list){
189
				taxonMap.put(CdmBase.deproxy(taxon.getName(), NonViralName.class).getGenusOrUninomial(), taxon);
190
			}
191
			result.put(nameSpace, taxonMap);
192
			
193
			//sourceReference
194
			Reference<?> sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
195
			Map<String, Reference> referenceMap = new HashMap<String, Reference>();
196
			referenceMap.put(SOURCE_REFERENCE, sourceReference);
197
			result.put(NAMESPACE_REFERENCE, referenceMap);
198
			
199
		} catch (SQLException e) {
200
			throw new RuntimeException(e);
201
		}
202
		return result;
203
	}
204
	
205

    
206
	@Override
207
	protected boolean doCheck(IndexFungorumImportState state){
208
		return true;
209
	}
210
	
211
	@Override
212
	protected boolean isIgnore(IndexFungorumImportState state){
213
		return ! state.getConfig().isDoTaxa();
214
	}
215

    
216

    
217

    
218

    
219

    
220
}
(2-2/10)