Project

General

Profile

Download (6.88 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.commons.lang.StringUtils;
21
import org.apache.log4j.Logger;
22
import org.springframework.stereotype.Component;
23

    
24
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
25
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
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
/**
37
 * @author a.mueller
38
 * @since 27.02.2012
39
 */
40
@Component
41
public class IndexFungorumGeneraImport  extends IndexFungorumImportBase {
42

    
43
    private static final long serialVersionUID = -265928225339065992L;
44
    private static final Logger logger = Logger.getLogger(IndexFungorumGeneraImport.class);
45

    
46
	private static final String pluralString = "genera";
47
	private static final String dbTableName = "tblGenera";
48

    
49
	public IndexFungorumGeneraImport(){
50
		super(pluralString, dbTableName, null);
51
	}
52

    
53
	@Override
54
	protected String getIdQuery() {
55
		String result = " SELECT [RECORD NUMBER] FROM " + getTableName() +
56
				" ORDER BY [NAME OF FUNGUS] ";
57
		return result;
58
	}
59

    
60
	@Override
61
	protected String getRecordQuery(IndexFungorumImportConfigurator config) {
62
		String strRecordQuery =
63
				" SELECT DISTINCT c.[Family name], c.[Order name], c.[Subclass name], c.[Class name], c.[Subphylum name], c.[Phylum name], c.[Kingdom name], g.* " +
64
                " FROM tblGenera AS g LEFT OUTER JOIN  dbo.[tblPESIfungi-Classification] AS c ON g.[RECORD NUMBER] = c.PreferredNameFDCnumber " +
65
			" WHERE ( g.[RECORD NUMBER] IN (" + ID_LIST_TOKEN + ") )" +
66
			"";
67
		return strRecordQuery;
68
	}
69

    
70
	@Override
71
	public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner,
72
	        IndexFungorumImportState state) {
73

    
74
	    boolean success =true;
75
		Reference sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
76
		ResultSet rs = partitioner.getResultSet();
77
		Classification classification = getClassification(state);
78
		try {
79
			while (rs.next()){
80

    
81
				//TODO
82
				//DisplayName, NomRefCache
83

    
84
				Double id = (Double)rs.getObject("RECORD NUMBER");
85

    
86
				String preferredName = rs.getString("NAME OF FUNGUS");
87
				if (StringUtils.isBlank(preferredName)){
88
					logger.warn("Preferred name is blank. This case is not yet handled by IF import. RECORD NUMBER" + id);
89
				}
90

    
91
				Rank rank = Rank.GENUS();
92
				TaxonName name = TaxonNameFactory.NewBotanicalInstance(rank);
93
				name.setGenusOrUninomial(preferredName);
94

    
95
				Taxon taxon = Taxon.NewInstance(name, sourceReference);
96
				Taxon parent = getParentTaxon(state, rs);
97
				classification.addParentChild(parent, taxon, null, null);
98

    
99
				//author + publication
100
				makeAuthorAndPublication(state, rs, name);
101
				//source
102
				//makeSource(state, taxon, id, NAMESPACE_GENERA );
103
				if (id != null){
104
					makeSource(state, taxon, id.intValue(), NAMESPACE_GENERA );
105
				} else{
106
					makeSource(state, taxon, null,NAMESPACE_GENERA);
107
				}
108
				getTaxonService().saveOrUpdate(taxon);
109
			}
110
		} catch (Exception e) {
111
			e.printStackTrace();
112
			logger.error(e.getMessage());
113
			state.setSuccess(false);
114
			success = false;
115
		}
116
		return success;
117

    
118
	}
119

    
120
	private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
121
		String parentName = getParentNameString(rs);
122
		if (parentName == null){
123
			logger.warn("Parent name not found for: " + rs.getString("NAME OF FUNGUS"));
124
		}
125
		Taxon taxon = state.getRelatedObject(NAMESPACE_SUPRAGENERIC_NAMES, parentName, Taxon.class);
126
		if (taxon == null){
127
			logger.warn("Taxon not found for " + parentName + " name of fungus: " +rs.getString("NAME OF FUNGUS") );
128
		}
129
		return taxon;
130
	}
131

    
132
	private String getParentNameString(ResultSet rs) throws SQLException {
133
		String parentName = rs.getString("Family name");
134
		if (parentName == null){
135
			logger.warn(rs.getObject("NAME OF FUNGUS") + " has no family name. ");
136
			return null;
137
		}
138
		if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
139
			parentName = rs.getString("Order name");
140
			if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
141
				parentName = rs.getString("Subclass name");
142
				if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
143
					parentName = rs.getString("Class name");
144
					if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
145
						parentName = rs.getString("Subphylum name");
146
						if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
147
							parentName = rs.getString("Phylum name");
148
							if (parentName.equalsIgnoreCase(INCERTAE_SEDIS) || parentName.equalsIgnoreCase(FOSSIL_FUNGI) ){
149
								parentName = rs.getString("Kingdom name");
150
							}
151
						}
152
					}
153
				}
154
			}
155
		}
156
		return parentName;
157
	}
158

    
159
	@Override
160
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
161
		String nameSpace;
162
		Class<?> cdmClass;
163
		Set<String> idSet;
164
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
165

    
166
		try{
167
			Set<String> taxonNameSet = new HashSet<String>();
168
			while (rs.next()){
169
//				handleForeignKey(rs, taxonIdSet,"tu_acctaxon" );
170
			}
171

    
172
			//taxon map
173
			nameSpace = NAMESPACE_SUPRAGENERIC_NAMES ;
174
			cdmClass = TaxonBase.class;
175
//			idSet = taxonNameSet;
176
			Map<String, TaxonBase<?>> taxonMap = new HashMap<String, TaxonBase<?>>();
177
			List<TaxonBase> list = getTaxonService().listTaxaByName(Taxon.class, "*", null, null, null, "*", null, 1000000, null);
178
			for (TaxonBase<?> taxon : list){
179
				taxonMap.put(CdmBase.deproxy(taxon.getName()).getGenusOrUninomial(), taxon);
180
			}
181
			result.put(nameSpace, taxonMap);
182

    
183
			//sourceReference
184
			Reference sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
185
			Map<String, Reference> referenceMap = new HashMap<String, Reference>();
186
			referenceMap.put(SOURCE_REFERENCE, sourceReference);
187
			result.put(NAMESPACE_REFERENCE, referenceMap);
188

    
189
		} catch (SQLException e) {
190
			throw new RuntimeException(e);
191
		}
192
		return result;
193
	}
194

    
195
	@Override
196
	protected boolean doCheck(IndexFungorumImportState state){
197
		return true;
198
	}
199

    
200
	@Override
201
	protected boolean isIgnore(IndexFungorumImportState state){
202
		return ! state.getConfig().isDoTaxa();
203
	}
204
}
(2-2/10)