Project

General

Profile

Download (6.23 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.io.common.ResultSetPartitioner;
24
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.common.Marker;
27
import eu.etaxonomy.cdm.model.common.MarkerType;
28
import eu.etaxonomy.cdm.model.description.Distribution;
29
import eu.etaxonomy.cdm.model.description.PresenceTerm;
30
import eu.etaxonomy.cdm.model.description.TaxonDescription;
31
import eu.etaxonomy.cdm.model.location.NamedArea;
32
import eu.etaxonomy.cdm.model.reference.Reference;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
35

    
36

    
37
/**
38
 * @author a.mueller
39
 * @created 27.02.2012
40
 */
41
@Component
42
public class IndexFungorumDistributionImport  extends IndexFungorumImportBase {
43
	private static final Logger logger = Logger.getLogger(IndexFungorumDistributionImport.class);
44
	
45
	private static final String pluralString = "distributions";
46
	private static final String dbTableName = "[tblPESIfungi]";
47

    
48
	public IndexFungorumDistributionImport(){
49
		super(pluralString, dbTableName, null);
50
	}
51

    
52

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

    
62

    
63

    
64

    
65
	/* (non-Javadoc)
66
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
67
	 */
68
	@Override
69
	protected String getRecordQuery(IndexFungorumImportConfigurator config) {
70
		String strRecordQuery = 
71
				" SELECT distribution.* " +
72
				" FROM tblPESIfungi AS distribution  " +
73
			" WHERE ( distribution.PreferredNameIFnumber  IN (" + ID_LIST_TOKEN + ") )" +
74
			"";
75
		return strRecordQuery;
76
	}
77

    
78
	
79
	@Override
80
	public boolean doPartition(ResultSetPartitioner partitioner, IndexFungorumImportState state) {
81
		boolean success = true;
82
//		Reference<?> sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
83
		ResultSet rs = partitioner.getResultSet();
84
		
85
		try {
86
			//column names that do not hold distribution information
87
			Set<String> excludedColumns = new HashSet<String>();
88
			excludedColumns.add("PreferredName");
89
			excludedColumns.add("PreferredNameIFnumber");
90
			excludedColumns.add("PreferredNameFDCnumber");
91
			
92
			PresenceTerm status = PresenceTerm.PRESENT();
93
			MarkerType noLastActionMarkerType = getNoLastActionMarkerType(state);
94
			while (rs.next()){
95

    
96
				//get taxon description
97
				Integer id = rs.getInt("PreferredNameIFnumber");
98
				Taxon taxon = state.getRelatedObject(NAMESPACE_SPECIES, String.valueOf(id), Taxon.class);
99
				Reference<?> ref = null;
100
				TaxonDescription description = getTaxonDescription(taxon, ref, false, true);
101
				
102
				//handle single distributions
103
				int count = rs.getMetaData().getColumnCount();
104
				for (int i=1; i <= count; i++ ){
105
					String colName = rs.getMetaData().getColumnName(i);
106
					//exclude non distribution columns
107
					if (! excludedColumns.contains(colName)){
108
						String distributionValue = rs.getString(i);
109
						if (StringUtils.isNotBlank(distributionValue)){
110
							//create distribution for existing occurrences
111
							if (! distributionValue.equals("X")){
112
								logger.warn("Unexpected distribution value '" + distributionValue + "' for area " + colName);
113
							}
114
							NamedArea area = state.getTransformer().getNamedAreaByKey(colName);
115
							Distribution distribution = Distribution.NewInstance(area, status);
116
							description.addElement(distribution);
117
							//no last action
118
							distribution.addMarker(Marker.NewInstance(noLastActionMarkerType, true));
119
						}
120
						
121
					}
122
				}
123
				
124
				//save
125
				getTaxonService().saveOrUpdate(taxon);
126
			}
127

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

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

    
149

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

    
183
	@Override
184
	protected boolean doCheck(IndexFungorumImportState state){
185
		return true;
186
	}
187
	
188
	@Override
189
	protected boolean isIgnore(IndexFungorumImportState state){
190
		return ! state.getConfig().isDoOccurrence();
191
	}
192

    
193

    
194

    
195

    
196

    
197
}
(1-1/9)