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.globis;
|
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.IOValidator;
|
24
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
25
|
import eu.etaxonomy.cdm.io.globis.validation.GlobisCurrentSpeciesImportValidator;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.common.Language;
|
28
|
import eu.etaxonomy.cdm.model.media.Media;
|
29
|
import eu.etaxonomy.cdm.model.name.Rank;
|
30
|
import eu.etaxonomy.cdm.model.name.ZoologicalName;
|
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.model.taxon.TaxonNode;
|
36
|
import eu.etaxonomy.cdm.strategy.parser.INonViralNameParser;
|
37
|
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
|
38
|
|
39
|
|
40
|
/**
|
41
|
* @author a.mueller
|
42
|
* @created 20.02.2010
|
43
|
* @version 1.0
|
44
|
*/
|
45
|
@Component
|
46
|
public class GlobisImageImport extends GlobisImportBase<Taxon> {
|
47
|
private static final Logger logger = Logger.getLogger(GlobisImageImport.class);
|
48
|
|
49
|
private int modCount = 10000;
|
50
|
private static final String pluralString = "images";
|
51
|
private static final String dbTableName = "Einzelbilder";
|
52
|
private static final Class cdmTargetClass = Media
|
53
|
.class; //not needed
|
54
|
|
55
|
private static final String IMAGE_NAMESPACE = "Einzelbilder";
|
56
|
|
57
|
public GlobisImageImport(){
|
58
|
super(pluralString, dbTableName, cdmTargetClass);
|
59
|
}
|
60
|
|
61
|
|
62
|
|
63
|
|
64
|
/* (non-Javadoc)
|
65
|
* @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#getIdQuery()
|
66
|
*/
|
67
|
@Override
|
68
|
protected String getIdQuery() {
|
69
|
String strRecordQuery =
|
70
|
" SELECT BildId " +
|
71
|
" FROM " + dbTableName;
|
72
|
return strRecordQuery;
|
73
|
}
|
74
|
|
75
|
|
76
|
|
77
|
|
78
|
/* (non-Javadoc)
|
79
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
|
80
|
*/
|
81
|
@Override
|
82
|
protected String getRecordQuery(GlobisImportConfigurator config) {
|
83
|
String strRecordQuery =
|
84
|
" SELECT i.*, NULL as Created_When, NULL as Created_Who," +
|
85
|
" NULL as Updated_who, NULL as Updated_When, NULL as Notes " +
|
86
|
" FROM " + getTableName() + " i " +
|
87
|
" WHERE ( i.BildId IN (" + ID_LIST_TOKEN + ") )";
|
88
|
return strRecordQuery;
|
89
|
}
|
90
|
|
91
|
|
92
|
|
93
|
/* (non-Javadoc)
|
94
|
* @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#doPartition(eu.etaxonomy.cdm.io.common.ResultSetPartitioner, eu.etaxonomy.cdm.io.globis.GlobisImportState)
|
95
|
*/
|
96
|
@Override
|
97
|
public boolean doPartition(ResultSetPartitioner partitioner, GlobisImportState state) {
|
98
|
boolean success = true;
|
99
|
|
100
|
Set<TaxonBase> objectsToSave = new HashSet<TaxonBase>();
|
101
|
|
102
|
Map<String, Taxon> taxonMap = (Map<String, Taxon>) partitioner.getObjectMap(TAXON_NAMESPACE);
|
103
|
// Map<String, DerivedUnit> ecoFactDerivedUnitMap = (Map<String, DerivedUnit>) partitioner.getObjectMap(ECO_FACT_DERIVED_UNIT_NAMESPACE);
|
104
|
|
105
|
ResultSet rs = partitioner.getResultSet();
|
106
|
|
107
|
Classification classification = getClassification(state);
|
108
|
|
109
|
try {
|
110
|
|
111
|
int i = 0;
|
112
|
|
113
|
//for each reference
|
114
|
while (rs.next()){
|
115
|
|
116
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
|
117
|
|
118
|
Integer taxonId = rs.getInt("IDcurrentspec");
|
119
|
|
120
|
|
121
|
//TODO
|
122
|
//String dtSpcJahr,
|
123
|
//dtSpcFamakt,dtSpcSubfamakt,dtSpcTribakt,
|
124
|
//fiSpcLiteratur, fiSpcspcgrptax, dtSpcCountries,vernacularnames
|
125
|
|
126
|
try {
|
127
|
|
128
|
//source ref
|
129
|
Reference<?> sourceRef = state.getTransactionalSourceReference();
|
130
|
Taxon nextHigherTaxon = null;
|
131
|
|
132
|
Taxon species = createObject(rs, state);
|
133
|
|
134
|
//subgenus
|
135
|
String subGenusStr = rs.getString("dtSpcSubgenakt");
|
136
|
String subGenusAuthorStr = rs.getString("dtSpcSubgenaktauthor");
|
137
|
boolean hasSubgenus = StringUtils.isNotBlank(subGenusStr) || StringUtils.isNotBlank(subGenusAuthorStr);
|
138
|
if (hasSubgenus){
|
139
|
Taxon subGenus = getTaxon(state, rs, subGenusStr, Rank.SUBGENUS(), subGenusAuthorStr, taxonMap);
|
140
|
classification.addParentChild(subGenus, species, sourceRef, null);
|
141
|
nextHigherTaxon = getParent(subGenus, classification);
|
142
|
}
|
143
|
|
144
|
//genus
|
145
|
String genusStr = rs.getString("dtSpcGenusakt");
|
146
|
String genusAuthorStr = rs.getString("dtSpcGenusaktauthor");
|
147
|
Taxon genus = getTaxon(state, rs, genusStr, Rank.GENUS(), genusAuthorStr, taxonMap);
|
148
|
if (nextHigherTaxon != null){
|
149
|
if (! compareTaxa(genus, nextHigherTaxon)){
|
150
|
logger.warn("Current genus and parent of subgenus are not equal: " + taxonId);
|
151
|
}
|
152
|
}else{
|
153
|
classification.addParentChild(genus, species, sourceRef, null);
|
154
|
nextHigherTaxon = getParent(genus, classification);
|
155
|
}
|
156
|
|
157
|
this.doIdCreatedUpdatedNotes(state, species, rs, taxonId, REFERENCE_NAMESPACE);
|
158
|
|
159
|
objectsToSave.add(species);
|
160
|
|
161
|
|
162
|
} catch (Exception e) {
|
163
|
logger.warn("Exception in current_species: IDcurrentspec " + taxonId + ". " + e.getMessage());
|
164
|
// e.printStackTrace();
|
165
|
}
|
166
|
|
167
|
}
|
168
|
|
169
|
// logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
|
170
|
|
171
|
logger.warn(pluralString + " to save: " + objectsToSave.size());
|
172
|
getTaxonService().save(objectsToSave);
|
173
|
|
174
|
return success;
|
175
|
} catch (SQLException e) {
|
176
|
logger.error("SQLException:" + e);
|
177
|
return false;
|
178
|
}
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* Compares 2 taxa, returns true of both taxa look similar
|
183
|
* @param genus
|
184
|
* @param nextHigherTaxon
|
185
|
* @return
|
186
|
*/
|
187
|
private boolean compareTaxa(Taxon taxon1, Taxon taxon2) {
|
188
|
ZoologicalName name1 = CdmBase.deproxy(taxon1.getName(), ZoologicalName.class);
|
189
|
ZoologicalName name2 = CdmBase.deproxy(taxon2.getName(), ZoologicalName.class);
|
190
|
if (!name1.getRank().equals(name2.getRank())){
|
191
|
return false;
|
192
|
}
|
193
|
if (! name1.getTitleCache().equals(name2.getTitleCache())){
|
194
|
return false;
|
195
|
}
|
196
|
return true;
|
197
|
}
|
198
|
|
199
|
|
200
|
|
201
|
|
202
|
private Taxon getParent(Taxon subgenus, Classification classification) {
|
203
|
for (TaxonNode node : subgenus.getTaxonNodes()){
|
204
|
if (node.getClassification().equals(classification)){
|
205
|
return node.getParent().getTaxon();
|
206
|
}
|
207
|
}
|
208
|
return null;
|
209
|
}
|
210
|
|
211
|
|
212
|
|
213
|
|
214
|
private Taxon getTaxon(GlobisImportState state, ResultSet rs, String subGenus, Rank rank, String author, Map<String, Taxon> taxonMap) {
|
215
|
String key = subGenus + "@" + "subGenusAuthor" + "@" + rank.getTitleCache();
|
216
|
Taxon taxon = taxonMap.get(key);
|
217
|
if (taxon == null){
|
218
|
ZoologicalName name = ZoologicalName.NewInstance(rank);
|
219
|
taxon = Taxon.NewInstance(name, state.getTransactionalSourceReference());
|
220
|
handleAuthorAndYear(author, name);
|
221
|
getTaxonService().save(taxon);
|
222
|
}
|
223
|
|
224
|
return taxon;
|
225
|
}
|
226
|
|
227
|
|
228
|
//fast and dirty is enough here
|
229
|
private Classification classification;
|
230
|
|
231
|
private Classification getClassification(GlobisImportState state) {
|
232
|
if (this.classification == null){
|
233
|
String name = state.getConfig().getClassificationName();
|
234
|
Reference<?> reference = state.getTransactionalSourceReference();
|
235
|
this.classification = Classification.NewInstance(name, reference, Language.DEFAULT());
|
236
|
classification.setUuid(state.getConfig().getClassificationUuid());
|
237
|
getClassificationService().save(classification);
|
238
|
}
|
239
|
return this.classification;
|
240
|
|
241
|
}
|
242
|
|
243
|
private INonViralNameParser parser = NonViralNameParserImpl.NewInstance();
|
244
|
|
245
|
|
246
|
/* (non-Javadoc)
|
247
|
* @see eu.etaxonomy.cdm.io.common.mapping.IMappingImport#createObject(java.sql.ResultSet, eu.etaxonomy.cdm.io.common.ImportStateBase)
|
248
|
*/
|
249
|
public Taxon createObject(ResultSet rs, GlobisImportState state)
|
250
|
throws SQLException {
|
251
|
String speciesEpi = rs.getString("dtSpcSpcakt");
|
252
|
String subGenusEpi = rs.getString("dtSpcSubgenakt");
|
253
|
String genusEpi = rs.getString("dtSpcGenusakt");
|
254
|
String author = rs.getString("dtSpcAutor");
|
255
|
|
256
|
|
257
|
ZoologicalName zooName = ZoologicalName.NewInstance(Rank.SPECIES());
|
258
|
zooName.setSpecificEpithet(speciesEpi);
|
259
|
if (StringUtils.isNotBlank(subGenusEpi)){
|
260
|
zooName.setInfraSpecificEpithet(subGenusEpi);
|
261
|
}
|
262
|
zooName.setGenusOrUninomial(genusEpi);
|
263
|
handleAuthorAndYear(author, zooName);
|
264
|
|
265
|
Taxon taxon = Taxon.NewInstance(zooName, state.getTransactionalSourceReference());
|
266
|
|
267
|
return taxon;
|
268
|
}
|
269
|
|
270
|
|
271
|
/* (non-Javadoc)
|
272
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
|
273
|
*/
|
274
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
|
275
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
276
|
return result; //not needed
|
277
|
}
|
278
|
|
279
|
/* (non-Javadoc)
|
280
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
281
|
*/
|
282
|
@Override
|
283
|
protected boolean doCheck(GlobisImportState state){
|
284
|
IOValidator<GlobisImportState> validator = new GlobisCurrentSpeciesImportValidator();
|
285
|
return validator.validate(state);
|
286
|
}
|
287
|
|
288
|
|
289
|
/* (non-Javadoc)
|
290
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
291
|
*/
|
292
|
protected boolean isIgnore(GlobisImportState state){
|
293
|
return ! state.getConfig().isDoImages();
|
294
|
}
|
295
|
|
296
|
|
297
|
|
298
|
|
299
|
|
300
|
}
|