- update label cache
[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 //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 if(CdmStore.getService(IOccurrenceService.class).exists(fieldUnit.getUuid()) && !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 label += mediaSpecimen.getBarcode()!=null?mediaSpecimen.getBarcode()+separator:emptyString;
191 }
192 //TissueSample + DnaSample
193 else if(derivate instanceof DnaSample){
194 DnaSample dnaSample = (DnaSample)derivate;
195 if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.DnaSample){
196 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
197 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
198 label += currentSampleDesignation.getIdentifier()+separator;
199 }
200 else{
201 label += NO_SAMPLE_DESIGNATION+separator;
202 }
203 }
204 else if(((DnaSample) derivate).getRecordBasis()==SpecimenOrObservationType.TissueSample){
205 label += dnaSample.getKindOfUnit()+separator;
206 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(dnaSample);
207 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
208 label += currentSampleDesignation.getIdentifier()+separator;
209 }
210 else{
211 label += NO_SAMPLE_DESIGNATION+separator;
212 }
213 }
214
215 }
216 //DerivedUnit + TissueSample
217 else if(derivate instanceof DerivedUnit){
218 DerivedUnit derivedUnit = (DerivedUnit)derivate;
219 if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
220 java.util.Collection<FieldUnit> fieldUnits = CdmStore.getService(IOccurrenceService.class).getFieldUnits(derivedUnit.getUuid());
221 if(fieldUnits!=null && !fieldUnits.isEmpty()){
222 FieldUnit fieldUnit = fieldUnits.iterator().next();
223 GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
224 // gatheringEvent = HibernateProxyHelper.deproxy(gatheringEvent, GatheringEvent.class);
225 if(gatheringEvent!=null){
226 label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+separator:emptyString;
227 }
228 label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber()+separator:emptyString;
229 }
230 eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnit.getCollection();
231 if(collection!=null){
232 label += collection.getName()!=null?collection.getName()+" ":emptyString;
233 label += collection.getCode()!=null?"("+collection.getCode()+"), ":emptyString;
234 }
235 label += derivedUnit.getAccessionNumber()!=null?derivedUnit.getAccessionNumber()+separator:emptyString;
236 label += derivedUnit.getBarcode()!=null?derivedUnit.getBarcode():emptyString;
237 }
238 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
239 //TissueSample should only be created by using it's own class
240 //in future using only one class with different SpecimenOrObservationTypes is desired
241 // label += derivedUnit.getKindOfUnit() + NO_SAMPLE_DESIGNATION;
242 }
243 }
244 //Sequence
245 else if(derivate instanceof Sequence){
246 Sequence sequence = (Sequence)derivate;
247 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
248 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
249 label += currentSampleDesignation.getIdentifier()+separator;
250 }
251 else{
252 label += NO_SAMPLE_DESIGNATION+separator;
253 }
254 label += sequence.getDnaMarker()!=null?sequence.getDnaMarker():emptyString;
255 }
256 //SingleRead
257 else if(derivate instanceof SingleRead){
258 SingleRead singleRead = (SingleRead)derivate;
259 if(parentNode!=null && parentNode.getValue() instanceof Sequence){
260 Sequence sequence = (Sequence) parentNode.getValue();
261 Identifier<DnaSample> currentSampleDesignation = getCurrentSampleDesignation(sequence);
262 if(currentSampleDesignation!=null && currentSampleDesignation.getIdentifier()!=null){
263 label = currentSampleDesignation.getIdentifier()+separator;
264 }
265 else{
266 label += NO_SAMPLE_DESIGNATION+separator;
267 }
268 label += singleRead.getPrimer()!=null?singleRead.getPrimer().getLabel()+separator:emptyString;
269 if(sequence!=null && sequence.getDnaMarker()!=null){
270 label += sequence.getDnaMarker()+separator;
271 }
272 if(singleRead.getAmplificationResult()!=null && singleRead.getAmplificationResult().getAmplification()!=null){
273 label += singleRead.getAmplificationResult().getAmplification().getLabelCache()+separator;
274 }
275 }
276 }
277 //SOOB
278 else if(derivate instanceof SpecimenOrObservationBase){
279 SpecimenOrObservationBase<?> specimen = (SpecimenOrObservationBase<?>) derivate;
280 SpecimenOrObservationType type = specimen.getRecordBasis();
281 return specimen.getTitleCache() + (type!=null?" ["+type.toString()+"]":emptyString);
282 }
283 if(label.isEmpty()){
284 label = derivate.toString();
285 }
286 //remove last comma
287 else if(label.endsWith(separator)){
288 label = label.substring(0, label.length()-separator.length());
289 }
290 return label;
291 }
292
293 /* (non-Javadoc)
294 * @see org.eclipse.jface.viewers.ColumnLabelProvider#getImage(java.lang.Object)
295 */
296 @Override
297 public Image getImage(Object element) {
298 if(element instanceof TreeNode){
299 element = ((TreeNode) element).getValue();
300 }
301 if(element instanceof FieldUnit){
302 return ImageResources.getImage(ImageResources.FIELD_UNIT);
303 }
304 else if(element instanceof DerivedUnit){
305 DerivedUnit derivedUnit = (DerivedUnit)element;
306 if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.FieldUnit){
307 return ImageResources.getImage(ImageResources.FIELD_UNIT);
308 }
309 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.DnaSample){
310 return ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE);
311 }
312 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
313 return ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE);
314 }
315 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
316 return ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE);
317 }
318 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.Media){
319 if(derivedUnit.getKindOfUnit()!=null){
320 if(derivedUnit.getKindOfUnit().equals(getArtworkTerm())){
321 return ImageResources.getImage(ImageResources.ARTWORK_DERIVATE);
322 }
323 else if(derivedUnit.getKindOfUnit().equals(getLivingPlantPhotoTerm())){
324 return ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE);
325 }
326 else if(derivedUnit.getKindOfUnit().equals(getSpecimenScanTerm())){
327 return ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE);
328 }
329 else if(derivedUnit.getKindOfUnit().equals(getDetailImageTerm())){
330 return ImageResources.getImage(ImageResources.DETAIL_IMAGE_DERIVATE);
331 }
332 }
333 }
334 }
335 else if(element instanceof Sequence){
336 return ImageResources.getImage(ImageResources.SEQUENCE_DERIVATE);
337 }
338
339 else if(element instanceof SingleRead){
340 if(multiLinkSingleReads.contains(element)){
341 return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE_MULTILINK);
342 }
343 else{
344 return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE);
345 }
346 }
347 return super.getImage(element);
348 }
349
350 public static Identifier<DnaSample> getCurrentSampleDesignation(CdmBase entity) {
351 if(entity instanceof DnaSample){
352 DnaSample dnaSample = (DnaSample)entity;
353 for (Identifier<DnaSample> identifier : dnaSample.getIdentifiers()) {
354 if(identifier.getType()!=null && identifier.getType().equals(DerivateLabelProvider.getSampleDesignationTerm())){
355 //first sample designation is the current
356 return identifier;
357 }
358 }
359 }
360 else if(entity instanceof Sequence){
361 Sequence sequence = (Sequence)entity;
362 if(sequence.getDnaSample()!=null){
363 return getCurrentSampleDesignation(sequence.getDnaSample());
364 }
365 }
366 return null;
367 }
368
369 /**
370 * @param multiLinkSingleReads2
371 */
372 public void setMultiLinkSingleReads(Set<SingleRead> multiLinkSingleReads) {
373 this.multiLinkSingleReads = multiLinkSingleReads;
374 }
375
376 }