Project

General

Profile

Download (22.1 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.taxeditor.view.derivateSearch;
11

    
12
import java.util.ArrayList;
13
import java.util.Collection;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Map.Entry;
19
import java.util.Set;
20
import java.util.UUID;
21

    
22
import org.eclipse.jface.viewers.ColumnLabelProvider;
23
import org.eclipse.jface.viewers.TreeNode;
24
import org.eclipse.swt.graphics.Image;
25
import org.hibernate.LazyInitializationException;
26

    
27
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
28
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
29
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
30
import eu.etaxonomy.cdm.format.CdmFormatterFactory;
31
import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey;
32
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
33
import eu.etaxonomy.cdm.model.common.CdmBase;
34
import eu.etaxonomy.cdm.model.common.DefinedTerm;
35
import eu.etaxonomy.cdm.model.common.Identifier;
36
import eu.etaxonomy.cdm.model.molecular.DnaSample;
37
import eu.etaxonomy.cdm.model.molecular.Sequence;
38
import eu.etaxonomy.cdm.model.molecular.SingleRead;
39
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
40
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
41
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
42
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
43
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
44
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
45
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
46
import eu.etaxonomy.taxeditor.model.ImageResources;
47
import eu.etaxonomy.taxeditor.store.CdmStore;
48

    
49
/**
50
 * Label provider for the views to show {@link SpecimenOrObservationBase}s.<br>
51
 * <br>
52
 * <b>Note:</b> If you use this label provider you need to assure that you
53
 * created a {@link ConversationHolder} resp. have an open session because
54
 * the labels are generated from various fields of the derivate hierarchy which
55
 * are lazy loaded and could therefore throw a {@link LazyInitializationException}.<br>
56
 * Use <b>{@link #setConversation(ConversationHolder)}</b> to assign the session to this provider.
57
 */
58
public class DerivateLabelProvider extends ColumnLabelProvider {
59

    
60
    private static final String NO_SAMPLE_DESIGNATION = "[no sample designation]";
61

    
62
    private static Set<SingleRead> multiLinkSingleReads;
63

    
64
    private static Map<DerivedUnit, Collection<SpecimenTypeDesignation>> typeDesignations;
65

    
66
    private ConversationHolder conversation;
67

    
68
    private static DefinedTerm photoTerm = null;
69
    private static DefinedTerm drawingTerm = null;
70
    private static DefinedTerm specimenScanTerm = null;
71
    private static DefinedTerm detailImageTerm = null;
72
    private static DefinedTerm sampleDesignationTerm = null;
73

    
74
    //FIXME: move static term getters to new singleton utility class
75
    private static void initializeTerms() {
76
        List<DefinedTerm> preferredTerms = CdmStore.getTermManager().getPreferredTerms(DefinedTerm.class);
77
        for (DefinedTerm definedTerm : preferredTerms) {
78
            if(definedTerm.getUuid().equals(UUID.fromString("c5c59c42-f254-471e-96c6-09f459f7c903"))){
79
                photoTerm = definedTerm;
80
            }
81
            else if(definedTerm.getUuid().equals(UUID.fromString("669b0409-4aa4-4695-aae4-a95ed27bad4c"))){
82
                drawingTerm = definedTerm;
83
            }
84
            else if(definedTerm.getUuid().equals(UUID.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03"))){
85
                specimenScanTerm = definedTerm;
86
            }
87
            else if(definedTerm.getUuid().equals(UUID.fromString("31eb8d02-bf5d-437c-bcc6-87a626445f34"))){
88
                detailImageTerm = definedTerm;
89
            }
90
            else if(definedTerm.getUuid().equals(UUID.fromString("fadeba12-1be3-4bc7-9ff5-361b088d86fc"))){
91
                sampleDesignationTerm = definedTerm;
92
            }
93
        }
94
    }
95

    
96
    public static DefinedTerm getLivingPlantPhotoTerm(){
97
        if(photoTerm==null){
98
            initializeTerms();
99
        }
100
        return photoTerm;
101
    }
102

    
103
    public static DefinedTerm getArtworkTerm(){
104
        if(drawingTerm==null){
105
            initializeTerms();
106
        }
107
        return drawingTerm;
108
    }
109

    
110
    public static DefinedTerm getSpecimenScanTerm(){
111
        if(specimenScanTerm==null){
112
            initializeTerms();
113
        }
114
        return specimenScanTerm;
115
    }
116

    
117
    public static DefinedTerm getDetailImageTerm(){
118
        if(detailImageTerm==null){
119
            initializeTerms();
120
        }
121
        return detailImageTerm;
122
    }
123

    
124
    public static DefinedTerm getSampleDesignationTerm(){
125
        if(sampleDesignationTerm==null){
126
            initializeTerms();
127
        }
128
        return sampleDesignationTerm;
129
    }
130

    
131

    
132
    /** {@inheritDoc} */
133
    @Override
134
    public String getText(Object element) {
135
        if(element instanceof TreeNode){
136
            element = ((TreeNode) element).getValue();
137
        }
138
        return CdmFormatterFactory.format(element,
139
                FormatKey.GATHERING_COUNTRY, FormatKey.SPACE,
140
                FormatKey.GATHERING_LOCALITY_TEXT, FormatKey.SPACE,
141
                FormatKey.GATHERING_DATE, FormatKey.SPACE,
142
                FormatKey.GATHERING_COLLECTOR, FormatKey.SPACE,
143
                FormatKey.FIELD_NUMBER, FormatKey.SPACE,
144
                FormatKey.COLLECTION_CODE, FormatKey.SPACE,
145
                FormatKey.MOST_SIGNIFICANT_IDENTIFIER, FormatKey.SPACE,
146
                FormatKey.KIND_OF_UNIT, FormatKey.SPACE,
147
                FormatKey.SAMPLE_DESIGNATION, FormatKey.SPACE,
148
                FormatKey.SINGLE_READ_PRIMER, FormatKey.SPACE,
149
                FormatKey.SEQUENCE_DNA_MARKER, FormatKey.SPACE,
150
                FormatKey.AMPLIFICATION_LABEL, FormatKey.SPACE,
151
                FormatKey.MEDIA_TITLE, FormatKey.SPACE,
152
                FormatKey.MEDIA_ARTIST, FormatKey.SPACE
153
                );
154
    }
155

    
156
    /** {@inheritDoc} */
157
    @Override
158
    public String getToolTipText(Object element) {
159
        return getDerivateText(element);
160
    }
161

    
162
    /**
163
     * @param conversation the conversation to set
164
     */
165
    public void setConversation(ConversationHolder conversation) {
166
        this.conversation = conversation;
167
    }
168

    
169
    public String getDerivateText(Object element){
170
        return getDerivateText(element, conversation);
171
    }
172

    
173
    public static String getDerivateText(Object element, ConversationHolder conversation){
174
        //TODO: use list of strings to assemble labels to avoid adding the separator every time and to allow null values
175
        TreeNode parentNode = null;
176
        TreeNode node = null;
177
        Object derivate = element;
178
        if(element instanceof TreeNode){
179
            node = (TreeNode) element;
180
            parentNode = node.getParent();
181
            //unwrap specimen from TreeNode
182
            derivate = node.getValue();
183
        }
184

    
185
        if(conversation!=null){
186
            conversation.bind();
187
        }
188

    
189
        final String emptyString = "";
190
        final String separator = " ";
191

    
192
        String label = emptyString;
193

    
194
        //Field Unit
195
        if(derivate instanceof FieldUnit){
196
            FieldUnit fieldUnit = (FieldUnit)derivate;
197
            if(fieldUnit.getGatheringEvent()!=null){
198
                GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
199
                label += gatheringEvent.getCountry()!=null?gatheringEvent.getCountry().getLabel()+separator:emptyString;
200
                label += gatheringEvent.getLocality()!=null?gatheringEvent.getLocality().getText()+separator:emptyString;
201
                label += gatheringEvent.getGatheringDate()!=null?gatheringEvent.getGatheringDate()+separator:emptyString;
202
                label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
203
            }
204
            label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber():emptyString;
205
        }
206
        //MediaSpecimen
207
        else if(derivate instanceof MediaSpecimen){
208
            MediaSpecimen mediaSpecimen = (MediaSpecimen)derivate;
209
            if(mediaSpecimen.getMediaSpecimen()!=null){
210
                label += mediaSpecimen.getMediaSpecimen().getTitle()!=null?mediaSpecimen.getMediaSpecimen().getTitle().getText()+separator:"[no motif]";
211
                label += mediaSpecimen.getMediaSpecimen().getArtist()!=null?mediaSpecimen.getMediaSpecimen().getArtist()+separator:emptyString;
212
            }
213
            eu.etaxonomy.cdm.model.occurrence.Collection collection = mediaSpecimen.getCollection();
214
            if(collection!=null){
215
                label += collection.getName()!=null?collection.getName()+" ":emptyString;
216
                label += collection.getCode()!=null?"("+collection.getCode()+")"+separator:emptyString;
217
            }
218
            label += mediaSpecimen.getAccessionNumber()!=null?mediaSpecimen.getAccessionNumber()+separator:emptyString;
219
            label += mediaSpecimen.getBarcode()!=null?mediaSpecimen.getBarcode()+separator:emptyString;
220
        }
221
        //TissueSample + DnaSample
222
        else if(derivate instanceof DnaSample){
223
            DnaSample dnaSample = (DnaSample)derivate;
224
            if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.DnaSample){
225
                Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
226
                if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
227
                    label += currentSampleDesignation.getIdentifier()+separator;
228
                }
229
                else{
230
                    label += NO_SAMPLE_DESIGNATION+separator;
231
                }
232
            }
233
            else if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.TissueSample){
234
                if(dnaSample.getKindOfUnit()!=null){
235
                    label += dnaSample.getKindOfUnit()+separator;
236
                }
237
                Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
238
                if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
239
                    label += currentSampleDesignation.getIdentifier()+separator;
240
                }
241
                else{
242
                    label += NO_SAMPLE_DESIGNATION+separator;
243
                }
244
            }
245

    
246
        }
247
        //DerivedUnit + TissueSample
248
        else if(derivate instanceof DerivedUnit){
249
            DerivedUnit derivedUnit = (DerivedUnit)derivate;
250
            if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
251
                //check for type designation
252
                if(typeDesignations.get(derivedUnit)==null){
253
                    for (SpecimenTypeDesignation specimenTypeDesignation : CdmStore.getService(IOccurrenceService.class).listTypeDesignations(derivedUnit, null, null, null, null)) {
254
                        addTypeDesignation(derivedUnit, specimenTypeDesignation);
255
                    }
256
                }
257
                //java.util.Collection<FieldUnit> fieldUnits = CdmStore.getService(IOccurrenceService.class).getFieldUnits(derivedUnit.getUuid());
258
                //TODO : This is not generic anymore for performance reasons
259
                Set<SpecimenOrObservationBase> originals = derivedUnit.getOriginals();
260
                if(originals!=null && originals.size() ==1) {
261
                    SpecimenOrObservationBase specimen = originals.iterator().next();
262
                    if(specimen instanceof FieldUnit) {
263
                        FieldUnit fieldUnit = (FieldUnit)specimen;
264
                        GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
265
                        if(gatheringEvent!=null){
266
                            label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
267
                        }
268
                        label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber()+separator:emptyString;
269
                    }
270
                }
271

    
272
                eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnit.getCollection();
273
                if(collection!=null){
274
                    label += collection.getCode()!=null?"("+collection.getCode()+")"+separator:emptyString;
275
                }
276
                String mostSignificantIdentifier = derivedUnit.getMostSignificantIdentifier();
277
                label += mostSignificantIdentifier!=null?mostSignificantIdentifier+separator:emptyString;
278
            }
279
            else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
280
                //TissueSample should only be created by using it's own class
281
                //in future using only one class with different SpecimenOrObservationTypes is desired
282
//                label += derivedUnit.getKindOfUnit() + NO_SAMPLE_DESIGNATION;
283
            }
284
        }
285
        //Sequence
286
        else if(derivate instanceof Sequence){
287
            Sequence sequence = (Sequence)derivate;
288
            Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
289
            if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
290
                label += currentSampleDesignation.getIdentifier()+separator;
291
            }
292
            else{
293
                label += NO_SAMPLE_DESIGNATION+separator;
294
            }
295
            label += sequence.getDnaMarker()!=null?sequence.getDnaMarker():emptyString;
296
        }
297
        //SingleRead
298
        else if(derivate instanceof SingleRead){
299
            SingleRead singleRead = (SingleRead)derivate;
300
            if(parentNode!=null && parentNode.getValue() instanceof Sequence){
301
                Sequence sequence = (Sequence) parentNode.getValue();
302
                Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
303
                if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
304
                    label = currentSampleDesignation.getIdentifier()+separator;
305
                }
306
                else{
307
                    label += NO_SAMPLE_DESIGNATION+separator;
308
                }
309
                label += singleRead.getPrimer()!=null?singleRead.getPrimer().getLabel()+separator:emptyString;
310
                if(sequence!=null && sequence.getDnaMarker()!=null){
311
                    label += sequence.getDnaMarker()+separator;
312
                }
313
                if(singleRead.getAmplificationResult()!=null && singleRead.getAmplificationResult().getAmplification()!=null){
314
                    label += singleRead.getAmplificationResult().getAmplification().getLabelCache()+separator;
315
                }
316
            }
317
        }
318
        //SOOB
319
        else if(derivate instanceof SpecimenOrObservationBase){
320
            SpecimenOrObservationBase<?> specimen = (SpecimenOrObservationBase<?>) derivate;
321
            SpecimenOrObservationType type = specimen.getRecordBasis();
322
            return specimen.getTitleCache() + (type!=null?" ["+type.toString()+"]":emptyString);
323
        }
324
        if(label.isEmpty()){
325
            label = derivate.toString();
326
        }
327
        //remove last comma
328
        else if(label.endsWith(separator)){
329
            label = label.substring(0, label.length()-separator.length());
330
        }
331
        return label;
332
    }
333

    
334
    @Override
335
    public Image getImage(Object element) {
336
        if(element instanceof TreeNode){
337
            element = ((TreeNode) element).getValue();
338
        }
339
        if(element instanceof CdmBase){
340
            CdmBase cdmBase = (CdmBase)element;
341
            boolean hasCharacterData = false;
342
            if(cdmBase.isInstanceOf(SpecimenOrObservationBase.class)){
343
                SpecimenOrObservationBase<?> specimen = HibernateProxyHelper.deproxy(cdmBase, SpecimenOrObservationBase.class);
344
                if(specimen.hasCharacterData()){
345
                    hasCharacterData = true;
346
                }
347
            }
348
            if(cdmBase.isInstanceOf(FieldUnit.class)){
349
                return hasCharacterData?ImageResources.getImage(ImageResources.FIELD_UNIT_CHARACTER_DATA):ImageResources.getImage(ImageResources.FIELD_UNIT);
350
            }
351
            else if(cdmBase.isInstanceOf(DerivedUnit.class)){
352
                DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(element, DerivedUnit.class);
353

    
354
                if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.FieldUnit){
355
                    return hasCharacterData?ImageResources.getImage(ImageResources.FIELD_UNIT_CHARACTER_DATA):ImageResources.getImage(ImageResources.FIELD_UNIT);
356
                }
357
                else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.DnaSample){
358
                    return hasCharacterData?ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE);
359
                }
360
                else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
361
                    return hasCharacterData?ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE);
362
                }
363
                else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
364
                    if(typeDesignations.get(derivedUnit)!=null && !typeDesignations.get(derivedUnit).isEmpty()){
365
                        return ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE_TYPE);
366
                    }
367
                    return hasCharacterData?ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE);
368
                }
369
                else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.Media){
370
                    if(derivedUnit.getKindOfUnit()!=null){
371
                        if(derivedUnit.getKindOfUnit().equals(getArtworkTerm())){
372
                            return hasCharacterData?ImageResources.getImage(ImageResources.ARTWORK_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.ARTWORK_DERIVATE);
373
                        }
374
                        else if(derivedUnit.getKindOfUnit().equals(getLivingPlantPhotoTerm())){
375
                            return hasCharacterData?ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE);
376
                        }
377
                        else if(derivedUnit.getKindOfUnit().equals(getSpecimenScanTerm())){
378
                            return hasCharacterData?ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE);
379
                        }
380
                        else if(derivedUnit.getKindOfUnit().equals(getDetailImageTerm())){
381
                            return hasCharacterData?ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE);
382
                        }
383
                    }
384
                }
385
            }
386
            else if(cdmBase.isInstanceOf(Sequence.class)){
387
                return ImageResources.getImage(ImageResources.SEQUENCE_DERIVATE);
388
            }
389

    
390
            else if(cdmBase.isInstanceOf(SingleRead.class)){
391
                if(multiLinkSingleReads!=null && multiLinkSingleReads.contains(element)){
392
                    return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE_MULTILINK);
393
                }
394
                else{
395
                    return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE);
396
                }
397
            }
398
        }
399
        return super.getImage(element);
400
    }
401

    
402
    public static Identifier<DnaSample> getCurrentSampleDesignation(CdmBase entity) {
403
        if(entity.isInstanceOf(DnaSample.class)){
404
            DnaSample dnaSample = HibernateProxyHelper.deproxy(entity, DnaSample.class);
405
            for (Identifier<DnaSample> identifier : dnaSample.getIdentifiers()) {
406
                if(identifier.getType()!=null && identifier.getType().equals(DerivateLabelProvider.getSampleDesignationTerm())){
407
                    //first sample designation is the current
408
                    return identifier;
409
                }
410
            }
411
        }
412
        else if(entity.isInstanceOf(Sequence.class)){
413
            Sequence sequence = HibernateProxyHelper.deproxy(entity, Sequence.class);
414
            if(sequence.getDnaSample()!=null){
415
                return getCurrentSampleDesignation(sequence.getDnaSample());
416
            }
417
        }
418
        return null;
419
    }
420

    
421
    private static void addTypeDesignation(DerivedUnit derivedUnit, SpecimenTypeDesignation typeDesignation){
422
        Collection<SpecimenTypeDesignation> list = typeDesignations.get(derivedUnit);
423
        if(list==null){
424
            list = new ArrayList<SpecimenTypeDesignation>();
425
        }
426
        list.add(typeDesignation);
427
        typeDesignations.put(derivedUnit, list);
428
    }
429

    
430
    public static Set<SingleRead> getMultiLinkSingleReads() {
431
        return multiLinkSingleReads;
432
    }
433

    
434
    public void updateLabelCache(Collection<SpecimenOrObservationBase<?>> rootElements) {
435
        DerivateLabelProvider.multiLinkSingleReads = new HashSet<SingleRead>();
436
        DerivateLabelProvider.typeDesignations = new HashMap<DerivedUnit, Collection<SpecimenTypeDesignation>>();
437
        for(Entry<SingleRead, Collection<Sequence>> entry:CdmStore.getService(ISequenceService.class).getSingleReadSequencesMap().entrySet()){
438
            if(entry.getValue().size()>1){
439
                multiLinkSingleReads.add(entry.getKey());
440
            }
441
        }
442
        if(rootElements!=null){
443
            Collection<DerivedUnit> derivedUnits = new ArrayList<DerivedUnit>();
444
            for (SpecimenOrObservationBase specimenOrObservationBase : rootElements) {
445
                derivedUnits.addAll(CdmStore.getService(IOccurrenceService.class).getAllChildDerivatives(specimenOrObservationBase.getUuid()));
446
                if(specimenOrObservationBase.isInstanceOf(DerivedUnit.class)){
447
                    derivedUnits.add(HibernateProxyHelper.deproxy(specimenOrObservationBase, DerivedUnit.class));
448
                }
449
            }
450
            typeDesignations = CdmStore.getService(IOccurrenceService.class).listTypeDesignations(derivedUnits, null, null, null, null);
451
        }
452
    }
453

    
454
}
(2-2/2)