- extended DnaQuality details view
[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.api.service.IOccurrenceService;
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 private static void initializeTerms() {
62 List<DefinedTerm> preferredTerms = CdmStore.getTermManager().getPreferredTerms(DefinedTerm.class);
63 for (DefinedTerm definedTerm : preferredTerms) {
64 if(definedTerm.getUuid().equals(UUID.fromString("c5c59c42-f254-471e-96c6-09f459f7c903"))){
65 photoTerm = definedTerm;
66 }
67 else if(definedTerm.getUuid().equals(UUID.fromString("669b0409-4aa4-4695-aae4-a95ed27bad4c"))){
68 drawingTerm = definedTerm;
69 }
70 else if(definedTerm.getUuid().equals(UUID.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03"))){
71 specimenScanTerm = definedTerm;
72 }
73 else if(definedTerm.getUuid().equals(UUID.fromString("31eb8d02-bf5d-437c-bcc6-87a626445f34"))){
74 detailImageTerm = definedTerm;
75 }
76 else if(definedTerm.getUuid().equals(UUID.fromString("fadeba12-1be3-4bc7-9ff5-361b088d86fc"))){
77 sampleDesignationTerm = definedTerm;
78 }
79 }
80 }
81
82 public static DefinedTerm getLivingPlantPhotoTerm(){
83 if(photoTerm==null){
84 initializeTerms();
85 }
86 return photoTerm;
87 }
88
89 public static DefinedTerm getArtworkTerm(){
90 if(drawingTerm==null){
91 initializeTerms();
92 }
93 return drawingTerm;
94 }
95
96 public static DefinedTerm getSpecimenScanTerm(){
97 if(specimenScanTerm==null){
98 initializeTerms();
99 }
100 return specimenScanTerm;
101 }
102
103 public static DefinedTerm getDetailImageTerm(){
104 if(detailImageTerm==null){
105 initializeTerms();
106 }
107 return detailImageTerm;
108 }
109
110 public static DefinedTerm getSampleDesignationTerm(){
111 if(sampleDesignationTerm==null){
112 initializeTerms();
113 }
114 return sampleDesignationTerm;
115 }
116
117
118 /** {@inheritDoc} */
119 @Override
120 public String getText(Object element) {
121 return getDerivateText(element);
122 }
123
124 /** {@inheritDoc} */
125 @Override
126 public String getToolTipText(Object element) {
127 return getDerivateText(element);
128 }
129
130 /**
131 * @param conversation the conversation to set
132 */
133 public void setConversation(ConversationHolder conversation) {
134 this.conversation = conversation;
135 }
136
137 /**
138 *
139 * @param derivate
140 * @return
141 */
142 public String getDerivateText(Object element){
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 if(!conversation.getSession().contains(gatheringEvent)){
167 fieldUnit = (FieldUnit) CdmStore.getService(IOccurrenceService.class).load(fieldUnit.getUuid());
168 gatheringEvent = fieldUnit.getGatheringEvent();
169 }
170 label += gatheringEvent.getCountry()!=null?gatheringEvent.getCountry().getLabel()+separator:emptyString;
171 label += gatheringEvent.getLocality()!=null?gatheringEvent.getLocality().getText()+separator:emptyString;
172 label += gatheringEvent.getGatheringDate()!=null?gatheringEvent.getGatheringDate()+separator:emptyString;
173 label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
174 }
175 label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber():emptyString;
176 }
177 //MediaSpecimen
178 else if(derivate instanceof MediaSpecimen){
179 MediaSpecimen mediaSpecimen = (MediaSpecimen)derivate;
180 if(mediaSpecimen.getMediaSpecimen()!=null){
181 label += mediaSpecimen.getMediaSpecimen().getTitle()!=null?mediaSpecimen.getMediaSpecimen().getTitle().getText()+separator:"[no motif]";
182 label += mediaSpecimen.getMediaSpecimen().getArtist()!=null?mediaSpecimen.getMediaSpecimen().getArtist()+separator:emptyString;
183 }
184 eu.etaxonomy.cdm.model.occurrence.Collection collection = mediaSpecimen.getCollection();
185 if(collection!=null){
186 label += collection.getName()!=null?collection.getName()+" ":emptyString;
187 label += collection.getCode()!=null?"("+collection.getCode()+")"+separator:emptyString;
188 }
189 label += mediaSpecimen.getAccessionNumber()!=null?mediaSpecimen.getAccessionNumber()+separator:emptyString;
190 }
191 //TissueSample + DnaSample
192 else if(derivate instanceof DnaSample){
193 DnaSample dnaSample = (DnaSample)derivate;
194 if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.DnaSample){
195 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
196 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
197 label += currentSampleDesignation.getIdentifier()+separator;
198 }
199 else{
200 label += NO_SAMPLE_DESIGNATION+separator;
201 }
202 }
203 else if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.TissueSample){
204 label += dnaSample.getKindOfUnit()+separator;
205 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
206 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
207 label += currentSampleDesignation.getIdentifier()+separator;
208 }
209 else{
210 label += NO_SAMPLE_DESIGNATION+separator;
211 }
212 }
213
214 }
215 //DerivedUnit + TissueSample
216 else if(derivate instanceof DerivedUnit){
217 DerivedUnit derivedUnit = (DerivedUnit)derivate;
218 if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
219 java.util.Collection<FieldUnit> fieldUnits = CdmStore.getService(IOccurrenceService.class).getFieldUnits(derivedUnit.getUuid());
220 if(fieldUnits!=null && !fieldUnits.isEmpty()){
221 FieldUnit fieldUnit = fieldUnits.iterator().next();
222 GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
223 // gatheringEvent = HibernateProxyHelper.deproxy(gatheringEvent, GatheringEvent.class);
224 if(gatheringEvent!=null){
225 label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
226 }
227 label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber()+separator:emptyString;
228 }
229 eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnit.getCollection();
230 if(collection!=null){
231 label += collection.getName()!=null?collection.getName()+" ":emptyString;
232 label += collection.getCode()!=null?"("+collection.getCode()+"), ":emptyString;
233 }
234 label += derivedUnit.getAccessionNumber()!=null?derivedUnit.getAccessionNumber()+separator:emptyString;
235 label += derivedUnit.getBarcode()!=null?derivedUnit.getBarcode():emptyString;
236 }
237 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
238 label += derivedUnit.getKindOfUnit() + NO_SAMPLE_DESIGNATION;
239 }
240 }
241 //Sequence
242 else if(derivate instanceof Sequence){
243 Sequence sequence = (Sequence)derivate;
244 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
245 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
246 label += currentSampleDesignation.getIdentifier()+separator;
247 }
248 else{
249 label += NO_SAMPLE_DESIGNATION+separator;
250 }
251 label += sequence.getDnaMarker()!=null?sequence.getDnaMarker():emptyString;
252 }
253 //SingleRead
254 else if(derivate instanceof SingleRead){
255 SingleRead singleRead = (SingleRead)derivate;
256 if(parentNode!=null && parentNode.getValue() instanceof Sequence){
257 Sequence sequence = (Sequence) parentNode.getValue();
258 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
259 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
260 label = currentSampleDesignation.getIdentifier()+separator;
261 }
262 else{
263 label += NO_SAMPLE_DESIGNATION+separator;
264 }
265 label += singleRead.getPrimer()!=null?singleRead.getPrimer().getLabel()+separator:emptyString;
266 if(sequence!=null){
267 label += sequence.getDnaMarker()!=null?sequence.getDnaMarker():emptyString;
268 }
269 }
270 }
271 //SOOB
272 else if(derivate instanceof SpecimenOrObservationBase){
273 SpecimenOrObservationBase<?> specimen = (SpecimenOrObservationBase<?>) derivate;
274 SpecimenOrObservationType type = specimen.getRecordBasis();
275 return specimen.getTitleCache() + (type!=null?" ["+type.toString()+"]":emptyString);
276 }
277 if(label.isEmpty()){
278 label = derivate.toString();
279 }
280 //remove last comma
281 else if(label.endsWith(separator)){
282 label = label.substring(0, label.length()-separator.length());
283 }
284 return label;
285 }
286
287 /* (non-Javadoc)
288 * @see org.eclipse.jface.viewers.ColumnLabelProvider#getImage(java.lang.Object)
289 */
290 @Override
291 public Image getImage(Object element) {
292 if(element instanceof TreeNode){
293 element = ((TreeNode) element).getValue();
294 }
295 if(element instanceof FieldUnit){
296 return ImageResources.getImage(ImageResources.FIELD_UNIT);
297 }
298 else if(element instanceof DerivedUnit){
299 DerivedUnit derivedUnit = (DerivedUnit)element;
300 if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.FieldUnit){
301 return ImageResources.getImage(ImageResources.FIELD_UNIT);
302 }
303 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.DnaSample){
304 return ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE);
305 }
306 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
307 return ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE);
308 }
309 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
310 return ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE);
311 }
312 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.Media){
313 if(derivedUnit.getKindOfUnit()!=null){
314 if(derivedUnit.getKindOfUnit().equals(getArtworkTerm())){
315 return ImageResources.getImage(ImageResources.ARTWORK_DERIVATE);
316 }
317 else if(derivedUnit.getKindOfUnit().equals(getLivingPlantPhotoTerm())){
318 return ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE);
319 }
320 else if(derivedUnit.getKindOfUnit().equals(getSpecimenScanTerm())){
321 return ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE);
322 }
323 else if(derivedUnit.getKindOfUnit().equals(getDetailImageTerm())){
324 return ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE);
325 }
326 }
327 }
328 }
329 else if(element instanceof Sequence){
330 return ImageResources.getImage(ImageResources.SEQUENCE_DERIVATE);
331 }
332
333 else if(element instanceof SingleRead){
334 if(multiLinkSingleReads.contains(element)){
335 return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE_MULTILINK);
336 }
337 else{
338 return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE);
339 }
340 }
341 return super.getImage(element);
342 }
343
344 public static Identifier<DnaSample> getCurrentSampleDesignation(CdmBase entity) {
345 if(entity instanceof DnaSample){
346 DnaSample dnaSample = (DnaSample)entity;
347 for (Identifier<DnaSample> identifier : dnaSample.getIdentifiers()) {
348 if(identifier.getType()!=null && identifier.getType().equals(DerivateLabelProvider.getSampleDesignationTerm())){
349 //first sample designation is the current
350 return identifier;
351 }
352 }
353 }
354 else if(entity instanceof Sequence){
355 Sequence sequence = (Sequence)entity;
356 if(sequence.getDnaSample()!=null){
357 return getCurrentSampleDesignation(sequence.getDnaSample());
358 }
359 }
360 return null;
361 }
362
363 /**
364 * @param multiLinkSingleReads2
365 */
366 public void setMultiLinkSingleReads(Set<SingleRead> multiLinkSingleReads) {
367 this.multiLinkSingleReads = multiLinkSingleReads;
368 }
369
370 }