540748a0e23b39be4684ad14a169f44e905503ed
[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.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;
49
50 /**
51 * Label provider for the views to show {@link SpecimenOrObservationBase}s.<br>
52 * <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.
58 */
59 public class DerivateLabelProvider extends ColumnLabelProvider {
60
61 private static final String NO_SAMPLE_DESIGNATION = "[no sample designation]";
62
63 private static Set<SingleRead> multiLinkSingleReads;
64
65 private static Map<DerivedUnit, Collection<SpecimenTypeDesignation>> typeDesignations;
66
67 private ConversationHolder conversation;
68
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;
74
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;
81 }
82 else if(definedTerm.getUuid().equals(UUID.fromString("669b0409-4aa4-4695-aae4-a95ed27bad4c"))){
83 drawingTerm = definedTerm;
84 }
85 else if(definedTerm.getUuid().equals(UUID.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03"))){
86 specimenScanTerm = definedTerm;
87 }
88 else if(definedTerm.getUuid().equals(UUID.fromString("31eb8d02-bf5d-437c-bcc6-87a626445f34"))){
89 detailImageTerm = definedTerm;
90 }
91 else if(definedTerm.getUuid().equals(UUID.fromString("fadeba12-1be3-4bc7-9ff5-361b088d86fc"))){
92 sampleDesignationTerm = definedTerm;
93 }
94 }
95 }
96
97 public static DefinedTerm getLivingPlantPhotoTerm(){
98 if(photoTerm==null){
99 initializeTerms();
100 }
101 return photoTerm;
102 }
103
104 public static DefinedTerm getArtworkTerm(){
105 if(drawingTerm==null){
106 initializeTerms();
107 }
108 return drawingTerm;
109 }
110
111 public static DefinedTerm getSpecimenScanTerm(){
112 if(specimenScanTerm==null){
113 initializeTerms();
114 }
115 return specimenScanTerm;
116 }
117
118 public static DefinedTerm getDetailImageTerm(){
119 if(detailImageTerm==null){
120 initializeTerms();
121 }
122 return detailImageTerm;
123 }
124
125 public static DefinedTerm getSampleDesignationTerm(){
126 if(sampleDesignationTerm==null){
127 initializeTerms();
128 }
129 return sampleDesignationTerm;
130 }
131
132
133 /** {@inheritDoc} */
134 @Override
135 public String getText(Object element) {
136 if(element instanceof TreeNode){
137 element = ((TreeNode) element).getValue();
138 }
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
154 };
155 return CdmFormatterFactory.format(element, formatKeys);
156 }
157
158 /** {@inheritDoc} */
159 @Override
160 public String getToolTipText(Object element) {
161 return getDerivateText(element);
162 }
163
164 /**
165 * @param conversation the conversation to set
166 */
167 public void setConversation(ConversationHolder conversation) {
168 this.conversation = conversation;
169 }
170
171 public String getDerivateText(Object element){
172 return getDerivateText(element, conversation);
173 }
174
175 public static String getDerivateText(Object element, ConversationHolder conversation){
176 //TODO: use list of strings to assemble labels to avoid adding the separator every time and to allow null values
177 TreeNode parentNode = null;
178 TreeNode node = null;
179 Object derivate = element;
180 if(element instanceof TreeNode){
181 node = (TreeNode) element;
182 parentNode = node.getParent();
183 //unwrap specimen from TreeNode
184 derivate = node.getValue();
185 }
186
187 if(conversation!=null){
188 conversation.bind();
189 }
190
191 final String emptyString = "";
192 final String separator = " ";
193
194 String label = emptyString;
195
196 //Field Unit
197 if(derivate instanceof FieldUnit){
198 FieldUnit fieldUnit = (FieldUnit)derivate;
199 if(fieldUnit.getGatheringEvent()!=null){
200 GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
201 label += gatheringEvent.getCountry()!=null?gatheringEvent.getCountry().getLabel()+separator:emptyString;
202 label += gatheringEvent.getLocality()!=null?gatheringEvent.getLocality().getText()+separator:emptyString;
203 label += gatheringEvent.getGatheringDate()!=null?gatheringEvent.getGatheringDate()+separator:emptyString;
204 label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
205 }
206 label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber():emptyString;
207 }
208 //MediaSpecimen
209 else if(derivate instanceof MediaSpecimen){
210 MediaSpecimen mediaSpecimen = (MediaSpecimen)derivate;
211 if(mediaSpecimen.getMediaSpecimen()!=null){
212 label += mediaSpecimen.getMediaSpecimen().getTitle()!=null?mediaSpecimen.getMediaSpecimen().getTitle().getText()+separator:"[no motif]";
213 label += mediaSpecimen.getMediaSpecimen().getArtist()!=null?mediaSpecimen.getMediaSpecimen().getArtist()+separator:emptyString;
214 }
215 eu.etaxonomy.cdm.model.occurrence.Collection collection = mediaSpecimen.getCollection();
216 if(collection!=null){
217 label += collection.getName()!=null?collection.getName()+" ":emptyString;
218 label += collection.getCode()!=null?"("+collection.getCode()+")"+separator:emptyString;
219 }
220 label += mediaSpecimen.getAccessionNumber()!=null?mediaSpecimen.getAccessionNumber()+separator:emptyString;
221 label += mediaSpecimen.getBarcode()!=null?mediaSpecimen.getBarcode()+separator:emptyString;
222 }
223 //TissueSample + DnaSample
224 else if(derivate instanceof DnaSample){
225 DnaSample dnaSample = (DnaSample)derivate;
226 if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.DnaSample){
227 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
228 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
229 label += currentSampleDesignation.getIdentifier()+separator;
230 }
231 else{
232 label += NO_SAMPLE_DESIGNATION+separator;
233 }
234 }
235 else if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.TissueSample){
236 if(dnaSample.getKindOfUnit()!=null){
237 label += dnaSample.getKindOfUnit()+separator;
238 }
239 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
240 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
241 label += currentSampleDesignation.getIdentifier()+separator;
242 }
243 else{
244 label += NO_SAMPLE_DESIGNATION+separator;
245 }
246 }
247
248 }
249 //DerivedUnit + TissueSample
250 else if(derivate instanceof DerivedUnit){
251 DerivedUnit derivedUnit = (DerivedUnit)derivate;
252 if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
253 //check for type designation
254 if(typeDesignations.get(derivedUnit)==null){
255 for (SpecimenTypeDesignation specimenTypeDesignation : CdmStore.getService(IOccurrenceService.class).listTypeDesignations(derivedUnit, null, null, null, null)) {
256 addTypeDesignation(derivedUnit, specimenTypeDesignation);
257 }
258 }
259 //java.util.Collection<FieldUnit> fieldUnits = CdmStore.getService(IOccurrenceService.class).getFieldUnits(derivedUnit.getUuid());
260 //TODO : This is not generic anymore for performance reasons
261 Set<SpecimenOrObservationBase> originals = derivedUnit.getOriginals();
262 if(originals!=null && originals.size() ==1) {
263 SpecimenOrObservationBase specimen = originals.iterator().next();
264 if(specimen instanceof FieldUnit) {
265 FieldUnit fieldUnit = (FieldUnit)specimen;
266 GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
267 if(gatheringEvent!=null){
268 label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
269 }
270 label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber()+separator:emptyString;
271 }
272 }
273
274 eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnit.getCollection();
275 if(collection!=null){
276 label += collection.getCode()!=null?"("+collection.getCode()+")"+separator:emptyString;
277 }
278 String mostSignificantIdentifier = derivedUnit.getMostSignificantIdentifier();
279 label += mostSignificantIdentifier!=null?mostSignificantIdentifier+separator:emptyString;
280 }
281 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
282 //TissueSample should only be created by using it's own class
283 //in future using only one class with different SpecimenOrObservationTypes is desired
284 // label += derivedUnit.getKindOfUnit() + NO_SAMPLE_DESIGNATION;
285 }
286 }
287 //Sequence
288 else if(derivate instanceof Sequence){
289 Sequence sequence = (Sequence)derivate;
290 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
291 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
292 label += currentSampleDesignation.getIdentifier()+separator;
293 }
294 else{
295 label += NO_SAMPLE_DESIGNATION+separator;
296 }
297 label += sequence.getDnaMarker()!=null?sequence.getDnaMarker():emptyString;
298 }
299 //SingleRead
300 else if(derivate instanceof SingleRead){
301 SingleRead singleRead = (SingleRead)derivate;
302 if(parentNode!=null && parentNode.getValue() instanceof Sequence){
303 Sequence sequence = (Sequence) parentNode.getValue();
304 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
305 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
306 label = currentSampleDesignation.getIdentifier()+separator;
307 }
308 else{
309 label += NO_SAMPLE_DESIGNATION+separator;
310 }
311 label += singleRead.getPrimer()!=null?singleRead.getPrimer().getLabel()+separator:emptyString;
312 if(sequence!=null && sequence.getDnaMarker()!=null){
313 label += sequence.getDnaMarker()+separator;
314 }
315 if(singleRead.getAmplificationResult()!=null && singleRead.getAmplificationResult().getAmplification()!=null){
316 label += singleRead.getAmplificationResult().getAmplification().getLabelCache()+separator;
317 }
318 }
319 }
320 //SOOB
321 else if(derivate instanceof SpecimenOrObservationBase){
322 SpecimenOrObservationBase<?> specimen = (SpecimenOrObservationBase<?>) derivate;
323 SpecimenOrObservationType type = specimen.getRecordBasis();
324 return specimen.getTitleCache() + (type!=null?" ["+type.toString()+"]":emptyString);
325 }
326 if(label.isEmpty()){
327 label = derivate.toString();
328 }
329 //remove last comma
330 else if(label.endsWith(separator)){
331 label = label.substring(0, label.length()-separator.length());
332 }
333 return label;
334 }
335
336 @Override
337 public Image getImage(Object element) {
338 if(element instanceof TreeNode){
339 element = ((TreeNode) element).getValue();
340 }
341 if(element instanceof CdmBase){
342 CdmBase cdmBase = (CdmBase)element;
343 boolean hasCharacterData = false;
344 if(cdmBase.isInstanceOf(SpecimenOrObservationBase.class)){
345 SpecimenOrObservationBase<?> specimen = HibernateProxyHelper.deproxy(cdmBase, SpecimenOrObservationBase.class);
346 if(specimen.hasCharacterData()){
347 hasCharacterData = true;
348 }
349 }
350 if(cdmBase.isInstanceOf(FieldUnit.class)){
351 return hasCharacterData?ImageResources.getImage(ImageResources.FIELD_UNIT_CHARACTER_DATA):ImageResources.getImage(ImageResources.FIELD_UNIT);
352 }
353 else if(cdmBase.isInstanceOf(DerivedUnit.class)){
354 DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(element, DerivedUnit.class);
355
356 if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.FieldUnit){
357 return hasCharacterData?ImageResources.getImage(ImageResources.FIELD_UNIT_CHARACTER_DATA):ImageResources.getImage(ImageResources.FIELD_UNIT);
358 }
359 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.DnaSample){
360 return hasCharacterData?ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE);
361 }
362 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
363 return hasCharacterData?ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE);
364 }
365 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
366 if(typeDesignations.get(derivedUnit)!=null && !typeDesignations.get(derivedUnit).isEmpty()){
367 return ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE_TYPE);
368 }
369 return hasCharacterData?ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE);
370 }
371 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.Media){
372 if(derivedUnit.getKindOfUnit()!=null){
373 if(derivedUnit.getKindOfUnit().equals(getArtworkTerm())){
374 return hasCharacterData?ImageResources.getImage(ImageResources.ARTWORK_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.ARTWORK_DERIVATE);
375 }
376 else if(derivedUnit.getKindOfUnit().equals(getLivingPlantPhotoTerm())){
377 return hasCharacterData?ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE);
378 }
379 else if(derivedUnit.getKindOfUnit().equals(getSpecimenScanTerm())){
380 return hasCharacterData?ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE);
381 }
382 else if(derivedUnit.getKindOfUnit().equals(getDetailImageTerm())){
383 return hasCharacterData?ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE);
384 }
385 }
386 }
387 }
388 else if(cdmBase.isInstanceOf(Sequence.class)){
389 return ImageResources.getImage(ImageResources.SEQUENCE_DERIVATE);
390 }
391
392 else if(cdmBase.isInstanceOf(SingleRead.class)){
393 if(multiLinkSingleReads!=null && multiLinkSingleReads.contains(element)){
394 return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE_MULTILINK);
395 }
396 else{
397 return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE);
398 }
399 }
400 }
401 return super.getImage(element);
402 }
403
404 public static Identifier<DnaSample> getCurrentSampleDesignation(CdmBase entity) {
405 if(entity.isInstanceOf(DnaSample.class)){
406 DnaSample dnaSample = HibernateProxyHelper.deproxy(entity, DnaSample.class);
407 for (Identifier<DnaSample> identifier : dnaSample.getIdentifiers()) {
408 if(identifier.getType()!=null && identifier.getType().equals(DerivateLabelProvider.getSampleDesignationTerm())){
409 //first sample designation is the current
410 return identifier;
411 }
412 }
413 }
414 else if(entity.isInstanceOf(Sequence.class)){
415 Sequence sequence = HibernateProxyHelper.deproxy(entity, Sequence.class);
416 if(sequence.getDnaSample()!=null){
417 return getCurrentSampleDesignation(sequence.getDnaSample());
418 }
419 }
420 return null;
421 }
422
423 private static void addTypeDesignation(DerivedUnit derivedUnit, SpecimenTypeDesignation typeDesignation){
424 Collection<SpecimenTypeDesignation> list = typeDesignations.get(derivedUnit);
425 if(list==null){
426 list = new ArrayList<SpecimenTypeDesignation>();
427 }
428 list.add(typeDesignation);
429 typeDesignations.put(derivedUnit, list);
430 }
431
432 public static Set<SingleRead> getMultiLinkSingleReads() {
433 return multiLinkSingleReads;
434 }
435
436 public void updateLabelCache(Collection<SpecimenOrObservationBase<?>> rootElements) {
437 multiLinkSingleReads = new HashSet<SingleRead>();
438 typeDesignations = new HashMap<DerivedUnit, Collection<SpecimenTypeDesignation>>();
439 for(Entry<SingleRead, Collection<Sequence>> entry:CdmStore.getService(ISequenceService.class).getSingleReadSequencesMap().entrySet()){
440 if(entry.getValue().size()>1){
441 multiLinkSingleReads.add(entry.getKey());
442 }
443 }
444 if(rootElements!=null){
445 Collection<DerivedUnit> derivedUnits = new ArrayList<DerivedUnit>();
446 for (SpecimenOrObservationBase specimenOrObservationBase : rootElements) {
447 derivedUnits.addAll(CdmStore.getService(IOccurrenceService.class).getAllChildDerivatives(specimenOrObservationBase.getUuid()));
448 if(specimenOrObservationBase.isInstanceOf(DerivedUnit.class)){
449 derivedUnits.add(HibernateProxyHelper.deproxy(specimenOrObservationBase, DerivedUnit.class));
450 }
451 }
452 typeDesignations = CdmStore.getService(IOccurrenceService.class).listTypeDesignations(derivedUnits, null, null, null, null);
453 }
454 }
455
456 }