Project

General

Profile

Download (6.98 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.BotanicalName;
28
import eu.etaxonomy.cdm.model.name.NonViralName;
29
import eu.etaxonomy.cdm.model.name.Rank;
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
 * @created 27.02.2012
39
 */
40
@Component
41
public class IndexFungorumGeneraImport  extends IndexFungorumImportBase {
42
	private static final Logger logger = Logger.getLogger(IndexFungorumGeneraImport.class);
43

    
44
	private static final String pluralString = "genera";
45
	private static final String dbTableName = "tblGenera";
46

    
47
	public IndexFungorumGeneraImport(){
48
		super(pluralString, dbTableName, null);
49
	}
50

    
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
	/* (non-Javadoc)
61
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
62
	 */
63
	@Override
64
	protected String getRecordQuery(IndexFungorumImportConfigurator config) {
65
		String strRecordQuery =
66
				" SELECT DISTINCT c.[Family name], c.[Order name], c.[Subclass name], c.[Class name], c.[Subphylum name], c.[Phylum name], c.[Kingdom name], g.* " +
67
                " FROM tblGenera AS g LEFT OUTER JOIN  dbo.[tblPESIfungi-Classification] AS c ON g.[RECORD NUMBER] = c.PreferredNameFDCnumber " +
68
			" WHERE ( g.[RECORD NUMBER] IN (" + ID_LIST_TOKEN + ") )" +
69
			"";
70
		return strRecordQuery;
71
	}
72

    
73

    
74

    
75

    
76
	@Override
77
	public boolean doPartition(ResultSetPartitioner partitioner, IndexFungorumImportState state) {
78
		boolean success =true;
79
		Reference sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
80
		ResultSet rs = partitioner.getResultSet();
81
		Classification classification = getClassification(state);
82
		try {
83
			while (rs.next()){
84

    
85
				//TODO
86
				//DisplayName, NomRefCache
87

    
88
				Double id = (Double)rs.getObject("RECORD NUMBER");
89

    
90

    
91
				String preferredName = rs.getString("NAME OF FUNGUS");
92
				if (StringUtils.isBlank(preferredName)){
93
					logger.warn("Preferred name is blank. This case is not yet handled by IF import. RECORD NUMBER" + id);
94
				}
95

    
96
				Rank rank = Rank.GENUS();
97
				NonViralName<?> name = BotanicalName.NewInstance(rank);
98
				name.setGenusOrUninomial(preferredName);
99

    
100
				Taxon taxon = Taxon.NewInstance(name, sourceReference);
101
				Taxon parent = getParentTaxon(state, rs);
102
				classification.addParentChild(parent, taxon, null, null);
103

    
104
				//author + publication
105
				makeAuthorAndPublication(state, rs, name);
106
				//source
107
				//makeSource(state, taxon, id, NAMESPACE_GENERA );
108
				if (id != null){
109
					makeSource(state, taxon, id.intValue(), NAMESPACE_GENERA );
110
				} else{
111
					makeSource(state, taxon, null,NAMESPACE_GENERA);
112
				}
113
				getTaxonService().saveOrUpdate(taxon);
114
			}
115

    
116

    
117
		} catch (Exception e) {
118
			e.printStackTrace();
119
			logger.error(e.getMessage());
120
			state.setSuccess(false);
121
			success = false;
122
		}
123
		return success;
124

    
125
	}
126

    
127
	private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
128
		String parentName = getParentNameString(rs);
129
		if (parentName == null){
130
			logger.warn("Parent name not found for: " + rs.getString("NAME OF FUNGUS"));
131
		}
132
		Taxon taxon = state.getRelatedObject(NAMESPACE_SUPRAGENERIC_NAMES, parentName, Taxon.class);
133
		if (taxon == null){
134
			logger.warn("Taxon not found for " + parentName + " name of fungus: " +rs.getString("NAME OF FUNGUS") );
135
		}
136
		return taxon;
137
	}
138

    
139

    
140
	private String getParentNameString(ResultSet rs) throws SQLException {
141
		String parentName = rs.getString("Family name");
142
		if (parentName == null){
143
			logger.warn(rs.getObject("NAME OF FUNGUS") + " has no family name. ");
144
			return null;
145
		}
146
		if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
147
			parentName = rs.getString("Order name");
148
			if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
149
				parentName = rs.getString("Subclass name");
150
				if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
151
					parentName = rs.getString("Class name");
152
					if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
153
						parentName = rs.getString("Subphylum name");
154
						if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
155
							parentName = rs.getString("Phylum name");
156
							if (parentName.equalsIgnoreCase(INCERTAE_SEDIS) || parentName.equalsIgnoreCase(FOSSIL_FUNGI) ){
157
								parentName = rs.getString("Kingdom name");
158
							}
159
						}
160
					}
161
				}
162
			}
163
		}
164
		return parentName;
165
	}
166

    
167

    
168
	@Override
169
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
170
		String nameSpace;
171
		Class<?> cdmClass;
172
		Set<String> idSet;
173
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
174

    
175
		try{
176
			Set<String> taxonNameSet = new HashSet<String>();
177
			while (rs.next()){
178
//				handleForeignKey(rs, taxonIdSet,"tu_acctaxon" );
179
			}
180

    
181
			//taxon map
182
			nameSpace = NAMESPACE_SUPRAGENERIC_NAMES ;
183
			cdmClass = TaxonBase.class;
184
//			idSet = taxonNameSet;
185
			Map<String, TaxonBase<?>> taxonMap = new HashMap<String, TaxonBase<?>>();
186
			List<TaxonBase> list = getTaxonService().listTaxaByName(Taxon.class, "*", null, null, null, null, 1000000, null);
187
			for (TaxonBase<?> taxon : list){
188
				taxonMap.put(CdmBase.deproxy(taxon.getName(), NonViralName.class).getGenusOrUninomial(), taxon);
189
			}
190
			result.put(nameSpace, taxonMap);
191

    
192
			//sourceReference
193
			Reference sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
194
			Map<String, Reference> referenceMap = new HashMap<String, Reference>();
195
			referenceMap.put(SOURCE_REFERENCE, sourceReference);
196
			result.put(NAMESPACE_REFERENCE, referenceMap);
197

    
198
		} catch (SQLException e) {
199
			throw new RuntimeException(e);
200
		}
201
		return result;
202
	}
203

    
204

    
205
	@Override
206
	protected boolean doCheck(IndexFungorumImportState state){
207
		return true;
208
	}
209

    
210
	@Override
211
	protected boolean isIgnore(IndexFungorumImportState state){
212
		return ! state.getConfig().isDoTaxa();
213
	}
214

    
215

    
216

    
217

    
218

    
219
}
(2-2/10)