2 * Copyright (C) 2013 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.view
.derivateSearch
;
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
;
17 import java
.util
.Map
.Entry
;
19 import java
.util
.UUID
;
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
;
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
.taxeditor
.model
.ImageResources
;
48 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
51 * Label provider for the views to show {@link SpecimenOrObservationBase}s.<br>
53 * <b>Note:</b> If you use this label provider you need to assure that you
54 * created a {@link ConversationHolder} resp. have an open session because
55 * the labels are generated from various fields of the derivate hierarchy which
56 * are lazy loaded and could therefore throw a {@link LazyInitializationException}.<br>
57 * Use <b>{@link #setConversation(ConversationHolder)}</b> to assign the session to this provider.
59 public class DerivateLabelProvider
extends ColumnLabelProvider
{
61 private static final String NO_SAMPLE_DESIGNATION
= "[no sample designation]";
63 private static Set
<SingleRead
> multiLinkSingleReads
;
65 private static Map
<DerivedUnit
, Collection
<SpecimenTypeDesignation
>> typeDesignations
;
67 private ConversationHolder conversation
;
69 private static DefinedTerm photoTerm
= null;
70 private static DefinedTerm drawingTerm
= null;
71 private static DefinedTerm specimenScanTerm
= null;
72 private static DefinedTerm detailImageTerm
= null;
73 private static DefinedTerm sampleDesignationTerm
= null;
75 //FIXME: move static term getters to new singleton utility class
76 private static void initializeTerms() {
77 List
<DefinedTerm
> preferredTerms
= CdmStore
.getTermManager().getPreferredTerms(DefinedTerm
.class);
78 for (DefinedTerm definedTerm
: preferredTerms
) {
79 if(definedTerm
.getUuid().equals(UUID
.fromString("c5c59c42-f254-471e-96c6-09f459f7c903"))){
80 photoTerm
= definedTerm
;
82 else if(definedTerm
.getUuid().equals(UUID
.fromString("669b0409-4aa4-4695-aae4-a95ed27bad4c"))){
83 drawingTerm
= definedTerm
;
85 else if(definedTerm
.getUuid().equals(UUID
.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03"))){
86 specimenScanTerm
= definedTerm
;
88 else if(definedTerm
.getUuid().equals(UUID
.fromString("31eb8d02-bf5d-437c-bcc6-87a626445f34"))){
89 detailImageTerm
= definedTerm
;
91 else if(definedTerm
.getUuid().equals(UUID
.fromString("fadeba12-1be3-4bc7-9ff5-361b088d86fc"))){
92 sampleDesignationTerm
= definedTerm
;
97 public static DefinedTerm
getLivingPlantPhotoTerm(){
104 public static DefinedTerm
getArtworkTerm(){
105 if(drawingTerm
==null){
111 public static DefinedTerm
getSpecimenScanTerm(){
112 if(specimenScanTerm
==null){
115 return specimenScanTerm
;
118 public static DefinedTerm
getDetailImageTerm(){
119 if(detailImageTerm
==null){
122 return detailImageTerm
;
125 public static DefinedTerm
getSampleDesignationTerm(){
126 if(sampleDesignationTerm
==null){
129 return sampleDesignationTerm
;
135 public String
getText(Object element
) {
136 if(element
instanceof TreeNode
){
137 element
= ((TreeNode
) element
).getValue();
139 FormatKey
[] formatKeys
= {
140 FormatKey
.GATHERING_COUNTRY
, FormatKey
.SPACE
,
141 FormatKey
.GATHERING_LOCALITY_TEXT
, FormatKey
.SPACE
,
142 FormatKey
.GATHERING_DATE
, FormatKey
.SPACE
,
143 FormatKey
.GATHERING_COLLECTOR
, FormatKey
.SPACE
,
144 FormatKey
.FIELD_NUMBER
, FormatKey
.SPACE
,
145 FormatKey
.COLLECTION_CODE
, FormatKey
.SPACE
,
146 FormatKey
.MOST_SIGNIFICANT_IDENTIFIER
, FormatKey
.SPACE
,
147 // FormatKey.KIND_OF_UNIT, FormatKey.SPACE,
148 FormatKey
.SAMPLE_DESIGNATION
, FormatKey
.SPACE
,
149 FormatKey
.SINGLE_READ_PRIMER
, FormatKey
.SPACE
,
150 FormatKey
.SEQUENCE_DNA_MARKER
, FormatKey
.SPACE
,
151 FormatKey
.AMPLIFICATION_LABEL
, FormatKey
.SPACE
,
152 FormatKey
.MEDIA_TITLE
, FormatKey
.SPACE
,
153 FormatKey
.MEDIA_ARTIST
, FormatKey
.SPACE
155 String text
= CdmFormatterFactory
.format(element
, formatKeys
);
156 if(element
instanceof IdentifiableEntity
){
157 IdentifiableEntity identifiableEntity
= (IdentifiableEntity
) element
;
158 if(identifiableEntity
.isProtectedTitleCache()){
159 text
= identifiableEntity
.getTitleCache();
162 if(CdmUtils
.isBlank(text
)){
170 public String
getToolTipText(Object element
) {
171 return getDerivateText(element
);
175 * @param conversation the conversation to set
177 public void setConversation(ConversationHolder conversation
) {
178 this.conversation
= conversation
;
181 public String
getDerivateText(Object element
){
182 return getDerivateText(element
, conversation
);
185 public static String
getDerivateText(Object element
, ConversationHolder conversation
){
186 //TODO: use list of strings to assemble labels to avoid adding the separator every time and to allow null values
187 TreeNode parentNode
= null;
188 TreeNode node
= null;
189 Object derivate
= element
;
190 if(element
instanceof TreeNode
){
191 node
= (TreeNode
) element
;
192 parentNode
= node
.getParent();
193 //unwrap specimen from TreeNode
194 derivate
= node
.getValue();
197 if(conversation
!=null){
201 final String emptyString
= "";
202 final String separator
= " ";
204 String label
= emptyString
;
207 if(derivate
instanceof FieldUnit
){
208 FieldUnit fieldUnit
= (FieldUnit
)derivate
;
209 if(fieldUnit
.getGatheringEvent()!=null){
210 GatheringEvent gatheringEvent
= fieldUnit
.getGatheringEvent();
211 label
+= gatheringEvent
.getCountry()!=null?gatheringEvent
.getCountry().getLabel()+separator
:emptyString
;
212 label
+= gatheringEvent
.getLocality()!=null?gatheringEvent
.getLocality().getText()+separator
:emptyString
;
213 label
+= gatheringEvent
.getGatheringDate()!=null?gatheringEvent
.getGatheringDate()+separator
:emptyString
;
214 label
+= gatheringEvent
.getCollector()!=null?gatheringEvent
.getCollector()+separator
:emptyString
;
216 label
+= fieldUnit
.getFieldNumber()!=null?fieldUnit
.getFieldNumber():emptyString
;
219 else if(derivate
instanceof MediaSpecimen
){
220 MediaSpecimen mediaSpecimen
= (MediaSpecimen
)derivate
;
221 if(mediaSpecimen
.getMediaSpecimen()!=null){
222 label
+= mediaSpecimen
.getMediaSpecimen().getTitle()!=null?mediaSpecimen
.getMediaSpecimen().getTitle().getText()+separator
:"[no motif]";
223 label
+= mediaSpecimen
.getMediaSpecimen().getArtist()!=null?mediaSpecimen
.getMediaSpecimen().getArtist()+separator
:emptyString
;
225 eu
.etaxonomy
.cdm
.model
.occurrence
.Collection collection
= mediaSpecimen
.getCollection();
226 if(collection
!=null){
227 label
+= collection
.getName()!=null?collection
.getName()+" ":emptyString
;
228 label
+= collection
.getCode()!=null?
"("+collection
.getCode()+")"+separator
:emptyString
;
230 label
+= mediaSpecimen
.getAccessionNumber()!=null?mediaSpecimen
.getAccessionNumber()+separator
:emptyString
;
231 label
+= mediaSpecimen
.getBarcode()!=null?mediaSpecimen
.getBarcode()+separator
:emptyString
;
233 //TissueSample + DnaSample
234 else if(derivate
instanceof DnaSample
){
235 DnaSample dnaSample
= (DnaSample
)derivate
;
236 if(((DnaSample
) derivate
).getRecordBasis()==SpecimenOrObservationType
.DnaSample
){
237 Identifier
<DnaSample
> currentSampleDesignation
= getCurrentSampleDesignation(dnaSample
);
238 if(currentSampleDesignation
!=null && currentSampleDesignation
.getIdentifier()!=null){
239 label
+= currentSampleDesignation
.getIdentifier()+separator
;
242 label
+= NO_SAMPLE_DESIGNATION
+separator
;
245 else if(((DnaSample
) derivate
).getRecordBasis()==SpecimenOrObservationType
.TissueSample
){
246 if(dnaSample
.getKindOfUnit()!=null){
247 label
+= dnaSample
.getKindOfUnit()+separator
;
249 Identifier
<DnaSample
> currentSampleDesignation
= getCurrentSampleDesignation(dnaSample
);
250 if(currentSampleDesignation
!=null && currentSampleDesignation
.getIdentifier()!=null){
251 label
+= currentSampleDesignation
.getIdentifier()+separator
;
254 label
+= NO_SAMPLE_DESIGNATION
+separator
;
259 //DerivedUnit + TissueSample
260 else if(derivate
instanceof DerivedUnit
){
261 DerivedUnit derivedUnit
= (DerivedUnit
)derivate
;
262 if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.PreservedSpecimen
){
263 //check for type designation
264 if(typeDesignations
.get(derivedUnit
)==null){
265 for (SpecimenTypeDesignation specimenTypeDesignation
: CdmStore
.getService(IOccurrenceService
.class).listTypeDesignations(derivedUnit
, null, null, null, null)) {
266 addTypeDesignation(derivedUnit
, specimenTypeDesignation
);
269 //java.util.Collection<FieldUnit> fieldUnits = CdmStore.getService(IOccurrenceService.class).getFieldUnits(derivedUnit.getUuid());
270 //TODO : This is not generic anymore for performance reasons
271 Set
<SpecimenOrObservationBase
> originals
= derivedUnit
.getOriginals();
272 if(originals
!=null && originals
.size() ==1) {
273 SpecimenOrObservationBase specimen
= originals
.iterator().next();
274 if(specimen
instanceof FieldUnit
) {
275 FieldUnit fieldUnit
= (FieldUnit
)specimen
;
276 GatheringEvent gatheringEvent
= fieldUnit
.getGatheringEvent();
277 if(gatheringEvent
!=null){
278 label
+= gatheringEvent
.getCollector()!=null?gatheringEvent
.getCollector()+separator
:emptyString
;
280 label
+= fieldUnit
.getFieldNumber()!=null?fieldUnit
.getFieldNumber()+separator
:emptyString
;
284 eu
.etaxonomy
.cdm
.model
.occurrence
.Collection collection
= derivedUnit
.getCollection();
285 if(collection
!=null){
286 label
+= collection
.getCode()!=null?
"("+collection
.getCode()+")"+separator
:emptyString
;
288 String mostSignificantIdentifier
= derivedUnit
.getMostSignificantIdentifier();
289 label
+= mostSignificantIdentifier
!=null?mostSignificantIdentifier
+separator
:emptyString
;
291 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.TissueSample
){
292 //TissueSample should only be created by using it's own class
293 //in future using only one class with different SpecimenOrObservationTypes is desired
294 // label += derivedUnit.getKindOfUnit() + NO_SAMPLE_DESIGNATION;
298 else if(derivate
instanceof Sequence
){
299 Sequence sequence
= (Sequence
)derivate
;
300 Identifier
<DnaSample
> currentSampleDesignation
= getCurrentSampleDesignation(sequence
);
301 if(currentSampleDesignation
!=null && currentSampleDesignation
.getIdentifier()!=null){
302 label
+= currentSampleDesignation
.getIdentifier()+separator
;
305 label
+= NO_SAMPLE_DESIGNATION
+separator
;
307 label
+= sequence
.getDnaMarker()!=null?sequence
.getDnaMarker():emptyString
;
310 else if(derivate
instanceof SingleRead
){
311 SingleRead singleRead
= (SingleRead
)derivate
;
312 if(parentNode
!=null && parentNode
.getValue() instanceof Sequence
){
313 Sequence sequence
= (Sequence
) parentNode
.getValue();
314 Identifier
<DnaSample
> currentSampleDesignation
= getCurrentSampleDesignation(sequence
);
315 if(currentSampleDesignation
!=null && currentSampleDesignation
.getIdentifier()!=null){
316 label
= currentSampleDesignation
.getIdentifier()+separator
;
319 label
+= NO_SAMPLE_DESIGNATION
+separator
;
321 label
+= singleRead
.getPrimer()!=null?singleRead
.getPrimer().getLabel()+separator
:emptyString
;
322 if(sequence
!=null && sequence
.getDnaMarker()!=null){
323 label
+= sequence
.getDnaMarker()+separator
;
325 if(singleRead
.getAmplificationResult()!=null && singleRead
.getAmplificationResult().getAmplification()!=null){
326 label
+= singleRead
.getAmplificationResult().getAmplification().getLabelCache()+separator
;
331 else if(derivate
instanceof SpecimenOrObservationBase
){
332 SpecimenOrObservationBase
<?
> specimen
= (SpecimenOrObservationBase
<?
>) derivate
;
333 SpecimenOrObservationType type
= specimen
.getRecordBasis();
334 return specimen
.getTitleCache() + (type
!=null?
" ["+type
.toString()+"]":emptyString
);
337 label
= derivate
.toString();
340 else if(label
.endsWith(separator
)){
341 label
= label
.substring(0, label
.length()-separator
.length());
347 public Image
getImage(Object element
) {
348 if(element
instanceof TreeNode
){
349 element
= ((TreeNode
) element
).getValue();
351 if(element
instanceof CdmBase
){
352 CdmBase cdmBase
= (CdmBase
)element
;
353 boolean hasCharacterData
= false;
354 if(cdmBase
.isInstanceOf(SpecimenOrObservationBase
.class)){
355 SpecimenOrObservationBase
<?
> specimen
= HibernateProxyHelper
.deproxy(cdmBase
, SpecimenOrObservationBase
.class);
356 if(specimen
.hasCharacterData()){
357 hasCharacterData
= true;
360 if(cdmBase
.isInstanceOf(FieldUnit
.class)){
361 return hasCharacterData?ImageResources
.getImage(ImageResources
.FIELD_UNIT_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.FIELD_UNIT
);
363 else if(cdmBase
.isInstanceOf(DerivedUnit
.class)){
364 DerivedUnit derivedUnit
= HibernateProxyHelper
.deproxy(element
, DerivedUnit
.class);
366 if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.FieldUnit
){
367 return hasCharacterData?ImageResources
.getImage(ImageResources
.FIELD_UNIT_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.FIELD_UNIT
);
369 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.DnaSample
){
370 return hasCharacterData?ImageResources
.getImage(ImageResources
.DNA_SAMPLE_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.DNA_SAMPLE_DERIVATE
);
372 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.TissueSample
){
373 return hasCharacterData?ImageResources
.getImage(ImageResources
.TISSUE_SAMPLE_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.TISSUE_SAMPLE_DERIVATE
);
375 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.PreservedSpecimen
){
376 if(typeDesignations
.get(derivedUnit
)!=null && !typeDesignations
.get(derivedUnit
).isEmpty()){
377 return ImageResources
.getImage(ImageResources
.SPECIMEN_DERIVATE_TYPE
);
379 return hasCharacterData?ImageResources
.getImage(ImageResources
.SPECIMEN_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.SPECIMEN_DERIVATE
);
381 else if(derivedUnit
.getRecordBasis().isKindOf(SpecimenOrObservationType
.Media
)){
382 if(derivedUnit
.getKindOfUnit()!=null){
383 if(derivedUnit
.getKindOfUnit().equals(getArtworkTerm())){
384 return hasCharacterData?ImageResources
.getImage(ImageResources
.ARTWORK_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.ARTWORK_DERIVATE
);
386 else if(derivedUnit
.getKindOfUnit().equals(getLivingPlantPhotoTerm())){
387 return hasCharacterData?ImageResources
.getImage(ImageResources
.LIVING_PLANT_PHOTO_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.LIVING_PLANT_PHOTO_DERIVATE
);
389 else if(derivedUnit
.getKindOfUnit().equals(getSpecimenScanTerm())){
390 return hasCharacterData?ImageResources
.getImage(ImageResources
.SPECIMEN_SCAN_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.SPECIMEN_SCAN_DERIVATE
);
392 else if(derivedUnit
.getKindOfUnit().equals(getDetailImageTerm())){
393 return hasCharacterData?ImageResources
.getImage(ImageResources
.DETAIL_IMAGE_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.DETAIL_IMAGE_DERIVATE
);
398 else if(cdmBase
.isInstanceOf(Sequence
.class)){
399 return ImageResources
.getImage(ImageResources
.SEQUENCE_DERIVATE
);
402 else if(cdmBase
.isInstanceOf(SingleRead
.class)){
403 if(multiLinkSingleReads
!=null && multiLinkSingleReads
.contains(element
)){
404 return ImageResources
.getImage(ImageResources
.SINGLE_READ_DERIVATE_MULTILINK
);
407 return ImageResources
.getImage(ImageResources
.SINGLE_READ_DERIVATE
);
411 return ImageResources
.getImage(ImageResources
.DEFAULT_DERIVATIVE
);
414 public static Identifier
<DnaSample
> getCurrentSampleDesignation(CdmBase entity
) {
415 if(entity
.isInstanceOf(DnaSample
.class)){
416 DnaSample dnaSample
= HibernateProxyHelper
.deproxy(entity
, DnaSample
.class);
417 for (Identifier
<DnaSample
> identifier
: dnaSample
.getIdentifiers()) {
418 if(identifier
.getType()!=null && identifier
.getType().equals(DerivateLabelProvider
.getSampleDesignationTerm())){
419 //first sample designation is the current
424 else if(entity
.isInstanceOf(Sequence
.class)){
425 Sequence sequence
= HibernateProxyHelper
.deproxy(entity
, Sequence
.class);
426 if(sequence
.getDnaSample()!=null){
427 return getCurrentSampleDesignation(sequence
.getDnaSample());
433 private static void addTypeDesignation(DerivedUnit derivedUnit
, SpecimenTypeDesignation typeDesignation
){
434 Collection
<SpecimenTypeDesignation
> list
= typeDesignations
.get(derivedUnit
);
436 list
= new ArrayList
<SpecimenTypeDesignation
>();
438 list
.add(typeDesignation
);
439 typeDesignations
.put(derivedUnit
, list
);
442 public static Set
<SingleRead
> getMultiLinkSingleReads() {
443 return multiLinkSingleReads
;
446 public void updateLabelCache(Collection
<SpecimenOrObservationBase
<?
>> rootElements
) {
447 multiLinkSingleReads
= new HashSet
<SingleRead
>();
448 typeDesignations
= new HashMap
<DerivedUnit
, Collection
<SpecimenTypeDesignation
>>();
449 for(Entry
<SingleRead
, Collection
<Sequence
>> entry
:CdmStore
.getService(ISequenceService
.class).getSingleReadSequencesMap().entrySet()){
450 if(entry
.getValue().size()>1){
451 multiLinkSingleReads
.add(entry
.getKey());
454 if(rootElements
!=null){
455 Collection
<DerivedUnit
> derivedUnits
= new ArrayList
<DerivedUnit
>();
456 for (SpecimenOrObservationBase specimenOrObservationBase
: rootElements
) {
457 derivedUnits
.addAll(CdmStore
.getService(IOccurrenceService
.class).getAllChildDerivatives(specimenOrObservationBase
.getUuid()));
458 if(specimenOrObservationBase
.isInstanceOf(DerivedUnit
.class)){
459 derivedUnits
.add(HibernateProxyHelper
.deproxy(specimenOrObservationBase
, DerivedUnit
.class));
462 typeDesignations
= CdmStore
.getService(IOccurrenceService
.class).listTypeDesignations(derivedUnits
, null, null, null, null);