3a7c48b0c54d2fae1242a5301fa0c2a6a81467c3
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / derivateSearch / DerivateLabelProvider.java
1 // $Id$
2 /**
3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.taxeditor.view.derivateSearch;
11
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;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.Set;
20 import java.util.UUID;
21
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;
26
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.DeterminationEvent;
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;
48
49 /**
50 * Label provider for the views to show {@link SpecimenOrObservationBase}s.<br>
51 * <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.
57 */
58 public class DerivateLabelProvider extends ColumnLabelProvider {
59
60 private static final String NO_SAMPLE_DESIGNATION = "[no sample designation]";
61
62 private static Set<SingleRead> multiLinkSingleReads;
63
64 private static Map<DerivedUnit, List<DeterminationEvent>> determinationEvents;
65
66 private static Map<DerivedUnit, List<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 return getDerivateText(element);
138 }
139
140 /** {@inheritDoc} */
141 @Override
142 public String getToolTipText(Object element) {
143 return getDerivateText(element);
144 }
145
146 /**
147 * @param conversation the conversation to set
148 */
149 public void setConversation(ConversationHolder conversation) {
150 this.conversation = conversation;
151 }
152
153 public String getDerivateText(Object element){
154 return getDerivateText(element, conversation);
155 }
156
157 public static String getDerivateText(Object element, ConversationHolder conversation){
158 //TODO: use list of strings to assemble labels to avoid adding the separator every time and to allow null values
159 TreeNode parentNode = null;
160 TreeNode node = null;
161 Object derivate = element;
162 if(element instanceof TreeNode){
163 node = (TreeNode) element;
164 parentNode = node.getParent();
165 //unwrap specimen from TreeNode
166 derivate = node.getValue();
167 }
168
169 if(conversation!=null){
170 conversation.bind();
171 }
172
173 final String emptyString = "";
174 final String separator = " ";
175
176 String label = emptyString;
177
178 //Field Unit
179 if(derivate instanceof FieldUnit){
180 FieldUnit fieldUnit = (FieldUnit)derivate;
181 if(fieldUnit.getGatheringEvent()!=null){
182 GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
183 label += gatheringEvent.getCountry()!=null?gatheringEvent.getCountry().getLabel()+separator:emptyString;
184 label += gatheringEvent.getLocality()!=null?gatheringEvent.getLocality().getText()+separator:emptyString;
185 label += gatheringEvent.getGatheringDate()!=null?gatheringEvent.getGatheringDate()+separator:emptyString;
186 label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
187 }
188 label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber():emptyString;
189 }
190 //MediaSpecimen
191 else if(derivate instanceof MediaSpecimen){
192 MediaSpecimen mediaSpecimen = (MediaSpecimen)derivate;
193 if(mediaSpecimen.getMediaSpecimen()!=null){
194 label += mediaSpecimen.getMediaSpecimen().getTitle()!=null?mediaSpecimen.getMediaSpecimen().getTitle().getText()+separator:"[no motif]";
195 label += mediaSpecimen.getMediaSpecimen().getArtist()!=null?mediaSpecimen.getMediaSpecimen().getArtist()+separator:emptyString;
196 }
197 eu.etaxonomy.cdm.model.occurrence.Collection collection = mediaSpecimen.getCollection();
198 if(collection!=null){
199 label += collection.getName()!=null?collection.getName()+" ":emptyString;
200 label += collection.getCode()!=null?"("+collection.getCode()+")"+separator:emptyString;
201 }
202 label += mediaSpecimen.getAccessionNumber()!=null?mediaSpecimen.getAccessionNumber()+separator:emptyString;
203 label += mediaSpecimen.getBarcode()!=null?mediaSpecimen.getBarcode()+separator:emptyString;
204 }
205 //TissueSample + DnaSample
206 else if(derivate instanceof DnaSample){
207 DnaSample dnaSample = (DnaSample)derivate;
208 if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.DnaSample){
209 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
210 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
211 label += currentSampleDesignation.getIdentifier()+separator;
212 }
213 else{
214 label += NO_SAMPLE_DESIGNATION+separator;
215 }
216 }
217 else if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.TissueSample){
218 if(dnaSample.getKindOfUnit()!=null){
219 label += dnaSample.getKindOfUnit()+separator;
220 }
221 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
222 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
223 label += currentSampleDesignation.getIdentifier()+separator;
224 }
225 else{
226 label += NO_SAMPLE_DESIGNATION+separator;
227 }
228 }
229
230 }
231 //DerivedUnit + TissueSample
232 else if(derivate instanceof DerivedUnit){
233 DerivedUnit derivedUnit = (DerivedUnit)derivate;
234 if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
235 //check for type designation
236 if(typeDesignations.get(derivedUnit)==null){
237 for (SpecimenTypeDesignation specimenTypeDesignation : CdmStore.getService(IOccurrenceService.class).listTypeDesignations(derivedUnit, null, null, null, null)) {
238 addTypeDesignation(derivedUnit, specimenTypeDesignation);
239 }
240 }
241 //check for determination events
242 if(determinationEvents.get(derivedUnit)==null){
243 for (DeterminationEvent determinationEvent : CdmStore.getService(IOccurrenceService.class).listDeterminationEvents(derivedUnit, null, null, null, null)) {
244 addDeterminationEvent(derivedUnit, determinationEvent);
245 }
246 }
247 //java.util.Collection<FieldUnit> fieldUnits = CdmStore.getService(IOccurrenceService.class).getFieldUnits(derivedUnit.getUuid());
248 //TODO : This is not generic anymore for performance reasons
249 Set<SpecimenOrObservationBase> originals = derivedUnit.getOriginals();
250 if(originals!=null && originals.size() ==1) {
251 SpecimenOrObservationBase specimen = originals.iterator().next();
252 if(specimen instanceof FieldUnit) {
253 FieldUnit fieldUnit = (FieldUnit)specimen;
254 GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
255 if(gatheringEvent!=null){
256 label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
257 }
258 label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber()+separator:emptyString;
259 }
260 }
261
262 eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnit.getCollection();
263 if(collection!=null){
264 label += collection.getCode()!=null?"("+collection.getCode()+")"+separator:emptyString;
265 }
266 String mostSignificantIdentifier = derivedUnit.getMostSignificantIdentifier();
267 label += mostSignificantIdentifier!=null?mostSignificantIdentifier+separator:emptyString;
268 //type designation extension
269 List<SpecimenTypeDesignation> typeDesignationList = typeDesignations.get(derivedUnit);
270 if(typeDesignationList!=null){
271 for (SpecimenTypeDesignation specimenTypeDesignation : typeDesignationList) {
272 label += "("+specimenTypeDesignation.getTypeStatus()+" of ";
273 for (TaxonNameBase taxonNameBase : specimenTypeDesignation.getTypifiedNames()) {
274 label += taxonNameBase+separator;
275 }
276 if(label.endsWith(separator)){
277 label = label.substring(0, label.length()-separator.length());
278 }
279 label += ")";
280 }
281 }
282 //determination event extension
283 List<DeterminationEvent> determinationEventList = determinationEvents.get(derivedUnit);
284 if(determinationEventList!=null){
285 for (DeterminationEvent determinationEvent : determinationEventList) {
286 label += "(";
287 if(determinationEvent.getTaxon()!=null){
288 label += "Determined as "+determinationEvent.getTaxon();
289 }
290
291 if(label.endsWith(separator)){
292 label = label.substring(0, label.length()-separator.length());
293 }
294 label += ")";
295 }
296 }
297 }
298 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
299 //TissueSample should only be created by using it's own class
300 //in future using only one class with different SpecimenOrObservationTypes is desired
301 // label += derivedUnit.getKindOfUnit() + NO_SAMPLE_DESIGNATION;
302 }
303 }
304 //Sequence
305 else if(derivate instanceof Sequence){
306 Sequence sequence = (Sequence)derivate;
307 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
308 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
309 label += currentSampleDesignation.getIdentifier()+separator;
310 }
311 else{
312 label += NO_SAMPLE_DESIGNATION+separator;
313 }
314 label += sequence.getDnaMarker()!=null?sequence.getDnaMarker():emptyString;
315 }
316 //SingleRead
317 else if(derivate instanceof SingleRead){
318 SingleRead singleRead = (SingleRead)derivate;
319 if(parentNode!=null && parentNode.getValue() instanceof Sequence){
320 Sequence sequence = (Sequence) parentNode.getValue();
321 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
322 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
323 label = currentSampleDesignation.getIdentifier()+separator;
324 }
325 else{
326 label += NO_SAMPLE_DESIGNATION+separator;
327 }
328 label += singleRead.getPrimer()!=null?singleRead.getPrimer().getLabel()+separator:emptyString;
329 if(sequence!=null && sequence.getDnaMarker()!=null){
330 label += sequence.getDnaMarker()+separator;
331 }
332 if(singleRead.getAmplificationResult()!=null && singleRead.getAmplificationResult().getAmplification()!=null){
333 label += singleRead.getAmplificationResult().getAmplification().getLabelCache()+separator;
334 }
335 }
336 }
337 //SOOB
338 else if(derivate instanceof SpecimenOrObservationBase){
339 SpecimenOrObservationBase<?> specimen = (SpecimenOrObservationBase<?>) derivate;
340 SpecimenOrObservationType type = specimen.getRecordBasis();
341 return specimen.getTitleCache() + (type!=null?" ["+type.toString()+"]":emptyString);
342 }
343 if(label.isEmpty()){
344 label = derivate.toString();
345 }
346 //remove last comma
347 else if(label.endsWith(separator)){
348 label = label.substring(0, label.length()-separator.length());
349 }
350 return label;
351 }
352
353 @Override
354 public Image getImage(Object element) {
355 if(element instanceof TreeNode){
356 element = ((TreeNode) element).getValue();
357 }
358 if(element instanceof CdmBase){
359 CdmBase cdmBase = (CdmBase)element;
360 boolean hasCharacterData = false;
361 if(cdmBase.isInstanceOf(SpecimenOrObservationBase.class)){
362 SpecimenOrObservationBase<?> specimen = HibernateProxyHelper.deproxy(cdmBase, SpecimenOrObservationBase.class);
363 if(specimen.hasCharacterData()){
364 hasCharacterData = true;
365 }
366 }
367 if(cdmBase.isInstanceOf(FieldUnit.class)){
368 return hasCharacterData?ImageResources.getImage(ImageResources.FIELD_UNIT_CHARACTER_DATA):ImageResources.getImage(ImageResources.FIELD_UNIT);
369 }
370 else if(cdmBase.isInstanceOf(DerivedUnit.class)){
371 DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(element, DerivedUnit.class);
372 if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.FieldUnit){
373 return hasCharacterData?ImageResources.getImage(ImageResources.FIELD_UNIT_CHARACTER_DATA):ImageResources.getImage(ImageResources.FIELD_UNIT);
374 }
375 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.DnaSample){
376 return hasCharacterData?ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE);
377 }
378 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
379 return hasCharacterData?ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE);
380 }
381 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
382 return hasCharacterData?ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE);
383 }
384 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.Media){
385 if(derivedUnit.getKindOfUnit()!=null){
386 if(derivedUnit.getKindOfUnit().equals(getArtworkTerm())){
387 return hasCharacterData?ImageResources.getImage(ImageResources.ARTWORK_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.ARTWORK_DERIVATE);
388 }
389 else if(derivedUnit.getKindOfUnit().equals(getLivingPlantPhotoTerm())){
390 return hasCharacterData?ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE);
391 }
392 else if(derivedUnit.getKindOfUnit().equals(getSpecimenScanTerm())){
393 return hasCharacterData?ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE);
394 }
395 else if(derivedUnit.getKindOfUnit().equals(getDetailImageTerm())){
396 return hasCharacterData?ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE);
397 }
398 }
399 }
400 }
401 else if(cdmBase.isInstanceOf(Sequence.class)){
402 return ImageResources.getImage(ImageResources.SEQUENCE_DERIVATE);
403 }
404
405 else if(cdmBase.isInstanceOf(SingleRead.class)){
406 if(multiLinkSingleReads!=null && multiLinkSingleReads.contains(element)){
407 return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE_MULTILINK);
408 }
409 else{
410 return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE);
411 }
412 }
413 }
414 return super.getImage(element);
415 }
416
417 public static Identifier<DnaSample> getCurrentSampleDesignation(CdmBase entity) {
418 if(entity.isInstanceOf(DnaSample.class)){
419 DnaSample dnaSample = HibernateProxyHelper.deproxy(entity, DnaSample.class);
420 for (Identifier<DnaSample> identifier : dnaSample.getIdentifiers()) {
421 if(identifier.getType()!=null && identifier.getType().equals(DerivateLabelProvider.getSampleDesignationTerm())){
422 //first sample designation is the current
423 return identifier;
424 }
425 }
426 }
427 else if(entity.isInstanceOf(Sequence.class)){
428 Sequence sequence = HibernateProxyHelper.deproxy(entity, Sequence.class);
429 if(sequence.getDnaSample()!=null){
430 return getCurrentSampleDesignation(sequence.getDnaSample());
431 }
432 }
433 return null;
434 }
435
436 /**
437 * Refreshes cached label extensions
438 * @param multiLinkSingleReads
439 */
440 public void refresh() {
441 DerivateLabelProvider.multiLinkSingleReads = new HashSet<SingleRead>();
442 for(Entry<SingleRead, Collection<Sequence>> entry:CdmStore.getService(ISequenceService.class).getSingleReadSequencesMap().entrySet()){
443 if(entry.getValue().size()>1){
444 multiLinkSingleReads.add(entry.getKey());
445 }
446 }
447 DerivateLabelProvider.typeDesignations = new HashMap<DerivedUnit, List<SpecimenTypeDesignation>>();
448 DerivateLabelProvider.determinationEvents = new HashMap<DerivedUnit, List<DeterminationEvent>>();
449 }
450
451 private static void addDeterminationEvent(DerivedUnit derivedUnit, DeterminationEvent determinationEvent){
452 List<DeterminationEvent> list = determinationEvents.get(derivedUnit);
453 if(list==null){
454 list = new ArrayList<DeterminationEvent>();
455 }
456 list.add(determinationEvent);
457 determinationEvents.put(derivedUnit, list);
458 }
459
460 private static void addTypeDesignation(DerivedUnit derivedUnit, SpecimenTypeDesignation typeDesignation){
461 List<SpecimenTypeDesignation> list = typeDesignations.get(derivedUnit);
462 if(list==null){
463 list = new ArrayList<SpecimenTypeDesignation>();
464 }
465 list.add(typeDesignation);
466 typeDesignations.put(derivedUnit, list);
467 }
468
469 public static Set<SingleRead> getMultiLinkSingleReads() {
470 return multiLinkSingleReads;
471 }
472
473 }