Project

General

Profile

Download (10.9 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 KingdomName, PhylumName, SubphylumName, ClassName, SubclassName, OrderName, FamilyName, g.PreferredName 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 KingdomName, PhylumName, SubphylumName, ClassName, SubclassName, OrderName, FamilyName, 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("KingdomName");
106
				String phylum = rs.getString("PhylumName");
107
				String subphylum = rs.getString("SubphylumName");
108
				String classname = rs.getString("ClassName");
109
				String subclass = rs.getString("SubclassName");
110
				String order = rs.getString("OrderName");
111
				String family = rs.getString("FamilyName");
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
						getClassification(state).addParentChild(higherTaxon, taxonFamily, null, null);
183
					}
184
					higherTaxon = isIncertisSedis(family) ? higherTaxon : taxonFamily;
185
					lastFamily = family;
186
					getTaxonService().saveOrUpdate(higherTaxon);
187
				}
188
//						else{
189
//							higherTaxon = taxonFamily;
190
//						}
191
//						taxonGenus = makeTaxon(state, genus, Rank.GENUS());
192
//						if (taxonGenus != null){
193
//							getClassification(state).addParentChild(higherTaxon, taxonGenus, null, null);
194
//						}
195
//						higherTaxon = isIncertisSedis(genus) ? higherTaxon : taxonGenus;
196
//						lastGenus = genus;
197
//					}else{
198
//						higherTaxon = taxonGenus;
199
//					}
200
//					taxonSpecies = makeTaxon(state, species, Rank.SPECIES());
201
//					if (taxonSpecies != null){
202
//						getClassification(state).addParentChild(higherTaxon, taxonSpecies, null, null);
203
//					}
204
//					higherTaxon = isIncertisSedis(species) ? higherTaxon : taxonSpecies;
205
//					lastSpecies = species;
206
//					getTaxonService().saveOrUpdate(higherTaxon);
207
//				}
208
				getTaxonService().saveOrUpdate(higherTaxon);
209
			}
210

    
211
			
212
		} catch (SQLException e) {
213
			e.printStackTrace();
214
			logger.error(e.getMessage());
215
			tx.setRollbackOnly();
216
			state.setSuccess(false);
217
		}
218
		commitTransaction(tx);
219
		return;
220
		
221
	}
222

    
223

    
224
	private boolean isIncertisSedis(String uninomial) {
225
		return  uninomial.equalsIgnoreCase(INCERTAE_SEDIS) || uninomial.equalsIgnoreCase(FOSSIL_FUNGI);
226
	}
227

    
228

    
229
	private boolean isNewTaxon(String uninomial, String lastUninomial) {
230
		boolean result =  !uninomial.equalsIgnoreCase(lastUninomial);
231
		result |= lastUninomial.equalsIgnoreCase(INCERTAE_SEDIS);
232
		result |= lastUninomial.equalsIgnoreCase(FOSSIL_FUNGI);
233
		return result;
234
	}
235

    
236
	private Taxon makeTaxon(IndexFungorumImportState state, String uninomial, Rank newRank) {
237
		if (uninomial.equalsIgnoreCase(INCERTAE_SEDIS) || uninomial.equalsIgnoreCase(FOSSIL_FUNGI)){
238
			return null;
239
		}
240
		Taxon taxon = state.getRelatedObject(IndexFungorumSupraGeneraImport.NAMESPACE_SUPRAGENERIC_NAMES, uninomial, Taxon.class);
241
		if (taxon == null){
242
			if (! newRank.equals(Rank.KINGDOM())){
243
				logger.warn("Taxon not found for " + uninomial);
244
			}
245
			NonViralName<?> name = BotanicalName.NewInstance(newRank);
246
			name.setGenusOrUninomial(uninomial);
247
			Reference<?> sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
248
			taxon = Taxon.NewInstance(name, sourceReference);
249
			taxon.addMarker(Marker.NewInstance(getMissingGUIDMarkerType(state), true));
250
		}else if (newRank != null){
251
			taxon.getName().setRank(newRank);
252
		}
253
		return taxon;
254
	}
255

    
256

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

    
303

    
304

    
305

    
306

    
307
}
(3-3/9)