Project

General

Profile

Download (7 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
 * @author a.mueller
41
 * @since 27.02.2012
42
 */
43
@Component
44
public class IndexFungorumSpeciesImport  extends IndexFungorumImportBase {
45

    
46
    private static final long serialVersionUID = -1148034079632876980L;
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
	@Override
57
	protected String getIdQuery() {
58
		String result = " SELECT PreferredNameIFnumber FROM " + getTableName() +
59
				" ORDER BY PreferredName ";
60
		return result;
61
	}
62

    
63
	@Override
64
	protected String getRecordQuery(IndexFungorumImportConfigurator config) {
65
		String strRecordQuery =
66
				" SELECT DISTINCT distribution.PreferredNameFDCnumber, species.* , cl.[Phylum name]" +
67
				" FROM tblPESIfungi AS distribution RIGHT OUTER JOIN  dbo.[tblPESIfungi-IFdata] AS species ON distribution.PreferredNameIFnumber = species.PreferredNameIFnumber " +
68
					" LEFT OUTER JOIN [tblPESIfungi-Classification] cl ON species.PreferredName   = cl.PreferredName " +
69
				" WHERE ( species.PreferredNameIFnumber IN (" + ID_LIST_TOKEN + ") )" +
70
			"";
71
		return strRecordQuery;
72
	}
73

    
74
	@Override
75
    protected void doInvoke(IndexFungorumImportState state){
76
        System.out.println("start make " + getPluralString() + " ...");
77
        super.doInvoke(state);
78
	}
79

    
80
	@Override
81
	public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner,
82
	        IndexFungorumImportState state) {
83

    
84
		boolean success = true;
85
		Reference sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
86
		ResultSet rs = partitioner.getResultSet();
87
		Classification classification = getClassification(state);
88

    
89
		try {
90
			while (rs.next()){
91

    
92
				//DisplayName, NomRefCache -> don't use, created by Marc
93

    
94
				Integer id = (Integer)rs.getObject("PreferredNameIFnumber");
95
				String phylumName = rs.getString("Phylum name");
96

    
97
				String preferredName = rs.getString("PreferredName");
98
				if (StringUtils.isBlank(preferredName)){
99
					logger.warn("Preferred name is blank. This case is not yet handled by IF import. RECORD UMBER" + CdmUtils.Nz(id));
100
				}
101

    
102
				//Rank rank = Rank.SPECIES();
103

    
104
				NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
105
				INonViralName name = parser.parseSimpleName(preferredName, NomenclaturalCode.ICNAFP, null);
106

    
107
				Taxon taxon = Taxon.NewInstance(name, sourceReference);
108
				//if name is infraspecific the parent should be the species not the genus
109
				Taxon parent;
110
				if (!name.isInfraSpecific()){
111
				    parent = getParentTaxon(state, rs);
112
				    if (parent == null){
113
	                    logger.warn("parent not found for name:" +preferredName);
114
	                }
115
				    classification.addParentChild(parent, taxon, null, null);
116
				}
117

    
118
				//author + publication
119
				makeAuthorAndPublication(state, rs, name);
120
				//source
121
				makeSource(state, taxon, id, NAMESPACE_SPECIES );
122

    
123
				//fossil
124
				if (FOSSIL_FUNGI.equalsIgnoreCase(phylumName)){
125
					ExtensionType fossilExtType = getExtensionType(state, ErmsTransformer.uuidExtFossilStatus, "fossil status", "fossil status", "fos. stat.");
126
					Extension.NewInstance(taxon, PesiTransformer.STR_FOSSIL_ONLY, fossilExtType);
127
				}
128
				//save
129

    
130
				UUID uuidTaxon = getTaxonService().saveOrUpdate(taxon);
131
				//getNameService().saveOrUpdate(name);
132
				if (name.isInfraSpecific()){
133
                    state.getInfraspecificTaxaUUIDs().add(uuidTaxon);
134
                }
135
			}
136
		} catch (Exception e) {
137
			e.printStackTrace();
138
			logger.error(e.getMessage());
139
			state.setSuccess(false);
140
			success = false;
141
		}
142
		return success;
143
	}
144

    
145
    private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
146
		Integer genusId = rs.getInt("PreferredNameFDCnumber");
147

    
148
		Taxon taxon = state.getRelatedObject(NAMESPACE_GENERA, String.valueOf(genusId), Taxon.class);
149
		if (taxon == null){
150
			logger.warn("Taxon not found for " + genusId);
151
		}
152
		return taxon;
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<Object, Map<String, ? extends CdmBase>>();
161

    
162
		try{
163
			Set<String> taxonIdSet = new HashSet<String>();
164
			Set<String> taxonSpeciesNames = new HashSet<String>();
165
 			while (rs.next()){
166
				handleForeignKey(rs, taxonIdSet,"PreferredNameFDCnumber" );
167
				handleForeignKey(rs, taxonSpeciesNames, "PreferredName");
168
			}
169

    
170
			//taxon map
171
			nameSpace = NAMESPACE_GENERA;
172
			cdmClass = TaxonBase.class;
173
			idSet = taxonIdSet;
174
			@SuppressWarnings({ "unchecked", "rawtypes" })
175
            Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
176
			result.put(nameSpace, taxonMap);
177

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

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

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

    
195
	@Override
196
	protected boolean isIgnore(IndexFungorumImportState state){
197
		return ! state.getConfig().isDoTaxa();
198
	}
199
}
(7-7/10)