Project

General

Profile

Download (6.49 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.Map;
17
import java.util.Set;
18

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

    
23
import eu.etaxonomy.cdm.common.CdmUtils;
24
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
25
import eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer;
26
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.Extension;
29
import eu.etaxonomy.cdm.model.common.ExtensionType;
30
import eu.etaxonomy.cdm.model.common.MarkerType;
31
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
32
import eu.etaxonomy.cdm.model.name.NonViralName;
33
import eu.etaxonomy.cdm.model.name.Rank;
34
import eu.etaxonomy.cdm.model.reference.Reference;
35
import eu.etaxonomy.cdm.model.taxon.Classification;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
39

    
40

    
41
/**
42
 * @author a.mueller
43
 * @created 27.02.2012
44
 */
45
@Component
46
public class IndexFungorumSpeciesImport  extends IndexFungorumImportBase {
47
	private static final Logger logger = Logger.getLogger(IndexFungorumSpeciesImport.class);
48
	
49
	private static final String pluralString = "species";
50
	private static final String dbTableName = "[tblPESIfungi-IFdata]";
51

    
52
	public IndexFungorumSpeciesImport(){
53
		super(pluralString, dbTableName, null);
54
	}
55

    
56

    
57
	
58
	
59
	@Override
60
	protected String getIdQuery() {
61
		String result = " SELECT PreferredNameIFnumber FROM " + getTableName() +
62
				" ORDER BY PreferredName ";
63
		return result;
64
	}
65

    
66

    
67

    
68

    
69
	/* (non-Javadoc)
70
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
71
	 */
72
	@Override
73
	protected String getRecordQuery(IndexFungorumImportConfigurator config) {
74
		String strRecordQuery = 
75
				" SELECT DISTINCT distribution.PreferredNameFDCnumber, species.* , cl.[Phylum name]" +
76
				" FROM tblPESIfungi AS distribution RIGHT OUTER JOIN  dbo.[tblPESIfungi-IFdata] AS species ON distribution.PreferredNameIFnumber = species.PreferredNameIFnumber " +
77
					" LEFT OUTER JOIN [tblPESIfungi-Classification] cl ON species.PreferredName   = cl.PreferredName " +
78
				" WHERE ( species.PreferredNameIFnumber IN (" + ID_LIST_TOKEN + ") )" +
79
			"";
80
		return strRecordQuery;
81
	}
82

    
83
	
84
	@Override
85
	public boolean doPartition(ResultSetPartitioner partitioner, IndexFungorumImportState state) {
86
		boolean success = true;
87
		Reference<?> sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
88
		ResultSet rs = partitioner.getResultSet();
89
		
90
		Classification classification = getClassification(state);
91
		try {
92
			while (rs.next()){
93

    
94
				//DisplayName, NomRefCache -> don't use, created by Marc
95

    
96
				Integer id = (Integer)rs.getObject("PreferredNameIFnumber");
97
				String phylumName = rs.getString("Phylum name");
98
				
99
				String preferredName = rs.getString("PreferredName");
100
				if (StringUtils.isBlank(preferredName)){
101
					logger.warn("Preferred name is blank. This case is not yet handled by IF import. RECORD UMBER" + CdmUtils.Nz(id));
102
				}
103
				
104
				Rank rank = Rank.SPECIES();
105
				
106
				NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
107
				NonViralName<?> name = parser.parseSimpleName(preferredName, NomenclaturalCode.ICNAFP, rank);
108
				
109
				Taxon taxon = Taxon.NewInstance(name, sourceReference);
110
				Taxon parent = getParentTaxon(state, rs);
111
				if (parent == null){
112
					logger.warn("parent not found for name:" +preferredName);
113
				}
114
				classification.addParentChild(parent, taxon, null, null);
115
				
116
				//author + publication
117
				makeAuthorAndPublication(state, rs, name);
118
				//source
119
				makeSource(state, taxon, id, NAMESPACE_SPECIES );
120
				
121
				//fossil
122
				if (FOSSIL_FUNGI.equalsIgnoreCase(phylumName)){
123
					ExtensionType fossilExtType = getExtensionType(state, ErmsTransformer.uuidFossilStatus, "fossil status", "fossil status", "fos. stat.");
124
					Extension.NewInstance(taxon, PesiTransformer.STR_FOSSIL_ONLY, fossilExtType); 
125
				}
126
				//save
127
				getTaxonService().saveOrUpdate(taxon);
128
			}
129

    
130
			
131
		} catch (Exception e) {
132
			e.printStackTrace();
133
			logger.error(e.getMessage());
134
			state.setSuccess(false);
135
			success = false;
136
		}
137
		return success;
138
	}
139

    
140
	
141
	private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
142
		Integer genusId = rs.getInt("PreferredNameFDCnumber");
143
		
144
		Taxon taxon = state.getRelatedObject(NAMESPACE_GENERA, String.valueOf(genusId), Taxon.class);
145
		if (taxon == null){
146
			logger.warn("Taxon not found for " + genusId);
147
		}
148
		return taxon;
149
	}
150

    
151

    
152
	@Override
153
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
154
		String nameSpace;
155
		Class<?> cdmClass;
156
		Set<String> idSet;
157
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
158
		
159
		try{
160
			Set<String> taxonIdSet = new HashSet<String>();
161
			while (rs.next()){
162
				handleForeignKey(rs, taxonIdSet,"PreferredNameFDCnumber" );
163
			}
164
			
165
			//taxon map
166
			nameSpace = NAMESPACE_GENERA;
167
			cdmClass = TaxonBase.class;
168
			idSet = taxonIdSet;
169
			Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
170
			result.put(nameSpace, taxonMap);
171
			
172
			//sourceReference
173
			Reference<?> sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
174
			Map<String, Reference> referenceMap = new HashMap<String, Reference>();
175
			referenceMap.put(SOURCE_REFERENCE, sourceReference);
176
			result.put(NAMESPACE_REFERENCE, referenceMap);
177
			
178
		} catch (SQLException e) {
179
			throw new RuntimeException(e);
180
		}
181
		return result;
182
	}
183
	
184
	@Override
185
	protected boolean doCheck(IndexFungorumImportState state){
186
		return true;
187
	}
188
	
189
	@Override
190
	protected boolean isIgnore(IndexFungorumImportState state){
191
		return ! state.getConfig().isDoTaxa();
192
	}
193

    
194

    
195

    
196

    
197

    
198
}
(7-7/9)