Project

General

Profile

Download (6.36 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 RECORD_NUMBER 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.PhylumName" +
76
				" FROM tblPESIfungi AS distribution RIGHT OUTER JOIN  dbo.[tblPESIfungi-IFdata] AS species ON distribution.PreferredNameIFnumber = species.RECORD_NUMBER " +
77
					" LEFT OUTER JOIN [tblPESIfungi-Classification] cl ON species.PreferredName   = cl.PreferredName " +
78
				" WHERE ( species.RECORD_NUMBER 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("RECORD_NUMBER");
97
				String phylumName = rs.getString("PhylumName");
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_NUMBER" + 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
				classification.addParentChild(parent, taxon, null, null);
112
				
113
				//author + publication
114
				makeAuthorAndPublication(state, rs, name);
115
				//source
116
				makeSource(state, taxon, id, NAMESPACE_SPECIES );
117
				
118
				//fossil
119
				if (FOSSIL_FUNGI.equalsIgnoreCase(phylumName)){
120
					ExtensionType fossilExtType = getExtensionType(state, ErmsTransformer.uuidFossilStatus, "fossil status", "fossil status", "fos. stat.");
121
					Extension.NewInstance(taxon, PesiTransformer.STR_FOSSIL_ONLY, fossilExtType); 
122
				}
123
				//save
124
				getTaxonService().saveOrUpdate(taxon);
125
			}
126

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

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

    
148

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

    
191

    
192

    
193

    
194

    
195
}
(7-7/9)