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
.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
;
49 * Label provider for the views to show {@link SpecimenOrObservationBase}s.<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.
57 public class DerivateLabelProvider
extends ColumnLabelProvider
{
59 private static final String NO_SAMPLE_DESIGNATION
= "[no sample designation]";
61 private static Set
<SingleRead
> multiLinkSingleReads
;
63 private static Map
<DerivedUnit
, List
<SpecimenTypeDesignation
>> typeDesignations
;
65 private ConversationHolder conversation
;
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;
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
;
80 else if(definedTerm
.getUuid().equals(UUID
.fromString("669b0409-4aa4-4695-aae4-a95ed27bad4c"))){
81 drawingTerm
= definedTerm
;
83 else if(definedTerm
.getUuid().equals(UUID
.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03"))){
84 specimenScanTerm
= definedTerm
;
86 else if(definedTerm
.getUuid().equals(UUID
.fromString("31eb8d02-bf5d-437c-bcc6-87a626445f34"))){
87 detailImageTerm
= definedTerm
;
89 else if(definedTerm
.getUuid().equals(UUID
.fromString("fadeba12-1be3-4bc7-9ff5-361b088d86fc"))){
90 sampleDesignationTerm
= definedTerm
;
95 public static DefinedTerm
getLivingPlantPhotoTerm(){
102 public static DefinedTerm
getArtworkTerm(){
103 if(drawingTerm
==null){
109 public static DefinedTerm
getSpecimenScanTerm(){
110 if(specimenScanTerm
==null){
113 return specimenScanTerm
;
116 public static DefinedTerm
getDetailImageTerm(){
117 if(detailImageTerm
==null){
120 return detailImageTerm
;
123 public static DefinedTerm
getSampleDesignationTerm(){
124 if(sampleDesignationTerm
==null){
127 return sampleDesignationTerm
;
133 public String
getText(Object element
) {
134 return getDerivateText(element
);
139 public String
getToolTipText(Object element
) {
140 return getDerivateText(element
);
144 * @param conversation the conversation to set
146 public void setConversation(ConversationHolder conversation
) {
147 this.conversation
= conversation
;
150 public String
getDerivateText(Object element
){
151 return getDerivateText(element
, conversation
);
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();
166 if(conversation
!=null){
170 final String emptyString
= "";
171 final String separator
= " ";
173 String label
= emptyString
;
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
;
185 label
+= fieldUnit
.getFieldNumber()!=null?fieldUnit
.getFieldNumber():emptyString
;
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
;
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
;
199 label
+= mediaSpecimen
.getAccessionNumber()!=null?mediaSpecimen
.getAccessionNumber()+separator
:emptyString
;
200 label
+= mediaSpecimen
.getBarcode()!=null?mediaSpecimen
.getBarcode()+separator
:emptyString
;
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
;
211 label
+= NO_SAMPLE_DESIGNATION
+separator
;
214 else if(((DnaSample
) derivate
).getRecordBasis()==SpecimenOrObservationType
.TissueSample
){
215 if(dnaSample
.getKindOfUnit()!=null){
216 label
+= dnaSample
.getKindOfUnit()+separator
;
218 Identifier
<DnaSample
> currentSampleDesignation
= getCurrentSampleDesignation(dnaSample
);
219 if(currentSampleDesignation
!=null && currentSampleDesignation
.getIdentifier()!=null){
220 label
+= currentSampleDesignation
.getIdentifier()+separator
;
223 label
+= NO_SAMPLE_DESIGNATION
+separator
;
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
);
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
;
249 label
+= fieldUnit
.getFieldNumber()!=null?fieldUnit
.getFieldNumber()+separator
:emptyString
;
253 eu
.etaxonomy
.cdm
.model
.occurrence
.Collection collection
= derivedUnit
.getCollection();
254 if(collection
!=null){
255 label
+= collection
.getCode()!=null?
"("+collection
.getCode()+")"+separator
:emptyString
;
257 String mostSignificantIdentifier
= derivedUnit
.getMostSignificantIdentifier();
258 label
+= mostSignificantIdentifier
!=null?mostSignificantIdentifier
+separator
:emptyString
;
259 //type designation extension
260 List
<SpecimenTypeDesignation
> list
= typeDesignations
.get(derivedUnit
);
262 for (SpecimenTypeDesignation specimenTypeDesignation
: list
) {
263 label
+= "("+specimenTypeDesignation
.getTypeStatus()+" of ";
264 for (TaxonNameBase taxonNameBase
: specimenTypeDesignation
.getTypifiedNames()) {
265 label
+= taxonNameBase
+separator
;
267 if(label
.endsWith(separator
)){
268 label
= label
.substring(0, label
.length()-separator
.length());
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;
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
;
288 label
+= NO_SAMPLE_DESIGNATION
+separator
;
290 label
+= sequence
.getDnaMarker()!=null?sequence
.getDnaMarker():emptyString
;
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
;
302 label
+= NO_SAMPLE_DESIGNATION
+separator
;
304 label
+= singleRead
.getPrimer()!=null?singleRead
.getPrimer().getLabel()+separator
:emptyString
;
305 if(sequence
!=null && sequence
.getDnaMarker()!=null){
306 label
+= sequence
.getDnaMarker()+separator
;
308 if(singleRead
.getAmplificationResult()!=null && singleRead
.getAmplificationResult().getAmplification()!=null){
309 label
+= singleRead
.getAmplificationResult().getAmplification().getLabelCache()+separator
;
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
);
320 label
= derivate
.toString();
323 else if(label
.endsWith(separator
)){
324 label
= label
.substring(0, label
.length()-separator
.length());
330 public Image
getImage(Object element
) {
331 if(element
instanceof TreeNode
){
332 element
= ((TreeNode
) element
).getValue();
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;
343 if(cdmBase
.isInstanceOf(FieldUnit
.class)){
344 return hasCharacterData?ImageResources
.getImage(ImageResources
.FIELD_UNIT_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.FIELD_UNIT
);
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
);
351 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.DnaSample
){
352 return hasCharacterData?ImageResources
.getImage(ImageResources
.DNA_SAMPLE_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.DNA_SAMPLE_DERIVATE
);
354 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.TissueSample
){
355 return hasCharacterData?ImageResources
.getImage(ImageResources
.TISSUE_SAMPLE_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.TISSUE_SAMPLE_DERIVATE
);
357 else if(derivedUnit
.getRecordBasis()==SpecimenOrObservationType
.PreservedSpecimen
){
358 return hasCharacterData?ImageResources
.getImage(ImageResources
.SPECIMEN_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.SPECIMEN_DERIVATE
);
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
);
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
);
368 else if(derivedUnit
.getKindOfUnit().equals(getSpecimenScanTerm())){
369 return hasCharacterData?ImageResources
.getImage(ImageResources
.SPECIMEN_SCAN_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.SPECIMEN_SCAN_DERIVATE
);
371 else if(derivedUnit
.getKindOfUnit().equals(getDetailImageTerm())){
372 return hasCharacterData?ImageResources
.getImage(ImageResources
.DETAIL_IMAGE_DERIVATE_CHARACTER_DATA
):ImageResources
.getImage(ImageResources
.DETAIL_IMAGE_DERIVATE
);
377 else if(cdmBase
.isInstanceOf(Sequence
.class)){
378 return ImageResources
.getImage(ImageResources
.SEQUENCE_DERIVATE
);
381 else if(cdmBase
.isInstanceOf(SingleRead
.class)){
382 if(multiLinkSingleReads
!=null && multiLinkSingleReads
.contains(element
)){
383 return ImageResources
.getImage(ImageResources
.SINGLE_READ_DERIVATE_MULTILINK
);
386 return ImageResources
.getImage(ImageResources
.SINGLE_READ_DERIVATE
);
390 return super.getImage(element
);
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
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());
413 * Refreshes cached label extensions
414 * @param multiLinkSingleReads
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());
423 DerivateLabelProvider
.typeDesignations
= new HashMap
<DerivedUnit
, List
<SpecimenTypeDesignation
>>();
426 private static void addTypeDesignation(DerivedUnit derivedUnit
, SpecimenTypeDesignation typeDesignation
){
427 List
<SpecimenTypeDesignation
> list
= typeDesignations
.get(derivedUnit
);
429 list
= new ArrayList
<SpecimenTypeDesignation
>();
431 list
.add(typeDesignation
);
432 typeDesignations
.put(derivedUnit
, list
);
435 public static Set
<SingleRead
> getMultiLinkSingleReads() {
436 return multiLinkSingleReads
;