98bf707de264f74204bc3354cc7d25c6e33f030c
[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.List;
13 import java.util.Set;
14 import java.util.UUID;
15
16 import org.eclipse.jface.viewers.ColumnLabelProvider;
17 import org.eclipse.jface.viewers.TreeNode;
18 import org.eclipse.swt.graphics.Image;
19 import org.hibernate.LazyInitializationException;
20
21 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
22 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
23 import eu.etaxonomy.cdm.model.common.CdmBase;
24 import eu.etaxonomy.cdm.model.common.DefinedTerm;
25 import eu.etaxonomy.cdm.model.common.Identifier;
26 import eu.etaxonomy.cdm.model.molecular.DnaSample;
27 import eu.etaxonomy.cdm.model.molecular.Sequence;
28 import eu.etaxonomy.cdm.model.molecular.SingleRead;
29 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
30 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
31 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
32 import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
33 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
34 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
35 import eu.etaxonomy.taxeditor.model.ImageResources;
36 import eu.etaxonomy.taxeditor.store.CdmStore;
37
38 /**
39 * Label provider for the views to show {@link SpecimenOrObservationBase}s.<br>
40 * <br>
41 * <b>Note:</b> If you use this label provider you need to assure that you
42 * created a {@link ConversationHolder} resp. have an open session because
43 * the labels are generated from various fields of the derivate hierarchy which
44 * are lazy loaded and could therefore throw a {@link LazyInitializationException}.<br>
45 * Use <b>{@link #setConversation(ConversationHolder)}</b> to assign the session to this provider.
46 */
47 public class DerivateLabelProvider extends ColumnLabelProvider {
48
49 private static final String NO_SAMPLE_DESIGNATION = "[no sample designation]";
50
51 private Set<SingleRead> multiLinkSingleReads;
52
53 private ConversationHolder conversation;
54
55 private static DefinedTerm photoTerm = null;
56 private static DefinedTerm drawingTerm = null;
57 private static DefinedTerm specimenScanTerm = null;
58 private static DefinedTerm detailImageTerm = null;
59 private static DefinedTerm sampleDesignationTerm = null;
60
61 //FIXME: move static term getters to new singleton utility class
62 private static void initializeTerms() {
63 List<DefinedTerm> preferredTerms = CdmStore.getTermManager().getPreferredTerms(DefinedTerm.class);
64 for (DefinedTerm definedTerm : preferredTerms) {
65 if(definedTerm.getUuid().equals(UUID.fromString("c5c59c42-f254-471e-96c6-09f459f7c903"))){
66 photoTerm = definedTerm;
67 }
68 else if(definedTerm.getUuid().equals(UUID.fromString("669b0409-4aa4-4695-aae4-a95ed27bad4c"))){
69 drawingTerm = definedTerm;
70 }
71 else if(definedTerm.getUuid().equals(UUID.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03"))){
72 specimenScanTerm = definedTerm;
73 }
74 else if(definedTerm.getUuid().equals(UUID.fromString("31eb8d02-bf5d-437c-bcc6-87a626445f34"))){
75 detailImageTerm = definedTerm;
76 }
77 else if(definedTerm.getUuid().equals(UUID.fromString("fadeba12-1be3-4bc7-9ff5-361b088d86fc"))){
78 sampleDesignationTerm = definedTerm;
79 }
80 }
81 }
82
83 public static DefinedTerm getLivingPlantPhotoTerm(){
84 if(photoTerm==null){
85 initializeTerms();
86 }
87 return photoTerm;
88 }
89
90 public static DefinedTerm getArtworkTerm(){
91 if(drawingTerm==null){
92 initializeTerms();
93 }
94 return drawingTerm;
95 }
96
97 public static DefinedTerm getSpecimenScanTerm(){
98 if(specimenScanTerm==null){
99 initializeTerms();
100 }
101 return specimenScanTerm;
102 }
103
104 public static DefinedTerm getDetailImageTerm(){
105 if(detailImageTerm==null){
106 initializeTerms();
107 }
108 return detailImageTerm;
109 }
110
111 public static DefinedTerm getSampleDesignationTerm(){
112 if(sampleDesignationTerm==null){
113 initializeTerms();
114 }
115 return sampleDesignationTerm;
116 }
117
118
119 /** {@inheritDoc} */
120 @Override
121 public String getText(Object element) {
122 return getDerivateText(element);
123 }
124
125 /** {@inheritDoc} */
126 @Override
127 public String getToolTipText(Object element) {
128 return getDerivateText(element);
129 }
130
131 /**
132 * @param conversation the conversation to set
133 */
134 public void setConversation(ConversationHolder conversation) {
135 this.conversation = conversation;
136 }
137
138 public String getDerivateText(Object element){
139 return getDerivateText(element, conversation);
140 }
141
142 public static String getDerivateText(Object element, ConversationHolder conversation){
143 //TODO: use list of strings to assemble labels to avoid adding the separator every time and to allow null values
144 TreeNode parentNode = null;
145 TreeNode node = null;
146 Object derivate = element;
147 if(element instanceof TreeNode){
148 node = (TreeNode) element;
149 parentNode = node.getParent();
150 //unwrap specimen from TreeNode
151 derivate = node.getValue();
152 }
153
154 conversation.bind();
155
156 final String emptyString = "";
157 final String separator = " ";
158
159 String label = emptyString;
160
161 //Field Unit
162 if(derivate instanceof FieldUnit){
163 FieldUnit fieldUnit = (FieldUnit)derivate;
164 if(fieldUnit.getGatheringEvent()!=null){
165 GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
166 label += gatheringEvent.getCountry()!=null?gatheringEvent.getCountry().getLabel()+separator:emptyString;
167 label += gatheringEvent.getLocality()!=null?gatheringEvent.getLocality().getText()+separator:emptyString;
168 label += gatheringEvent.getGatheringDate()!=null?gatheringEvent.getGatheringDate()+separator:emptyString;
169 label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
170 }
171 label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber():emptyString;
172 }
173 //MediaSpecimen
174 else if(derivate instanceof MediaSpecimen){
175 MediaSpecimen mediaSpecimen = (MediaSpecimen)derivate;
176 if(mediaSpecimen.getMediaSpecimen()!=null){
177 label += mediaSpecimen.getMediaSpecimen().getTitle()!=null?mediaSpecimen.getMediaSpecimen().getTitle().getText()+separator:"[no motif]";
178 label += mediaSpecimen.getMediaSpecimen().getArtist()!=null?mediaSpecimen.getMediaSpecimen().getArtist()+separator:emptyString;
179 }
180 eu.etaxonomy.cdm.model.occurrence.Collection collection = mediaSpecimen.getCollection();
181 if(collection!=null){
182 label += collection.getName()!=null?collection.getName()+" ":emptyString;
183 label += collection.getCode()!=null?"("+collection.getCode()+")"+separator:emptyString;
184 }
185 label += mediaSpecimen.getAccessionNumber()!=null?mediaSpecimen.getAccessionNumber()+separator:emptyString;
186 label += mediaSpecimen.getBarcode()!=null?mediaSpecimen.getBarcode()+separator:emptyString;
187 }
188 //TissueSample + DnaSample
189 else if(derivate instanceof DnaSample){
190 DnaSample dnaSample = (DnaSample)derivate;
191 if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.DnaSample){
192 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
193 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
194 label += currentSampleDesignation.getIdentifier()+separator;
195 }
196 else{
197 label += NO_SAMPLE_DESIGNATION+separator;
198 }
199 }
200 else if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.TissueSample){
201 if(dnaSample.getKindOfUnit()!=null){
202 label += dnaSample.getKindOfUnit()+separator;
203 }
204 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
205 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
206 label += currentSampleDesignation.getIdentifier()+separator;
207 }
208 else{
209 label += NO_SAMPLE_DESIGNATION+separator;
210 }
211 }
212
213 }
214 //DerivedUnit + TissueSample
215 else if(derivate instanceof DerivedUnit){
216 DerivedUnit derivedUnit = (DerivedUnit)derivate;
217 if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
218 //java.util.Collection<FieldUnit> fieldUnits = CdmStore.getService(IOccurrenceService.class).getFieldUnits(derivedUnit.getUuid());
219 //TODO : This is not generic anymore for performance reasons
220 Set<SpecimenOrObservationBase> originals = derivedUnit.getOriginals();
221 if(originals.size() ==1) {
222 SpecimenOrObservationBase specimen = originals.iterator().next();
223 if(specimen instanceof FieldUnit) {
224 FieldUnit fieldUnit = (FieldUnit)specimen;
225 GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
226 if(gatheringEvent!=null){
227 label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
228 }
229 label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber()+separator:emptyString;
230 }
231 }
232
233 eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnit.getCollection();
234 if(collection!=null){
235 label += collection.getCode()!=null?"("+collection.getCode()+")"+separator:emptyString;
236 }
237 String mostSignificantIdentifier = derivedUnit.getMostSignificantIdentifier();
238 label += mostSignificantIdentifier!=null?mostSignificantIdentifier+separator:emptyString;
239 }
240 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
241 //TissueSample should only be created by using it's own class
242 //in future using only one class with different SpecimenOrObservationTypes is desired
243 // label += derivedUnit.getKindOfUnit() + NO_SAMPLE_DESIGNATION;
244 }
245 }
246 //Sequence
247 else if(derivate instanceof Sequence){
248 Sequence sequence = (Sequence)derivate;
249 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
250 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
251 label += currentSampleDesignation.getIdentifier()+separator;
252 }
253 else{
254 label += NO_SAMPLE_DESIGNATION+separator;
255 }
256 label += sequence.getDnaMarker()!=null?sequence.getDnaMarker():emptyString;
257 }
258 //SingleRead
259 else if(derivate instanceof SingleRead){
260 SingleRead singleRead = (SingleRead)derivate;
261 if(parentNode!=null && parentNode.getValue() instanceof Sequence){
262 Sequence sequence = (Sequence) parentNode.getValue();
263 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
264 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
265 label = currentSampleDesignation.getIdentifier()+separator;
266 }
267 else{
268 label += NO_SAMPLE_DESIGNATION+separator;
269 }
270 label += singleRead.getPrimer()!=null?singleRead.getPrimer().getLabel()+separator:emptyString;
271 if(sequence!=null && sequence.getDnaMarker()!=null){
272 label += sequence.getDnaMarker()+separator;
273 }
274 if(singleRead.getAmplificationResult()!=null && singleRead.getAmplificationResult().getAmplification()!=null){
275 label += singleRead.getAmplificationResult().getAmplification().getLabelCache()+separator;
276 }
277 }
278 }
279 //SOOB
280 else if(derivate instanceof SpecimenOrObservationBase){
281 SpecimenOrObservationBase<?> specimen = (SpecimenOrObservationBase<?>) derivate;
282 SpecimenOrObservationType type = specimen.getRecordBasis();
283 return specimen.getTitleCache() + (type!=null?" ["+type.toString()+"]":emptyString);
284 }
285 if(label.isEmpty()){
286 label = derivate.toString();
287 }
288 //remove last comma
289 else if(label.endsWith(separator)){
290 label = label.substring(0, label.length()-separator.length());
291 }
292 return label;
293 }
294
295 /* (non-Javadoc)
296 * @see org.eclipse.jface.viewers.ColumnLabelProvider#getImage(java.lang.Object)
297 */
298 @Override
299 public Image getImage(Object element) {
300 if(element instanceof TreeNode){
301 element = ((TreeNode) element).getValue();
302 }
303 if(element instanceof CdmBase){
304 CdmBase cdmBase = (CdmBase)element;
305 boolean hasCharacterData = false;
306 if(cdmBase.isInstanceOf(SpecimenOrObservationBase.class)){
307 SpecimenOrObservationBase<?> specimen = HibernateProxyHelper.deproxy(cdmBase, SpecimenOrObservationBase.class);
308 if(specimen.hasCharacterData()){
309 hasCharacterData = true;
310 }
311 }
312 if(cdmBase.isInstanceOf(FieldUnit.class)){
313 return hasCharacterData?ImageResources.getImage(ImageResources.FIELD_UNIT_CHARACTER_DATA):ImageResources.getImage(ImageResources.FIELD_UNIT);
314 }
315 else if(cdmBase.isInstanceOf(DerivedUnit.class)){
316 DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(element, DerivedUnit.class);
317 if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.FieldUnit){
318 return hasCharacterData?ImageResources.getImage(ImageResources.FIELD_UNIT_CHARACTER_DATA):ImageResources.getImage(ImageResources.FIELD_UNIT);
319 }
320 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.DnaSample){
321 return hasCharacterData?ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE);
322 }
323 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
324 return hasCharacterData?ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE);
325 }
326 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
327 return hasCharacterData?ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE);
328 }
329 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.Media){
330 if(derivedUnit.getKindOfUnit()!=null){
331 if(derivedUnit.getKindOfUnit().equals(getArtworkTerm())){
332 return hasCharacterData?ImageResources.getImage(ImageResources.ARTWORK_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.ARTWORK_DERIVATE);
333 }
334 else if(derivedUnit.getKindOfUnit().equals(getLivingPlantPhotoTerm())){
335 return hasCharacterData?ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE);
336 }
337 else if(derivedUnit.getKindOfUnit().equals(getSpecimenScanTerm())){
338 return hasCharacterData?ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE);
339 }
340 else if(derivedUnit.getKindOfUnit().equals(getDetailImageTerm())){
341 return hasCharacterData?ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE_CHARACTER_DATA):ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE);
342 }
343 }
344 }
345 }
346 else if(cdmBase.isInstanceOf(Sequence.class)){
347 return ImageResources.getImage(ImageResources.SEQUENCE_DERIVATE);
348 }
349
350 else if(cdmBase.isInstanceOf(SingleRead.class)){
351 if(multiLinkSingleReads.contains(element)){
352 return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE_MULTILINK);
353 }
354 else{
355 return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE);
356 }
357 }
358 }
359 return super.getImage(element);
360 }
361
362 public static Identifier<DnaSample> getCurrentSampleDesignation(CdmBase entity) {
363 if(entity.isInstanceOf(DnaSample.class)){
364 DnaSample dnaSample = HibernateProxyHelper.deproxy(entity, DnaSample.class);
365 for (Identifier<DnaSample> identifier : dnaSample.getIdentifiers()) {
366 if(identifier.getType()!=null && identifier.getType().equals(DerivateLabelProvider.getSampleDesignationTerm())){
367 //first sample designation is the current
368 return identifier;
369 }
370 }
371 }
372 else if(entity.isInstanceOf(Sequence.class)){
373 Sequence sequence = HibernateProxyHelper.deproxy(entity, Sequence.class);
374 if(sequence.getDnaSample()!=null){
375 return getCurrentSampleDesignation(sequence.getDnaSample());
376 }
377 }
378 return null;
379 }
380
381 /**
382 * @param multiLinkSingleReads2
383 */
384 public void setMultiLinkSingleReads(Set<SingleRead> multiLinkSingleReads) {
385 this.multiLinkSingleReads = multiLinkSingleReads;
386 }
387
388 }