Project

General

Profile

Download (6.14 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.PresenceAbsenceTerm;
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
 * @author a.mueller
38
 * @since 27.02.2012
39
 */
40
@Component
41
public class IndexFungorumDistributionImport  extends IndexFungorumImportBase {
42

    
43
    private static final long serialVersionUID = -815842161276543719L;
44
    private static final Logger logger = Logger.getLogger(IndexFungorumDistributionImport.class);
45

    
46
	private static final String pluralString = "distributions";
47
	private static final String dbTableName = "[tblPESIfungi]";
48

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

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

    
60
	@Override
61
	protected String getRecordQuery(IndexFungorumImportConfigurator config) {
62
		String strRecordQuery =
63
				" SELECT distribution.* " +
64
				" FROM tblPESIfungi AS distribution  " +
65
			" WHERE ( distribution.PreferredNameIFnumber  IN (" + ID_LIST_TOKEN + ") )" +
66
			"";
67
		return strRecordQuery;
68
	}
69

    
70
	@Override
71
	public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner,
72
	            IndexFungorumImportState state) {
73

    
74
		boolean success = true;
75
//		Reference sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
76
		ResultSet rs = partitioner.getResultSet();
77

    
78
		try {
79
			//column names that do not hold distribution information
80
			Set<String> excludedColumns = new HashSet<>();
81
			excludedColumns.add("PreferredName");
82
			excludedColumns.add("PreferredNameIFnumber");
83
			excludedColumns.add("PreferredNameFDCnumber");
84

    
85
			PresenceAbsenceTerm status = PresenceAbsenceTerm.PRESENT();
86
			MarkerType noLastActionMarkerType = getNoLastActionMarkerType(state);
87
			while (rs.next()){
88

    
89
				//get taxon description
90
				Integer id = rs.getInt("PreferredNameIFnumber");
91
				Taxon taxon = state.getRelatedObject(NAMESPACE_SPECIES, String.valueOf(id), Taxon.class);
92
				Reference ref = null;
93
				if (taxon == null){
94
					logger.debug("taxon is null for id " + id);
95
				} else{
96
					TaxonDescription description = getTaxonDescription(taxon, ref, false, true);
97

    
98
					//handle single distributions
99
					int count = rs.getMetaData().getColumnCount();
100
					for (int i=1; i <= count; i++ ){
101
						String colName = rs.getMetaData().getColumnName(i);
102
						//exclude non distribution columns
103
						if (! excludedColumns.contains(colName)){
104
							String distributionValue = rs.getString(i);
105
							if (StringUtils.isNotBlank(distributionValue)){
106
								//create distribution for existing occurrences
107
								if (! distributionValue.equals("X")){
108
									logger.warn("Unexpected distribution value '" + distributionValue + "' for area " + colName);
109
								}
110
								NamedArea area = state.getTransformer().getNamedAreaByKey(colName);
111
								Distribution distribution = Distribution.NewInstance(area, status);
112
								description.addElement(distribution);
113
								//no last action
114
								distribution.addMarker(Marker.NewInstance(noLastActionMarkerType, true));
115
							}
116
						}
117
					}
118
					getTaxonService().saveOrUpdate(taxon);
119
				}
120
				//save
121
			}
122
		} catch (Exception e) {
123
			e.printStackTrace();
124
			logger.error(e.getMessage());
125
			state.setSuccess(false);
126
			success = false;
127
		}
128
		return success;
129
	}
130

    
131
	private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
132
		Integer genusId = rs.getInt("PreferredNameFDCnumber");
133

    
134
		Taxon taxon = state.getRelatedObject(NAMESPACE_GENERA, String.valueOf(genusId), Taxon.class);
135
		if (taxon == null){
136
			logger.warn("Taxon not found for " + genusId);
137
		}
138
		return taxon;
139
	}
140

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

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

    
148
		try{
149
			Set<String> taxonIdSet = new HashSet<>();
150
			while (rs.next()){
151
				handleForeignKey(rs, taxonIdSet, "PreferredNameIFnumber" );
152
			}
153

    
154
			//taxon map
155
			nameSpace = NAMESPACE_SPECIES;
156
			idSet = taxonIdSet;
157
			@SuppressWarnings({ "rawtypes" })
158
            Map<String, TaxonBase> taxonMap = getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, nameSpace);
159
			result.put(nameSpace, taxonMap);
160

    
161
			//sourceReference
162
			Reference sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
163
			Map<String, Reference> referenceMap = new HashMap<>();
164
			referenceMap.put(SOURCE_REFERENCE, sourceReference);
165
			result.put(NAMESPACE_REFERENCE, referenceMap);
166

    
167
		} catch (SQLException e) {
168
			throw new RuntimeException(e);
169
		}
170
		return result;
171
	}
172

    
173
	@Override
174
	protected boolean doCheck(IndexFungorumImportState state){
175
		return true;
176
	}
177

    
178
	@Override
179
	protected boolean isIgnore(IndexFungorumImportState state){
180
		return ! state.getConfig().isDoOccurrence();
181
	}
182
}
(1-1/10)