Project

General

Profile

Download (20.9 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.hibernate.HibernateProxyHelper;
31
import eu.etaxonomy.cdm.model.common.CdmBase;
32
import eu.etaxonomy.cdm.model.common.DefinedTerm;
33
import eu.etaxonomy.cdm.model.common.Identifier;
34
import eu.etaxonomy.cdm.model.molecular.DnaSample;
35
import eu.etaxonomy.cdm.model.molecular.Sequence;
36
import eu.etaxonomy.cdm.model.molecular.SingleRead;
37
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
38
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
39
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
40
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
41
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
42
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
43
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
44
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
45
import eu.etaxonomy.taxeditor.model.ImageResources;
46
import eu.etaxonomy.taxeditor.store.CdmStore;
47

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

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

    
61
    private static Set<SingleRead> multiLinkSingleReads;
62

    
63
    private static Map<DerivedUnit, List<SpecimenTypeDesignation>> typeDesignations;
64

    
65
    private ConversationHolder conversation;
66

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

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

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

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

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

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

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

    
130

    
131
    /** {@inheritDoc} */
132
    @Override
133
    public String getText(Object element) {
134
        return getDerivateText(element);
135
    }
136

    
137
    /** {@inheritDoc} */
138
    @Override
139
    public String getToolTipText(Object element) {
140
        return getDerivateText(element);
141
    }
142

    
143
    /**
144
     * @param conversation the conversation to set
145
     */
146
    public void setConversation(ConversationHolder conversation) {
147
        this.conversation = conversation;
148
    }
149

    
150
    public String getDerivateText(Object element){
151
        return getDerivateText(element, conversation);
152
    }
153

    
154
    public static String getDerivateText(Object element, ConversationHolder conversation){
155
        //TODO: use list of strings to assemble labels to avoid adding the separator every time and to allow null values
156
        TreeNode parentNode = null;
157
        TreeNode node = null;
158
        Object derivate = element;
159
        if(element instanceof TreeNode){
160
            node = (TreeNode) element;
161
            parentNode = node.getParent();
162
            //unwrap specimen from TreeNode
163
            derivate = node.getValue();
164
        }
165

    
166
        if(conversation!=null){
167
            conversation.bind();
168
        }
169

    
170
        final String emptyString = "";
171
        final String separator = " ";
172

    
173
        String label = emptyString;
174

    
175
        //Field Unit
176
        if(derivate instanceof FieldUnit){
177
            FieldUnit fieldUnit = (FieldUnit)derivate;
178
            if(fieldUnit.getGatheringEvent()!=null){
179
                GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
180
                label += gatheringEvent.getCountry()!=null?gatheringEvent.getCountry().getLabel()+separator:emptyString;
181
                label += gatheringEvent.getLocality()!=null?gatheringEvent.getLocality().getText()+separator:emptyString;
182
                label += gatheringEvent.getGatheringDate()!=null?gatheringEvent.getGatheringDate()+separator:emptyString;
183
                label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
184
            }
185
            label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber():emptyString;
186
        }
187
        //MediaSpecimen
188
        else if(derivate instanceof MediaSpecimen){
189
            MediaSpecimen mediaSpecimen = (MediaSpecimen)derivate;
190
            if(mediaSpecimen.getMediaSpecimen()!=null){
191
                label += mediaSpecimen.getMediaSpecimen().getTitle()!=null?mediaSpecimen.getMediaSpecimen().getTitle().getText()+separator:"[no motif]";
192
                label += mediaSpecimen.getMediaSpecimen().getArtist()!=null?mediaSpecimen.getMediaSpecimen().getArtist()+separator:emptyString;
193
            }
194
            eu.etaxonomy.cdm.model.occurrence.Collection collection = mediaSpecimen.getCollection();
195
            if(collection!=null){
196
                label += collection.getName()!=null?collection.getName()+" ":emptyString;
197
                label += collection.getCode()!=null?"("+collection.getCode()+")"+separator:emptyString;
198
            }
199
            label += mediaSpecimen.getAccessionNumber()!=null?mediaSpecimen.getAccessionNumber()+separator:emptyString;
200
            label += mediaSpecimen.getBarcode()!=null?mediaSpecimen.getBarcode()+separator:emptyString;
201
        }
202
        //TissueSample + DnaSample
203
        else if(derivate instanceof DnaSample){
204
            DnaSample dnaSample = (DnaSample)derivate;
205
            if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.DnaSample){
206
                Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
207
                if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
208
                    label += currentSampleDesignation.getIdentifier()+separator;
209
                }
210
                else{
211
                    label += NO_SAMPLE_DESIGNATION+separator;
212
                }
213
            }
214
            else if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.TissueSample){
215
                if(dnaSample.getKindOfUnit()!=null){
216
                    label += dnaSample.getKindOfUnit()+separator;
217
                }
218
                Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
219
                if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
220
                    label += currentSampleDesignation.getIdentifier()+separator;
221
                }
222
                else{
223
                    label += NO_SAMPLE_DESIGNATION+separator;
224
                }
225
            }
226

    
227
        }
228
        //DerivedUnit + TissueSample
229
        else if(derivate instanceof DerivedUnit){
230
            DerivedUnit derivedUnit = (DerivedUnit)derivate;
231
            if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
232
                //check for type designation
233
                if(typeDesignations.get(derivedUnit)==null){
234
                    for (SpecimenTypeDesignation specimenTypeDesignation : CdmStore.getService(IOccurrenceService.class).listTypeDesignations(derivedUnit, null, null, null, null)) {
235
                        addTypeDesignation(derivedUnit, specimenTypeDesignation);
236
                    }
237
                }
238
                //java.util.Collection<FieldUnit> fieldUnits = CdmStore.getService(IOccurrenceService.class).getFieldUnits(derivedUnit.getUuid());
239
                //TODO : This is not generic anymore for performance reasons
240
                Set<SpecimenOrObservationBase> originals = derivedUnit.getOriginals();
241
                if(originals!=null && originals.size() ==1) {
242
                    SpecimenOrObservationBase specimen = originals.iterator().next();
243
                    if(specimen instanceof FieldUnit) {
244
                        FieldUnit fieldUnit = (FieldUnit)specimen;
245
                        GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
246
                        if(gatheringEvent!=null){
247
                            label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
248
                        }
249
                        label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber()+separator:emptyString;
250
                    }
251
                }
252

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

    
329
    @Override
330
    public Image getImage(Object element) {
331
        if(element instanceof TreeNode){
332
            element = ((TreeNode) element).getValue();
333
        }
334
        if(element instanceof CdmBase){
335
            CdmBase cdmBase = (CdmBase)element;
336
            boolean hasCharacterData = false;
337
            if(cdmBase.isInstanceOf(SpecimenOrObservationBase.class)){
338
                SpecimenOrObservationBase<?> specimen = HibernateProxyHelper.deproxy(cdmBase, SpecimenOrObservationBase.class);
339
                if(specimen.hasCharacterData()){
340
                    hasCharacterData = true;
341
                }
342
            }
343
            if(cdmBase.isInstanceOf(FieldUnit.class)){
344
                return hasCharacterData?ImageResources.getImage(ImageResources.FIELD_UNIT_CHARACTER_DATA):ImageResources.getImage(ImageResources.FIELD_UNIT);
345
            }
346
            else if(cdmBase.isInstanceOf(DerivedUnit.class)){
347
                DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(element, DerivedUnit.class);
348
                if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.FieldUnit){
349
                    return hasCharacterData?ImageResources.getImage(ImageResources.FIELD_UNIT_CHARACTER_DATA):ImageResources.getImage(ImageResources.FIELD_UNIT);
350
                }
351
                else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.DnaSample){
352
                    return hasCharacterData?ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE);
353
                }
354
                else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
355
                    return hasCharacterData?ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE);
356
                }
357
                else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
358
                    return hasCharacterData?ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE);
359
                }
360
                else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.Media){
361
                    if(derivedUnit.getKindOfUnit()!=null){
362
                        if(derivedUnit.getKindOfUnit().equals(getArtworkTerm())){
363
                            return hasCharacterData?ImageResources.getImage(ImageResources.ARTWORK_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.ARTWORK_DERIVATE);
364
                        }
365
                        else if(derivedUnit.getKindOfUnit().equals(getLivingPlantPhotoTerm())){
366
                            return hasCharacterData?ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE);
367
                        }
368
                        else if(derivedUnit.getKindOfUnit().equals(getSpecimenScanTerm())){
369
                            return hasCharacterData?ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE);
370
                        }
371
                        else if(derivedUnit.getKindOfUnit().equals(getDetailImageTerm())){
372
                            return hasCharacterData?ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE);
373
                        }
374
                    }
375
                }
376
            }
377
            else if(cdmBase.isInstanceOf(Sequence.class)){
378
                return ImageResources.getImage(ImageResources.SEQUENCE_DERIVATE);
379
            }
380

    
381
            else if(cdmBase.isInstanceOf(SingleRead.class)){
382
                if(multiLinkSingleReads!=null && multiLinkSingleReads.contains(element)){
383
                    return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE_MULTILINK);
384
                }
385
                else{
386
                    return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE);
387
                }
388
            }
389
        }
390
        return super.getImage(element);
391
    }
392

    
393
    public static Identifier<DnaSample> getCurrentSampleDesignation(CdmBase entity) {
394
        if(entity.isInstanceOf(DnaSample.class)){
395
            DnaSample dnaSample = HibernateProxyHelper.deproxy(entity, DnaSample.class);
396
            for (Identifier<DnaSample> identifier : dnaSample.getIdentifiers()) {
397
                if(identifier.getType()!=null && identifier.getType().equals(DerivateLabelProvider.getSampleDesignationTerm())){
398
                    //first sample designation is the current
399
                    return identifier;
400
                }
401
            }
402
        }
403
        else if(entity.isInstanceOf(Sequence.class)){
404
            Sequence sequence = HibernateProxyHelper.deproxy(entity, Sequence.class);
405
            if(sequence.getDnaSample()!=null){
406
                return getCurrentSampleDesignation(sequence.getDnaSample());
407
            }
408
        }
409
        return null;
410
    }
411

    
412
    /**
413
     * Refreshes cached label extensions
414
     * @param multiLinkSingleReads
415
     */
416
    public void refresh() {
417
        DerivateLabelProvider.multiLinkSingleReads = new HashSet<SingleRead>();
418
        for(Entry<SingleRead, Collection<Sequence>> entry:CdmStore.getService(ISequenceService.class).getSingleReadSequencesMap().entrySet()){
419
            if(entry.getValue().size()>1){
420
                multiLinkSingleReads.add(entry.getKey());
421
            }
422
        }
423
        DerivateLabelProvider.typeDesignations = new HashMap<DerivedUnit, List<SpecimenTypeDesignation>>();
424
    }
425

    
426
    private static void addTypeDesignation(DerivedUnit derivedUnit, SpecimenTypeDesignation typeDesignation){
427
        List<SpecimenTypeDesignation> list = typeDesignations.get(derivedUnit);
428
        if(list==null){
429
            list = new ArrayList<SpecimenTypeDesignation>();
430
        }
431
        list.add(typeDesignation);
432
        typeDesignations.put(derivedUnit, list);
433
    }
434

    
435
    public static Set<SingleRead> getMultiLinkSingleReads() {
436
        return multiLinkSingleReads;
437
    }
438

    
439
}
(2-2/2)