Project

General

Profile

Download (17.4 KB) Statistics
| Branch: | Tag: | Revision:
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.log4j.Logger;
36

    
37
import eu.etaxonomy.cdm.api.service.IReferenceService;
38
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
39
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
40
import eu.etaxonomy.cdm.model.description.TaxonDescription;
41
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
42
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
43
import eu.etaxonomy.cdm.model.reference.Reference;
44
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
45
import eu.etaxonomy.cdm.model.taxon.Classification;
46
import eu.etaxonomy.cdm.model.taxon.Taxon;
47
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
48
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
49

    
50
/**
51
 * @author pkelbert
52
 * @since 21 juin 2013
53
 */
54
public class SpecimenUserInteraction implements ItemListener, Serializable {
55

    
56
    private static final long serialVersionUID = 6384263183459028523L;
57
    @SuppressWarnings("unused")
58
    private static Logger log = Logger.getLogger(SpecimenUserInteraction.class);
59

    
60
    public Classification askForClassification(Map<String, Classification> classMap){
61
        List<String> possibilities = new ArrayList<String>(classMap.keySet());
62
        Collections.sort(possibilities);
63

    
64
        if (classMap.keySet().size()>0) {
65
            classMap.put("Nothing matches, create a new classification",null);
66
            possibilities.add(0, "Nothing matches, create a new classification");
67
        } else {
68
            return null;
69
        }
70

    
71
        JTextArea textArea = new JTextArea("Which existing classification should be used ?");
72
        JScrollPane scrollPane = new JScrollPane(textArea);
73
        textArea.setLineWrap(true);
74
        textArea.setWrapStyleWord(true);
75
        scrollPane.setPreferredSize( new Dimension( 500, 50 ) );
76

    
77
        String s = null;
78
        while (s == null) {
79
            s= (String)JOptionPane.showInputDialog(
80
                    null,
81
                    scrollPane,
82
                    "Please select a classification in the list",
83
                    JOptionPane.PLAIN_MESSAGE,
84
                    null,
85
                    possibilities.toArray(),
86
                    "Nothing matches, create a new classification");
87
        }
88
        return classMap.get(s);
89
    }
90

    
91
    /**
92
     * @return the name for the new Classification
93
     */
94
    public String createNewClassification() {
95
        JTextArea textArea = new JTextArea("How should the classification be named ?");
96
        JScrollPane scrollPane = new JScrollPane(textArea);
97
        textArea.setLineWrap(true);
98
        textArea.setWrapStyleWord(true);
99
        scrollPane.setPreferredSize( new Dimension( 500, 50 ) );
100

    
101
        String s = null;
102
        while (s == null) {
103
            s=(String)JOptionPane.showInputDialog(
104
                    null,
105
                    scrollPane,
106
                    "Get full classification name",
107
                    JOptionPane.PLAIN_MESSAGE,
108
                    null,
109
                    null,
110
                    "ABCD Import");
111
        }
112
        return s;
113
    }
114

    
115
    /**
116
     * @param refMap
117
     * @return
118
     */
119
    public Reference askForReference(Map<String, Reference> refMap) {
120
        List<String>  possibilities = new ArrayList<>(refMap.keySet());
121
        Collections.sort(possibilities);
122
        if (refMap.keySet().size()>0) {
123
            refMap.put("Nothing matches, create a new reference",null);
124
            possibilities.add(0, "Nothing matches, create a new reference");
125
        } else {
126
            return null;
127
        }
128

    
129
        JTextArea textArea = new JTextArea("Which existing reference should be used?");
130
        JScrollPane scrollPane = new JScrollPane(textArea);
131
        textArea.setLineWrap(true);
132
        textArea.setWrapStyleWord(true);
133
        scrollPane.setPreferredSize( new Dimension( 700, 50 ) );
134

    
135
        String s = null;
136
        while (s == null) {
137
            s= (String)JOptionPane.showInputDialog(
138
                    null,
139
                    scrollPane,
140
                    "Please select a reference in the list",
141
                    JOptionPane.PLAIN_MESSAGE,
142
                    null,
143
                    possibilities.toArray(),
144
                    "ABCD reference");
145
        }
146
        return refMap.get(s);
147
    }
148

    
149
    /**
150
     * @param refMap
151
     * @param iReferenceService
152
     * @param docSources
153
     * @return
154
     */
155
    public List<OriginalSourceBase<?>> askForSource(Map<String, OriginalSourceBase<?>> refMap, String currentElement, String blabla,
156
            IReferenceService iReferenceService, List<String> docSources) {
157

    
158
//        System.out.println(refMap);
159
        List<String>  possibilities = new ArrayList<> (refMap.keySet());
160

    
161
        Set<String> all = new HashSet<>();
162
        all.addAll(possibilities);
163

    
164
        List<String> allList = new ArrayList<String>();
165
        allList.addAll(all);
166
        Collections.sort(allList);
167
        allList.add(0, "Create a new source");
168

    
169
        JLabel label = new JLabel(blabla);
170

    
171
        sources=new ArrayList<String>();
172

    
173
        JPanel checkPanel = null;
174
        ButtonGroup group = null;
175
        JScrollPane scrollPane = null;
176

    
177
        JRadioButton jcb = null;
178

    
179
        Object[] options = {"Add and close", "Add and continue - I want to add more sources","Close without adding anything"};
180

    
181
//        System.out.println(docSources);
182
        int n=1;
183
        while (n==1){
184
            group = new ButtonGroup();
185
            checkPanel = new JPanel();
186
            checkPanel.setLayout(new BoxLayout(checkPanel, BoxLayout.Y_AXIS));
187
//            allList.removeAll(sources);
188
            scrollPane = new JScrollPane(checkPanel);
189
            scrollPane.setPreferredSize( new Dimension( 700, 300 ) );
190

    
191
            checkPanel.add(label);
192

    
193
            for (String ch:allList){
194
                if (StringUtils.isBlank(ch)) {
195
                    continue;
196
                }
197
//                System.out.println("HOP ="+ch+"=");
198
                if(docSources.contains(ch)) {
199
                    jcb = new JRadioButton("<html>"+ch.replace("---", "<br/>")+"</html>");
200
                    jcb.setForeground(Color.blue);
201
                } else {
202
                    jcb = new JRadioButton("<html>"+ch.replace("---", "<br/>")+"</html>");
203
                    jcb.setForeground(Color.black);
204
                }
205
                jcb.addItemListener(this);
206
                group.add(jcb);
207
                checkPanel.add(jcb);
208
            }
209

    
210
            n = JOptionPane.showOptionDialog(null,
211
                    scrollPane,
212
                    "Choose a source for "+currentElement +"(in blue the source from the document)",
213
                    JOptionPane.YES_NO_CANCEL_OPTION,
214
                    JOptionPane.QUESTION_MESSAGE,
215
                    null,
216
                    options,
217
                    options[1]);
218
            if(n<3 && !currentSource.isEmpty() && !currentSource.equalsIgnoreCase("Create a new source")) {
219
                sources.add(currentSource);
220
            }
221
//            System.out.println("current source: "+currentSource);
222
            if(currentSource.equalsIgnoreCase("Create a new source")){
223
                String a = createNewSource();
224
                if (a!=null && !a.isEmpty()) {
225
                    sources.add(a);
226
                }
227
            }
228
        }
229

    
230
        List<OriginalSourceBase<?>> dess = new ArrayList<>();
231
        for (String src:sources){
232
            if (refMap.get(src) !=null) {
233
                dess.add(refMap.get(src));
234
            }
235
            else{
236
                Reference re = null;
237
                String titlecache="";
238
                String micro="";
239
                if (src.indexOf("---")>-1){
240
                    titlecache = src.split("---")[0].trim();
241
                    micro=src.split("---")[1].trim();
242
                }
243
                else{
244
                    titlecache= src.split("---")[0].trim();
245
                }
246

    
247
                List<Reference> references = iReferenceService.list(Reference.class, null, null, null, null);
248
                for (Reference reference:references){
249
                    if (reference.getTitleCache().equalsIgnoreCase(titlecache)) {
250
                        re=reference;
251
                    }
252
                }
253
                if(re == null){
254
                    re = ReferenceFactory.newGeneric();
255
                    re.setTitleCache(titlecache);
256
                    iReferenceService.saveOrUpdate(re);
257
                }
258

    
259
                dess.add(IdentifiableSource.NewInstance(OriginalSourceType.Import,null, null, re,micro));
260
            }
261
        }
262
        return dess;
263
    }
264

    
265
    public String createNewReference() {
266
        JTextArea textArea = new JTextArea("How should the reference be named ?");
267
        JScrollPane scrollPane = new JScrollPane(textArea);
268
        textArea.setLineWrap(true);
269
        textArea.setWrapStyleWord(true);
270
        scrollPane.setPreferredSize( new Dimension( 500, 50 ) );
271

    
272
        String s = null;
273
        while (s == null) {
274
            s= (String)JOptionPane.showInputDialog(
275
                    null,
276
                    scrollPane,
277
                    "Get full reference name",
278
                    JOptionPane.PLAIN_MESSAGE,
279
                    null,
280
                    null,
281
                    "ABCD Import from XML");
282
        }
283
        return s;
284
    }
285

    
286
    public String createNewSource() {
287
        JTextArea textArea = new JTextArea("How should the source be named? If there is a citation detail, prefix it with 3 minus signs ('---').");
288
        JScrollPane scrollPane = new JScrollPane(textArea);
289
        textArea.setLineWrap(true);
290
        textArea.setWrapStyleWord(true);
291
        scrollPane.setPreferredSize( new Dimension( 500, 50 ) );
292

    
293
        String s = null;
294
        while (s == null) {
295
            s= (String)JOptionPane.showInputDialog(
296
                    null,
297
                    scrollPane,
298
                    "Get full source name",
299
                    JOptionPane.PLAIN_MESSAGE,
300
                    null,
301
                    null,
302
                    "ABCD Import from XML");
303
        }
304
        return s;
305
    }
306

    
307
    public TaxonDescription askForDescriptionGroup(Set<TaxonDescription> descriptions) {
308
        JTextArea textArea = new JTextArea("One or several description group(s) does already exist for this taxon.");
309
        JScrollPane scrollPane = new JScrollPane(textArea);
310
        textArea.setLineWrap(true);
311
        textArea.setWrapStyleWord(true);
312
        scrollPane.setPreferredSize( new Dimension( 700, 50 ) );
313

    
314
        Map<String,TaxonDescription> descrMap = new HashMap<String, TaxonDescription>();
315

    
316
        int descCnt=1;
317
        for (TaxonDescription description : descriptions){
318
//            System.out.println("descr. titlecache "+description.getTitleCache());
319
            Set<IdentifiableSource> sources =  description.getTaxon().getSources();
320
            sources.addAll(description.getSources());
321
            List<String> src=new ArrayList<String>();
322
            for (IdentifiableSource s:sources) {
323
                src.add(s.getCitation().getTitleCache());
324
            }
325
            List<String> srcb = new ArrayList<String>(new HashSet<>(src));
326
            if (srcb.size()>0) {
327
                if(descrMap.containsKey(descCnt+": "+description.getTitleCache()+"("+StringUtils.join(srcb,";")+")")) {
328
                    descCnt+=1;
329
                }
330
                descrMap.put(descCnt+": "+description.getTitleCache()+"("+StringUtils.join(srcb,";")+")",description);
331
            }
332
            else {
333
                if(descrMap.containsKey(description.getTitleCache())) {
334
                    descCnt+=1;
335
                }
336
                descrMap.put(descCnt+": "+description.getTitleCache(),description);
337
                //            for (IdentifiableSource source:sources){
338
                //                if(ref.equals(source.getCitation())) {
339
                //                    taxonDescription = description;
340
                //                }
341
                //            }
342
            }
343
        }
344
        List<String>  possibilities = new ArrayList<>(descrMap.keySet());
345
        if (possibilities.size()==0) {
346
            return null;
347
        }
348
        Collections.sort(possibilities);
349

    
350
        descrMap.put("No, add a brand new description group", null);
351
        possibilities.add(0, "No, add a brand new description group");
352

    
353
        String s = null;
354
        while (s == null) {
355
            s= (String)JOptionPane.showInputDialog(
356
                    null,
357
                    scrollPane,
358
                    "What should be done? Should an existing group be reused ? ",
359
                    JOptionPane.PLAIN_MESSAGE,
360
                    null,
361
                    possibilities.toArray(),
362
                    "No, add a brand new description group");
363
        }
364

    
365
        if (descrMap.get(s) !=null) {
366
            return descrMap.get(s);
367
        } else {
368
            return null;
369
        }
370
    }
371

    
372
    /**
373
     * Look if the same name already exists in the ALL classifications and ask the user to select one or none.
374
     * @param scientificName
375
     * @param taxonList
376
     * @return null if not found, or the selected Taxon
377
     */
378
    @SuppressWarnings("rawtypes")
379
    public Taxon askWhereToFixData(String scientificName, List<TaxonBase> taxonList, Classification classification) {
380
        Map<String,TaxonNode> classMap = new HashMap<>();
381
        boolean sameClassification=false;
382
        Taxon n = null;
383
        for (TaxonBase taxonBase: taxonList){
384
            if(taxonBase.isInstanceOf(Taxon.class)){
385
                Taxon taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
386
                for (TaxonNode node : taxon.getTaxonNodes()){
387
                    classMap.put("Reuse the one from the classification \""+node.getClassification().getTitleCache()+"\"", node);
388
                    if (node.getClassification().getUuid().equals(classification.getUuid())) {
389
                        sameClassification=true;
390
                        n=node.getTaxon();
391
                    }
392
                }
393
            }
394
        }
395
        if (classMap.keySet().size()==1 && sameClassification) {
396
            return n;
397
        }
398

    
399
        JTextArea textArea = new JTextArea("The same taxon ("+scientificName+") already exists in an other classification.");
400
        JScrollPane scrollPane = new JScrollPane(textArea);
401
        textArea.setLineWrap(true);
402
        textArea.setWrapStyleWord(true);
403
        scrollPane.setPreferredSize( new Dimension( 700, 50 ) );
404

    
405
        List<String>  possibilities = new ArrayList<String> (classMap.keySet());
406
        if (possibilities.size()==0) {
407
            return null;
408
        }
409
        Collections.sort(possibilities);
410
        if(!sameClassification){
411
            classMap.put("Add a brand new Taxon to the current classification, no recycling please", null);
412
            possibilities.add(0, "Add a brand new Taxon to the current classification, no recycling please");
413
        }
414
        String s = null;
415
        while (s == null) {
416
            s= (String)JOptionPane.showInputDialog(
417
                    null,
418
                    scrollPane,
419
                    "What should be done? ",
420
                    JOptionPane.PLAIN_MESSAGE,
421
                    null,
422
                    possibilities.toArray(),
423
                    "Add a brand new Taxon to the current classification, no recycling please");
424
        }
425

    
426
        if (classMap.get(s) !=null) {
427
            return classMap.get(s).getTaxon();
428
        } else {
429
            return null;
430
        }
431
    }
432

    
433
    /**
434
     * Look if the same TaxonBase already exists in the SAME classification
435
     * @param taxonBaseList
436
     * @return null if not found, or the corresponding Taxon
437
     */
438
    @SuppressWarnings("rawtypes")
439
    public Taxon lookForTaxaIntoCurrentClassification(List<TaxonBase> taxonBaseList, Classification classification) {
440
        Taxon taxonFound =null;
441
        for (TaxonBase taxonBase:taxonBaseList){
442
            if(taxonBase.isInstanceOf(Taxon.class)){
443
                Taxon taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
444
                for (TaxonNode node : taxon.getTaxonNodes()){
445
                    UUID classUuid = node.getClassification().getUuid();
446
                    if (classification.getUuid().equals(classUuid)){
447
                        taxonFound=taxon;
448
                    }
449
                }
450
            }
451
        }
452
        return taxonFound;
453
    }
454

    
455
    List<String> sources = new ArrayList<>();
456
    String currentSource = "";
457

    
458
    @Override
459
    public void itemStateChanged(ItemEvent e) {
460
        JRadioButton cb = (JRadioButton) e.getItem();
461
        int state = e.getStateChange();
462
        if (state == ItemEvent.SELECTED) {
463
            currentSource=cb.getText().replace("</html>", "").replace("<html>","")
464
                    .replace("<br/>","---").replace("<font color=\"blue\">","").replace("</font>","");
465
        } else {
466
            currentSource="";
467
        }
468
    }
469
}
(5-5/7)