Project

General

Profile

Download (6.84 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.List;
16
import java.util.Map;
17

    
18
import org.apache.commons.lang.StringUtils;
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21

    
22
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
23
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.model.name.Rank;
26
import eu.etaxonomy.cdm.model.name.TaxonName;
27
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
28
import eu.etaxonomy.cdm.model.reference.Reference;
29
import eu.etaxonomy.cdm.model.taxon.Classification;
30
import eu.etaxonomy.cdm.model.taxon.Taxon;
31
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
32

    
33

    
34
/**
35
 * @author a.mueller
36
 * @since 27.02.2012
37
 */
38
@Component
39
public class IndexFungorumGeneraImport  extends IndexFungorumImportBase {
40

    
41
    private static final long serialVersionUID = -265928225339065992L;
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
	@Override
52
	protected String getIdQuery() {
53
		String result = " SELECT [RECORD NUMBER] FROM " + getTableName() +
54
				" ORDER BY [NAME OF FUNGUS] ";
55
		return result;
56
	}
57

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

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

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

    
79
				//Don't use (created bei Marc): DisplayName, NomRefCache
80

    
81
				Integer id =( (Number)rs.getObject("RECORD NUMBER")).intValue();
82

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

    
88
				Rank rank = Rank.GENUS();
89
				TaxonName name = TaxonNameFactory.NewBotanicalInstance(rank);
90
				name.setGenusOrUninomial(preferredName);
91

    
92
				Taxon taxon = Taxon.NewInstance(name, sourceReference);
93
				Taxon parent = getParentTaxon(state, rs);
94
				classification.addParentChild(parent, taxon, null, null);
95

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

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

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

    
155
	@Override
156
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
157
		String nameSpace;
158
//		Class<?> cdmClass;
159
//		Set<String> idSet;
160
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
161

    
162
		try{
163
//			Set<String> taxonNameSet = new HashSet<>();
164
			while (rs.next()){
165
//				handleForeignKey(rs, taxonIdSet,"tu_acctaxon" );
166
			}
167

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

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

    
186
		} catch (SQLException e) {
187
			throw new RuntimeException(e);
188
		}
189
		return result;
190
	}
191

    
192
	@Override
193
	protected boolean doCheck(IndexFungorumImportState state){
194
		return true;
195
	}
196

    
197
	@Override
198
	protected boolean isIgnore(IndexFungorumImportState state){
199
		return ! state.getConfig().isDoTaxa();
200
	}
201
}
(2-2/10)