Project

General

Profile

Download (7.04 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
import java.util.UUID;
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.erms.ErmsTransformer;
27
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.common.Extension;
30
import eu.etaxonomy.cdm.model.common.ExtensionType;
31
import eu.etaxonomy.cdm.model.name.INonViralName;
32
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
33
import eu.etaxonomy.cdm.model.reference.Reference;
34
import eu.etaxonomy.cdm.model.taxon.Classification;
35
import eu.etaxonomy.cdm.model.taxon.Taxon;
36
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
37
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
38

    
39

    
40
/**
41
 * @author a.mueller
42
 * @since 27.02.2012
43
 */
44
@Component
45
public class IndexFungorumSpeciesImport  extends IndexFungorumImportBase {
46
	private static final Logger logger = Logger.getLogger(IndexFungorumSpeciesImport.class);
47

    
48
	private static final String pluralString = "species";
49
	private static final String dbTableName = "[tblPESIfungi-IFdata]";
50

    
51

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

    
55
	}
56

    
57

    
58

    
59

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

    
67

    
68

    
69

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

    
88

    
89

    
90

    
91
	}
92

    
93
	@Override
94
	public boolean doPartition(ResultSetPartitioner partitioner, IndexFungorumImportState state) {
95
		boolean success = true;
96
		Reference sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
97
		ResultSet rs = partitioner.getResultSet();
98
		Classification classification = getClassification(state);
99

    
100
		try {
101
			while (rs.next()){
102

    
103
				//DisplayName, NomRefCache -> don't use, created by Marc
104

    
105
				Integer id = (Integer)rs.getObject("PreferredNameIFnumber");
106
				String phylumName = rs.getString("Phylum name");
107

    
108
				String preferredName = rs.getString("PreferredName");
109
				if (StringUtils.isBlank(preferredName)){
110
					logger.warn("Preferred name is blank. This case is not yet handled by IF import. RECORD UMBER" + CdmUtils.Nz(id));
111
				}
112

    
113
				//Rank rank = Rank.SPECIES();
114

    
115
				NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
116
				INonViralName name = parser.parseSimpleName(preferredName, NomenclaturalCode.ICNAFP, null);
117

    
118
				Taxon taxon = Taxon.NewInstance(name, sourceReference);
119
				//if name is infraspecific the parent should be the species not the genus
120
				Taxon parent;
121
				if (!name.isInfraSpecific()){
122
				    parent = getParentTaxon(state, rs);
123
				    if (parent == null){
124
	                    logger.warn("parent not found for name:" +preferredName);
125
	                }
126
				    classification.addParentChild(parent, taxon, null, null);
127
				}
128

    
129
				//author + publication
130
				makeAuthorAndPublication(state, rs, name);
131
				//source
132
				makeSource(state, taxon, id, NAMESPACE_SPECIES );
133

    
134
				//fossil
135
				if (FOSSIL_FUNGI.equalsIgnoreCase(phylumName)){
136
					ExtensionType fossilExtType = getExtensionType(state, ErmsTransformer.uuidFossilStatus, "fossil status", "fossil status", "fos. stat.");
137
					Extension.NewInstance(taxon, PesiTransformer.STR_FOSSIL_ONLY, fossilExtType);
138
				}
139
				//save
140

    
141
				UUID uuidTaxon = getTaxonService().saveOrUpdate(taxon);
142
				//getNameService().saveOrUpdate(name);
143
				if (name.isInfraSpecific()){
144
                    state.getInfraspecificTaxaUUIDs().add(uuidTaxon);
145
                }
146

    
147
			}
148

    
149

    
150
		} catch (Exception e) {
151
			e.printStackTrace();
152
			logger.error(e.getMessage());
153
			state.setSuccess(false);
154
			success = false;
155
		}
156
		return success;
157
	}
158

    
159

    
160

    
161

    
162

    
163

    
164

    
165
    private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
166
		Integer genusId = rs.getInt("PreferredNameFDCnumber");
167

    
168
		Taxon taxon = state.getRelatedObject(NAMESPACE_GENERA, String.valueOf(genusId), Taxon.class);
169
		if (taxon == null){
170
			logger.warn("Taxon not found for " + genusId);
171
		}
172
		return taxon;
173
	}
174

    
175

    
176
	@Override
177
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
178
		String nameSpace;
179
		Class<?> cdmClass;
180
		Set<String> idSet;
181
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
182

    
183
		try{
184
			Set<String> taxonIdSet = new HashSet<String>();
185
			Set<String> taxonSpeciesNames = new HashSet<String>();
186
 			while (rs.next()){
187
				handleForeignKey(rs, taxonIdSet,"PreferredNameFDCnumber" );
188
				handleForeignKey(rs, taxonSpeciesNames, "PreferredName");
189
			}
190

    
191
			//taxon map
192
			nameSpace = NAMESPACE_GENERA;
193
			cdmClass = TaxonBase.class;
194
			idSet = taxonIdSet;
195
			Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
196
			result.put(nameSpace, taxonMap);
197

    
198

    
199
			//sourceReference
200
			Reference sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
201
			Map<String, Reference> referenceMap = new HashMap<String, Reference>();
202
			referenceMap.put(SOURCE_REFERENCE, sourceReference);
203
			result.put(NAMESPACE_REFERENCE, referenceMap);
204

    
205
		} catch (SQLException e) {
206
			throw new RuntimeException(e);
207
		}
208
		return result;
209
	}
210

    
211
	@Override
212
	protected boolean doCheck(IndexFungorumImportState state){
213
		return true;
214
	}
215

    
216
	@Override
217
	protected boolean isIgnore(IndexFungorumImportState state){
218
		return ! state.getConfig().isDoTaxa();
219
	}
220

    
221

    
222

    
223

    
224

    
225
}
(7-7/10)