Project

General

Profile

Download (11.3 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.log4j.Logger;
21
import org.springframework.stereotype.Component;
22
import org.springframework.transaction.TransactionStatus;
23

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

    
35

    
36
/**
37
 * @author a.mueller
38
 * @created 27.02.2012
39
 */
40
@Component
41
public class IndexFungorumHigherClassificationImport  extends IndexFungorumImportBase {
42
	private static final Logger logger = Logger.getLogger(IndexFungorumHigherClassificationImport.class);
43
	
44
	private static final String pluralString = "higher classifications";
45
	private static final String dbTableName = "tblPESIfungi-Classification";
46

    
47
	public IndexFungorumHigherClassificationImport(){
48
		super(pluralString, dbTableName, null);
49
	}
50

    
51

    
52
	@Override
53
	protected String getRecordQuery(IndexFungorumImportConfigurator config) {
54
		String strRecordQuery = 
55
			" SELECT DISTINCT [Kingdom name], [Phylum name], [Subphylum name], [Class name], [Subclass name], [Order name], [Family name], g.[NAME OF FUNGUS] as GenusName, c.PreferredName as SpeciesName " + 
56
			" FROM [tblPESIfungi-Classification] c  LEFT OUTER JOIN " +
57
                      " tblGenera g ON c.PreferredNameFDCnumber = g.[RECORD NUMBER]" +
58
//			" WHERE ( dr.id IN (" + ID_LIST_TOKEN + ") )";
59
			" ORDER BY [Kingdom name], [Phylum name], [Subphylum name], [Class name], [Subclass name], [Order name],  [Family name], GenusName, SpeciesName ";
60
		return strRecordQuery;
61
	}
62

    
63
	
64
	
65
	
66
	
67
	@Override
68
	protected void doInvoke(IndexFungorumImportState state) {
69
		String sql = getRecordQuery(state.getConfig());
70
		ResultSet rs = state.getConfig().getSource().getResultSet(sql);
71
		
72
		//only 1 partition here
73
		
74
		String lastKingdom = "";
75
		String lastPhylum = "";
76
		String lastSubphylum = "";
77
		String lastClassname = "";
78
		String lastSubclass = "";
79
		String lastOrder = "";
80
		String lastFamily = "";
81
//		String lastGenus = "";
82
//		String lastSpecies = "";
83
		
84
		Taxon taxonKingdom = null;
85
		Taxon taxonPhylum = null;
86
		Taxon taxonSubphylum = null;
87
		Taxon taxonClass = null;
88
		Taxon taxonSubclass = null;
89
		Taxon taxonOrder = null;
90
		Taxon taxonFamily = null;
91
//		Taxon taxonGenus = null;
92
//		Taxon taxonSpecies = null;
93
		
94
		Taxon higherTaxon = null;
95
		
96
		
97
		TransactionStatus tx = startTransaction();
98
		ResultSet rsRelatedObjects = state.getConfig().getSource().getResultSet(sql);
99
		state.setRelatedObjects((Map)getRelatedObjectsForPartition(rsRelatedObjects, state));
100
		
101
		Classification classification = getClassification(state);
102
		
103
		try {
104
			while (rs.next()){
105
				String kingdom = rs.getString("Kingdom name");
106
				String phylum = rs.getString("Phylum name");
107
				String subphylum = rs.getString("Subphylum name");
108
				String classname = rs.getString("Class name");
109
				String subclass = rs.getString("Subclass name");
110
				String order = rs.getString("Order name");
111
				String family = rs.getString("Family name");
112
//				String genus = rs.getString("GenusName");
113
//				String species = rs.getString("SpeciesName");
114
				
115
//				if (isNewTaxon(species, lastSpecies)){
116
//					if (isNewTaxon(genus, lastGenus)){
117
				if (isNewTaxon(family, lastFamily)){
118
					if (isNewTaxon(order,lastOrder)){
119
						if (isNewTaxon(subclass,lastSubclass)){
120
							if (isNewTaxon(classname,lastClassname)){
121
								if (isNewTaxon(subphylum, lastSubphylum)){
122
									if (isNewTaxon(phylum,lastPhylum)){
123
										if (isNewTaxon(kingdom,lastKingdom)){
124
											taxonKingdom = makeTaxon(state, kingdom, Rank.KINGDOM());
125
											lastKingdom = kingdom;
126
											logger.info("Import kingdom " +  kingdom);
127
											getTaxonService().saveOrUpdate(taxonKingdom);
128
										}else{
129
											higherTaxon = taxonKingdom;
130
										}
131
										higherTaxon = isIncertisSedis(kingdom) ? higherTaxon : taxonKingdom;
132
										Rank newRank = (lastKingdom.equals("Fungi") ? null : Rank.PHYLUM());
133
										taxonPhylum = makeTaxon(state, phylum, newRank);
134
										if (taxonPhylum != null){
135
											classification.addParentChild(higherTaxon, taxonPhylum, null, null);
136
										}
137
										higherTaxon = isIncertisSedis(phylum) ? higherTaxon : taxonPhylum;
138
										lastPhylum = phylum;
139
										logger.info("Import Phylum " +  phylum);
140
									}else{
141
										higherTaxon = taxonPhylum;
142
									}
143
									Rank newRank = (lastKingdom.equals("Fungi") ? null : Rank.SUBPHYLUM());
144
									taxonSubphylum = makeTaxon(state, subphylum, newRank);
145
									if (taxonSubphylum != null){
146
										getClassification(state).addParentChild(higherTaxon,taxonSubphylum, null, null);
147
									}
148
									higherTaxon = isIncertisSedis(subphylum) ? higherTaxon : taxonSubphylum;
149
									lastSubphylum = subphylum;
150
								}else{
151
									higherTaxon = taxonSubphylum;
152
								}
153
								taxonClass = makeTaxon(state, classname, Rank.CLASS());
154
								if (taxonClass != null){
155
									getClassification(state).addParentChild(higherTaxon, taxonClass, null, null);
156
								}
157
								higherTaxon = isIncertisSedis(classname) ? higherTaxon : taxonClass;
158
								lastClassname = classname;
159
							}else{
160
								higherTaxon = taxonClass;
161
							}
162
							taxonSubclass = makeTaxon(state, subclass, Rank.SUBCLASS());
163
							if (taxonSubclass != null){
164
								getClassification(state).addParentChild(higherTaxon, taxonSubclass,null, null);
165
							}
166
							higherTaxon = isIncertisSedis(subclass) ? higherTaxon : taxonSubclass;
167
							lastSubclass = subclass;
168
						}else{
169
							higherTaxon = taxonSubclass;
170
						}
171
						taxonOrder = makeTaxon(state, order, Rank.ORDER());
172
						if (taxonOrder != null){
173
							getClassification(state).addParentChild(higherTaxon, taxonOrder, null, null);
174
						}
175
						higherTaxon = isIncertisSedis(order) ? higherTaxon : taxonOrder;
176
						lastOrder = order;
177
					}else{
178
						higherTaxon = taxonOrder;
179
					}
180
					taxonFamily = makeTaxon(state, family, Rank.FAMILY());
181
					if (taxonFamily != null){
182
						try{
183
							getClassification(state).addParentChild(higherTaxon, taxonFamily, null, null);
184
						}catch(IllegalStateException e){
185
							if (e.getMessage().startsWith("The child taxon is already part of the tree")){
186
								//TaxonNode node = getClassification(state).getNode(taxonFamily);
187
								logger.warn(e.getMessage() + taxonFamily.getTitleCache() + " " + higherTaxon.getTitleCache());
188
								
189
									
190
									
191
								}
192
							
193
						}
194
					}
195
					higherTaxon = isIncertisSedis(family) ? higherTaxon : taxonFamily;
196
					lastFamily = family;
197
					getTaxonService().saveOrUpdate(higherTaxon);
198
				}
199
//						else{
200
//							higherTaxon = taxonFamily;
201
//						}
202
//						taxonGenus = makeTaxon(state, genus, Rank.GENUS());
203
//						if (taxonGenus != null){
204
//							getClassification(state).addParentChild(higherTaxon, taxonGenus, null, null);
205
//						}
206
//						higherTaxon = isIncertisSedis(genus) ? higherTaxon : taxonGenus;
207
//						lastGenus = genus;
208
//					}else{
209
//						higherTaxon = taxonGenus;
210
//					}
211
//					taxonSpecies = makeTaxon(state, species, Rank.SPECIES());
212
//					if (taxonSpecies != null){
213
//						getClassification(state).addParentChild(higherTaxon, taxonSpecies, null, null);
214
//					}
215
//					higherTaxon = isIncertisSedis(species) ? higherTaxon : taxonSpecies;
216
//					lastSpecies = species;
217
//					getTaxonService().saveOrUpdate(higherTaxon);
218
//				}
219
				getTaxonService().saveOrUpdate(higherTaxon);
220
			}
221

    
222
			
223
		} catch (SQLException e) {
224
			e.printStackTrace();
225
			logger.error(e.getMessage());
226
			tx.setRollbackOnly();
227
			state.setSuccess(false);
228
		}
229
		commitTransaction(tx);
230
		return;
231
		
232
	}
233

    
234

    
235
	private boolean isIncertisSedis(String uninomial) {
236
		return  uninomial.equalsIgnoreCase(INCERTAE_SEDIS) || uninomial.equalsIgnoreCase(FOSSIL_FUNGI);
237
	}
238

    
239

    
240
	private boolean isNewTaxon(String uninomial, String lastUninomial) {
241
		boolean result =  !uninomial.equalsIgnoreCase(lastUninomial);
242
		result |= lastUninomial.equalsIgnoreCase(INCERTAE_SEDIS);
243
		result |= lastUninomial.equalsIgnoreCase(FOSSIL_FUNGI);
244
		return result;
245
	}
246

    
247
	private Taxon makeTaxon(IndexFungorumImportState state, String uninomial, Rank newRank) {
248
		if (uninomial.equalsIgnoreCase(INCERTAE_SEDIS) || uninomial.equalsIgnoreCase(FOSSIL_FUNGI)){
249
			return null;
250
		}
251
		Taxon taxon = state.getRelatedObject(IndexFungorumSupraGeneraImport.NAMESPACE_SUPRAGENERIC_NAMES, uninomial, Taxon.class);
252
		if (taxon == null){
253
			if (! newRank.equals(Rank.KINGDOM())){
254
				logger.warn("Taxon not found for uninomial " + uninomial);
255
			}
256
			NonViralName<?> name = BotanicalName.NewInstance(newRank);
257
			name.setGenusOrUninomial(uninomial);
258
			Reference<?> sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
259
			taxon = Taxon.NewInstance(name, sourceReference);
260
			taxon.addMarker(Marker.NewInstance(getMissingGUIDMarkerType(state), true));
261
		}else if (newRank != null){
262
			taxon.getName().setRank(newRank);
263
		}
264
		return taxon;
265
	}
266

    
267

    
268
	@Override
269
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
270
		String nameSpace;
271
		Class<?> cdmClass;
272
		Set<String> idSet;
273
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
274
		
275
		try{
276
			Set<String> taxonNameSet = new HashSet<String>();
277
//			while (rs.next()){
278
//				handleForeignKey(rs, taxonIdSet,"tu_acctaxon" );
279
//			}
280
			
281
			//taxon map
282
			nameSpace = IndexFungorumSupraGeneraImport.NAMESPACE_SUPRAGENERIC_NAMES ;
283
			cdmClass = TaxonBase.class;
284
//			idSet = taxonNameSet;
285
			Map<String, TaxonBase<?>> taxonMap = new HashMap<String, TaxonBase<?>>();
286
			List<Taxon> list = getTaxonService().list(Taxon.class, null, null, null, null);
287
			for (Taxon taxon : list){
288
				taxonMap.put(CdmBase.deproxy(taxon.getName(), NonViralName.class).getGenusOrUninomial(), taxon);
289
			}
290
			result.put(nameSpace, taxonMap);
291
			
292
			//source reference
293
			Reference<?> sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
294
			Map<String, Reference> referenceMap = new HashMap<String, Reference>();
295
			referenceMap.put(SOURCE_REFERENCE, sourceReference);
296
			result.put(NAMESPACE_REFERENCE, referenceMap);
297
			
298
		} catch (Exception e) {
299
			throw new RuntimeException(e);
300
		}
301
		return result;
302
	}
303
	
304
	@Override
305
	protected boolean doCheck(IndexFungorumImportState state){
306
		return true;
307
	}
308
	
309
	@Override
310
	protected boolean isIgnore(IndexFungorumImportState state){
311
		return ! state.getConfig().isDoRelTaxa();
312
	}
313

    
314

    
315

    
316

    
317

    
318
}
(3-3/10)