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