1
|
/**
|
2
|
* Copyright (C) 2013 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
|
package eu.etaxonomy.cdm.io.specimen;
|
10
|
|
11
|
import java.awt.Color;
|
12
|
import java.awt.Dimension;
|
13
|
import java.awt.event.ItemEvent;
|
14
|
import java.awt.event.ItemListener;
|
15
|
import java.io.Serializable;
|
16
|
import java.util.ArrayList;
|
17
|
import java.util.Collections;
|
18
|
import java.util.HashMap;
|
19
|
import java.util.HashSet;
|
20
|
import java.util.List;
|
21
|
import java.util.Map;
|
22
|
import java.util.Set;
|
23
|
import java.util.UUID;
|
24
|
|
25
|
import javax.swing.BoxLayout;
|
26
|
import javax.swing.ButtonGroup;
|
27
|
import javax.swing.JLabel;
|
28
|
import javax.swing.JOptionPane;
|
29
|
import javax.swing.JPanel;
|
30
|
import javax.swing.JRadioButton;
|
31
|
import javax.swing.JScrollPane;
|
32
|
import javax.swing.JTextArea;
|
33
|
|
34
|
import org.apache.commons.lang.StringUtils;
|
35
|
import org.apache.logging.log4j.LogManager;
|
36
|
import org.apache.logging.log4j.Logger;
|
37
|
|
38
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
39
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
40
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
41
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
42
|
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
|
43
|
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
|
44
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
45
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
46
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
47
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
48
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
49
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
50
|
|
51
|
/**
|
52
|
* @author pkelbert
|
53
|
* @since 21 juin 2013
|
54
|
*/
|
55
|
public class SpecimenUserInteraction implements ItemListener, Serializable {
|
56
|
|
57
|
private static final long serialVersionUID = 6384263183459028523L;
|
58
|
|
59
|
@SuppressWarnings("unused")
|
60
|
private static Logger log = LogManager.getLogger(SpecimenUserInteraction.class);
|
61
|
|
62
|
public Classification askForClassification(Map<String, Classification> classMap){
|
63
|
List<String> possibilities = new ArrayList<String>(classMap.keySet());
|
64
|
Collections.sort(possibilities);
|
65
|
|
66
|
if (classMap.keySet().size()>0) {
|
67
|
classMap.put("Nothing matches, create a new classification",null);
|
68
|
possibilities.add(0, "Nothing matches, create a new classification");
|
69
|
} else {
|
70
|
return null;
|
71
|
}
|
72
|
|
73
|
JTextArea textArea = new JTextArea("Which existing classification should be used ?");
|
74
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
75
|
textArea.setLineWrap(true);
|
76
|
textArea.setWrapStyleWord(true);
|
77
|
scrollPane.setPreferredSize( new Dimension( 500, 50 ) );
|
78
|
|
79
|
String s = null;
|
80
|
while (s == null) {
|
81
|
s= (String)JOptionPane.showInputDialog(
|
82
|
null,
|
83
|
scrollPane,
|
84
|
"Please select a classification in the list",
|
85
|
JOptionPane.PLAIN_MESSAGE,
|
86
|
null,
|
87
|
possibilities.toArray(),
|
88
|
"Nothing matches, create a new classification");
|
89
|
}
|
90
|
return classMap.get(s);
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* @return the name for the new Classification
|
95
|
*/
|
96
|
public String createNewClassification() {
|
97
|
JTextArea textArea = new JTextArea("How should the classification be named ?");
|
98
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
99
|
textArea.setLineWrap(true);
|
100
|
textArea.setWrapStyleWord(true);
|
101
|
scrollPane.setPreferredSize( new Dimension( 500, 50 ) );
|
102
|
|
103
|
String s = null;
|
104
|
while (s == null) {
|
105
|
s=(String)JOptionPane.showInputDialog(
|
106
|
null,
|
107
|
scrollPane,
|
108
|
"Get full classification name",
|
109
|
JOptionPane.PLAIN_MESSAGE,
|
110
|
null,
|
111
|
null,
|
112
|
"ABCD Import");
|
113
|
}
|
114
|
return s;
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* @param refMap
|
119
|
* @return
|
120
|
*/
|
121
|
public Reference askForReference(Map<String, Reference> refMap) {
|
122
|
List<String> possibilities = new ArrayList<>(refMap.keySet());
|
123
|
Collections.sort(possibilities);
|
124
|
if (refMap.keySet().size()>0) {
|
125
|
refMap.put("Nothing matches, create a new reference",null);
|
126
|
possibilities.add(0, "Nothing matches, create a new reference");
|
127
|
} else {
|
128
|
return null;
|
129
|
}
|
130
|
|
131
|
JTextArea textArea = new JTextArea("Which existing reference should be used?");
|
132
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
133
|
textArea.setLineWrap(true);
|
134
|
textArea.setWrapStyleWord(true);
|
135
|
scrollPane.setPreferredSize( new Dimension( 700, 50 ) );
|
136
|
|
137
|
String s = null;
|
138
|
while (s == null) {
|
139
|
s= (String)JOptionPane.showInputDialog(
|
140
|
null,
|
141
|
scrollPane,
|
142
|
"Please select a reference in the list",
|
143
|
JOptionPane.PLAIN_MESSAGE,
|
144
|
null,
|
145
|
possibilities.toArray(),
|
146
|
"ABCD reference");
|
147
|
}
|
148
|
return refMap.get(s);
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* @param refMap
|
153
|
* @param iReferenceService
|
154
|
* @param docSources
|
155
|
* @return
|
156
|
*/
|
157
|
public List<OriginalSourceBase> askForSource(Map<String, OriginalSourceBase> refMap, String currentElement, String blabla,
|
158
|
IReferenceService iReferenceService, List<String> docSources) {
|
159
|
|
160
|
// System.out.println(refMap);
|
161
|
List<String> possibilities = new ArrayList<> (refMap.keySet());
|
162
|
|
163
|
Set<String> all = new HashSet<>();
|
164
|
all.addAll(possibilities);
|
165
|
|
166
|
List<String> allList = new ArrayList<String>();
|
167
|
allList.addAll(all);
|
168
|
Collections.sort(allList);
|
169
|
allList.add(0, "Create a new source");
|
170
|
|
171
|
JLabel label = new JLabel(blabla);
|
172
|
|
173
|
sources=new ArrayList<String>();
|
174
|
|
175
|
JPanel checkPanel = null;
|
176
|
ButtonGroup group = null;
|
177
|
JScrollPane scrollPane = null;
|
178
|
|
179
|
JRadioButton jcb = null;
|
180
|
|
181
|
Object[] options = {"Add and close", "Add and continue - I want to add more sources","Close without adding anything"};
|
182
|
|
183
|
// System.out.println(docSources);
|
184
|
int n=1;
|
185
|
while (n==1){
|
186
|
group = new ButtonGroup();
|
187
|
checkPanel = new JPanel();
|
188
|
checkPanel.setLayout(new BoxLayout(checkPanel, BoxLayout.Y_AXIS));
|
189
|
// allList.removeAll(sources);
|
190
|
scrollPane = new JScrollPane(checkPanel);
|
191
|
scrollPane.setPreferredSize( new Dimension( 700, 300 ) );
|
192
|
|
193
|
checkPanel.add(label);
|
194
|
|
195
|
for (String ch:allList){
|
196
|
if (StringUtils.isBlank(ch)) {
|
197
|
continue;
|
198
|
}
|
199
|
// System.out.println("HOP ="+ch+"=");
|
200
|
if(docSources.contains(ch)) {
|
201
|
jcb = new JRadioButton("<html>"+ch.replace("---", "<br/>")+"</html>");
|
202
|
jcb.setForeground(Color.blue);
|
203
|
} else {
|
204
|
jcb = new JRadioButton("<html>"+ch.replace("---", "<br/>")+"</html>");
|
205
|
jcb.setForeground(Color.black);
|
206
|
}
|
207
|
jcb.addItemListener(this);
|
208
|
group.add(jcb);
|
209
|
checkPanel.add(jcb);
|
210
|
}
|
211
|
|
212
|
n = JOptionPane.showOptionDialog(null,
|
213
|
scrollPane,
|
214
|
"Choose a source for "+currentElement +"(in blue the source from the document)",
|
215
|
JOptionPane.YES_NO_CANCEL_OPTION,
|
216
|
JOptionPane.QUESTION_MESSAGE,
|
217
|
null,
|
218
|
options,
|
219
|
options[1]);
|
220
|
if(n<3 && !currentSource.isEmpty() && !currentSource.equalsIgnoreCase("Create a new source")) {
|
221
|
sources.add(currentSource);
|
222
|
}
|
223
|
// System.out.println("current source: "+currentSource);
|
224
|
if(currentSource.equalsIgnoreCase("Create a new source")){
|
225
|
String a = createNewSource();
|
226
|
if (a!=null && !a.isEmpty()) {
|
227
|
sources.add(a);
|
228
|
}
|
229
|
}
|
230
|
}
|
231
|
|
232
|
List<OriginalSourceBase> dess = new ArrayList<>();
|
233
|
for (String src:sources){
|
234
|
if (refMap.get(src) !=null) {
|
235
|
dess.add(refMap.get(src));
|
236
|
}
|
237
|
else{
|
238
|
Reference re = null;
|
239
|
String titlecache="";
|
240
|
String micro="";
|
241
|
if (src.indexOf("---")>-1){
|
242
|
titlecache = src.split("---")[0].trim();
|
243
|
micro=src.split("---")[1].trim();
|
244
|
}
|
245
|
else{
|
246
|
titlecache= src.split("---")[0].trim();
|
247
|
}
|
248
|
|
249
|
List<Reference> references = iReferenceService.list(Reference.class, null, null, null, null);
|
250
|
for (Reference reference:references){
|
251
|
if (reference.getTitleCache().equalsIgnoreCase(titlecache)) {
|
252
|
re=reference;
|
253
|
}
|
254
|
}
|
255
|
if(re == null){
|
256
|
re = ReferenceFactory.newGeneric();
|
257
|
re.setTitleCache(titlecache);
|
258
|
iReferenceService.saveOrUpdate(re);
|
259
|
}
|
260
|
|
261
|
dess.add(IdentifiableSource.NewInstance(OriginalSourceType.Import,null, null, re,micro));
|
262
|
}
|
263
|
}
|
264
|
return dess;
|
265
|
}
|
266
|
|
267
|
public String createNewReference() {
|
268
|
JTextArea textArea = new JTextArea("How should the reference be named ?");
|
269
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
270
|
textArea.setLineWrap(true);
|
271
|
textArea.setWrapStyleWord(true);
|
272
|
scrollPane.setPreferredSize( new Dimension( 500, 50 ) );
|
273
|
|
274
|
String s = null;
|
275
|
while (s == null) {
|
276
|
s= (String)JOptionPane.showInputDialog(
|
277
|
null,
|
278
|
scrollPane,
|
279
|
"Get full reference name",
|
280
|
JOptionPane.PLAIN_MESSAGE,
|
281
|
null,
|
282
|
null,
|
283
|
"ABCD Import from XML");
|
284
|
}
|
285
|
return s;
|
286
|
}
|
287
|
|
288
|
public String createNewSource() {
|
289
|
JTextArea textArea = new JTextArea("How should the source be named? If there is a citation detail, prefix it with 3 minus signs ('---').");
|
290
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
291
|
textArea.setLineWrap(true);
|
292
|
textArea.setWrapStyleWord(true);
|
293
|
scrollPane.setPreferredSize( new Dimension( 500, 50 ) );
|
294
|
|
295
|
String s = null;
|
296
|
while (s == null) {
|
297
|
s= (String)JOptionPane.showInputDialog(
|
298
|
null,
|
299
|
scrollPane,
|
300
|
"Get full source name",
|
301
|
JOptionPane.PLAIN_MESSAGE,
|
302
|
null,
|
303
|
null,
|
304
|
"ABCD Import from XML");
|
305
|
}
|
306
|
return s;
|
307
|
}
|
308
|
|
309
|
public TaxonDescription askForDescriptionGroup(Set<TaxonDescription> descriptions) {
|
310
|
JTextArea textArea = new JTextArea("One or several description group(s) does already exist for this taxon.");
|
311
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
312
|
textArea.setLineWrap(true);
|
313
|
textArea.setWrapStyleWord(true);
|
314
|
scrollPane.setPreferredSize( new Dimension( 700, 50 ) );
|
315
|
|
316
|
Map<String,TaxonDescription> descrMap = new HashMap<String, TaxonDescription>();
|
317
|
|
318
|
int descCnt=1;
|
319
|
for (TaxonDescription description : descriptions){
|
320
|
// System.out.println("descr. titlecache "+description.getTitleCache());
|
321
|
Set<IdentifiableSource> sources = description.getTaxon().getSources();
|
322
|
sources.addAll(description.getSources());
|
323
|
List<String> src=new ArrayList<String>();
|
324
|
for (IdentifiableSource s:sources) {
|
325
|
src.add(s.getCitation().getTitleCache());
|
326
|
}
|
327
|
List<String> srcb = new ArrayList<String>(new HashSet<>(src));
|
328
|
if (srcb.size()>0) {
|
329
|
if(descrMap.containsKey(descCnt+": "+description.getTitleCache()+"("+StringUtils.join(srcb,";")+")")) {
|
330
|
descCnt+=1;
|
331
|
}
|
332
|
descrMap.put(descCnt+": "+description.getTitleCache()+"("+StringUtils.join(srcb,";")+")",description);
|
333
|
}
|
334
|
else {
|
335
|
if(descrMap.containsKey(description.getTitleCache())) {
|
336
|
descCnt+=1;
|
337
|
}
|
338
|
descrMap.put(descCnt+": "+description.getTitleCache(),description);
|
339
|
// for (IdentifiableSource source:sources){
|
340
|
// if(ref.equals(source.getCitation())) {
|
341
|
// taxonDescription = description;
|
342
|
// }
|
343
|
// }
|
344
|
}
|
345
|
}
|
346
|
List<String> possibilities = new ArrayList<>(descrMap.keySet());
|
347
|
if (possibilities.size()==0) {
|
348
|
return null;
|
349
|
}
|
350
|
Collections.sort(possibilities);
|
351
|
|
352
|
descrMap.put("No, add a brand new description group", null);
|
353
|
possibilities.add(0, "No, add a brand new description group");
|
354
|
|
355
|
String s = null;
|
356
|
while (s == null) {
|
357
|
s= (String)JOptionPane.showInputDialog(
|
358
|
null,
|
359
|
scrollPane,
|
360
|
"What should be done? Should an existing group be reused ? ",
|
361
|
JOptionPane.PLAIN_MESSAGE,
|
362
|
null,
|
363
|
possibilities.toArray(),
|
364
|
"No, add a brand new description group");
|
365
|
}
|
366
|
|
367
|
if (descrMap.get(s) !=null) {
|
368
|
return descrMap.get(s);
|
369
|
} else {
|
370
|
return null;
|
371
|
}
|
372
|
}
|
373
|
|
374
|
/**
|
375
|
* Look if the same name already exists in the ALL classifications and ask the user to select one or none.
|
376
|
* @param scientificName
|
377
|
* @param taxonList
|
378
|
* @return null if not found, or the selected Taxon
|
379
|
*/
|
380
|
@SuppressWarnings("rawtypes")
|
381
|
public Taxon askWhereToFixData(String scientificName, List<TaxonBase> taxonList, Classification classification) {
|
382
|
Map<String,TaxonNode> classMap = new HashMap<>();
|
383
|
boolean sameClassification=false;
|
384
|
Taxon n = null;
|
385
|
for (TaxonBase taxonBase: taxonList){
|
386
|
if(taxonBase.isInstanceOf(Taxon.class)){
|
387
|
Taxon taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
|
388
|
for (TaxonNode node : taxon.getTaxonNodes()){
|
389
|
classMap.put("Reuse the one from the classification \""+node.getClassification().getTitleCache()+"\"", node);
|
390
|
if (node.getClassification().getUuid().equals(classification.getUuid())) {
|
391
|
sameClassification=true;
|
392
|
n=node.getTaxon();
|
393
|
}
|
394
|
}
|
395
|
}
|
396
|
}
|
397
|
if (classMap.keySet().size()==1 && sameClassification) {
|
398
|
return n;
|
399
|
}
|
400
|
|
401
|
JTextArea textArea = new JTextArea("The same taxon ("+scientificName+") already exists in an other classification.");
|
402
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
403
|
textArea.setLineWrap(true);
|
404
|
textArea.setWrapStyleWord(true);
|
405
|
scrollPane.setPreferredSize( new Dimension( 700, 50 ) );
|
406
|
|
407
|
List<String> possibilities = new ArrayList<String> (classMap.keySet());
|
408
|
if (possibilities.size()==0) {
|
409
|
return null;
|
410
|
}
|
411
|
Collections.sort(possibilities);
|
412
|
if(!sameClassification){
|
413
|
classMap.put("Add a brand new Taxon to the current classification, no recycling please", null);
|
414
|
possibilities.add(0, "Add a brand new Taxon to the current classification, no recycling please");
|
415
|
}
|
416
|
String s = null;
|
417
|
while (s == null) {
|
418
|
s= (String)JOptionPane.showInputDialog(
|
419
|
null,
|
420
|
scrollPane,
|
421
|
"What should be done? ",
|
422
|
JOptionPane.PLAIN_MESSAGE,
|
423
|
null,
|
424
|
possibilities.toArray(),
|
425
|
"Add a brand new Taxon to the current classification, no recycling please");
|
426
|
}
|
427
|
|
428
|
if (classMap.get(s) !=null) {
|
429
|
return classMap.get(s).getTaxon();
|
430
|
} else {
|
431
|
return null;
|
432
|
}
|
433
|
}
|
434
|
|
435
|
/**
|
436
|
* Look if the same TaxonBase already exists in the SAME classification
|
437
|
* @param taxonBaseList
|
438
|
* @return null if not found, or the corresponding Taxon
|
439
|
*/
|
440
|
@SuppressWarnings("rawtypes")
|
441
|
public Taxon lookForTaxaIntoCurrentClassification(List<TaxonBase> taxonBaseList, Classification classification) {
|
442
|
Taxon taxonFound =null;
|
443
|
for (TaxonBase taxonBase:taxonBaseList){
|
444
|
if(taxonBase.isInstanceOf(Taxon.class)){
|
445
|
Taxon taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
|
446
|
for (TaxonNode node : taxon.getTaxonNodes()){
|
447
|
UUID classUuid = node.getClassification().getUuid();
|
448
|
if (classification.getUuid().equals(classUuid)){
|
449
|
taxonFound=taxon;
|
450
|
}
|
451
|
}
|
452
|
}
|
453
|
}
|
454
|
return taxonFound;
|
455
|
}
|
456
|
|
457
|
List<String> sources = new ArrayList<>();
|
458
|
String currentSource = "";
|
459
|
|
460
|
@Override
|
461
|
public void itemStateChanged(ItemEvent e) {
|
462
|
JRadioButton cb = (JRadioButton) e.getItem();
|
463
|
int state = e.getStateChange();
|
464
|
if (state == ItemEvent.SELECTED) {
|
465
|
currentSource=cb.getText().replace("</html>", "").replace("<html>","")
|
466
|
.replace("<br/>","---").replace("<font color=\"blue\">","").replace("</font>","");
|
467
|
} else {
|
468
|
currentSource="";
|
469
|
}
|
470
|
}
|
471
|
}
|