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.berlinModel;
|
11
|
|
12
|
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.FACT_DESCRIPTION;
|
13
|
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.FACT_DISTRIBUTION_EM;
|
14
|
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.FACT_OBSERVATION;
|
15
|
|
16
|
import java.sql.ResultSet;
|
17
|
import java.sql.SQLException;
|
18
|
import java.util.Collection;
|
19
|
import java.util.HashSet;
|
20
|
import java.util.Map;
|
21
|
import java.util.Set;
|
22
|
|
23
|
import org.apache.log4j.Logger;
|
24
|
import org.springframework.stereotype.Component;
|
25
|
|
26
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
27
|
import eu.etaxonomy.cdm.io.common.ICdmIO;
|
28
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
|
29
|
import eu.etaxonomy.cdm.io.common.MapWrapper;
|
30
|
import eu.etaxonomy.cdm.io.common.Source;
|
31
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
32
|
import eu.etaxonomy.cdm.model.common.Language;
|
33
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
34
|
import eu.etaxonomy.cdm.model.description.Feature;
|
35
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
36
|
import eu.etaxonomy.cdm.model.description.TextData;
|
37
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
38
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
39
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
40
|
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
|
41
|
|
42
|
/**
|
43
|
* @author a.mueller
|
44
|
* @created 20.03.2008
|
45
|
* @version 1.0
|
46
|
*/
|
47
|
@Component
|
48
|
public class BerlinModelFactsImport extends BerlinModelImportBase {
|
49
|
private static final Logger logger = Logger.getLogger(BerlinModelFactsImport.class);
|
50
|
|
51
|
private int modCount = 10000;
|
52
|
|
53
|
public BerlinModelFactsImport(){
|
54
|
super();
|
55
|
}
|
56
|
|
57
|
/* (non-Javadoc)
|
58
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
59
|
*/
|
60
|
@Override
|
61
|
protected boolean doCheck(IImportConfigurator config){
|
62
|
boolean result = true;
|
63
|
logger.warn("Checking for Facts not yet implemented");
|
64
|
//result &= checkArticlesWithoutJournal(bmiConfig);
|
65
|
//result &= checkPartOfJournal(bmiConfig);
|
66
|
|
67
|
return result;
|
68
|
}
|
69
|
|
70
|
private TermVocabulary<Feature> getFeatureVocabulary(){
|
71
|
try {
|
72
|
//TODO work around until service method works
|
73
|
TermVocabulary<Feature> featureVocabulary = BerlinModelTransformer.factCategory2Feature(1).getVocabulary();
|
74
|
//TermVocabulary<Feature> vocabulary = getTermService().getVocabulary(vocabularyUuid);
|
75
|
return featureVocabulary;
|
76
|
} catch (UnknownCdmTypeException e) {
|
77
|
logger.error("Feature vocabulary not available. New vocabulary created");
|
78
|
return new TermVocabulary<Feature>() ;
|
79
|
}
|
80
|
}
|
81
|
|
82
|
private MapWrapper<Feature> invokeFactCategories(BerlinModelImportConfigurator bmiConfig){
|
83
|
|
84
|
// Map<Integer, Feature> featureMap = new HashMap<Integer, Feature>();
|
85
|
MapWrapper<Feature> result = bmiConfig.getFeatureMap();
|
86
|
|
87
|
Source source = bmiConfig.getSource();
|
88
|
|
89
|
|
90
|
try {
|
91
|
//get data from database
|
92
|
String strQuery =
|
93
|
" SELECT FactCategory.* " +
|
94
|
" FROM FactCategory "+
|
95
|
" WHERE (1=1)";
|
96
|
ResultSet rs = source.getResultSet(strQuery) ;
|
97
|
|
98
|
|
99
|
TermVocabulary<Feature> featureVocabulary = getFeatureVocabulary();
|
100
|
int i = 0;
|
101
|
//for each reference
|
102
|
while (rs.next()){
|
103
|
|
104
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("FactCategories handled: " + (i-1));}
|
105
|
|
106
|
int factCategoryId = rs.getInt("factCategoryId");
|
107
|
String factCategory = rs.getString("factCategory");
|
108
|
|
109
|
|
110
|
Feature feature;
|
111
|
try {
|
112
|
feature = BerlinModelTransformer.factCategory2Feature(factCategoryId);
|
113
|
} catch (UnknownCdmTypeException e) {
|
114
|
logger.warn("New Feature (FactCategoryId: " + factCategoryId + ")");
|
115
|
feature = Feature.NewInstance(factCategory, factCategory, null);
|
116
|
feature.setVocabulary(featureVocabulary);
|
117
|
feature.setSupportsTextData(true);
|
118
|
//TODO
|
119
|
// MaxFactNumber int Checked
|
120
|
// ExtensionTableName varchar(100) Checked
|
121
|
// Description nvarchar(1000) Checked
|
122
|
// locExtensionFormName nvarchar(80) Checked
|
123
|
// RankRestrictionFk int Checked
|
124
|
}
|
125
|
|
126
|
// featureMap.put(factCategoryId, feature);
|
127
|
result.put(factCategoryId, feature);
|
128
|
|
129
|
}
|
130
|
Collection<Feature> col = result.getAllValues();
|
131
|
getTermService().saveTermsAll(col);
|
132
|
return result;
|
133
|
} catch (SQLException e) {
|
134
|
logger.error("SQLException:" + e);
|
135
|
return null;
|
136
|
}
|
137
|
|
138
|
}
|
139
|
|
140
|
|
141
|
/* (non-Javadoc)
|
142
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
|
143
|
*/
|
144
|
@Override
|
145
|
protected boolean doInvoke(IImportConfigurator config,
|
146
|
Map<String, MapWrapper<? extends CdmBase>> stores){
|
147
|
boolean result = true;
|
148
|
|
149
|
MapWrapper<TaxonBase> taxonMap = (MapWrapper<TaxonBase>)stores.get(ICdmIO.TAXON_STORE);
|
150
|
MapWrapper<ReferenceBase> referenceMap = (MapWrapper<ReferenceBase>)stores.get(ICdmIO.REFERENCE_STORE);
|
151
|
MapWrapper<ReferenceBase> nomRefMap = (MapWrapper<ReferenceBase>)stores.get(ICdmIO.NOMREF_STORE);
|
152
|
|
153
|
Set<TaxonBase> taxonStore = new HashSet<TaxonBase>();
|
154
|
|
155
|
BerlinModelImportConfigurator bmiConfig = (BerlinModelImportConfigurator)config;
|
156
|
Source source = bmiConfig.getSource();
|
157
|
|
158
|
logger.info("start makeFacts ...");
|
159
|
|
160
|
MapWrapper<Feature> featureMap = invokeFactCategories(bmiConfig);
|
161
|
|
162
|
//for testing only
|
163
|
//TaxonBase taxonBase = Taxon.NewInstance(BotanicalName.NewInstance(null), null);
|
164
|
|
165
|
|
166
|
try {
|
167
|
//get data from database
|
168
|
String strQuery =
|
169
|
" SELECT Fact.*, PTaxon.RIdentifier as taxonId, RefDetail.Details " +
|
170
|
" FROM Fact " +
|
171
|
" INNER JOIN PTaxon ON Fact.PTNameFk = PTaxon.PTNameFk AND Fact.PTRefFk = PTaxon.PTRefFk " +
|
172
|
" LEFT OUTER JOIN RefDetail ON Fact.FactRefDetailFk = RefDetail.RefDetailId AND Fact.FactRefFk = RefDetail.RefFk " +
|
173
|
" WHERE (1=1)";
|
174
|
ResultSet rs = source.getResultSet(strQuery) ;
|
175
|
|
176
|
int i = 0;
|
177
|
//for each reference
|
178
|
while (rs.next()){
|
179
|
try{
|
180
|
if ((i++ % modCount) == 0){ logger.info("Facts handled: " + (i-1));}
|
181
|
|
182
|
//Map<String, Object> valueMap = getValueMap(rs);
|
183
|
|
184
|
int factId = rs.getInt("factId");
|
185
|
|
186
|
|
187
|
Object taxonIdObj = rs.getObject("taxonId");
|
188
|
int taxonId = rs.getInt("taxonId");
|
189
|
Object factRefFkObj = rs.getObject("factRefFk");
|
190
|
int factRefFk = rs.getInt("factRefFk");
|
191
|
Object categoryFkObj = rs.getObject("factCategoryFk");
|
192
|
Integer categoryFk = rs.getInt("factCategoryFk");
|
193
|
|
194
|
String details = rs.getString("Details");
|
195
|
// int ptDesignationRefFk = rs.getInt("PTDesignationRefFk");
|
196
|
// String ptDesignation details = rs.getInt("PTDesignationRefDetailFk");
|
197
|
String fact = CdmUtils.Nz(rs.getString("Fact"));
|
198
|
String notes = CdmUtils.Nz(rs.getString("notes"));
|
199
|
|
200
|
|
201
|
|
202
|
TaxonBase taxonBase;
|
203
|
if (taxonIdObj != null){
|
204
|
taxonBase = taxonMap.get(taxonId);
|
205
|
}else{
|
206
|
taxonBase = null;
|
207
|
}
|
208
|
Feature feature;
|
209
|
if (categoryFkObj != null){
|
210
|
feature = featureMap.get(categoryFk);
|
211
|
}else{
|
212
|
feature = null;
|
213
|
}
|
214
|
|
215
|
if (taxonBase != null){
|
216
|
Taxon taxon;
|
217
|
if ( taxonBase instanceof Taxon ) {
|
218
|
taxon = (Taxon) taxonBase;
|
219
|
}else{
|
220
|
logger.warn("TaxonBase " + (taxonIdObj==null?"(null)":taxonIdObj) + " for Fact " + factId + " was not of type Taxon but: " + taxonBase.getClass().getSimpleName());
|
221
|
continue;
|
222
|
}
|
223
|
|
224
|
TaxonDescription taxonDescription;
|
225
|
Set<TaxonDescription> descriptionSet= taxon.getDescriptions();
|
226
|
if (descriptionSet.size() > 0) {
|
227
|
taxonDescription = descriptionSet.iterator().next();
|
228
|
}else{
|
229
|
taxonDescription = TaxonDescription.NewInstance();
|
230
|
taxon.addDescription(taxonDescription);
|
231
|
}
|
232
|
|
233
|
//textData
|
234
|
TextData textData = TextData.NewInstance();
|
235
|
//TODO textData.putText(fact, bmiConfig.getFactLanguage()); //doesn't work because bmiConfig.getFactLanguage() is not not a persistent Language Object
|
236
|
//throws in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: eu.etaxonomy.cdm.model.common.Language; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: eu.etaxonomy.cdm.model.common.Language
|
237
|
|
238
|
//for diptera database
|
239
|
if (categoryFk == 99 && notes.contains("<OriginalName>")){
|
240
|
notes = notes.replaceAll("<OriginalName>", "");
|
241
|
notes = notes.replaceAll("</OriginalName>", "");
|
242
|
fact = notes + ": " + fact ;
|
243
|
}
|
244
|
textData.putText(fact, Language.DEFAULT());
|
245
|
textData.setType(feature);
|
246
|
|
247
|
//
|
248
|
ReferenceBase citation;
|
249
|
if (factRefFkObj != null){
|
250
|
citation = referenceMap.get(factRefFk);
|
251
|
if (citation == null){
|
252
|
citation = nomRefMap.get(factRefFk);
|
253
|
}
|
254
|
if (citation == null && (factRefFk != 0)){
|
255
|
logger.warn("Citation not found in referenceMap: " + factRefFk);
|
256
|
}
|
257
|
}else{
|
258
|
citation = null;
|
259
|
}
|
260
|
|
261
|
|
262
|
textData.setCitation(citation);
|
263
|
textData.setCitationMicroReference(details);
|
264
|
taxonDescription.addElement(textData);
|
265
|
|
266
|
|
267
|
// if (categoryFkObj == FACT_DESCRIPTION){
|
268
|
// //;
|
269
|
// }else if (categoryFkObj == FACT_OBSERVATION){
|
270
|
// //;
|
271
|
// }else if (categoryFkObj == FACT_DISTRIBUTION_EM){
|
272
|
// //
|
273
|
// }else {
|
274
|
// //TODO
|
275
|
// //logger.warn("FactCategory " + categoryFk + " not yet implemented");
|
276
|
// }
|
277
|
|
278
|
//TODO
|
279
|
//References
|
280
|
//etc.
|
281
|
|
282
|
//TODO created, notes
|
283
|
//doIdCreatedUpdatedNotes(bmiConfig, textData, rs, factId);
|
284
|
|
285
|
|
286
|
taxonStore.add(taxon);
|
287
|
}else{
|
288
|
//TODO
|
289
|
logger.warn("Taxon for Fact " + factId + " does not exist in store");
|
290
|
}
|
291
|
} catch (RuntimeException re){
|
292
|
logger.error("A runtime exception occurred during the facts import");
|
293
|
result = false;
|
294
|
throw re;
|
295
|
}
|
296
|
//put
|
297
|
}
|
298
|
logger.info("Facts handled: " + (i-1));
|
299
|
logger.info("Taxa to save: " + taxonStore.size());
|
300
|
getTaxonService().saveTaxonAll(taxonStore);
|
301
|
|
302
|
logger.info("end makeFacts ...");
|
303
|
return result;
|
304
|
} catch (SQLException e) {
|
305
|
logger.error("SQLException:" + e);
|
306
|
return false;
|
307
|
}
|
308
|
|
309
|
}
|
310
|
|
311
|
/* (non-Javadoc)
|
312
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
313
|
*/
|
314
|
protected boolean isIgnore(IImportConfigurator config){
|
315
|
return ! config.isDoFacts();
|
316
|
}
|
317
|
|
318
|
}
|