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.caryo;
|
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.log4j.Logger;
|
20
|
import org.springframework.stereotype.Component;
|
21
|
|
22
|
import eu.etaxonomy.cdm.io.common.DbImportBase;
|
23
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
24
|
import eu.etaxonomy.cdm.io.common.Source;
|
25
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.common.Language;
|
28
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
29
|
import eu.etaxonomy.cdm.model.name.Rank;
|
30
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
31
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
32
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
33
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
34
|
|
35
|
|
36
|
/**
|
37
|
* @author a.mueller
|
38
|
* @created 20.02.2010
|
39
|
* @version 1.0
|
40
|
*/
|
41
|
@Component
|
42
|
public class CaryoTaxonImport extends DbImportBase<CaryoImportState, CaryoImportConfigurator> {
|
43
|
private static final Logger logger = Logger.getLogger(CaryoTaxonImport.class);
|
44
|
|
45
|
private int modCount = 10000;
|
46
|
private static final String pluralString = "taxa";
|
47
|
private static final String dbTableName = "CARYOPHYLLALES";
|
48
|
private static final String FAMILY = "Family";
|
49
|
private static final String PERSON = "Person";
|
50
|
private static final String TEAM = "Team";
|
51
|
private static final String JOURNAL = "Journal";
|
52
|
private static final String BOOK = "Book";
|
53
|
|
54
|
|
55
|
private Map<String, Taxon> familyMap = new HashMap<String, Taxon>();
|
56
|
private Map<String, TeamOrPersonBase> personMap = new HashMap<String, TeamOrPersonBase>();
|
57
|
private Map<String, TeamOrPersonBase> teamMap = new HashMap<String, TeamOrPersonBase>();
|
58
|
private Map<String, Reference> journalMap = new HashMap<String, Reference>();
|
59
|
private Map<String, Reference> bookMap = new HashMap<String, Reference>();
|
60
|
|
61
|
|
62
|
private Classification classification;
|
63
|
|
64
|
|
65
|
public CaryoTaxonImport(){
|
66
|
super(pluralString, dbTableName);
|
67
|
}
|
68
|
|
69
|
|
70
|
|
71
|
|
72
|
/* (non-Javadoc)
|
73
|
* @see eu.etaxonomy.cdm.io.common.DbImportBase#getIdQuery(eu.etaxonomy.cdm.io.common.DbImportStateBase)
|
74
|
*/
|
75
|
@Override
|
76
|
protected String getIdQuery(CaryoImportState state) {
|
77
|
String strRecordQuery =
|
78
|
" SELECT ID " +
|
79
|
" FROM " + dbTableName;
|
80
|
return strRecordQuery;
|
81
|
}
|
82
|
|
83
|
|
84
|
/* (non-Javadoc)
|
85
|
* @see eu.etaxonomy.cdm.io.common.DbImportBase#getRecordQuery(eu.etaxonomy.cdm.io.common.DbImportConfiguratorBase)
|
86
|
*/
|
87
|
@Override
|
88
|
protected String getRecordQuery(CaryoImportConfigurator config) {
|
89
|
String strRecordQuery =
|
90
|
" SELECT cs.* " +
|
91
|
" FROM " + getTableName() + " t " +
|
92
|
" WHERE ( t.ID IN (" + ID_LIST_TOKEN + ") )";
|
93
|
return strRecordQuery;
|
94
|
}
|
95
|
|
96
|
|
97
|
|
98
|
/* (non-Javadoc)
|
99
|
* @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#doPartition(eu.etaxonomy.cdm.io.common.ResultSetPartitioner, eu.etaxonomy.cdm.io.globis.GlobisImportState)
|
100
|
*/
|
101
|
@Override
|
102
|
public boolean doPartition(ResultSetPartitioner partitioner, CaryoImportState state) {
|
103
|
boolean success = true;
|
104
|
|
105
|
Set<TaxonBase> objectsToSave = new HashSet<TaxonBase>();
|
106
|
|
107
|
// Map<String, Taxon> taxonMap = (Map<String, Taxon>) partitioner.getObjectMap(TAXON_NAMESPACE);
|
108
|
|
109
|
|
110
|
classification = getClassification(state);
|
111
|
|
112
|
try {
|
113
|
doFamilies(state);
|
114
|
doAuthors(state);
|
115
|
doJournals(state);
|
116
|
doBooks(state);
|
117
|
|
118
|
ResultSet rs = partitioner.getResultSet();
|
119
|
|
120
|
int i = 0;
|
121
|
|
122
|
//for each reference
|
123
|
while (rs.next()){
|
124
|
|
125
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
|
126
|
|
127
|
Integer taxonId = rs.getInt("IDcurrentspec");
|
128
|
|
129
|
|
130
|
//String dtSpcJahr -> ignore !
|
131
|
//empty: fiSpcLiteratur
|
132
|
|
133
|
//TODO
|
134
|
//fiSpcspcgrptax
|
135
|
|
136
|
|
137
|
|
138
|
try {
|
139
|
|
140
|
|
141
|
// classification.addParentChild(nextHigherTaxon, species, sourceRef, null);
|
142
|
//
|
143
|
//
|
144
|
// this.doIdCreatedUpdatedNotes(state, species, rs, taxonId, TAXON_NAMESPACE);
|
145
|
//
|
146
|
// objectsToSave.add(species);
|
147
|
|
148
|
|
149
|
} catch (Exception e) {
|
150
|
logger.warn("Exception in current_species: IDcurrentspec " + taxonId + ". " + e.getMessage());
|
151
|
// e.printStackTrace();
|
152
|
}
|
153
|
|
154
|
}
|
155
|
|
156
|
// logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
|
157
|
|
158
|
logger.warn(pluralString + " to save: " + objectsToSave.size());
|
159
|
getTaxonService().save(objectsToSave);
|
160
|
|
161
|
return success;
|
162
|
} catch (SQLException e) {
|
163
|
logger.error("SQLException:" + e);
|
164
|
return false;
|
165
|
}
|
166
|
}
|
167
|
|
168
|
|
169
|
|
170
|
|
171
|
|
172
|
|
173
|
|
174
|
private void doAuthors(CaryoImportState state) {
|
175
|
// TODO Auto-generated method stub
|
176
|
|
177
|
}
|
178
|
|
179
|
|
180
|
|
181
|
|
182
|
private void doBooks(CaryoImportState state) {
|
183
|
// TODO Auto-generated method stub
|
184
|
|
185
|
}
|
186
|
|
187
|
|
188
|
|
189
|
|
190
|
private void doJournals(CaryoImportState state) {
|
191
|
// TODO Auto-generated method stub
|
192
|
|
193
|
}
|
194
|
|
195
|
|
196
|
|
197
|
|
198
|
private void doFamilies(CaryoImportState state) throws SQLException {
|
199
|
Source source = state.getConfig().getSource();
|
200
|
String sqlFamily = "SELECT DISTINCT family FROM table WHERE family IS NOT NULL";
|
201
|
ResultSet rs = source.getResultSet(sqlFamily);
|
202
|
while (rs.next()){
|
203
|
String family = rs.getString("family");
|
204
|
BotanicalName name = BotanicalName.NewInstance(Rank.FAMILY());
|
205
|
name.setGenusOrUninomial(family);
|
206
|
Taxon taxon = Taxon.NewInstance(name, state.getTransactionalSourceReference());
|
207
|
classification.addChildTaxon(taxon, null, null, null);
|
208
|
// taxon.addSource(id, idNamespace, citation, null);
|
209
|
|
210
|
|
211
|
familyMap.put(family, taxon);
|
212
|
}
|
213
|
|
214
|
}
|
215
|
|
216
|
private Classification getClassification(CaryoImportState state) {
|
217
|
if (this.classification == null){
|
218
|
String name = state.getConfig().getClassificationName();
|
219
|
Reference<?> reference = state.getTransactionalSourceReference();
|
220
|
this.classification = Classification.NewInstance(name, reference, Language.DEFAULT());
|
221
|
if (state.getConfig().getClassificationUuid() != null){
|
222
|
classification.setUuid(state.getConfig().getClassificationUuid());
|
223
|
}
|
224
|
getClassificationService().save(classification);
|
225
|
}
|
226
|
return this.classification;
|
227
|
|
228
|
}
|
229
|
|
230
|
|
231
|
|
232
|
|
233
|
|
234
|
/* (non-Javadoc)
|
235
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
|
236
|
*/
|
237
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
|
238
|
// String nameSpace;
|
239
|
// Class cdmClass;
|
240
|
// Set<String> idSet;
|
241
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
242
|
// try{
|
243
|
// Set<String> taxonIdSet = new HashSet<String>();
|
244
|
//
|
245
|
// while (rs.next()){
|
246
|
//// handleForeignKey(rs, taxonIdSet, "taxonId");
|
247
|
// }
|
248
|
//
|
249
|
// //taxon map
|
250
|
// nameSpace = TAXON_NAMESPACE;
|
251
|
// cdmClass = Taxon.class;
|
252
|
// idSet = taxonIdSet;
|
253
|
// Map<String, Taxon> objectMap = (Map<String, Taxon>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
254
|
// result.put(nameSpace, objectMap);
|
255
|
//
|
256
|
//
|
257
|
// } catch (SQLException e) {
|
258
|
// throw new RuntimeException(e);
|
259
|
// }
|
260
|
return result;
|
261
|
}
|
262
|
|
263
|
/* (non-Javadoc)
|
264
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
265
|
*/
|
266
|
@Override
|
267
|
protected boolean doCheck(CaryoImportState state){
|
268
|
return true;
|
269
|
}
|
270
|
|
271
|
|
272
|
/* (non-Javadoc)
|
273
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
274
|
*/
|
275
|
protected boolean isIgnore(CaryoImportState state){
|
276
|
return ! state.getConfig().isDoTaxa();
|
277
|
}
|
278
|
|
279
|
|
280
|
|
281
|
|
282
|
|
283
|
|
284
|
|
285
|
}
|