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
.CdmFormatter
;
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
;
50 * Label provider for the views to show {@link SpecimenOrObservationBase}s.<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.
58 public class DerivateLabelProvider
extends ColumnLabelProvider
{
60 private static final String NO_SAMPLE_DESIGNATION
= "[no sample designation]";
62 private static Set
<SingleRead
> multiLinkSingleReads
;
64 private static Map
<DerivedUnit
, List
<SpecimenTypeDesignation
>> typeDesignations
;
66 private ConversationHolder conversation
;
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;
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
;
81 else if(definedTerm
.getUuid().equals(UUID
.fromString("669b0409-4aa4-4695-aae4-a95ed27bad4c"))){
82 drawingTerm
= definedTerm
;
84 else if(definedTerm
.getUuid().equals(UUID
.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03"))){
85 specimenScanTerm
= definedTerm
;
87 else if(definedTerm
.getUuid().equals(UUID
.fromString("31eb8d02-bf5d-437c-bcc6-87a626445f34"))){
88 detailImageTerm
= definedTerm
;
90 else if(definedTerm
.getUuid().equals(UUID
.fromString("fadeba12-1be3-4bc7-9ff5-361b088d86fc"))){
91 sampleDesignationTerm
= definedTerm
;
96 public static DefinedTerm
getLivingPlantPhotoTerm(){
103 public static DefinedTerm
getArtworkTerm(){
104 if(drawingTerm
==null){
110 public static DefinedTerm
getSpecimenScanTerm(){
111 if(specimenScanTerm
==null){
114 return specimenScanTerm
;
117 public static DefinedTerm
getDetailImageTerm(){
118 if(detailImageTerm
==null){
121 return detailImageTerm
;
124 public static DefinedTerm
getSampleDesignationTerm(){
125 if(sampleDesignationTerm
==null){
128 return sampleDesignationTerm
;
134 public String
getText(Object element
) {
135 if(element
instanceof TreeNode
){
136 element
= ((TreeNode
) element
).getValue();
138 CdmFormatter formatter
= new CdmFormatter(FormatKey
.ACCESSION_NUMBER
, FormatKey
.CLOSE_BRACKET
, FormatKey
.ACCESSION_NUMBER
, FormatKey
.OPEN_BRACKET
,
139 FormatKey
.LOCALITY_TEXT
, FormatKey
.CLOSE_BRACKET
);
140 return formatter
.format(element
);
141 // return getDerivateText(element);
146 public String
getToolTipText(Object element
) {
147 return getDerivateText(element
);
151 * @param conversation the conversation to set
153 public void setConversation(ConversationHolder conversation
) {
154 this.conversation
= conversation
;
157 public String
getDerivateText(Object element
){
158 return getDerivateText(element
, conversation
);
161 public static String
getDerivateText(Object element
, ConversationHolder conversation
){
162 //TODO: use list of strings to assemble labels to avoid adding the separator every time and to allow null values
163 TreeNode parentNode
= null;
164 TreeNode node
= null;
165 Object derivate
= element
;
166 if(element
instanceof TreeNode
){
167 node
= (TreeNode
) element
;
168 parentNode
= node
.getParent();
169 //unwrap specimen from TreeNode
170 derivate
= node
.getValue();
173 if(conversation
!=null){
177 final String emptyString
= "";
178 final String separator
= " ";
180 String label
= emptyString
;
183 if(derivate
instanceof FieldUnit
){
184 FieldUnit fieldUnit
= (FieldUnit
)derivate
;
185 if(fieldUnit
.getGatheringEvent()!=null){
186 GatheringEvent gatheringEvent
= fieldUnit
.getGatheringEvent();
187 label
+= gatheringEvent
.getCountry()!=null?gatheringEvent
.getCountry().getLabel()+separator
:emptyString
;
188 label
+= gatheringEvent
.getLocality()!=null?gatheringEvent
.getLocality().getText()+separator
:emptyString
;
189 label
+= gatheringEvent
.getGatheringDate()!=null?gatheringEvent
.getGatheringDate()+separator
:emptyString
;
190 label
+= gatheringEvent
.getCollector()!=null?gatheringEvent
.getCollector()+separator
:emptyString
;
192 label
+= fieldUnit
.getFieldNumber()!=null?fieldUnit
.getFieldNumber():emptyString
;
195 else if(derivate
instanceof MediaSpecimen
){
196 MediaSpecimen mediaSpecimen
= (MediaSpecimen
)derivate
;
197 if(mediaSpecimen
.getMediaSpecimen()!=null){
198 label
+= mediaSpecimen
.getMediaSpecimen().getTitle()!=null?mediaSpecimen
.getMediaSpecimen().getTitle().getText()+separator
:"[no motif]";
199 label
+= mediaSpecimen
.getMediaSpecimen().getArtist()!=null?mediaSpecimen
.getMediaSpecimen().getArtist()+separator
:emptyString
;
201 eu
.etaxonomy
.cdm
.model
.occurrence
.Collection collection
= mediaSpecimen
.getCollection();
202 if(collection
!=null){
203 label
+= collection
.getName()!=null?collection
.getName()+" ":emptyString
;
204 label
+= collection
.getCode()!=null?
"("+collection
.getCode()+")"+separator
:emptyString
;
206 label
+= mediaSpecimen
.getAccessionNumber()!=null?mediaSpecimen
.getAccessionNumber()+separator
:emptyString
;
207 label
+= mediaSpecimen
.getBarcode()!=null?mediaSpecimen
.getBarcode()+separator
:emptyString
;
209 //TissueSample + DnaSample
210 else if(derivate
instanceof DnaSample
){
211 DnaSample dnaSample
= (DnaSample
)derivate
;
212 if(((DnaSample
) derivate
).getRecordBasis()==SpecimenOrObservationType
.DnaSample
){
213 Identifier
<DnaSample
> currentSampleDesignation
= getCurrentSampleDesignation(dnaSample
);
214 if(currentSampleDesignation
!=null && currentSampleDesignation
.getIdentifier()!=null){
215 label
+= currentSampleDesignation
.getIdentifier()+separator
;
218 label
+= NO_SAMPLE_DESIGNATION
+separator
;
221 else if(((DnaSample
) derivate
).getRecordBasis()==SpecimenOrObservationType
.TissueSample
){
222 if(dnaSample
.getKindOfUnit()!=null){
223 label
+= dnaSample
.getKindOfUnit()+separator
;
225 Identifier
<DnaSample
> currentSampleDesignation
= getCurrentSampleDesignation(dnaSample
);
226 if(currentSampleDesignation
!=null && currentSampleDesignation
.getIdentifier()!=null){
227 label
+= currentSampleDesignation
.getIdentifier()+separator
;
230 label
+= NO_SAMPLE_DESIGNATION
+separator
;
235 //DerivedUnit + TissueSample
236 else if(derivate
instanceof DerivedUnit
){
237 DerivedUnit derivedUnit
= (DerivedUnit
)derivate
;
238 if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.PreservedSpecimen
){
239 //check for type designation
240 if(typeDesignations
.get(derivedUnit
)==null){
241 for (SpecimenTypeDesignation specimenTypeDesignation
: CdmStore
.getService(IOccurrenceService
.class).listTypeDesignations(derivedUnit
, null, null, null, null)) {
242 addTypeDesignation(derivedUnit
, specimenTypeDesignation
);
245 //java.util.Collection<FieldUnit> fieldUnits = CdmStore.getService(IOccurrenceService.class).getFieldUnits(derivedUnit.getUuid());
246 //TODO : This is not generic anymore for performance reasons
247 Set
<SpecimenOrObservationBase
> originals
= derivedUnit
.getOriginals();
248 if(originals
!=null && originals
.size() ==1) {
249 SpecimenOrObservationBase specimen
= originals
.iterator().next();
250 if(specimen
instanceof FieldUnit
) {
251 FieldUnit fieldUnit
= (FieldUnit
)specimen
;
252 GatheringEvent gatheringEvent
= fieldUnit
.getGatheringEvent();
253 if(gatheringEvent
!=null){
254 label
+= gatheringEvent
.getCollector()!=null?gatheringEvent
.getCollector()+separator
:emptyString
;
256 label
+= fieldUnit
.getFieldNumber()!=null?fieldUnit
.getFieldNumber()+separator
:emptyString
;
260 eu
.etaxonomy
.cdm
.model
.occurrence
.Collection collection
= derivedUnit
.getCollection();
261 if(collection
!=null){
262 label
+= collection
.getCode()!=null?
"("+collection
.getCode()+")"+separator
:emptyString
;
264 String mostSignificantIdentifier
= derivedUnit
.getMostSignificantIdentifier();
265 label
+= mostSignificantIdentifier
!=null?mostSignificantIdentifier
+separator
:emptyString
;
267 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.TissueSample
){
268 //TissueSample should only be created by using it's own class
269 //in future using only one class with different SpecimenOrObservationTypes is desired
270 // label += derivedUnit.getKindOfUnit() + NO_SAMPLE_DESIGNATION;
274 else if(derivate
instanceof Sequence
){
275 Sequence sequence
= (Sequence
)derivate
;
276 Identifier
<DnaSample
> currentSampleDesignation
= getCurrentSampleDesignation(sequence
);
277 if(currentSampleDesignation
!=null && currentSampleDesignation
.getIdentifier()!=null){
278 label
+= currentSampleDesignation
.getIdentifier()+separator
;
281 label
+= NO_SAMPLE_DESIGNATION
+separator
;
283 label
+= sequence
.getDnaMarker()!=null?sequence
.getDnaMarker():emptyString
;
286 else if(derivate
instanceof SingleRead
){
287 SingleRead singleRead
= (SingleRead
)derivate
;
288 if(parentNode
!=null && parentNode
.getValue() instanceof Sequence
){
289 Sequence sequence
= (Sequence
) parentNode
.getValue();
290 Identifier
<DnaSample
> currentSampleDesignation
= getCurrentSampleDesignation(sequence
);
291 if(currentSampleDesignation
!=null && currentSampleDesignation
.getIdentifier()!=null){
292 label
= currentSampleDesignation
.getIdentifier()+separator
;
295 label
+= NO_SAMPLE_DESIGNATION
+separator
;
297 label
+= singleRead
.getPrimer()!=null?singleRead
.getPrimer().getLabel()+separator
:emptyString
;
298 if(sequence
!=null && sequence
.getDnaMarker()!=null){
299 label
+= sequence
.getDnaMarker()+separator
;
301 if(singleRead
.getAmplificationResult()!=null && singleRead
.getAmplificationResult().getAmplification()!=null){
302 label
+= singleRead
.getAmplificationResult().getAmplification().getLabelCache()+separator
;
307 else if(derivate
instanceof SpecimenOrObservationBase
){
308 SpecimenOrObservationBase
<?
> specimen
= (SpecimenOrObservationBase
<?
>) derivate
;
309 SpecimenOrObservationType type
= specimen
.getRecordBasis();
310 return specimen
.getTitleCache() + (type
!=null?
" ["+type
.toString()+"]":emptyString
);
313 label
= derivate
.toString();
316 else if(label
.endsWith(separator
)){
317 label
= label
.substring(0, label
.length()-separator
.length());
323 public Image
getImage(Object element
) {
324 if(element
instanceof TreeNode
){
325 element
= ((TreeNode
) element
).getValue();
327 if(element
instanceof CdmBase
){
328 CdmBase cdmBase
= (CdmBase
)element
;
329 boolean hasCharacterData
= false;
330 if(cdmBase
.isInstanceOf(SpecimenOrObservationBase
.class)){
331 SpecimenOrObservationBase
<?
> specimen
= HibernateProxyHelper
.deproxy(cdmBase
, SpecimenOrObservationBase
.class);
332 if(specimen
.hasCharacterData()){
333 hasCharacterData
= true;
336 if(cdmBase
.isInstanceOf(FieldUnit
.class)){
337 return hasCharacterData?ImageResources
.getImage(ImageResources
.FIELD_UNIT_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.FIELD_UNIT
);
339 else if(cdmBase
.isInstanceOf(DerivedUnit
.class)){
340 DerivedUnit derivedUnit
= HibernateProxyHelper
.deproxy(element
, DerivedUnit
.class);
342 boolean isType
= false;
343 //type designation extension
344 if(typeDesignations
.get(derivedUnit
)!=null){
347 if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.FieldUnit
){
348 return hasCharacterData?ImageResources
.getImage(ImageResources
.FIELD_UNIT_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.FIELD_UNIT
);
350 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.DnaSample
){
351 return hasCharacterData?ImageResources
.getImage(ImageResources
.DNA_SAMPLE_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.DNA_SAMPLE_DERIVATE
);
353 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.TissueSample
){
354 return hasCharacterData?ImageResources
.getImage(ImageResources
.TISSUE_SAMPLE_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.TISSUE_SAMPLE_DERIVATE
);
356 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.PreservedSpecimen
){
357 return hasCharacterData?ImageResources
.getImage(ImageResources
.SPECIMEN_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.SPECIMEN_DERIVATE
);
359 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.Media
){
360 if(derivedUnit
.getKindOfUnit()!=null){
361 if(derivedUnit
.getKindOfUnit().equals(getArtworkTerm())){
362 return hasCharacterData?ImageResources
.getImage(ImageResources
.ARTWORK_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.ARTWORK_DERIVATE
);
364 else if(derivedUnit
.getKindOfUnit().equals(getLivingPlantPhotoTerm())){
365 return hasCharacterData?ImageResources
.getImage(ImageResources
.LIVING_PLANT_PHOTO_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.LIVING_PLANT_PHOTO_DERIVATE
);
367 else if(derivedUnit
.getKindOfUnit().equals(getSpecimenScanTerm())){
368 return hasCharacterData?ImageResources
.getImage(ImageResources
.SPECIMEN_SCAN_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.SPECIMEN_SCAN_DERIVATE
);
370 else if(derivedUnit
.getKindOfUnit().equals(getDetailImageTerm())){
371 return hasCharacterData?ImageResources
.getImage(ImageResources
.DETAIL_IMAGE_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.DETAIL_IMAGE_DERIVATE
);
376 else if(cdmBase
.isInstanceOf(Sequence
.class)){
377 return ImageResources
.getImage(ImageResources
.SEQUENCE_DERIVATE
);
380 else if(cdmBase
.isInstanceOf(SingleRead
.class)){
381 if(multiLinkSingleReads
!=null && multiLinkSingleReads
.contains(element
)){
382 return ImageResources
.getImage(ImageResources
.SINGLE_READ_DERIVATE_MULTILINK
);
385 return ImageResources
.getImage(ImageResources
.SINGLE_READ_DERIVATE
);
389 return super.getImage(element
);
392 public static Identifier
<DnaSample
> getCurrentSampleDesignation(CdmBase entity
) {
393 if(entity
.isInstanceOf(DnaSample
.class)){
394 DnaSample dnaSample
= HibernateProxyHelper
.deproxy(entity
, DnaSample
.class);
395 for (Identifier
<DnaSample
> identifier
: dnaSample
.getIdentifiers()) {
396 if(identifier
.getType()!=null && identifier
.getType().equals(DerivateLabelProvider
.getSampleDesignationTerm())){
397 //first sample designation is the current
402 else if(entity
.isInstanceOf(Sequence
.class)){
403 Sequence sequence
= HibernateProxyHelper
.deproxy(entity
, Sequence
.class);
404 if(sequence
.getDnaSample()!=null){
405 return getCurrentSampleDesignation(sequence
.getDnaSample());
412 * Refreshes cached label extensions
413 * @param multiLinkSingleReads
415 public void refresh() {
416 DerivateLabelProvider
.multiLinkSingleReads
= new HashSet
<SingleRead
>();
417 for(Entry
<SingleRead
, Collection
<Sequence
>> entry
:CdmStore
.getService(ISequenceService
.class).getSingleReadSequencesMap().entrySet()){
418 if(entry
.getValue().size()>1){
419 multiLinkSingleReads
.add(entry
.getKey());
422 DerivateLabelProvider
.typeDesignations
= new HashMap
<DerivedUnit
, List
<SpecimenTypeDesignation
>>();
425 private static void addTypeDesignation(DerivedUnit derivedUnit
, SpecimenTypeDesignation typeDesignation
){
426 List
<SpecimenTypeDesignation
> list
= typeDesignations
.get(derivedUnit
);
428 list
= new ArrayList
<SpecimenTypeDesignation
>();
430 list
.add(typeDesignation
);
431 typeDesignations
.put(derivedUnit
, list
);
434 public static Set
<SingleRead
> getMultiLinkSingleReads() {
435 return multiLinkSingleReads
;