Project

General

Profile

Download (17.6 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2013 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.cdm.io.specimen;
11

    
12
import java.awt.Color;
13
import java.awt.Dimension;
14
import java.awt.event.ItemEvent;
15
import java.awt.event.ItemListener;
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.common.OriginalSourceBase;
41
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
42
import eu.etaxonomy.cdm.model.description.TaxonDescription;
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
 * @date 21 juin 2013
53
 *
54
 */
55
public class SpecimenUserInteraction implements ItemListener {
56

    
57
    Logger log = Logger.getLogger(getClass());
58

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

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

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

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

    
90

    
91

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

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

    
116

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

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

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

    
152

    
153

    
154

    
155

    
156
    /**
157
     * @param refMap
158
     * @param iReferenceService
159
     * @param docSources
160
     * @return
161
     */
162
    @SuppressWarnings("rawtypes")
163
    public List<OriginalSourceBase<?>> askForSource(Map<String, OriginalSourceBase<?>> refMap, String currentElement, String blabla,
164
            IReferenceService iReferenceService, List<String> docSources) {
165

    
166
//        System.out.println(refMap);
167
        List<String>  possibilities = new ArrayList<String> (refMap.keySet());
168

    
169
        Set<String> all = new HashSet<String>();
170
        all.addAll(possibilities);
171

    
172
        List<String> allList = new ArrayList<String>();
173
        allList.addAll(all);
174
        Collections.sort(allList);
175
        allList.add(0, "Create a new source");
176

    
177
        JLabel label = new JLabel(blabla);
178

    
179
        sources=new ArrayList<String>();
180

    
181
        JPanel checkPanel = null;
182
        ButtonGroup group = null;
183
        JScrollPane scrollPane = null;
184

    
185
        JRadioButton jcb = null;
186

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

    
189
//        System.out.println(docSources);
190
        int n=1;
191
        while (n==1){
192
            group = new ButtonGroup();
193
            checkPanel = new JPanel();
194
            checkPanel.setLayout(new BoxLayout(checkPanel, BoxLayout.Y_AXIS));
195
//            allList.removeAll(sources);
196
            scrollPane = new JScrollPane(checkPanel);
197
            scrollPane.setPreferredSize( new Dimension( 700, 300 ) );
198

    
199
            checkPanel.add(label);
200

    
201
            for (String ch:allList){
202
                if (StringUtils.isBlank(ch)) {
203
                    continue;
204
                }
205
//                System.out.println("HOP ="+ch+"=");
206
                if(docSources.contains(ch)) {
207
                    jcb = new JRadioButton("<html>"+ch.replace("---", "<br/>")+"</html>");
208
                    jcb.setForeground(Color.blue);
209
                } else {
210
                    jcb = new JRadioButton("<html>"+ch.replace("---", "<br/>")+"</html>");
211
                    jcb.setForeground(Color.black);
212
                }
213
                jcb.addItemListener(this);
214
                group.add(jcb);
215
                checkPanel.add(jcb);
216
            }
217

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

    
237
        }
238

    
239

    
240

    
241
        List<OriginalSourceBase<?>> dess = new ArrayList<OriginalSourceBase<?>>();
242
        for (String src:sources){
243
            if (refMap.get(src) !=null) {
244
                dess.add(refMap.get(src));
245
            }
246
            else{
247
                Reference<?> re = null;
248
                String titlecache="";
249
                String micro="";
250
                if (src.indexOf("---")>-1){
251
                    titlecache = src.split("---")[0].trim();
252
                    micro=src.split("---")[1].trim();
253
                }
254
                else{
255
                    titlecache= src.split("---")[0].trim();
256
                }
257

    
258
                List<Reference> references = iReferenceService.list(Reference.class, null, null, null, null);
259
                for (Reference<?> reference:references){
260
                    if (reference.getTitleCache().equalsIgnoreCase(titlecache)) {
261
                        re=reference;
262
                    }
263
                }
264
                if(re == null){
265
                    re = ReferenceFactory.newGeneric();
266
                    re.setTitleCache(titlecache);
267
                    iReferenceService.saveOrUpdate(re);
268
                }
269

    
270
                dess.add(IdentifiableSource.NewInstance(OriginalSourceType.Import,null, null, re,micro));
271
            }
272
        }
273
        return dess;
274
    }
275

    
276

    
277
    /**
278
     * @return
279
     */
280
    public String createNewReference() {
281
        JTextArea textArea = new JTextArea("How should the reference be named ?");
282
        JScrollPane scrollPane = new JScrollPane(textArea);
283
        textArea.setLineWrap(true);
284
        textArea.setWrapStyleWord(true);
285
        scrollPane.setPreferredSize( new Dimension( 500, 50 ) );
286

    
287
        String s = null;
288
        while (s == null) {
289
            s= (String)JOptionPane.showInputDialog(
290
                    null,
291
                    scrollPane,
292
                    "Get full reference name",
293
                    JOptionPane.PLAIN_MESSAGE,
294
                    null,
295
                    null,
296
                    "ABCD Import from XML");
297
        }
298
        return s;
299
    }
300

    
301
    /**
302
     * @return
303
     */
304
    public String createNewSource() {
305
        JTextArea textArea = new JTextArea("How should the source be named? If there is a citation detail, prefix it with 3 minus signs ('---').");
306
        JScrollPane scrollPane = new JScrollPane(textArea);
307
        textArea.setLineWrap(true);
308
        textArea.setWrapStyleWord(true);
309
        scrollPane.setPreferredSize( new Dimension( 500, 50 ) );
310

    
311
        String s = null;
312
        while (s == null) {
313
            s= (String)JOptionPane.showInputDialog(
314
                    null,
315
                    scrollPane,
316
                    "Get full source name",
317
                    JOptionPane.PLAIN_MESSAGE,
318
                    null,
319
                    null,
320
                    "ABCD Import from XML");
321
        }
322
        return s;
323
    }
324

    
325

    
326
    /**
327
     * @param descriptions
328
     */
329
    public TaxonDescription askForDescriptionGroup(Set<TaxonDescription> descriptions) {
330
        JTextArea textArea = new JTextArea("One or several description group(s) does already exist for this taxon.");
331
        JScrollPane scrollPane = new JScrollPane(textArea);
332
        textArea.setLineWrap(true);
333
        textArea.setWrapStyleWord(true);
334
        scrollPane.setPreferredSize( new Dimension( 700, 50 ) );
335

    
336
        Map<String,TaxonDescription> descrMap = new HashMap<String, TaxonDescription>();
337

    
338
        int descCnt=1;
339
        for (TaxonDescription description : descriptions){
340
//            System.out.println("descr. titlecache "+description.getTitleCache());
341
            Set<IdentifiableSource> sources =  description.getTaxon().getSources();
342
            sources.addAll(description.getSources());
343
            List<String> src=new ArrayList<String>();
344
            for (IdentifiableSource s:sources) {
345
                src.add(s.getCitation().getTitleCache());
346
            }
347
            List<String> srcb = new ArrayList<String>(new HashSet<String>(src));
348
            if (srcb.size()>0) {
349
                if(descrMap.containsKey(descCnt+": "+description.getTitleCache()+"("+StringUtils.join(srcb,";")+")")) {
350
                    descCnt+=1;
351
                }
352
                descrMap.put(descCnt+": "+description.getTitleCache()+"("+StringUtils.join(srcb,";")+")",description);
353
            }
354
            else {
355
                if(descrMap.containsKey(description.getTitleCache())) {
356
                    descCnt+=1;
357
                }
358
                descrMap.put(descCnt+": "+description.getTitleCache(),description);
359
                //            for (IdentifiableSource source:sources){
360
                //                if(ref.equals(source.getCitation())) {
361
                //                    taxonDescription = description;
362
                //                }
363
                //            }
364
            }
365
        }
366
        List<String>  possibilities = new ArrayList<String> (descrMap.keySet());
367
        if (possibilities.size()==0) {
368
            return null;
369
        }
370
        Collections.sort(possibilities);
371

    
372
        descrMap.put("No, add a brand new description group", null);
373
        possibilities.add(0, "No, add a brand new description group");
374

    
375
        String s = null;
376
        while (s == null) {
377
            s= (String)JOptionPane.showInputDialog(
378
                    null,
379
                    scrollPane,
380
                    "What should be done? Should an existing group be reused ? ",
381
                    JOptionPane.PLAIN_MESSAGE,
382
                    null,
383
                    possibilities.toArray(),
384
                    "No, add a brand new description group");
385
        }
386

    
387
        if (descrMap.get(s) !=null) {
388
            return descrMap.get(s);
389
        } else {
390
            return null;
391
        }
392

    
393
    }
394

    
395

    
396
    /**
397
     * Look if the same name already exists in the ALL classifications and ask the user to select one or none.
398
     * @param scientificName
399
     * @param taxonList
400
     * @return null if not found, or the selected Taxon
401
     */
402
    @SuppressWarnings("rawtypes")
403
    public Taxon askWhereToFixData(String scientificName, List<TaxonBase> taxonList, Classification classification) {
404
        Map<String,TaxonNode> classMap = new HashMap<String, TaxonNode>();
405
        boolean sameClassification=false;
406
        Taxon n = null;
407
        for (TaxonBase taxonBase: taxonList){
408
            if(taxonBase.isInstanceOf(Taxon.class)){
409
                Taxon taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
410
                for (TaxonNode node : taxon.getTaxonNodes()){
411
                    classMap.put("Reuse the one from the classification \""+node.getClassification().getTitleCache()+"\"", node);
412
                    if (node.getClassification().getUuid().equals(classification.getUuid())) {
413
                        sameClassification=true;
414
                        n=node.getTaxon();
415
                    }
416
                }
417
            }
418
        }
419
        if (classMap.keySet().size()==1 && sameClassification) {
420
            return n;
421
        }
422

    
423
        JTextArea textArea = new JTextArea("The same taxon ("+scientificName+") already exists in an other classification.");
424
        JScrollPane scrollPane = new JScrollPane(textArea);
425
        textArea.setLineWrap(true);
426
        textArea.setWrapStyleWord(true);
427
        scrollPane.setPreferredSize( new Dimension( 700, 50 ) );
428

    
429
        List<String>  possibilities = new ArrayList<String> (classMap.keySet());
430
        if (possibilities.size()==0) {
431
            return null;
432
        }
433
        Collections.sort(possibilities);
434
        if(!sameClassification){
435
            classMap.put("Add a brand new Taxon to the current classification, no recycling please", null);
436
            possibilities.add(0, "Add a brand new Taxon to the current classification, no recycling please");
437
        }
438
        String s = null;
439
        while (s == null) {
440
            s= (String)JOptionPane.showInputDialog(
441
                    null,
442
                    scrollPane,
443
                    "What should be done? ",
444
                    JOptionPane.PLAIN_MESSAGE,
445
                    null,
446
                    possibilities.toArray(),
447
                    "Add a brand new Taxon to the current classification, no recycling please");
448
        }
449

    
450
        if (classMap.get(s) !=null) {
451
            return classMap.get(s).getTaxon();
452
        } else {
453
            return null;
454
        }
455
    }
456

    
457
    /**
458
     * Look if the same TaxonBase already exists in the SAME classification
459
     * @param taxonBaseList
460
     * @return null if not found, or the corresponding Taxon
461
     */
462
    @SuppressWarnings("rawtypes")
463
    public Taxon lookForTaxaIntoCurrentClassification(List<TaxonBase> taxonBaseList, Classification classification) {
464
        Taxon taxonFound =null;
465
        for (TaxonBase taxonBase:taxonBaseList){
466
            if(taxonBase.isInstanceOf(Taxon.class)){
467
                Taxon taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
468
                for (TaxonNode node : taxon.getTaxonNodes()){
469
                    UUID classUuid = node.getClassification().getUuid();
470
                    if (classification.getUuid().equals(classUuid)){
471
                        taxonFound=taxon;
472
                    }
473
                }
474
            }
475
        }
476
        return taxonFound;
477
    }
478

    
479

    
480
    List<String> sources = new ArrayList<String>();
481
    String currentSource = "";
482
    /* (non-Javadoc)
483
     * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
484
     */
485
    @Override
486
    public void itemStateChanged(ItemEvent e) {
487
        JRadioButton cb = (JRadioButton) e.getItem();
488
        int state = e.getStateChange();
489
        if (state == ItemEvent.SELECTED) {
490
            currentSource=cb.getText().replace("</html>", "").replace("<html>","")
491
                    .replace("<br/>","---").replace("<font color=\"blue\">","").replace("</font>","");
492
        } else {
493
            currentSource="";
494
        }
495
    }
496

    
497

    
498

    
499
}
(2-2/4)