Project

General

Profile

Download (24.9 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.taxeditor.view.derivateSearch;
10

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

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

    
26
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
27
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
28
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
29
import eu.etaxonomy.cdm.common.CdmUtils;
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.IdentifiableEntity;
36
import eu.etaxonomy.cdm.model.common.Identifier;
37
import eu.etaxonomy.cdm.model.molecular.DnaSample;
38
import eu.etaxonomy.cdm.model.molecular.Sequence;
39
import eu.etaxonomy.cdm.model.molecular.SingleRead;
40
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
41
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
42
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
43
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
44
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
45
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
46
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
47
import eu.etaxonomy.cdm.strategy.cache.common.IdentifiableEntityDefaultCacheStrategy;
48
import eu.etaxonomy.taxeditor.model.ImageResources;
49
import eu.etaxonomy.taxeditor.store.CdmStore;
50

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

    
62
    private static final String NO_SAMPLE_DESIGNATION = "[no sample designation]";
63

    
64
    private static Set<SingleRead> multiLinkSingleReads;
65

    
66
    private static Map<DerivedUnit, Collection<SpecimenTypeDesignation>> typeDesignations;
67

    
68
    private ConversationHolder conversation;
69

    
70
    private static DefinedTerm photoTerm = null;
71
    private static DefinedTerm drawingTerm = null;
72
    private static DefinedTerm specimenScanTerm = null;
73
    private static DefinedTerm detailImageTerm = null;
74
    private static DefinedTerm sampleDesignationTerm = null;
75

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

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

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

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

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

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

    
133

    
134
    /** {@inheritDoc} */
135
    @Override
136
    public String getText(Object element) {
137
        if(element instanceof TreeNode){
138
            element = ((TreeNode) element).getValue();
139
        }
140
        String text = null;
141

    
142
        //check if collection code does not exist -> use collection name then
143
        FormatKey collectionKey = FormatKey.COLLECTION_CODE;
144
        text = CdmFormatterFactory.format(element,new FormatKey[]{FormatKey.COLLECTION_CODE});
145
        if(CdmUtils.isBlank(text)){
146
            collectionKey = FormatKey.COLLECTION_NAME;
147
        }
148

    
149
        //Use titlecache for FieldUnits
150
        if(element instanceof FieldUnit){
151
            text = ((FieldUnit) element).getTitleCache();
152
        }
153
        else if(element instanceof MediaSpecimen){
154
            text = CdmFormatterFactory.format(element,
155
                    new FormatKey[]{
156
//                            FormatKey.MEDIA_TITLE_CACHE, FormatKey.SPACE,
157
                            collectionKey, FormatKey.SPACE,
158
                            FormatKey.MOST_SIGNIFICANT_IDENTIFIER, FormatKey.SPACE,
159
                            FormatKey.MEDIA_TITLE, FormatKey.SPACE,
160
                            FormatKey.MEDIA_ARTIST, FormatKey.SPACE,
161
                            });
162
        }
163
        else if (element instanceof DnaSample) {
164
            text = CdmFormatterFactory.format(element,
165
                    new FormatKey[] {
166
                            collectionKey, FormatKey.SPACE,
167
                            FormatKey.MOST_SIGNIFICANT_IDENTIFIER, FormatKey.SPACE,
168
                            FormatKey.SAMPLE_DESIGNATION, FormatKey.SPACE
169
                            });
170
        }
171
        else if (element instanceof DerivedUnit) {
172
            text = CdmFormatterFactory.format(element,
173
                    new FormatKey[] {
174
                            collectionKey, FormatKey.SPACE,
175
                            FormatKey.MOST_SIGNIFICANT_IDENTIFIER, FormatKey.SPACE
176
                            });
177
        }
178
        else if (element instanceof Sequence) {
179
            text = CdmFormatterFactory.format(element,
180
                    new FormatKey[] {
181
                            FormatKey.SEQUENCE_DNA_MARKER, FormatKey.SPACE
182
                            });
183
        }
184
        else if (element instanceof SingleRead) {
185
            text = CdmFormatterFactory.format(element,
186
                    new FormatKey[] {
187
                            FormatKey.SINGLE_READ_PHEROGRAM_TITLE_CACHE, FormatKey.SPACE,
188
                            FormatKey.AMPLIFICATION_LABEL, FormatKey.SPACE,
189
            });
190
        }
191
        else if(element instanceof IdentifiableEntity){
192

    
193
        	IdentifiableEntity identifiableEntity = (IdentifiableEntity) element;
194
        	if(identifiableEntity.isProtectedTitleCache()){
195
        		text = identifiableEntity.getTitleCache();
196
        	}
197
        }
198
        if(CdmUtils.isBlank(text) || text.equals(IdentifiableEntityDefaultCacheStrategy.TITLE_CACHE_GENERATION_NOT_IMPLEMENTED)){
199
            if(element instanceof CdmBase){
200
                text = ((CdmBase) element).getUuid().toString();
201
            }
202
            else{
203
                text = element.toString();
204
            }
205
        }
206
        //remove dot at the end
207
        if(text.endsWith(".")){
208
            text = text.substring(0, text.length()-1);
209
        }
210
		return text;
211
    }
212

    
213
    /** {@inheritDoc} */
214
    @Override
215
    public String getToolTipText(Object element) {
216
        return getDerivateText(element);
217
    }
218

    
219
    /**
220
     * @param conversation the conversation to set
221
     */
222
    public void setConversation(ConversationHolder conversation) {
223
        this.conversation = conversation;
224
    }
225

    
226
    public String getDerivateText(Object element){
227
        return getDerivateText(element, conversation);
228
    }
229

    
230
    public static String getDerivateText(Object element, ConversationHolder conversation){
231
        //TODO: use list of strings to assemble labels to avoid adding the separator every time and to allow null values
232
        TreeNode parentNode = null;
233
        TreeNode node = null;
234
        Object derivate = element;
235
        if(element instanceof TreeNode){
236
            node = (TreeNode) element;
237
            parentNode = node.getParent();
238
            //unwrap specimen from TreeNode
239
            derivate = node.getValue();
240
        }
241

    
242
        if(conversation!=null){
243
            conversation.bind();
244
        }
245

    
246
        final String emptyString = "";
247
        final String separator = " ";
248

    
249
        String label = emptyString;
250

    
251
        //Field Unit
252
        if(derivate instanceof FieldUnit){
253
            FieldUnit fieldUnit = (FieldUnit)derivate;
254
            if(fieldUnit.getGatheringEvent()!=null){
255
                GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
256
                label += gatheringEvent.getCountry()!=null?gatheringEvent.getCountry().getLabel()+separator:emptyString;
257
                label += gatheringEvent.getLocality()!=null?gatheringEvent.getLocality().getText()+separator:emptyString;
258
                label += gatheringEvent.getGatheringDate()!=null?gatheringEvent.getGatheringDate()+separator:emptyString;
259
                label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
260
            }
261
            label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber():emptyString;
262
        }
263
        //MediaSpecimen
264
        else if(derivate instanceof MediaSpecimen){
265
            MediaSpecimen mediaSpecimen = (MediaSpecimen)derivate;
266
            if(mediaSpecimen.getMediaSpecimen()!=null){
267
                label += mediaSpecimen.getMediaSpecimen().getTitle()!=null?mediaSpecimen.getMediaSpecimen().getTitle().getText()+separator:"[no motif]";
268
                label += mediaSpecimen.getMediaSpecimen().getArtist()!=null?mediaSpecimen.getMediaSpecimen().getArtist()+separator:emptyString;
269
            }
270
            eu.etaxonomy.cdm.model.occurrence.Collection collection = mediaSpecimen.getCollection();
271
            if(collection!=null){
272
                label += collection.getName()!=null?collection.getName()+" ":emptyString;
273
                label += collection.getCode()!=null?"("+collection.getCode()+")"+separator:emptyString;
274
            }
275
            label += mediaSpecimen.getAccessionNumber()!=null?mediaSpecimen.getAccessionNumber()+separator:emptyString;
276
            label += mediaSpecimen.getBarcode()!=null?mediaSpecimen.getBarcode()+separator:emptyString;
277
        }
278
        //TissueSample + DnaSample
279
        else if(derivate instanceof DnaSample){
280
            DnaSample dnaSample = (DnaSample)derivate;
281
            if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.DnaSample){
282
                Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
283
                if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
284
                    label += currentSampleDesignation.getIdentifier()+separator;
285
                }
286
                else{
287
                    label += NO_SAMPLE_DESIGNATION+separator;
288
                }
289
            }
290
            else if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.TissueSample){
291
                if(dnaSample.getKindOfUnit()!=null){
292
                    label += dnaSample.getKindOfUnit()+separator;
293
                }
294
                Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
295
                if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
296
                    label += currentSampleDesignation.getIdentifier()+separator;
297
                }
298
                else{
299
                    label += NO_SAMPLE_DESIGNATION+separator;
300
                }
301
            }
302

    
303
        }
304
        //DerivedUnit + TissueSample
305
        else if(derivate instanceof DerivedUnit){
306
            DerivedUnit derivedUnit = (DerivedUnit)derivate;
307
            if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
308
                //check for type designation
309
                if(typeDesignations.get(derivedUnit)==null){
310
                    for (SpecimenTypeDesignation specimenTypeDesignation : derivedUnit.getSpecimenTypeDesignations()) {
311
                        addTypeDesignation(derivedUnit, specimenTypeDesignation);
312
                    }
313
                }
314
                //java.util.Collection<FieldUnit> fieldUnits = CdmStore.getService(IOccurrenceService.class).getFieldUnits(derivedUnit.getUuid());
315
                //TODO : This is not generic anymore for performance reasons
316
                Set<SpecimenOrObservationBase> originals = derivedUnit.getOriginals();
317
                if(originals!=null && originals.size() ==1) {
318
                    SpecimenOrObservationBase specimen = originals.iterator().next();
319
                    if(specimen instanceof FieldUnit) {
320
                        FieldUnit fieldUnit = (FieldUnit)specimen;
321
                        GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
322
                        if(gatheringEvent!=null){
323
                            label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
324
                        }
325
                        label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber()+separator:emptyString;
326
                    }
327
                }
328

    
329
                eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnit.getCollection();
330
                if(collection!=null){
331
                    label += collection.getCode()!=null?"("+collection.getCode()+")"+separator:emptyString;
332
                }
333
                String mostSignificantIdentifier = derivedUnit.getMostSignificantIdentifier();
334
                label += mostSignificantIdentifier!=null?mostSignificantIdentifier+separator:emptyString;
335
            }
336
            else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
337
                //TissueSample should only be created by using it's own class
338
                //in future using only one class with different SpecimenOrObservationTypes is desired
339
//                label += derivedUnit.getKindOfUnit() + NO_SAMPLE_DESIGNATION;
340
            }
341
        }
342
        //Sequence
343
        else if(derivate instanceof Sequence){
344
            Sequence sequence = (Sequence)derivate;
345
            Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
346
            if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
347
                label += currentSampleDesignation.getIdentifier()+separator;
348
            }
349
            else{
350
                label += NO_SAMPLE_DESIGNATION+separator;
351
            }
352
            label += sequence.getDnaMarker()!=null?sequence.getDnaMarker():emptyString;
353
        }
354
        //SingleRead
355
        else if(derivate instanceof SingleRead){
356
            SingleRead singleRead = (SingleRead)derivate;
357
            if(parentNode!=null && parentNode.getValue() instanceof Sequence){
358
                Sequence sequence = (Sequence) parentNode.getValue();
359
                Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
360
                if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
361
                    label = currentSampleDesignation.getIdentifier()+separator;
362
                }
363
                else{
364
                    label += NO_SAMPLE_DESIGNATION+separator;
365
                }
366
                label += singleRead.getPrimer()!=null?singleRead.getPrimer().getLabel()+separator:emptyString;
367
                if(sequence!=null && sequence.getDnaMarker()!=null){
368
                    label += sequence.getDnaMarker()+separator;
369
                }
370
                if(singleRead.getAmplificationResult()!=null && singleRead.getAmplificationResult().getAmplification()!=null){
371
                    label += singleRead.getAmplificationResult().getAmplification().getLabelCache()+separator;
372
                }
373
            }
374
        }
375
        //SOOB
376
        else if(derivate instanceof SpecimenOrObservationBase){
377
            SpecimenOrObservationBase<?> specimen = (SpecimenOrObservationBase<?>) derivate;
378
            SpecimenOrObservationType type = specimen.getRecordBasis();
379
            return specimen.getTitleCache() + (type!=null?" ["+type.toString()+"]":emptyString);
380
        }
381
        if(label.isEmpty()){
382
            label = derivate.toString();
383
        }
384
        //remove last comma
385
        else if(label.endsWith(separator)){
386
            label = label.substring(0, label.length()-separator.length());
387
        }
388
        return label;
389
    }
390

    
391
    @Override
392
    public Image getImage(Object element) {
393
        if(element instanceof TreeNode){
394
            element = ((TreeNode) element).getValue();
395
        }
396
        if(element instanceof CdmBase){
397
            CdmBase cdmBase = (CdmBase)element;
398
            boolean hasCharacterData = false;
399
            if(cdmBase.isInstanceOf(SpecimenOrObservationBase.class)){
400
                SpecimenOrObservationBase<?> specimen = HibernateProxyHelper.deproxy(cdmBase, SpecimenOrObservationBase.class);
401
                if(specimen.hasCharacterData()){
402
                    hasCharacterData = true;
403
                }
404
            }
405
            if(cdmBase.isInstanceOf(FieldUnit.class)){
406
                return hasCharacterData?ImageResources.getImage(ImageResources.FIELD_UNIT_CHARACTER_DATA):ImageResources.getImage(ImageResources.FIELD_UNIT);
407
            }
408
            else if(cdmBase.isInstanceOf(DerivedUnit.class)){
409
                DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(element, DerivedUnit.class);
410

    
411
                if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.FieldUnit){
412
                    return hasCharacterData?ImageResources.getImage(ImageResources.FIELD_UNIT_CHARACTER_DATA):ImageResources.getImage(ImageResources.FIELD_UNIT);
413
                }
414
                else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.DnaSample){
415
                    return hasCharacterData?ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE);
416
                }
417
                else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
418
                    return hasCharacterData?ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE);
419
                }
420
                else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
421
                    if(typeDesignations.get(derivedUnit)!=null && !typeDesignations.get(derivedUnit).isEmpty()){
422
                        return ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE_TYPE);
423
                    }
424
                    return hasCharacterData?ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE);
425
                }
426
                else if(derivedUnit.getRecordBasis().isMedia()
427
                        || derivedUnit.getRecordBasis().isKindOf(SpecimenOrObservationType.Media)){
428
                    if(derivedUnit.getKindOfUnit()!=null){
429
                        if(derivedUnit.getKindOfUnit().equals(getArtworkTerm())){
430
                            return hasCharacterData?ImageResources.getImage(ImageResources.ARTWORK_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.ARTWORK_DERIVATE);
431
                        }
432
                        else if(derivedUnit.getKindOfUnit().equals(getLivingPlantPhotoTerm())){
433
                            return hasCharacterData?ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE);
434
                        }
435
                        else if(derivedUnit.getKindOfUnit().equals(getSpecimenScanTerm())){
436
                            return hasCharacterData?ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE);
437
                        }
438
                        else if(derivedUnit.getKindOfUnit().equals(getDetailImageTerm())){
439
                            return hasCharacterData?ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE);
440
                        }
441
                    }
442
                }
443
            }
444
            else if(cdmBase.isInstanceOf(Sequence.class)){
445
                return ImageResources.getImage(ImageResources.SEQUENCE_DERIVATE);
446
            }
447

    
448
            else if(cdmBase.isInstanceOf(SingleRead.class)){
449
                if(multiLinkSingleReads!=null && multiLinkSingleReads.contains(element)){
450
                    return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE_MULTILINK);
451
                }
452
                else{
453
                    return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE);
454
                }
455
            }
456
        }
457
        return ImageResources.getImage(ImageResources.DEFAULT_DERIVATIVE);
458
    }
459

    
460
    public static Identifier<DnaSample> getCurrentSampleDesignation(CdmBase entity) {
461
        if(entity.isInstanceOf(DnaSample.class)){
462
            DnaSample dnaSample = HibernateProxyHelper.deproxy(entity, DnaSample.class);
463
            for (Identifier<DnaSample> identifier : dnaSample.getIdentifiers()) {
464
                if(identifier.getType()!=null && identifier.getType().equals(DerivateLabelProvider.getSampleDesignationTerm())){
465
                    //first sample designation is the current
466
                    return identifier;
467
                }
468
            }
469
        }
470
        else if(entity.isInstanceOf(Sequence.class)){
471
            Sequence sequence = HibernateProxyHelper.deproxy(entity, Sequence.class);
472
            if(sequence.getDnaSample()!=null){
473
                return getCurrentSampleDesignation(sequence.getDnaSample());
474
            }
475
        }
476
        return null;
477
    }
478

    
479
    private static void addTypeDesignation(DerivedUnit derivedUnit, SpecimenTypeDesignation typeDesignation){
480
        Collection<SpecimenTypeDesignation> list = typeDesignations.get(derivedUnit);
481
        if(list==null){
482
            list = new ArrayList<SpecimenTypeDesignation>();
483
        }
484
        list.add(typeDesignation);
485
        typeDesignations.put(derivedUnit, list);
486
    }
487

    
488
    public static Set<SingleRead> getMultiLinkSingleReads() {
489
        return multiLinkSingleReads;
490
    }
491

    
492
    public void updateLabelCache(Collection<SpecimenOrObservationBase<?>> rootElements) {
493
        multiLinkSingleReads = new HashSet<SingleRead>();
494
        typeDesignations = new HashMap<DerivedUnit, Collection<SpecimenTypeDesignation>>();
495
        for(Entry<SingleRead, Collection<Sequence>> entry:CdmStore.getService(ISequenceService.class).getSingleReadSequencesMap().entrySet()){
496
            if(entry.getValue().size()>1){
497
                multiLinkSingleReads.add(entry.getKey());
498
            }
499
        }
500
        if(rootElements!=null){
501
            Collection<DerivedUnit> derivedUnits = new ArrayList<DerivedUnit>();
502
            for (SpecimenOrObservationBase specimenOrObservationBase : rootElements) {
503
                List<DerivedUnit> childUnits = CdmStore.getService(IOccurrenceService.class).getAllChildDerivatives(specimenOrObservationBase.getUuid());
504
                if (childUnits != null){
505
                    derivedUnits.addAll(childUnits);
506
                }
507
                if(specimenOrObservationBase.isInstanceOf(DerivedUnit.class)){
508
                    specimenOrObservationBase = CdmStore.getService(IOccurrenceService.class).load(specimenOrObservationBase.getUuid());
509
                    if (specimenOrObservationBase != null){
510
                        derivedUnits.add(HibernateProxyHelper.deproxy(specimenOrObservationBase, DerivedUnit.class));
511
                    }
512
                }
513
            }
514
            for (DerivedUnit derivedUnit : derivedUnits) {
515
                if(!derivedUnit.getSpecimenTypeDesignations().isEmpty()){
516
                    typeDesignations.put(derivedUnit, derivedUnit.getSpecimenTypeDesignations());
517
                }
518
            }
519
        }
520
    }
521

    
522
}
(2-2/2)