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.taxonx2013;
|
11
|
|
12
|
import java.awt.Dimension;
|
13
|
import java.io.File;
|
14
|
import java.io.FileWriter;
|
15
|
import java.io.IOException;
|
16
|
import java.io.InputStream;
|
17
|
import java.net.URI;
|
18
|
import java.net.URL;
|
19
|
import java.util.ArrayList;
|
20
|
import java.util.HashMap;
|
21
|
import java.util.List;
|
22
|
import java.util.Map;
|
23
|
|
24
|
import javax.swing.JOptionPane;
|
25
|
import javax.swing.JScrollPane;
|
26
|
import javax.swing.JTextArea;
|
27
|
import javax.xml.parsers.DocumentBuilder;
|
28
|
import javax.xml.parsers.DocumentBuilderFactory;
|
29
|
|
30
|
import org.apache.log4j.Logger;
|
31
|
import org.springframework.stereotype.Component;
|
32
|
import org.springframework.transaction.TransactionStatus;
|
33
|
import org.w3c.dom.Document;
|
34
|
|
35
|
import eu.etaxonomy.cdm.io.common.ICdmIO;
|
36
|
import eu.etaxonomy.cdm.io.specimen.SpecimenImportBase;
|
37
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
38
|
import eu.etaxonomy.cdm.model.common.Language;
|
39
|
import eu.etaxonomy.cdm.model.description.Feature;
|
40
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
41
|
import eu.etaxonomy.cdm.model.name.Rank;
|
42
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
43
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
44
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
45
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
46
|
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
|
47
|
|
48
|
/**
|
49
|
* @author p.kelbert 2013
|
50
|
*/
|
51
|
@Component
|
52
|
public class TaxonXImport extends SpecimenImportBase<TaxonXImportConfigurator, TaxonXImportState> implements ICdmIO<TaxonXImportState> {
|
53
|
private static final Logger logger = Logger.getLogger(TaxonXImport.class);
|
54
|
private static String prefix = "";
|
55
|
|
56
|
private Classification classification = null;
|
57
|
private Reference<?> ref = null;
|
58
|
|
59
|
private TaxonXImportState taxonXstate;
|
60
|
private TaxonXDataHolder dataHolder;
|
61
|
private DerivedUnit derivedUnitBase;
|
62
|
|
63
|
private TransactionStatus tx;
|
64
|
|
65
|
private TaxonXXMLFieldGetter taxonXFieldGetter;
|
66
|
|
67
|
public TaxonXImport() {
|
68
|
super();
|
69
|
}
|
70
|
|
71
|
private Map<String,Feature> featuresMap = new HashMap<String, Feature>();
|
72
|
|
73
|
@Override
|
74
|
protected boolean doCheck(TaxonXImportState state) {
|
75
|
logger.warn("Checking not yet implemented for " + this.getClass().getSimpleName());
|
76
|
this.taxonXstate = state;
|
77
|
return true;
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* getClassification
|
82
|
* asks for user interaction for decision
|
83
|
* @param classificationName : the name of the classification we are looking for
|
84
|
* @set the global classification object
|
85
|
*/
|
86
|
private void setClassification(String classificationName, String defaultClassificatioName) {
|
87
|
logger.info("SET CLASSIFICATION "+classification);
|
88
|
|
89
|
List<Classification> classifList = getClassificationService().list(Classification.class, null, null, null, null);
|
90
|
Map<String,Classification> classifDic = new HashMap<String,Classification>();
|
91
|
ArrayList<String> nodeList = new ArrayList<String>();
|
92
|
String citation ="";
|
93
|
String title ="";
|
94
|
for (Classification cla:classifList){
|
95
|
try{
|
96
|
citation = cla.getCitation().toString();
|
97
|
}catch(Exception e){
|
98
|
citation="";
|
99
|
}
|
100
|
try{
|
101
|
title=cla.getTitleCache().toString();
|
102
|
}catch(Exception e){
|
103
|
title="no name";
|
104
|
}
|
105
|
if (citation.length()>title.length()){
|
106
|
nodeList.add(citation);
|
107
|
classifDic.put(citation, cla);
|
108
|
}
|
109
|
else{
|
110
|
nodeList.add(title);
|
111
|
classifDic.put(title, cla);
|
112
|
}
|
113
|
}
|
114
|
if (nodeList.indexOf(classificationName)<0) {
|
115
|
nodeList.add(classificationName);
|
116
|
}
|
117
|
nodeList.add("Other classification - add a new one");
|
118
|
|
119
|
String s="";
|
120
|
if (defaultClassificatioName != null && nodeList.indexOf(defaultClassificatioName)>-1) {
|
121
|
s=defaultClassificatioName;
|
122
|
//System.out.println("classifdic: "+classifDic);
|
123
|
} else{
|
124
|
JTextArea textArea = new JTextArea("Which classification do you want to use ? \nThe current value is "+classificationName);
|
125
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
126
|
textArea.setLineWrap(true);
|
127
|
textArea.setWrapStyleWord(true);
|
128
|
scrollPane.setPreferredSize( new Dimension( 600, 100 ) );
|
129
|
|
130
|
s = (String)JOptionPane.showInputDialog(
|
131
|
null,
|
132
|
scrollPane,
|
133
|
"Classification",
|
134
|
JOptionPane.PLAIN_MESSAGE,
|
135
|
null,
|
136
|
nodeList.toArray(),
|
137
|
defaultClassificatioName);
|
138
|
}
|
139
|
ref=getReferenceService().find(ref.getUuid());
|
140
|
if (!classifDic.containsKey(s)){
|
141
|
//System.out.println("Classif inconnue ?? "+s+", "+classifDic);
|
142
|
if (s.equalsIgnoreCase("Other classification - add a new one")){
|
143
|
classificationName = askForValue("classification name ?",classificationName);
|
144
|
}
|
145
|
//new classification
|
146
|
classification = Classification.NewInstance(classificationName, null, Language.DEFAULT());
|
147
|
getClassificationService().saveOrUpdate(classification);
|
148
|
refreshTransaction();
|
149
|
}
|
150
|
else{
|
151
|
classification = classifDic.get(s);
|
152
|
}
|
153
|
if (classification == null) {
|
154
|
String name = taxonXstate.getConfig().getClassificationName();
|
155
|
classification = Classification.NewInstance(name, ref, Language.DEFAULT());
|
156
|
if (taxonXstate.getConfig().getClassificationUuid() != null) {
|
157
|
classification.setUuid(taxonXstate.getConfig().getClassificationUuid());
|
158
|
}
|
159
|
getClassificationService().saveOrUpdate(classification);
|
160
|
refreshTransaction();
|
161
|
}
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* asks users for decision
|
166
|
* @param string : the parameter name we are looking at
|
167
|
* @param defaultValue : the default value
|
168
|
* @return
|
169
|
*/
|
170
|
private String askForValue(String string, String defaultValue) {
|
171
|
JTextArea textArea = new JTextArea(string);
|
172
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
173
|
textArea.setLineWrap(true);
|
174
|
textArea.setWrapStyleWord(true);
|
175
|
scrollPane.setPreferredSize( new Dimension( 600, 100 ) );
|
176
|
|
177
|
String s = (String)JOptionPane.showInputDialog(
|
178
|
null,
|
179
|
scrollPane,
|
180
|
"What should be the "+string,
|
181
|
JOptionPane.PLAIN_MESSAGE,
|
182
|
null,
|
183
|
null,
|
184
|
defaultValue);
|
185
|
|
186
|
return s;
|
187
|
|
188
|
}
|
189
|
|
190
|
@Override
|
191
|
public void doInvoke(TaxonXImportState state) {
|
192
|
System.out.println("INVOKE?");
|
193
|
taxonXstate = state;
|
194
|
tx = startTransaction();
|
195
|
|
196
|
logger.info("INVOKE TaxonXImport ");
|
197
|
URI sourceName = this.taxonXstate.getConfig().getSource();
|
198
|
|
199
|
// this.taxonXstate.getConfig().getClassificationName();
|
200
|
// this.taxonXstate.getConfig().getClassificationUuid();
|
201
|
|
202
|
ref = taxonXstate.getConfig().getSourceReference();
|
203
|
|
204
|
|
205
|
|
206
|
Reference<?> secundum = taxonXstate.getConfig().getSecundum();
|
207
|
List<Reference> references = this.getReferenceService().list(Reference.class, null, null, null, null);
|
208
|
boolean refFound=false;
|
209
|
for (Reference<?> re:references){
|
210
|
if (re.getCitation().equalsIgnoreCase(secundum.getCitation())){
|
211
|
refFound=true;
|
212
|
secundum =re;
|
213
|
}
|
214
|
}
|
215
|
if (refFound) {
|
216
|
secundum = CdmBase.deproxy(secundum, Reference.class);
|
217
|
taxonXstate.getConfig().setSecundum(secundum);
|
218
|
} else {
|
219
|
this.getReferenceService().saveOrUpdate(secundum);
|
220
|
secundum = CdmBase.deproxy(secundum, Reference.class);
|
221
|
taxonXstate.getConfig().setSecundum(secundum);
|
222
|
}
|
223
|
|
224
|
|
225
|
Reference urlRef = taxonXstate.getConfig().getOriginalSourceURL();
|
226
|
for (Reference<?> re:references){
|
227
|
if (re.getCitation().equalsIgnoreCase(urlRef.getCitation())){
|
228
|
urlRef=re;
|
229
|
}
|
230
|
}
|
231
|
this.getReferenceService().saveOrUpdate(urlRef);
|
232
|
urlRef=getReferenceService().find(urlRef.getUuid());
|
233
|
|
234
|
if(!taxonXstate.getConfig().hasAskedForHigherRank()){
|
235
|
Rank maxRank = askForHigherRank(taxonXstate.getConfig().getNomenclaturalCode());
|
236
|
taxonXstate.getConfig().setMaxRank(maxRank);
|
237
|
taxonXstate.getConfig().setHasAskedForHigherRank(true);
|
238
|
}
|
239
|
|
240
|
|
241
|
String message = "go taxonx!";
|
242
|
logger.info(message);
|
243
|
updateProgress(this.taxonXstate, message);
|
244
|
dataHolder = new TaxonXDataHolder();
|
245
|
|
246
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
247
|
DocumentBuilder builder;
|
248
|
URL url;
|
249
|
|
250
|
try {
|
251
|
builder = factory.newDocumentBuilder();
|
252
|
url = sourceName.toURL();
|
253
|
Object o = url.getContent();
|
254
|
InputStream is = (InputStream) o;
|
255
|
Document document = builder.parse(is);
|
256
|
|
257
|
taxonXFieldGetter = new TaxonXXMLFieldGetter(dataHolder, prefix,document, this,taxonXstate,classification,featuresMap);
|
258
|
/*parse the Mods from the TaxonX file
|
259
|
*create the appropriate Reference object
|
260
|
*/
|
261
|
ref = taxonXFieldGetter.parseMods();
|
262
|
// logger.info("REF : "+ref.getCitation());
|
263
|
// logger.info("CLASSNAME :" +taxonXstate.getConfig().getClassificationName());
|
264
|
setClassification(taxonXstate.getConfig().getClassificationName(),taxonXstate.getConfig().getImportClassificationName());
|
265
|
taxonXFieldGetter.updateClassification(classification);
|
266
|
// logger.info("classif :"+classification);
|
267
|
taxonXFieldGetter.parseTreatment(ref,sourceName);
|
268
|
featuresMap = taxonXFieldGetter.getFeaturesUsed();
|
269
|
// System.out.println("featuresMap after: "+featuresMap.toString());
|
270
|
|
271
|
// } catch (MalformedURLException e) {
|
272
|
// // TODO Auto-generated catch block
|
273
|
// e.printStackTrace();
|
274
|
// } catch (SAXException e) {
|
275
|
// // TODO Auto-generated catch block
|
276
|
// e.printStackTrace();
|
277
|
// } catch (IOException e) {
|
278
|
// // TODO Auto-generated catch block
|
279
|
// e.printStackTrace();
|
280
|
// } catch (ParserConfigurationException e) {
|
281
|
// // TODO Auto-generated catch block
|
282
|
// e.printStackTrace();
|
283
|
// }
|
284
|
}catch(Exception e){
|
285
|
e.printStackTrace();
|
286
|
logger.warn(e);
|
287
|
|
288
|
File file = new File("/home/pkelbert/Bureau/urlTaxonX.txt");
|
289
|
FileWriter writer;
|
290
|
try {
|
291
|
writer = new FileWriter(file ,true);
|
292
|
writer.write(sourceName+"\n");
|
293
|
writer.flush();
|
294
|
writer.close();
|
295
|
} catch (IOException e1) {
|
296
|
// TODO Auto-generated catch block
|
297
|
e1.printStackTrace();
|
298
|
}
|
299
|
|
300
|
logger.warn(sourceName);
|
301
|
}
|
302
|
|
303
|
//TODO:check how deduplicate work (and if it works..)
|
304
|
// if(taxonXstate.getConfig().getLastImport()) {
|
305
|
// deduplicate();
|
306
|
// }
|
307
|
commitTransaction(tx);
|
308
|
}
|
309
|
|
310
|
public void deduplicate(){
|
311
|
//System.out.println("DEDUPLICATE REFERENCE");
|
312
|
getReferenceService().deduplicate(Reference.class, null,null);
|
313
|
//System.out.println("DEDUPLICATE TAXONBASE");
|
314
|
getTaxonService().deduplicate(TaxonBase.class, null, null);
|
315
|
//System.out.println("DEDUP END");
|
316
|
}
|
317
|
|
318
|
|
319
|
|
320
|
/* (non-Javadoc)
|
321
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
|
322
|
*/
|
323
|
@Override
|
324
|
protected boolean isIgnore(TaxonXImportState state) {
|
325
|
// TODO Auto-generated method stub
|
326
|
return false;
|
327
|
}
|
328
|
|
329
|
private void refreshTransaction(){
|
330
|
commitTransaction(tx);
|
331
|
tx = startTransaction();
|
332
|
ref = getReferenceService().find(ref.getUuid());
|
333
|
classification = getClassificationService().find(classification.getUuid());
|
334
|
try{
|
335
|
derivedUnitBase = (DerivedUnit) getOccurrenceService().find(derivedUnitBase.getUuid());
|
336
|
}catch(Exception e){
|
337
|
//logger.warn("derivedunit up to date or not created yet");
|
338
|
}
|
339
|
}
|
340
|
|
341
|
/**
|
342
|
* @return
|
343
|
*/
|
344
|
private Rank askForHigherRank(NomenclaturalCode nomenclaturalCode) {
|
345
|
JTextArea textArea = new JTextArea("Everything below that rank should be imported:");
|
346
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
347
|
textArea.setLineWrap(true);
|
348
|
textArea.setWrapStyleWord(true);
|
349
|
scrollPane.setPreferredSize( new Dimension( 600, 50 ) );
|
350
|
|
351
|
List<Rank> rankList = new ArrayList<Rank>();
|
352
|
rankList = getTermService().listByTermClass(Rank.class, null, null, null, null);
|
353
|
|
354
|
List<String> rankListStr = new ArrayList<String>();
|
355
|
for (Rank r:rankList) {
|
356
|
rankListStr.add(r.toString());
|
357
|
}
|
358
|
String s = (String)JOptionPane.showInputDialog(
|
359
|
null,
|
360
|
scrollPane,
|
361
|
null,
|
362
|
JOptionPane.PLAIN_MESSAGE,
|
363
|
null,
|
364
|
rankListStr.toArray(),
|
365
|
"Suprageneric Taxon");
|
366
|
|
367
|
Rank cR = null;
|
368
|
try {
|
369
|
cR = Rank.getRankByEnglishName(s,nomenclaturalCode,true);
|
370
|
} catch (UnknownCdmTypeException e) {
|
371
|
logger.warn("Unknown rank ?!"+s);
|
372
|
logger.warn(e);
|
373
|
}
|
374
|
return cR;
|
375
|
}
|
376
|
|
377
|
|
378
|
}
|