Project

General

Profile

Download (6.55 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.log4j.Logger;
20
import org.springframework.stereotype.Component;
21

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

    
37
/**
38
 * @author a.mueller
39
 * @since 27.02.2012
40
 */
41
@Component
42
public class IndexFungorumSpeciesImport  extends IndexFungorumImportBase {
43

    
44
    private static final long serialVersionUID = -1148034079632876980L;
45
    private static final Logger logger = Logger.getLogger(IndexFungorumSpeciesImport.class);
46

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

    
50
	public IndexFungorumSpeciesImport(){
51
		super(pluralString, dbTableName, null);
52
	}
53

    
54
	@Override
55
	protected String getIdQuery() {
56
		String result = " SELECT PreferredNameIFnumber "
57
		        + " FROM " + getTableName()
58
				+ " ORDER BY PreferredName ";
59
		return result;
60
	}
61

    
62
	@Override
63
	protected String getRecordQuery(IndexFungorumImportConfigurator config) {
64
		String strRecordQuery =
65
				"   SELECT DISTINCT distribution.PreferredNameFDCnumber, species.* , cl.[Phylum name]"
66
				+ " FROM tblPESIfungi AS distribution "
67
				+ "   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
	public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner,
76
	        IndexFungorumImportState state) {
77

    
78
		boolean success = true;
79
		Reference sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
80
		ResultSet rs = partitioner.getResultSet();
81
		Classification classification = getClassification(state);
82

    
83
		try {
84
			while (rs.next()){
85

    
86
				//DisplayName, NomRefCache -> don't use, created by Marc
87

    
88
				Integer id = (Integer)rs.getObject("PreferredNameIFnumber");
89
				String phylumName = rs.getString("Phylum name");
90

    
91
				String preferredName = rs.getString("PreferredName");
92
				if (isBlank(preferredName)){
93
					logger.warn("Preferred name is blank. This case is not yet handled by IF import. RECORD NUMBER" + CdmUtils.Nz(id));
94
				}
95

    
96
				//Rank rank = Rank.SPECIES();
97

    
98
				NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
99
				INonViralName name = parser.parseSimpleName(preferredName, NomenclaturalCode.ICNAFP, null);
100

    
101
				Taxon taxon = Taxon.NewInstance(name, sourceReference);
102
				//if name is infra-specific the parent should be the species not the genus
103
				Integer genusId = rs.getInt("PreferredNameFDCnumber");
104
		        if (!name.isInfraSpecific()){
105
				    Taxon parent = getParentTaxon(state, genusId);
106
				    if (parent == null){
107
	                    logger.warn("parent not found for name:" + preferredName + "ID(PreferredNameIFnumber)" +id+ "; GenusId(PreferredNameFDCnumber): ");
108
	                }else{
109
	                    classification.addParentChild(parent, taxon, null, null);
110
	                }
111
				}else {
112
                    state.getInfraspecificTaxaUUIDs().put(taxon.getUuid(), genusId);
113
                }
114

    
115
				//author + publication
116
				makeAuthorAndPublication(state, rs, name);
117
				//source
118
				makeSource(state, taxon, id, NAMESPACE_SPECIES );
119

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

    
126
				//save
127
				getTaxonService().saveOrUpdate(taxon);
128

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

    
139
    private Taxon getParentTaxon(IndexFungorumImportState state, Integer genusId) {
140
        return state.getRelatedObject(NAMESPACE_GENERA, String.valueOf(genusId), Taxon.class);
141
	}
142

    
143
	@Override
144
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
145

    
146
	    String nameSpace;
147
		Set<String> idSet;
148
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
149

    
150
		try{
151
			Set<String> taxonIdSet = new HashSet<>();
152
			Set<String> taxonSpeciesNames = new HashSet<>();
153
 			while (rs.next()){
154
				handleForeignKey(rs, taxonIdSet,"PreferredNameFDCnumber" );
155
				handleForeignKey(rs, taxonSpeciesNames, "PreferredName");
156
			}
157

    
158
			//taxon map
159
			nameSpace = NAMESPACE_GENERA;
160
			idSet = taxonIdSet;
161
			@SuppressWarnings({ "rawtypes" })
162
            Map<String, TaxonBase> taxonMap = getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, nameSpace);
163
			result.put(nameSpace, taxonMap);
164

    
165
			//sourceReference
166
			Reference sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
167
			Map<String, Reference> referenceMap = new HashMap<>();
168
			referenceMap.put(SOURCE_REFERENCE, sourceReference);
169
			result.put(NAMESPACE_REFERENCE, referenceMap);
170

    
171
		} catch (SQLException e) {
172
			throw new RuntimeException(e);
173
		}
174
		return result;
175
	}
176

    
177
	@Override
178
	protected boolean doCheck(IndexFungorumImportState state){
179
		return true;
180
	}
181

    
182
	@Override
183
	protected boolean isIgnore(IndexFungorumImportState state){
184
		return ! state.getConfig().isDoTaxa();
185
	}
186
}
(7-7/10)