3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.view
.derivateSearch
;
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
;
18 import java
.util
.Map
.Entry
;
20 import java
.util
.UUID
;
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
;
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
;
32 import eu
.etaxonomy
.cdm
.format
.ICdmFormatter
.FormatKey
;
33 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
34 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
35 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTerm
;
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 static Map
<Class
<?
extends Object
>, ICdmFormatter
> formatterMap
= new HashMap
<>();
69 private ConversationHolder conversation
;
71 private static DefinedTerm photoTerm
= null;
72 private static DefinedTerm drawingTerm
= null;
73 private static DefinedTerm specimenScanTerm
= null;
74 private static DefinedTerm detailImageTerm
= null;
75 private static DefinedTerm sampleDesignationTerm
= null;
77 //FIXME: move static term getters to new singleton utility class
78 private static void initializeTerms() {
79 List
<DefinedTerm
> preferredTerms
= CdmStore
.getTermManager().getPreferredTerms(DefinedTerm
.class);
80 for (DefinedTerm definedTerm
: preferredTerms
) {
81 if(definedTerm
.getUuid().equals(UUID
.fromString("c5c59c42-f254-471e-96c6-09f459f7c903"))){
82 photoTerm
= definedTerm
;
84 else if(definedTerm
.getUuid().equals(UUID
.fromString("669b0409-4aa4-4695-aae4-a95ed27bad4c"))){
85 drawingTerm
= definedTerm
;
87 else if(definedTerm
.getUuid().equals(UUID
.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03"))){
88 specimenScanTerm
= definedTerm
;
90 else if(definedTerm
.getUuid().equals(UUID
.fromString("31eb8d02-bf5d-437c-bcc6-87a626445f34"))){
91 detailImageTerm
= definedTerm
;
93 else if(definedTerm
.getUuid().equals(UUID
.fromString("fadeba12-1be3-4bc7-9ff5-361b088d86fc"))){
94 sampleDesignationTerm
= definedTerm
;
99 public static DefinedTerm
getLivingPlantPhotoTerm(){
106 public static DefinedTerm
getArtworkTerm(){
107 if(drawingTerm
==null){
113 public static DefinedTerm
getSpecimenScanTerm(){
114 if(specimenScanTerm
==null){
117 return specimenScanTerm
;
120 public static DefinedTerm
getDetailImageTerm(){
121 if(detailImageTerm
==null){
124 return detailImageTerm
;
127 public static DefinedTerm
getSampleDesignationTerm(){
128 if(sampleDesignationTerm
==null){
131 return sampleDesignationTerm
;
137 public String
getText(Object element
) {
138 if(element
instanceof TreeNode
){
139 element
= ((TreeNode
) element
).getValue();
141 FormatKey
[] formatKeys
= {
142 FormatKey
.GATHERING_COUNTRY
, FormatKey
.SPACE
,
143 FormatKey
.GATHERING_LOCALITY_TEXT
, FormatKey
.SPACE
,
144 FormatKey
.GATHERING_DATE
, FormatKey
.SPACE
,
145 FormatKey
.GATHERING_COLLECTOR
, FormatKey
.SPACE
,
146 FormatKey
.FIELD_NUMBER
, FormatKey
.SPACE
,
147 FormatKey
.COLLECTION_CODE
, FormatKey
.SPACE
,
148 FormatKey
.MOST_SIGNIFICANT_IDENTIFIER
, FormatKey
.SPACE
,
149 FormatKey
.KIND_OF_UNIT
, FormatKey
.SPACE
,
150 FormatKey
.SAMPLE_DESIGNATION
, FormatKey
.SPACE
,
151 FormatKey
.SINGLE_READ_PRIMER
, FormatKey
.SPACE
,
152 FormatKey
.SEQUENCE_DNA_MARKER
, FormatKey
.SPACE
,
153 FormatKey
.AMPLIFICATION_LABEL
, FormatKey
.SPACE
,
154 FormatKey
.MEDIA_TITLE
, FormatKey
.SPACE
,
155 FormatKey
.MEDIA_ARTIST
, FormatKey
.SPACE
157 ICdmFormatter formatter
= formatterMap
.get(element
.getClass());
159 formatter
= CdmFormatterFactory
.getFormatter(element
, formatKeys
);
160 formatterMap
.put(element
.getClass(), formatter
);
162 return formatter
.format(element
);
167 public String
getToolTipText(Object element
) {
168 return getDerivateText(element
);
172 * @param conversation the conversation to set
174 public void setConversation(ConversationHolder conversation
) {
175 this.conversation
= conversation
;
178 public String
getDerivateText(Object element
){
179 return getDerivateText(element
, conversation
);
182 public static String
getDerivateText(Object element
, ConversationHolder conversation
){
183 //TODO: use list of strings to assemble labels to avoid adding the separator every time and to allow null values
184 TreeNode parentNode
= null;
185 TreeNode node
= null;
186 Object derivate
= element
;
187 if(element
instanceof TreeNode
){
188 node
= (TreeNode
) element
;
189 parentNode
= node
.getParent();
190 //unwrap specimen from TreeNode
191 derivate
= node
.getValue();
194 if(conversation
!=null){
198 final String emptyString
= "";
199 final String separator
= " ";
201 String label
= emptyString
;
204 if(derivate
instanceof FieldUnit
){
205 FieldUnit fieldUnit
= (FieldUnit
)derivate
;
206 if(fieldUnit
.getGatheringEvent()!=null){
207 GatheringEvent gatheringEvent
= fieldUnit
.getGatheringEvent();
208 label
+= gatheringEvent
.getCountry()!=null?gatheringEvent
.getCountry().getLabel()+separator
:emptyString
;
209 label
+= gatheringEvent
.getLocality()!=null?gatheringEvent
.getLocality().getText()+separator
:emptyString
;
210 label
+= gatheringEvent
.getGatheringDate()!=null?gatheringEvent
.getGatheringDate()+separator
:emptyString
;
211 label
+= gatheringEvent
.getCollector()!=null?gatheringEvent
.getCollector()+separator
:emptyString
;
213 label
+= fieldUnit
.getFieldNumber()!=null?fieldUnit
.getFieldNumber():emptyString
;
216 else if(derivate
instanceof MediaSpecimen
){
217 MediaSpecimen mediaSpecimen
= (MediaSpecimen
)derivate
;
218 if(mediaSpecimen
.getMediaSpecimen()!=null){
219 label
+= mediaSpecimen
.getMediaSpecimen().getTitle()!=null?mediaSpecimen
.getMediaSpecimen().getTitle().getText()+separator
:"[no motif]";
220 label
+= mediaSpecimen
.getMediaSpecimen().getArtist()!=null?mediaSpecimen
.getMediaSpecimen().getArtist()+separator
:emptyString
;
222 eu
.etaxonomy
.cdm
.model
.occurrence
.Collection collection
= mediaSpecimen
.getCollection();
223 if(collection
!=null){
224 label
+= collection
.getName()!=null?collection
.getName()+" ":emptyString
;
225 label
+= collection
.getCode()!=null?
"("+collection
.getCode()+")"+separator
:emptyString
;
227 label
+= mediaSpecimen
.getAccessionNumber()!=null?mediaSpecimen
.getAccessionNumber()+separator
:emptyString
;
228 label
+= mediaSpecimen
.getBarcode()!=null?mediaSpecimen
.getBarcode()+separator
:emptyString
;
230 //TissueSample + DnaSample
231 else if(derivate
instanceof DnaSample
){
232 DnaSample dnaSample
= (DnaSample
)derivate
;
233 if(((DnaSample
) derivate
).getRecordBasis()==SpecimenOrObservationType
.DnaSample
){
234 Identifier
<DnaSample
> currentSampleDesignation
= getCurrentSampleDesignation(dnaSample
);
235 if(currentSampleDesignation
!=null && currentSampleDesignation
.getIdentifier()!=null){
236 label
+= currentSampleDesignation
.getIdentifier()+separator
;
239 label
+= NO_SAMPLE_DESIGNATION
+separator
;
242 else if(((DnaSample
) derivate
).getRecordBasis()==SpecimenOrObservationType
.TissueSample
){
243 if(dnaSample
.getKindOfUnit()!=null){
244 label
+= dnaSample
.getKindOfUnit()+separator
;
246 Identifier
<DnaSample
> currentSampleDesignation
= getCurrentSampleDesignation(dnaSample
);
247 if(currentSampleDesignation
!=null && currentSampleDesignation
.getIdentifier()!=null){
248 label
+= currentSampleDesignation
.getIdentifier()+separator
;
251 label
+= NO_SAMPLE_DESIGNATION
+separator
;
256 //DerivedUnit + TissueSample
257 else if(derivate
instanceof DerivedUnit
){
258 DerivedUnit derivedUnit
= (DerivedUnit
)derivate
;
259 if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.PreservedSpecimen
){
260 //check for type designation
261 if(typeDesignations
.get(derivedUnit
)==null){
262 for (SpecimenTypeDesignation specimenTypeDesignation
: CdmStore
.getService(IOccurrenceService
.class).listTypeDesignations(derivedUnit
, null, null, null, null)) {
263 addTypeDesignation(derivedUnit
, specimenTypeDesignation
);
266 //java.util.Collection<FieldUnit> fieldUnits = CdmStore.getService(IOccurrenceService.class).getFieldUnits(derivedUnit.getUuid());
267 //TODO : This is not generic anymore for performance reasons
268 Set
<SpecimenOrObservationBase
> originals
= derivedUnit
.getOriginals();
269 if(originals
!=null && originals
.size() ==1) {
270 SpecimenOrObservationBase specimen
= originals
.iterator().next();
271 if(specimen
instanceof FieldUnit
) {
272 FieldUnit fieldUnit
= (FieldUnit
)specimen
;
273 GatheringEvent gatheringEvent
= fieldUnit
.getGatheringEvent();
274 if(gatheringEvent
!=null){
275 label
+= gatheringEvent
.getCollector()!=null?gatheringEvent
.getCollector()+separator
:emptyString
;
277 label
+= fieldUnit
.getFieldNumber()!=null?fieldUnit
.getFieldNumber()+separator
:emptyString
;
281 eu
.etaxonomy
.cdm
.model
.occurrence
.Collection collection
= derivedUnit
.getCollection();
282 if(collection
!=null){
283 label
+= collection
.getCode()!=null?
"("+collection
.getCode()+")"+separator
:emptyString
;
285 String mostSignificantIdentifier
= derivedUnit
.getMostSignificantIdentifier();
286 label
+= mostSignificantIdentifier
!=null?mostSignificantIdentifier
+separator
:emptyString
;
288 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.TissueSample
){
289 //TissueSample should only be created by using it's own class
290 //in future using only one class with different SpecimenOrObservationTypes is desired
291 // label += derivedUnit.getKindOfUnit() + NO_SAMPLE_DESIGNATION;
295 else if(derivate
instanceof Sequence
){
296 Sequence sequence
= (Sequence
)derivate
;
297 Identifier
<DnaSample
> currentSampleDesignation
= getCurrentSampleDesignation(sequence
);
298 if(currentSampleDesignation
!=null && currentSampleDesignation
.getIdentifier()!=null){
299 label
+= currentSampleDesignation
.getIdentifier()+separator
;
302 label
+= NO_SAMPLE_DESIGNATION
+separator
;
304 label
+= sequence
.getDnaMarker()!=null?sequence
.getDnaMarker():emptyString
;
307 else if(derivate
instanceof SingleRead
){
308 SingleRead singleRead
= (SingleRead
)derivate
;
309 if(parentNode
!=null && parentNode
.getValue() instanceof Sequence
){
310 Sequence sequence
= (Sequence
) parentNode
.getValue();
311 Identifier
<DnaSample
> currentSampleDesignation
= getCurrentSampleDesignation(sequence
);
312 if(currentSampleDesignation
!=null && currentSampleDesignation
.getIdentifier()!=null){
313 label
= currentSampleDesignation
.getIdentifier()+separator
;
316 label
+= NO_SAMPLE_DESIGNATION
+separator
;
318 label
+= singleRead
.getPrimer()!=null?singleRead
.getPrimer().getLabel()+separator
:emptyString
;
319 if(sequence
!=null && sequence
.getDnaMarker()!=null){
320 label
+= sequence
.getDnaMarker()+separator
;
322 if(singleRead
.getAmplificationResult()!=null && singleRead
.getAmplificationResult().getAmplification()!=null){
323 label
+= singleRead
.getAmplificationResult().getAmplification().getLabelCache()+separator
;
328 else if(derivate
instanceof SpecimenOrObservationBase
){
329 SpecimenOrObservationBase
<?
> specimen
= (SpecimenOrObservationBase
<?
>) derivate
;
330 SpecimenOrObservationType type
= specimen
.getRecordBasis();
331 return specimen
.getTitleCache() + (type
!=null?
" ["+type
.toString()+"]":emptyString
);
334 label
= derivate
.toString();
337 else if(label
.endsWith(separator
)){
338 label
= label
.substring(0, label
.length()-separator
.length());
344 public Image
getImage(Object element
) {
345 if(element
instanceof TreeNode
){
346 element
= ((TreeNode
) element
).getValue();
348 if(element
instanceof CdmBase
){
349 CdmBase cdmBase
= (CdmBase
)element
;
350 boolean hasCharacterData
= false;
351 if(cdmBase
.isInstanceOf(SpecimenOrObservationBase
.class)){
352 SpecimenOrObservationBase
<?
> specimen
= HibernateProxyHelper
.deproxy(cdmBase
, SpecimenOrObservationBase
.class);
353 if(specimen
.hasCharacterData()){
354 hasCharacterData
= true;
357 if(cdmBase
.isInstanceOf(FieldUnit
.class)){
358 return hasCharacterData?ImageResources
.getImage(ImageResources
.FIELD_UNIT_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.FIELD_UNIT
);
360 else if(cdmBase
.isInstanceOf(DerivedUnit
.class)){
361 DerivedUnit derivedUnit
= HibernateProxyHelper
.deproxy(element
, DerivedUnit
.class);
363 if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.FieldUnit
){
364 return hasCharacterData?ImageResources
.getImage(ImageResources
.FIELD_UNIT_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.FIELD_UNIT
);
366 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.DnaSample
){
367 return hasCharacterData?ImageResources
.getImage(ImageResources
.DNA_SAMPLE_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.DNA_SAMPLE_DERIVATE
);
369 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.TissueSample
){
370 return hasCharacterData?ImageResources
.getImage(ImageResources
.TISSUE_SAMPLE_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.TISSUE_SAMPLE_DERIVATE
);
372 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.PreservedSpecimen
){
373 if(typeDesignations
.get(derivedUnit
)!=null && !typeDesignations
.get(derivedUnit
).isEmpty()){
374 return ImageResources
.getImage(ImageResources
.SPECIMEN_DERIVATE_TYPE
);
376 return hasCharacterData?ImageResources
.getImage(ImageResources
.SPECIMEN_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.SPECIMEN_DERIVATE
);
378 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.Media
){
379 if(derivedUnit
.getKindOfUnit()!=null){
380 if(derivedUnit
.getKindOfUnit().equals(getArtworkTerm())){
381 return hasCharacterData?ImageResources
.getImage(ImageResources
.ARTWORK_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.ARTWORK_DERIVATE
);
383 else if(derivedUnit
.getKindOfUnit().equals(getLivingPlantPhotoTerm())){
384 return hasCharacterData?ImageResources
.getImage(ImageResources
.LIVING_PLANT_PHOTO_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.LIVING_PLANT_PHOTO_DERIVATE
);
386 else if(derivedUnit
.getKindOfUnit().equals(getSpecimenScanTerm())){
387 return hasCharacterData?ImageResources
.getImage(ImageResources
.SPECIMEN_SCAN_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.SPECIMEN_SCAN_DERIVATE
);
389 else if(derivedUnit
.getKindOfUnit().equals(getDetailImageTerm())){
390 return hasCharacterData?ImageResources
.getImage(ImageResources
.DETAIL_IMAGE_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.DETAIL_IMAGE_DERIVATE
);
395 else if(cdmBase
.isInstanceOf(Sequence
.class)){
396 return ImageResources
.getImage(ImageResources
.SEQUENCE_DERIVATE
);
399 else if(cdmBase
.isInstanceOf(SingleRead
.class)){
400 if(multiLinkSingleReads
!=null && multiLinkSingleReads
.contains(element
)){
401 return ImageResources
.getImage(ImageResources
.SINGLE_READ_DERIVATE_MULTILINK
);
404 return ImageResources
.getImage(ImageResources
.SINGLE_READ_DERIVATE
);
408 return super.getImage(element
);
411 public static Identifier
<DnaSample
> getCurrentSampleDesignation(CdmBase entity
) {
412 if(entity
.isInstanceOf(DnaSample
.class)){
413 DnaSample dnaSample
= HibernateProxyHelper
.deproxy(entity
, DnaSample
.class);
414 for (Identifier
<DnaSample
> identifier
: dnaSample
.getIdentifiers()) {
415 if(identifier
.getType()!=null && identifier
.getType().equals(DerivateLabelProvider
.getSampleDesignationTerm())){
416 //first sample designation is the current
421 else if(entity
.isInstanceOf(Sequence
.class)){
422 Sequence sequence
= HibernateProxyHelper
.deproxy(entity
, Sequence
.class);
423 if(sequence
.getDnaSample()!=null){
424 return getCurrentSampleDesignation(sequence
.getDnaSample());
430 private static void addTypeDesignation(DerivedUnit derivedUnit
, SpecimenTypeDesignation typeDesignation
){
431 Collection
<SpecimenTypeDesignation
> list
= typeDesignations
.get(derivedUnit
);
433 list
= new ArrayList
<SpecimenTypeDesignation
>();
435 list
.add(typeDesignation
);
436 typeDesignations
.put(derivedUnit
, list
);
439 public static Set
<SingleRead
> getMultiLinkSingleReads() {
440 return multiLinkSingleReads
;
443 public void updateLabelCache(Collection
<SpecimenOrObservationBase
<?
>> rootElements
) {
444 multiLinkSingleReads
= new HashSet
<SingleRead
>();
445 typeDesignations
= new HashMap
<DerivedUnit
, Collection
<SpecimenTypeDesignation
>>();
446 for(Entry
<SingleRead
, Collection
<Sequence
>> entry
:CdmStore
.getService(ISequenceService
.class).getSingleReadSequencesMap().entrySet()){
447 if(entry
.getValue().size()>1){
448 multiLinkSingleReads
.add(entry
.getKey());
451 if(rootElements
!=null){
452 Collection
<DerivedUnit
> derivedUnits
= new ArrayList
<DerivedUnit
>();
453 for (SpecimenOrObservationBase specimenOrObservationBase
: rootElements
) {
454 derivedUnits
.addAll(CdmStore
.getService(IOccurrenceService
.class).getAllChildDerivatives(specimenOrObservationBase
.getUuid()));
455 if(specimenOrObservationBase
.isInstanceOf(DerivedUnit
.class)){
456 derivedUnits
.add(HibernateProxyHelper
.deproxy(specimenOrObservationBase
, DerivedUnit
.class));
459 typeDesignations
= CdmStore
.getService(IOccurrenceService
.class).listTypeDesignations(derivedUnits
, null, null, null, null);