Project

General

Profile

Download (7.38 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.ArrayList;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.List;
18
import java.util.Map;
19
import java.util.Set;
20
import java.util.UUID;
21

    
22
import org.apache.commons.lang.StringUtils;
23
import org.apache.log4j.Logger;
24
import org.springframework.stereotype.Component;
25

    
26
import eu.etaxonomy.cdm.api.service.config.MatchingTaxonConfigurator;
27
import eu.etaxonomy.cdm.common.CdmUtils;
28
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
29
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
30
import eu.etaxonomy.cdm.io.common.Source;
31
import eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer;
32
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
33
import eu.etaxonomy.cdm.model.common.CdmBase;
34
import eu.etaxonomy.cdm.model.common.Extension;
35
import eu.etaxonomy.cdm.model.common.ExtensionType;
36
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
37
import eu.etaxonomy.cdm.model.name.NonViralName;
38
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40
import eu.etaxonomy.cdm.model.taxon.Classification;
41
import eu.etaxonomy.cdm.model.taxon.Taxon;
42
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
43
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
44
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
45

    
46

    
47
/**
48
 * @author a.mueller
49
 * @created 27.02.2012
50
 */
51
@Component
52
public class IndexFungorumSpeciesImport  extends IndexFungorumImportBase {
53
	private static final Logger logger = Logger.getLogger(IndexFungorumSpeciesImport.class);
54

    
55
	private static final String pluralString = "species";
56
	private static final String dbTableName = "[tblPESIfungi-IFdata]";
57
	
58

    
59
	public IndexFungorumSpeciesImport(){
60
		super(pluralString, dbTableName, null);
61

    
62
	}
63

    
64

    
65

    
66

    
67
	@Override
68
	protected String getIdQuery() {
69
		String result = " SELECT PreferredNameIFnumber FROM " + getTableName() +
70
				" ORDER BY PreferredName ";
71
		return result;
72
	}
73

    
74

    
75

    
76

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

    
97

    
98
	}
99

    
100
	@Override
101
	public boolean doPartition(ResultSetPartitioner partitioner, IndexFungorumImportState state) {
102
		boolean success = true;
103
		Reference<?> sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
104
		ResultSet rs = partitioner.getResultSet();
105
		Classification classification = getClassification(state);
106

    
107
		try {
108
			while (rs.next()){
109

    
110
				//DisplayName, NomRefCache -> don't use, created by Marc
111

    
112
				Integer id = (Integer)rs.getObject("PreferredNameIFnumber");
113
				String phylumName = rs.getString("Phylum name");
114

    
115
				String preferredName = rs.getString("PreferredName");
116
				if (StringUtils.isBlank(preferredName)){
117
					logger.warn("Preferred name is blank. This case is not yet handled by IF import. RECORD UMBER" + CdmUtils.Nz(id));
118
				}
119

    
120
				//Rank rank = Rank.SPECIES();
121

    
122
				NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
123
				NonViralName<?> name = parser.parseSimpleName(preferredName, NomenclaturalCode.ICNAFP, null);
124

    
125
				Taxon taxon = Taxon.NewInstance(name, sourceReference);
126
				//if name is infraspecific the parent should be the species not the genus
127
				Taxon parent;
128
				if (!name.isInfraSpecific()){
129
				    parent = getParentTaxon(state, rs);
130
				    if (parent == null){
131
	                    logger.warn("parent not found for name:" +preferredName);
132
	                }
133
				    classification.addParentChild(parent, taxon, null, null);
134
				}
135

    
136
				//author + publication
137
				makeAuthorAndPublication(state, rs, name);
138
				//source
139
				makeSource(state, taxon, id, NAMESPACE_SPECIES );
140

    
141
				//fossil
142
				if (FOSSIL_FUNGI.equalsIgnoreCase(phylumName)){
143
					ExtensionType fossilExtType = getExtensionType(state, ErmsTransformer.uuidFossilStatus, "fossil status", "fossil status", "fos. stat.");
144
					Extension.NewInstance(taxon, PesiTransformer.STR_FOSSIL_ONLY, fossilExtType);
145
				}
146
				//save
147

    
148
				UUID uuidTaxon = getTaxonService().saveOrUpdate(taxon);
149
				//getNameService().saveOrUpdate(name);
150
				if (name.isInfraSpecific()){
151
                    state.getInfraspecificTaxaUUIDs().add(uuidTaxon);
152
                }
153

    
154
			}
155

    
156

    
157
		} catch (Exception e) {
158
			e.printStackTrace();
159
			logger.error(e.getMessage());
160
			state.setSuccess(false);
161
			success = false;
162
		}
163
		return success;
164
	}
165

    
166

    
167
	
168

    
169

    
170

    
171

    
172
    private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
173
		Integer genusId = rs.getInt("PreferredNameFDCnumber");
174

    
175
		Taxon taxon = state.getRelatedObject(NAMESPACE_GENERA, String.valueOf(genusId), Taxon.class);
176
		if (taxon == null){
177
			logger.warn("Taxon not found for " + genusId);
178
		}
179
		return taxon;
180
	}
181

    
182

    
183
	@Override
184
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
185
		String nameSpace;
186
		Class<?> cdmClass;
187
		Set<String> idSet;
188
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
189

    
190
		try{
191
			Set<String> taxonIdSet = new HashSet<String>();
192
			Set<String> taxonSpeciesNames = new HashSet<String>();
193
 			while (rs.next()){
194
				handleForeignKey(rs, taxonIdSet,"PreferredNameFDCnumber" );
195
				handleForeignKey(rs, taxonSpeciesNames, "PreferredName");
196
			}
197

    
198
			//taxon map
199
			nameSpace = NAMESPACE_GENERA;
200
			cdmClass = TaxonBase.class;
201
			idSet = taxonIdSet;
202
			Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
203
			result.put(nameSpace, taxonMap);
204

    
205

    
206
			//sourceReference
207
			Reference<?> sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
208
			Map<String, Reference> referenceMap = new HashMap<String, Reference>();
209
			referenceMap.put(SOURCE_REFERENCE, sourceReference);
210
			result.put(NAMESPACE_REFERENCE, referenceMap);
211

    
212
		} catch (SQLException e) {
213
			throw new RuntimeException(e);
214
		}
215
		return result;
216
	}
217

    
218
	@Override
219
	protected boolean doCheck(IndexFungorumImportState state){
220
		return true;
221
	}
222

    
223
	@Override
224
	protected boolean isIgnore(IndexFungorumImportState state){
225
		return ! state.getConfig().isDoTaxa();
226
	}
227

    
228

    
229

    
230

    
231

    
232
}
(7-7/10)