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
|
if (taxon == null){
|
101
|
logger.debug("taxon is null for id " + id);
|
102
|
} else{
|
103
|
TaxonDescription description = getTaxonDescription(taxon, ref, false, true);
|
104
|
|
105
|
//handle single distributions
|
106
|
int count = rs.getMetaData().getColumnCount();
|
107
|
for (int i=1; i <= count; i++ ){
|
108
|
String colName = rs.getMetaData().getColumnName(i);
|
109
|
//exclude non distribution columns
|
110
|
if (! excludedColumns.contains(colName)){
|
111
|
String distributionValue = rs.getString(i);
|
112
|
if (StringUtils.isNotBlank(distributionValue)){
|
113
|
//create distribution for existing occurrences
|
114
|
if (! distributionValue.equals("X")){
|
115
|
logger.warn("Unexpected distribution value '" + distributionValue + "' for area " + colName);
|
116
|
}
|
117
|
NamedArea area = state.getTransformer().getNamedAreaByKey(colName);
|
118
|
Distribution distribution = Distribution.NewInstance(area, status);
|
119
|
description.addElement(distribution);
|
120
|
//no last action
|
121
|
distribution.addMarker(Marker.NewInstance(noLastActionMarkerType, true));
|
122
|
}
|
123
|
|
124
|
}
|
125
|
}
|
126
|
getTaxonService().saveOrUpdate(taxon);
|
127
|
}
|
128
|
|
129
|
//save
|
130
|
|
131
|
}
|
132
|
|
133
|
|
134
|
} catch (Exception e) {
|
135
|
e.printStackTrace();
|
136
|
logger.error(e.getMessage());
|
137
|
state.setSuccess(false);
|
138
|
success = false;
|
139
|
}
|
140
|
return success;
|
141
|
}
|
142
|
|
143
|
|
144
|
private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
|
145
|
Integer genusId = rs.getInt("PreferredNameFDCnumber");
|
146
|
|
147
|
Taxon taxon = state.getRelatedObject(NAMESPACE_GENERA, String.valueOf(genusId), Taxon.class);
|
148
|
if (taxon == null){
|
149
|
logger.warn("Taxon not found for " + genusId);
|
150
|
}
|
151
|
return taxon;
|
152
|
}
|
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
|
while (rs.next()){
|
165
|
handleForeignKey(rs, taxonIdSet, "PreferredNameIFnumber" );
|
166
|
}
|
167
|
|
168
|
//taxon map
|
169
|
nameSpace = NAMESPACE_SPECIES;
|
170
|
cdmClass = TaxonBase.class;
|
171
|
idSet = taxonIdSet;
|
172
|
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
173
|
result.put(nameSpace, taxonMap);
|
174
|
|
175
|
//sourceReference
|
176
|
Reference<?> sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
|
177
|
Map<String, Reference> referenceMap = new HashMap<String, Reference>();
|
178
|
referenceMap.put(SOURCE_REFERENCE, sourceReference);
|
179
|
result.put(NAMESPACE_REFERENCE, referenceMap);
|
180
|
|
181
|
} catch (SQLException e) {
|
182
|
throw new RuntimeException(e);
|
183
|
}
|
184
|
return result;
|
185
|
}
|
186
|
|
187
|
|
188
|
@Override
|
189
|
protected boolean doCheck(IndexFungorumImportState state){
|
190
|
return true;
|
191
|
}
|
192
|
|
193
|
@Override
|
194
|
protected boolean isIgnore(IndexFungorumImportState state){
|
195
|
return ! state.getConfig().isDoOccurrence();
|
196
|
}
|
197
|
|
198
|
|
199
|
|
200
|
|
201
|
|
202
|
}
|