Project

General

Profile

Download (4.7 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.util.HashMap;
14
import java.util.Map;
15

    
16
import org.apache.log4j.Logger;
17
import org.springframework.stereotype.Component;
18
import org.springframework.transaction.TransactionStatus;
19

    
20
import eu.etaxonomy.cdm.common.CdmUtils;
21
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
22
import eu.etaxonomy.cdm.model.common.CdmBase;
23
import eu.etaxonomy.cdm.model.name.BotanicalName;
24
import eu.etaxonomy.cdm.model.name.NonViralName;
25
import eu.etaxonomy.cdm.model.name.Rank;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.model.taxon.Taxon;
28

    
29

    
30
/**
31
 * @author a.mueller
32
 * @created 27.02.2012
33
 */
34
@Component
35
public class IndexFungorumSupraGeneraImport  extends IndexFungorumImportBase {
36
	private static final Logger logger = Logger.getLogger(IndexFungorumSupraGeneraImport.class);
37
	
38
	private static final String pluralString = "Supragenera";
39
	private static final String dbTableName = "tblSupragenericNames";
40

    
41
	public IndexFungorumSupraGeneraImport(){
42
		super(pluralString, dbTableName, null);
43
	}
44

    
45

    
46
	@Override
47
	protected String getRecordQuery(IndexFungorumImportConfigurator config) {
48
		String strRecordQuery = 
49
			" SELECT * " + 
50
			" FROM [tblSupragenericNames] " +
51
//			" WHERE ( dr.id IN (" + ID_LIST_TOKEN + ") )";
52
			"";
53
		return strRecordQuery;
54
	}
55

    
56
	
57
	@Override
58
	protected void doInvoke(IndexFungorumImportState state) {
59
		
60
		
61
		//handle source reference first
62
		Reference sourceReference = state.getConfig().getSourceReference();
63
		getReferenceService().save(sourceReference);
64
		
65
		//query
66
		String sql = getRecordQuery(state.getConfig());
67
		ResultSet rs = state.getConfig().getSource().getResultSet(sql);
68
		
69
		//transaction and related objects
70
		TransactionStatus tx = startTransaction();
71
		state.setRelatedObjects((Map)getRelatedObjectsForPartition(null, state));
72
		sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
73
		
74
		try {
75
			while (rs.next()){
76

    
77
				//TODO
78
				//DisplayName, NomRefCache
79

    
80
				Double id = (Double)rs.getObject("RECORD NUMBER");
81
				
82
				String supragenericNames = rs.getString("Suprageneric names");
83
				//String preferredName = rs.getString("PreferredName");
84
				Integer rankFk = rs.getInt("PESI_RankFk");
85
				
86
				//name
87
				Rank rank = state.getTransformer().getRankByKey(String.valueOf(rankFk));
88
				NonViralName<?> name = BotanicalName.NewInstance(rank);
89
				name.setGenusOrUninomial(supragenericNames);
90
				/*if (preferredName != null && !preferredName.equals(supragenericNames)){
91
					logger.warn("Suprageneric names and preferredName is not equal. This case is not yet handled by IF import. I take SupragenericNames for import. RECORD NUMBER" +id);
92
				}*/
93
				
94
				//taxon
95
				Taxon taxon = Taxon.NewInstance(name, sourceReference);
96
				//author + nom.ref.
97
				makeAuthorAndPublication(state, rs, name);
98
				//source
99
				if (id != null){
100
					makeSource(state, taxon, id.intValue(), NAMESPACE_SUPRAGENERIC_NAMES );
101
				} else{
102
					makeSource(state, taxon, null,NAMESPACE_SUPRAGENERIC_NAMES);
103
				}
104
				getTaxonService().saveOrUpdate(taxon);
105
			}
106

    
107
			
108
		} catch (Exception e) {
109
			e.printStackTrace();
110
			logger.error(e.getMessage());
111
			tx.setRollbackOnly();
112
			state.setSuccess(false);
113
		}
114
		commitTransaction(tx);
115
		return;
116
		
117
	}
118

    
119

    
120
	private Taxon makeTaxon(IndexFungorumImportState state, String uninomial, Rank rank) {
121
		NonViralName<?> name = BotanicalName.NewInstance(rank);
122
		name.setGenusOrUninomial(uninomial);
123
		return Taxon.NewInstance(name, state.getConfig().getSourceReference());
124
	}
125

    
126

    
127
	@Override
128
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
129
		HashMap<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String,? extends CdmBase>>();  //not needed here
130
		
131
		//sourceReference
132
		Reference<?> sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
133
		Map<String, Reference> referenceMap = new HashMap<String, Reference>();
134
		referenceMap.put(SOURCE_REFERENCE, sourceReference);
135
		result.put(NAMESPACE_REFERENCE, referenceMap);
136

    
137
		return result;
138
	}
139
	
140
	@Override
141
	protected boolean doCheck(IndexFungorumImportState state){
142
		return true;
143
	}
144
	
145
	@Override
146
	protected boolean isIgnore(IndexFungorumImportState state){
147
		return ! state.getConfig().isDoTaxa();
148
	}
149

    
150

    
151

    
152

    
153

    
154
}
(9-9/10)