Project

General

Profile

Download (4.63 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.io.pesi.out.PesiTransformer;
21
import eu.etaxonomy.cdm.model.common.CdmBase;
22
import eu.etaxonomy.cdm.model.name.Rank;
23
import eu.etaxonomy.cdm.model.name.TaxonName;
24
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
25
import eu.etaxonomy.cdm.model.reference.Reference;
26
import eu.etaxonomy.cdm.model.taxon.Taxon;
27

    
28

    
29
/**
30
 * @author a.mueller
31
 * @since 27.02.2012
32
 */
33
@Component
34
public class IndexFungorumSupraGeneraImport  extends IndexFungorumImportBase {
35
	private static final Logger logger = Logger.getLogger(IndexFungorumSupraGeneraImport.class);
36

    
37
	private static final String pluralString = "Supragenera";
38
	private static final String dbTableName = "tblSupragenericNames";
39

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

    
44

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

    
55

    
56
	@Override
57
	protected void doInvoke(IndexFungorumImportState state) {
58

    
59

    
60
		//handle source reference first
61
		Reference sourceReference = state.getConfig().getSourceReference();
62
		getReferenceService().save(sourceReference);
63

    
64
		//query
65
		String sql = getRecordQuery(state.getConfig());
66
		ResultSet rs = state.getConfig().getSource().getResultSet(sql);
67

    
68
		//transaction and related objects
69
		TransactionStatus tx = startTransaction();
70
		state.setRelatedObjects(getRelatedObjectsForPartition(null, state));
71
		sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
72

    
73
		try {
74
			while (rs.next()){
75

    
76
				//TODO
77
				//DisplayName, NomRefCache
78

    
79
				Double id = (Double)rs.getObject("RECORD NUMBER");
80

    
81
				String supragenericNames = rs.getString("Suprageneric names");
82
				//String preferredName = rs.getString("PreferredName");
83
				Integer rankFk = rs.getInt("PESI_RankFk");
84

    
85
				//name
86
				Rank rank = state.getTransformer().getRankByKey(String.valueOf(rankFk));
87
				TaxonName name = TaxonNameFactory.NewBotanicalInstance(rank);
88
				name.setGenusOrUninomial(supragenericNames);
89
				/*if (preferredName != null && !preferredName.equals(supragenericNames)){
90
					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);
91
				}*/
92

    
93
				//taxon
94
				Taxon taxon = Taxon.NewInstance(name, sourceReference);
95
				//author + nom.ref.
96
				makeAuthorAndPublication(state, rs, name);
97
				//source
98
				if (id != null){
99
					makeSource(state, taxon, id.intValue(), NAMESPACE_SUPRAGENERIC_NAMES );
100
				} else{
101
					makeSource(state, taxon, null,NAMESPACE_SUPRAGENERIC_NAMES);
102
				}
103
				getTaxonService().saveOrUpdate(taxon);
104
			}
105

    
106

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

    
116
	}
117

    
118

    
119
	private Taxon makeTaxon(IndexFungorumImportState state, String uninomial, Rank rank) {
120
	    TaxonName name = TaxonNameFactory.NewBotanicalInstance(rank);
121
		name.setGenusOrUninomial(uninomial);
122
		return Taxon.NewInstance(name, state.getConfig().getSourceReference());
123
	}
124

    
125

    
126
	@Override
127
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
128
		HashMap<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String,? extends CdmBase>>();  //not needed here
129

    
130
		//sourceReference
131
		Reference sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
132
		Map<String, Reference> referenceMap = new HashMap<String, Reference>();
133
		referenceMap.put(SOURCE_REFERENCE, sourceReference);
134
		result.put(NAMESPACE_REFERENCE, referenceMap);
135

    
136
		return result;
137
	}
138

    
139
	@Override
140
	protected boolean doCheck(IndexFungorumImportState state){
141
		return true;
142
	}
143

    
144
	@Override
145
	protected boolean isIgnore(IndexFungorumImportState state){
146
		return ! state.getConfig().isDoTaxa();
147
	}
148

    
149

    
150

    
151

    
152

    
153
}
(9-9/10)