Project

General

Profile

Download (6.94 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.common.CdmUtils;
25
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
26
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.name.BotanicalName;
29
import eu.etaxonomy.cdm.model.name.NonViralName;
30
import eu.etaxonomy.cdm.model.name.Rank;
31
import eu.etaxonomy.cdm.model.reference.Reference;
32
import eu.etaxonomy.cdm.model.taxon.Classification;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
35

    
36

    
37
/**
38
 * @author a.mueller
39
 * @created 27.02.2012
40
 */
41
@Component
42
public class IndexFungorumGeneraImport  extends IndexFungorumImportBase {
43
	private static final Logger logger = Logger.getLogger(IndexFungorumGeneraImport.class);
44
	
45
	private static final String pluralString = "genera";
46
	private static final String dbTableName = "tblGenera";
47

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

    
52
	
53
	
54
	@Override
55
	protected String getIdQuery() {
56
		String result = " SELECT RECORD_NUMBER FROM " + getTableName() +
57
				" ORDER BY PreferredName ";
58
		return result;
59
	}
60

    
61
	/* (non-Javadoc)
62
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
63
	 */
64
	@Override
65
	protected String getRecordQuery(IndexFungorumImportConfigurator config) {
66
		String strRecordQuery = 
67
				" SELECT DISTINCT c.FamilyName, c.OrderName, c.SubclassName, c.ClassName, c.SubphylumName, c.PhylumName, c.KingdomName, g.* " +
68
                " FROM tblGenera AS g LEFT OUTER JOIN  dbo.[tblPESIfungi-Classification] AS c ON g.RECORD_NUMBER = c.PreferredNameFDCnumber " +
69
			" WHERE ( g.RECORD_NUMBER IN (" + ID_LIST_TOKEN + ") )" + 
70
			"";
71
		return strRecordQuery;
72
	}
73

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

    
86
				//TODO
87
				//DisplayName, NomRefCache
88

    
89
				Integer id = (Integer)rs.getObject("RECORD_NUMBER");
90
				
91
				
92
				String preferredName = rs.getString("PreferredName");
93
				if (StringUtils.isBlank(preferredName)){
94
					logger.warn("Preferred name is blank. This case is not yet handled by IF import. RECORD_NUMBER" + CdmUtils.Nz(id));
95
				}
96
				
97
				Rank rank = Rank.GENUS();
98
				NonViralName<?> name = BotanicalName.NewInstance(rank);
99
				name.setGenusOrUninomial(preferredName);
100
				
101
				Taxon taxon = Taxon.NewInstance(name, sourceReference);
102
				Taxon parent = getParentTaxon(state, rs);
103
				classification.addParentChild(parent, taxon, null, null);
104
				
105
				//author + publication
106
				makeAuthorAndPublication(state, rs, name);
107
				//source
108
				makeSource(state, taxon, id, NAMESPACE_GENERA );
109
				
110
				getTaxonService().saveOrUpdate(taxon);
111
			}
112

    
113
			
114
		} catch (Exception e) {
115
			e.printStackTrace();
116
			logger.error(e.getMessage());
117
			state.setSuccess(false);
118
			success = false;
119
		}
120
		return success;
121
		
122
	}
123
	
124
	private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
125
		String parentName = getParentNameString(rs);
126
		
127
		Taxon taxon = state.getRelatedObject(NAMESPACE_SUPRAGENERIC_NAMES, parentName, Taxon.class);
128
		if (taxon == null){
129
			logger.warn("Taxon not found for " + parentName);
130
		}
131
		return taxon;
132
	}
133

    
134

    
135
	private String getParentNameString(ResultSet rs) throws SQLException {
136
		String parentName = rs.getString("FamilyName");
137
		if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
138
			parentName = rs.getString("OrderName");
139
			if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
140
				parentName = rs.getString("SubclassName");
141
				if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
142
					parentName = rs.getString("ClassName");
143
					if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
144
						parentName = rs.getString("SubphylumName");
145
						if (parentName.equalsIgnoreCase(INCERTAE_SEDIS)){
146
							parentName = rs.getString("PhylumName");
147
							if (parentName.equalsIgnoreCase(INCERTAE_SEDIS) || parentName.equalsIgnoreCase(FOSSIL_FUNGI) ){
148
								parentName = rs.getString("KingdomName");
149
							}
150
						}
151
					}
152
				}
153
			}	
154
		}
155
		return parentName;
156
	}
157

    
158
	/* (non-Javadoc)
159
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
160
	 */
161
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
162
		String nameSpace;
163
		Class cdmClass;
164
		Set<String> idSet;
165
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
166
		
167
		try{
168
			Set<String> taxonNameSet = new HashSet<String>();
169
			while (rs.next()){
170
//				handleForeignKey(rs, taxonIdSet,"tu_acctaxon" );
171
			}
172
			
173
			//taxon map
174
			nameSpace = NAMESPACE_SUPRAGENERIC_NAMES ;
175
			cdmClass = TaxonBase.class;
176
//			idSet = taxonNameSet;
177
			Map<String, TaxonBase<?>> taxonMap = new HashMap<String, TaxonBase<?>>();
178
			List<TaxonBase> list = getTaxonService().listTaxaByName(Taxon.class, "*", null, null, null, null, 1000000, null);
179
			for (TaxonBase<?> taxon : list){
180
				taxonMap.put(CdmBase.deproxy(taxon.getName(), NonViralName.class).getGenusOrUninomial(), taxon);
181
			}
182
			result.put(nameSpace, taxonMap);
183
			
184
			//sourceReference
185
			Reference<?> sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
186
			Map<String, Reference> referenceMap = new HashMap<String, Reference>();
187
			referenceMap.put(SOURCE_REFERENCE, sourceReference);
188
			result.put(NAMESPACE_REFERENCE, referenceMap);
189
			
190
		} catch (SQLException e) {
191
			throw new RuntimeException(e);
192
		}
193
		return result;
194
	}
195
	
196

    
197

    
198
	/* (non-Javadoc)
199
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
200
	 */
201
	@Override
202
	protected boolean doCheck(IndexFungorumImportState state){
203
		return true;
204
	}
205
	
206
	/* (non-Javadoc)
207
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
208
	 */
209
	protected boolean isIgnore(IndexFungorumImportState state){
210
		return ! state.getConfig().isDoTaxa();
211
	}
212

    
213

    
214

    
215

    
216

    
217
}
(2-2/9)