Project

General

Profile

Download (11.5 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.Rank;
28
import eu.etaxonomy.cdm.model.name.TaxonName;
29
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
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
 * @author a.mueller
37
 * @since 27.02.2012
38
 */
39
@Component
40
public class IndexFungorumHigherClassificationImport  extends IndexFungorumImportBase {
41

    
42
    private static final long serialVersionUID = -6841466146506309309L;
43
    private static final Logger logger = Logger.getLogger(IndexFungorumHigherClassificationImport.class);
44

    
45
	private static final String pluralString = "higher classifications";
46
	private static final String dbTableName = "tblPESIfungi-Classification";
47

    
48
	public IndexFungorumHigherClassificationImport(){
49
		super(pluralString, dbTableName, null);
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
	@Override
64
	protected void doInvoke(IndexFungorumImportState state) {
65

    
66
	    logger.info("Start higher classification ...");
67

    
68
		String sql = getRecordQuery(state.getConfig());
69
		ResultSet rs = state.getConfig().getSource().getResultSet(sql);
70

    
71
		//only 1 partition here
72

    
73
		String lastKingdom = "";
74
		String lastPhylum = "";
75
		String lastSubphylum = "";
76
		String lastClassname = "";
77
		String lastSubclass = "";
78
		String lastOrder = "";
79
		String lastFamily = "";
80
//		String lastGenus = "";
81
//		String lastSpecies = "";
82

    
83
		Taxon taxonKingdom = null;
84
		Taxon taxonPhylum = null;
85
		Taxon taxonSubphylum = null;
86
		Taxon taxonClass = null;
87
		Taxon taxonSubclass = null;
88
		Taxon taxonOrder = null;
89
		Taxon taxonFamily = null;
90
//		Taxon taxonGenus = null;
91
//		Taxon taxonSpecies = null;
92

    
93
		Taxon higherTaxon = null;
94

    
95
		TransactionStatus tx = startTransaction();
96
		ResultSet rsRelatedObjects = state.getConfig().getSource().getResultSet(sql);
97
		state.setRelatedObjects(getRelatedObjectsForPartition(rsRelatedObjects, state));
98

    
99
		Classification classification = getClassification(state);
100

    
101
		try {
102
			while (rs.next()){
103
				String kingdom = rs.getString("Kingdom name");
104
				String phylum = rs.getString("Phylum name");
105
				String subphylum = rs.getString("Subphylum name");
106
				String classname = rs.getString("Class name");
107
				String subclass = rs.getString("Subclass name");
108
				String order = rs.getString("Order name");
109
				String family = rs.getString("Family name");
110
//				String genus = rs.getString("GenusName");
111
//				String species = rs.getString("SpeciesName");
112

    
113
//				if (isNewTaxon(species, lastSpecies)){
114
//					if (isNewTaxon(genus, lastGenus)){
115
				if (isNewTaxon(family, lastFamily)){
116
					if (isNewTaxon(order,lastOrder)){
117
						if (isNewTaxon(subclass,lastSubclass)){
118
							if (isNewTaxon(classname,lastClassname)){
119
								if (isNewTaxon(subphylum, lastSubphylum)){
120
									if (isNewTaxon(phylum,lastPhylum)){
121
										if (isNewTaxon(kingdom,lastKingdom)){
122
											taxonKingdom = makeTaxon(state, kingdom, Rank.KINGDOM());
123
											lastKingdom = kingdom;
124
											logger.info("Import kingdom " +  kingdom);
125
											getTaxonService().saveOrUpdate(taxonKingdom);
126
										}else{
127
											higherTaxon = taxonKingdom;
128
										}
129
										higherTaxon = isIncertisSedis(kingdom) ? higherTaxon : taxonKingdom;
130
										Rank newRank = (lastKingdom.equals("Fungi") ? null : Rank.PHYLUM());
131
										taxonPhylum = makeTaxon(state, phylum, newRank);
132
										if (taxonPhylum != null){
133
											classification.addParentChild(higherTaxon, taxonPhylum, null, null);
134
										}
135
										higherTaxon = isIncertisSedis(phylum) ? higherTaxon : taxonPhylum;
136
										lastPhylum = phylum;
137
										logger.info("Import Phylum " +  phylum);
138
									}else{
139
										higherTaxon = taxonPhylum;
140
									}
141
									Rank newRank = (lastKingdom.equals("Fungi") ? null : Rank.SUBPHYLUM());
142
									taxonSubphylum = makeTaxon(state, subphylum, newRank);
143
									if (taxonSubphylum != null){
144
										getClassification(state).addParentChild(higherTaxon,taxonSubphylum, null, null);
145
									}
146
									higherTaxon = isIncertisSedis(subphylum) ? higherTaxon : taxonSubphylum;
147
									lastSubphylum = subphylum;
148
								}else{
149
									higherTaxon = taxonSubphylum;
150
								}
151
								taxonClass = makeTaxon(state, classname, Rank.CLASS());
152
								if (taxonClass != null){
153
									getClassification(state).addParentChild(higherTaxon, taxonClass, null, null);
154
								}
155
								higherTaxon = isIncertisSedis(classname) ? higherTaxon : taxonClass;
156
								lastClassname = classname;
157
							}else{
158
								higherTaxon = taxonClass;
159
							}
160
							taxonSubclass = makeTaxon(state, subclass, Rank.SUBCLASS());
161
							if (taxonSubclass != null){
162
								getClassification(state).addParentChild(higherTaxon, taxonSubclass,null, null);
163
							}
164
							higherTaxon = isIncertisSedis(subclass) ? higherTaxon : taxonSubclass;
165
							lastSubclass = subclass;
166
						}else{
167
							higherTaxon = taxonSubclass;
168
						}
169
						taxonOrder = makeTaxon(state, order, Rank.ORDER());
170
						if (taxonOrder != null){
171
							getClassification(state).addParentChild(higherTaxon, taxonOrder, null, null);
172
						}
173
						higherTaxon = isIncertisSedis(order) ? higherTaxon : taxonOrder;
174
						lastOrder = order;
175
					}else{
176
						higherTaxon = taxonOrder;
177
					}
178
					taxonFamily = makeTaxon(state, family, Rank.FAMILY());
179
					if (taxonFamily != null){
180
						try{
181
							//if this shown a warning see single issue in #2826 about Glomerelllaceae (which has 2 different parents)
182
						    getClassification(state).addParentChild(higherTaxon, taxonFamily, null, null);
183
						}catch(IllegalStateException e){
184
							if (e.getMessage().startsWith("The child taxon is already part of the tree")){
185
								//TaxonNode node = getClassification(state).getNode(taxonFamily);
186
								logger.warn(e.getMessage() + taxonFamily.getTitleCache() + " " + higherTaxon.getTitleCache());
187

    
188
								}
189
						}
190
					}
191
					higherTaxon = isIncertisSedis(family) ? higherTaxon : taxonFamily;
192
					lastFamily = family;
193
					getTaxonService().saveOrUpdate(higherTaxon);
194
				}
195
//						else{
196
//							higherTaxon = taxonFamily;
197
//						}
198
//						taxonGenus = makeTaxon(state, genus, Rank.GENUS());
199
//						if (taxonGenus != null){
200
//							getClassification(state).addParentChild(higherTaxon, taxonGenus, null, null);
201
//						}
202
//						higherTaxon = isIncertisSedis(genus) ? higherTaxon : taxonGenus;
203
//						lastGenus = genus;
204
//					}else{
205
//						higherTaxon = taxonGenus;
206
//					}
207
//					taxonSpecies = makeTaxon(state, species, Rank.SPECIES());
208
//					if (taxonSpecies != null){
209
//						getClassification(state).addParentChild(higherTaxon, taxonSpecies, null, null);
210
//					}
211
//					higherTaxon = isIncertisSedis(species) ? higherTaxon : taxonSpecies;
212
//					lastSpecies = species;
213
//					getTaxonService().saveOrUpdate(higherTaxon);
214
//				}
215
				getTaxonService().saveOrUpdate(higherTaxon);
216
			}
217

    
218

    
219
		} catch (SQLException e) {
220
			e.printStackTrace();
221
			logger.error(e.getMessage());
222
			tx.setRollbackOnly();
223
			state.setSuccess(false);
224
		}
225
		commitTransaction(tx);
226
		logger.info("End higher classification ...");
227

    
228
		return;
229

    
230
	}
231

    
232

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

    
237

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

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

    
265

    
266
	@Override
267
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
268
		String nameSpace;
269
		Class<?> cdmClass;
270
		Set<String> idSet;
271
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
272

    
273
		try{
274
			Set<String> taxonNameSet = new HashSet<String>();
275
//			while (rs.next()){
276
//				handleForeignKey(rs, taxonIdSet,"tu_accfinal" );
277
//			}
278

    
279
			//taxon map
280
			nameSpace = IndexFungorumSupraGeneraImport.NAMESPACE_SUPRAGENERIC_NAMES ;
281
			cdmClass = TaxonBase.class;
282
//			idSet = taxonNameSet;
283
			Map<String, TaxonBase<?>> taxonMap = new HashMap<String, TaxonBase<?>>();
284
			List<Taxon> list = getTaxonService().list(Taxon.class, null, null, null, null);
285
			for (Taxon taxon : list){
286
				taxonMap.put(CdmBase.deproxy(taxon.getName()).getGenusOrUninomial(), taxon);
287
			}
288
			result.put(nameSpace, taxonMap);
289

    
290
			//source reference
291
			Reference sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
292
			Map<String, Reference> referenceMap = new HashMap<String, Reference>();
293
			referenceMap.put(SOURCE_REFERENCE, sourceReference);
294
			result.put(NAMESPACE_REFERENCE, referenceMap);
295

    
296
		} catch (Exception e) {
297
			throw new RuntimeException(e);
298
		}
299
		return result;
300
	}
301

    
302
	@Override
303
	protected boolean doCheck(IndexFungorumImportState state){
304
		return true;
305
	}
306

    
307
	@Override
308
	protected boolean isIgnore(IndexFungorumImportState state){
309
		return ! state.getConfig().isDoRelTaxa();
310
	}
311

    
312

    
313

    
314

    
315

    
316
}
(3-3/10)