Project

General

Profile

Download (4.89 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
 * @author a.mueller
30
 * @since 27.02.2012
31
 */
32
@Component
33
public class IndexFungorumSupraGeneraImport  extends IndexFungorumImportBase {
34

    
35
    private static final long serialVersionUID = -8504227175493151403L;
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
	private static final String SUPRAGENERIC_NAMES = "Suprageneric names";
42
	private static final String COL_RECORD_NUMBER = "RECORD NUMBER";
43

    
44
	public IndexFungorumSupraGeneraImport(){
45
		super(pluralString, dbTableName, null);
46
	}
47

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

    
58
	@Override
59
	protected void doInvoke(IndexFungorumImportState state) {
60

    
61
	    logger.info("Start supra genera ...");
62
		//handle source reference first
63
		Reference sourceReference = state.getConfig().getSourceReference();
64
		getReferenceService().save(sourceReference);
65

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

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

    
75
		try {
76
			while (rs.next()){
77

    
78
				//Don't use (created by Marc): DisplayName, NomRefCache
79

    
80
				Integer id = ((Number)rs.getObject(COL_RECORD_NUMBER)).intValue();
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
				TaxonName name = TaxonNameFactory.NewBotanicalInstance(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
		} catch (Exception e) {
107
			e.printStackTrace();
108
			logger.error(e.getMessage());
109
			tx.setRollbackOnly();
110
			state.setSuccess(false);
111
		}
112
		commitTransaction(tx);
113
	    logger.info("End supra genera ...");
114
		return;
115
	}
116

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

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

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

    
133
		return result;
134
	}
135

    
136
	@Override
137
	protected boolean doCheck(IndexFungorumImportState state){
138
		return true;
139
	}
140

    
141
	@Override
142
	protected boolean isIgnore(IndexFungorumImportState state){
143
		return ! state.getConfig().isDoTaxa();
144
	}
145
}
(9-9/10)