Project

General

Profile

Download (14.4 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 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

    
11
package eu.etaxonomy.taxeditor.model;
12

    
13
import java.util.Arrays;
14
import java.util.List;
15

    
16
import org.hibernate.LazyInitializationException;
17

    
18
import eu.etaxonomy.cdm.api.service.DefaultCategoricalDescriptionBuilder;
19
import eu.etaxonomy.cdm.api.service.DefaultQuantitativeDescriptionBuilder;
20
import eu.etaxonomy.cdm.api.service.DescriptionBuilder;
21
import eu.etaxonomy.cdm.common.CdmUtils;
22
import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
23
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
24
import eu.etaxonomy.cdm.model.common.ISourceable;
25
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
26
import eu.etaxonomy.cdm.model.common.Language;
27
import eu.etaxonomy.cdm.model.common.LanguageString;
28
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
29
import eu.etaxonomy.cdm.model.common.Marker;
30
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
31
import eu.etaxonomy.cdm.model.description.CategoricalData;
32
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
33
import eu.etaxonomy.cdm.model.description.DescriptionBase;
34
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
35
import eu.etaxonomy.cdm.model.description.Distribution;
36
import eu.etaxonomy.cdm.model.description.Feature;
37
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
38
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
39
import eu.etaxonomy.cdm.model.description.QuantitativeData;
40
import eu.etaxonomy.cdm.model.description.TaxonInteraction;
41
import eu.etaxonomy.cdm.model.description.TextData;
42
import eu.etaxonomy.cdm.model.location.NamedArea;
43
import eu.etaxonomy.cdm.model.media.Media;
44
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
45
import eu.etaxonomy.cdm.model.taxon.Taxon;
46
import eu.etaxonomy.taxeditor.store.CdmStore;
47
import eu.etaxonomy.taxeditor.store.StoreUtil;
48

    
49
/**
50
 * <p>DescriptionHelper class.</p>
51
 *
52
 * @author p.ciardelli
53
 * @author n.hoffmann
54
 * @created 02.04.2009
55
 * @version 1.0
56
 */
57
public class DescriptionHelper {
58
	
59
	/**
60
	 * Returns whatever the element's title cache equivalent is,
61
	 * depending on its class.
62
	 *
63
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
64
	 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
65
	 * @return a {@link java.lang.String} object.
66
	 */
67
	public static String getCache(DescriptionElementBase element, 
68
			Language language) {
69
		String cache = null;
70
		if (element instanceof TextData) {
71
			cache = ((TextData) element).getText(language);
72
		}
73
		if (element instanceof CommonTaxonName) {
74
			cache = ((CommonTaxonName) element).getName();
75
		}
76
		if (element instanceof TaxonInteraction) {
77
			Taxon taxon2 = ((TaxonInteraction) element).getTaxon2();
78
			if(taxon2 != null && taxon2.getName() != null){
79
				cache = taxon2.getName().getTitleCache();
80
			}else{
81
				cache = "No taxon chosen";
82
			}
83
			
84
		}
85
		if (element instanceof Distribution) {
86
			Distribution distribution = (Distribution) element;
87
			
88
			NamedArea area = distribution.getArea();
89
			if(area != null){
90
				cache =  area.getLabel();
91
				
92
				PresenceAbsenceTermBase<?> status = distribution.getStatus();
93
				if (status == null){
94
					cache += ", no status";
95
				}else {
96
					cache += ", " + status.getLabel();
97
				}
98
			}
99
		}
100
		return cache == null ? "" : cache;
101
	}
102

    
103
	/**
104
	 * Returns whatever the element's title cache equivalent is,
105
	 * depending on its class, using the default language.
106
	 *
107
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
108
	 * @return a {@link java.lang.String} object.
109
	 */
110
	public static String getCache(DescriptionElementBase element) {
111
		return getCache(element, CdmStore.getDefaultLanguage());
112
	}
113
	
114
	/**
115
	 * Set whatever the element's title cache equivalent is,
116
	 * depending on its class.
117
	 *
118
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
119
	 * @param value a {@link java.lang.String} object.
120
	 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
121
	 */
122
	public static void setCache(DescriptionElementBase element,
123
			String value, Language language) {
124
		if (element instanceof TextData) {
125
			((TextData) element).putText(language, value);
126
			return;
127
		}
128
		if (element instanceof CommonTaxonName) {
129
			((CommonTaxonName) element).setName(value);
130
			return;
131
		}
132
		if (element instanceof TaxonInteraction) {
133
			
134
		}
135
		if(element instanceof Distribution){
136
			StoreUtil.warn(DescriptionHelper.class, "trying to set cache on distribution, don't know what to do at the moment.");
137
			return;
138
		}
139
		StoreUtil.warn(DescriptionHelper.class, "No matching subclass found for DescriptionElementBase object, 'cache' not set.");
140
	}
141
	
142
	/**
143
	 * Set whatever the element's title cache equivalent is,
144
	 * depending on its class, using the default language.
145
	 *
146
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
147
	 * @param value a {@link java.lang.String} object.
148
	 */
149
	public static void setCache(DescriptionElementBase element,
150
			String value) {
151
		setCache(element, value, CdmStore.getDefaultLanguage());
152
	}
153
	
154
	/* (non-Javadoc)
155
	 * @see eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.IReferencingObjectsService#getObjectDescription(java.lang.Object)
156
	 */
157
	/**
158
	 * <p>getObjectDescription</p>
159
	 *
160
	 * @param element a {@link java.lang.Object} object.
161
	 * @return a {@link java.lang.String} object.
162
	 */
163
	public static String getObjectDescription(Object element) {
164
		if (element instanceof IdentifiableEntity) {
165
			try{
166
				return ((IdentifiableEntity) element).getTitleCache();
167
			}catch(LazyInitializationException e){
168
				String result = "No Session to initialize title cache for IdentifiableEntity";
169
				StoreUtil.error(DescriptionHelper.class, result, e);
170
				return "TODO: " + result;
171
			}
172
		}
173
		if (element instanceof OriginalSourceBase) {
174
			try{
175
				OriginalSourceBase originalSource = (OriginalSourceBase) element;
176
				ISourceable sourcedObject = originalSource.getSourcedObj();
177
				String sourceObjectTitle = "";
178
				if(sourcedObject instanceof IIdentifiableEntity){
179
					sourceObjectTitle = ((IdentifiableEntity) sourcedObject).getTitleCache();
180
				}else if(sourcedObject instanceof DescriptionElementBase){
181
					sourceObjectTitle = "Element for description: " + ((DescriptionElementBase) sourcedObject).getInDescription().getTitleCache();
182
				}else{
183
					throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
184
				}
185
				
186
				return CdmUtils.concat("; ", new String[]{originalSource.getIdNamespace(), originalSource.getIdInSource(), sourceObjectTitle});
187
			}catch(LazyInitializationException e){
188
				String result = "Error initializing title cache for ISourceable of an OriginalSourceBase";
189
				StoreUtil.error(DescriptionHelper.class, result, e);
190
				return "TODO: " + result;
191
			}
192
		}
193
		if (element instanceof LanguageStringBase) {
194
			return ((LanguageStringBase) element).getText();
195
		}
196
		if (element instanceof DescriptionElementBase) {
197
			return getCache((DescriptionElementBase) element);		
198
		}
199
		if (element instanceof Marker) {
200
			Marker marker = (Marker) element;
201
			return marker.getMarkerType().getLabel() + " (" + marker.getFlag() + ")";
202
		}
203
		// TODO write return texts for NameRelationship, HomotypicalGroup, SpecimenTypeDesignation, etc.
204
		return element.toString();
205
	}
206

    
207
	/**
208
	 * <p>getObjectClassname</p>
209
	 *
210
	 * @param element a {@link java.lang.Object} object.
211
	 * @return a {@link java.lang.String} object.
212
	 */
213
	public static String getObjectClassname(Object element) {
214
		return element.getClass().getSimpleName();
215
	}
216
	
217
	/**
218
	 * <p>getFeatureNodeContainerText</p>
219
	 *
220
	 * @param element a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
221
	 * @return a {@link java.lang.String} object.
222
	 */
223
	public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
224
		if(element.getFeatureNode() != null && element.getFeatureNode().getFeature() != null){
225
			return element.getFeatureNode().getFeature().getLabel(CdmStore.getDefaultLanguage());
226
		}
227
		return "No label set";
228
	}
229

    
230
	/**
231
	 * <p>getQuantitativeDataText</p>
232
	 *
233
	 * @param element a {@link eu.etaxonomy.cdm.model.description.QuantitativeData} object.
234
	 * @return a {@link java.lang.String} object.
235
	 */
236
	public static String getQuantitativeDataText(QuantitativeData element) {
237
		TextData textData = quantitativeDescriptionBuilder.build(element, getLanguageList());
238
		
239
		return textData.getText(CdmStore.getDefaultLanguage());
240
	}
241

    
242
	/**
243
	 * <p>getCategoricalDataText</p>
244
	 *
245
	 * @param element a {@link eu.etaxonomy.cdm.model.description.CategoricalData} object.
246
	 * @return a {@link java.lang.String} object.
247
	 */
248
	public static String getCategoricalDataText(CategoricalData element) {
249
		TextData textData = categoricalDescriptionBuilder.build(element, getLanguageList());
250
		
251
		return textData.getText(CdmStore.getDefaultLanguage());
252
	}
253
	
254
	private static List<Language> getLanguageList(){
255
		return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
256
	}
257
	
258
	private static DescriptionBuilder<QuantitativeData> quantitativeDescriptionBuilder = new DefaultQuantitativeDescriptionBuilder();
259
	private static DescriptionBuilder<CategoricalData> categoricalDescriptionBuilder = new DefaultCategoricalDescriptionBuilder();
260
	
261

    
262
	/**
263
	 * <p>getDistributionText</p>
264
	 *
265
	 * @param element a {@link eu.etaxonomy.cdm.model.description.Distribution} object.
266
	 * @return a {@link java.lang.String} object.
267
	 */
268
	public static String getDistributionText(Distribution element) {
269
		
270
		String text = "EMPTY";
271
		
272
		Distribution distribution = (Distribution) element;
273
		
274
		NamedArea area = distribution.getArea();
275
		if(area != null){
276
			
277
			text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
278
			
279
			PresenceAbsenceTermBase<?> status = distribution.getStatus();
280
			
281
			if (status != null) {
282
				text += ", " + status.getLabel();
283
			}else{
284
				text += ", NO STATUS";
285
			}
286
		}
287
		return text;
288
	}
289

    
290
	/**
291
	 * <p>getImageText</p>
292
	 *
293
	 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
294
	 * @return a {@link java.lang.String} object.
295
	 */
296
	public static  String getImageText(Media media) {
297
		 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
298
		if (title != null) {
299
			return title.getText();
300
		}
301
		return "No title.";
302
	}
303

    
304
	
305
	
306
	/**
307
	 * <p>getElementText</p>
308
	 *
309
	 * @param element a {@link eu.etaxonomy.cdm.model.description.TextData} object.
310
	 * @return a {@link java.lang.String} object.
311
	 */
312
	public static  String getElementText(TextData element) {	
313
		String text = null;
314
		if(element.getFeature().equals(Feature.CITATION())){
315
			text = "";
316
			for(DescriptionElementSource source : element.getSources()){
317
				if(source.getCitation() != null){
318
					text += source.getCitation().getTitleCache();
319
				}
320
				if(source.getNameUsedInSource() != null){
321
					text += " [ " + source.getNameUsedInSource().getTitleCache() + " ]";
322
				}
323
			}
324
			if(CdmUtils.isEmpty(text)){
325
				text = "No sources provided";
326
			}
327
		}else{
328
			List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
329
			LanguageString languageString = element.getPreferredLanguageString(languages);
330
			text = languageString != null ? languageString.getText() : null;
331
		}
332
		return CdmUtils.isEmpty(text) ? "No text provided" : text;
333
	}
334
	
335
	/**
336
	 * <p>getTaxonInteractionText</p>
337
	 *
338
	 * @param element a {@link eu.etaxonomy.cdm.model.description.TaxonInteraction} object.
339
	 * @return a {@link java.lang.String} object.
340
	 */
341
	public static  String getTaxonInteractionText(TaxonInteraction element) {
342
		String text = "";
343
		Taxon taxon2 = ((TaxonInteraction) element).getTaxon2();
344
		if(taxon2 != null && taxon2.getName() != null){
345
			text = taxon2.getName().getTitleCache();
346
		}else{
347
			text = "No taxon chosen";
348
		}
349
		
350
		return text;
351
	}
352

    
353
	/**
354
	 * <p>getCommonNameText</p>
355
	 *
356
	 * @param commonName a {@link eu.etaxonomy.cdm.model.description.CommonTaxonName} object.
357
	 * @return a {@link java.lang.String} object.
358
	 */
359
	public static  String getCommonNameText(CommonTaxonName commonName) {
360
		String name = commonName.getName();
361
		if (name == null || name.length() == 0) {
362
			name = "No name provided";
363
		}
364
		Language language = commonName.getLanguage();
365
		if (language == null) {
366
			name += " (No language provided)";
367
		} else {
368
			name += " (" + language.getDescription() + ")";
369
		}
370
		return name;
371
	}
372

    
373
	/**
374
	 * <p>getDescriptionText</p>
375
	 *
376
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
377
	 * @return a {@link java.lang.String} object.
378
	 */
379
	public static  String getDescriptionText(DescriptionBase element) {
380
		String text = element.getTitleCache();
381
		if (text == null || text.length() == 0) {
382
			text = "No label provided";
383
		}
384
		return "Description: " + text;
385
	}
386
	
387
	/**
388
	 * <p>getIndividualsAssociationText</p>
389
	 *
390
	 * @param element a {@link eu.etaxonomy.cdm.model.description.IndividualsAssociation} object.
391
	 * @return a {@link java.lang.String} object.
392
	 */
393
	public static  String getIndividualsAssociationText(IndividualsAssociation element) {
394
		SpecimenOrObservationBase derivedUnit = element.getAssociatedSpecimenOrObservation();
395
		if(derivedUnit != null){
396
			return derivedUnit.getTitleCache();
397
		}
398
		return "No text provided";
399
	}
400

    
401
	/**
402
	 * <p>getLabel</p>
403
	 *
404
	 * @param element a {@link java.lang.Object} object.
405
	 * @return a {@link java.lang.String} object.
406
	 */
407
	public static String getLabel(Object element){
408
		if (element instanceof FeatureNodeContainer){
409
			return getFeatureNodeContainerText((FeatureNodeContainer) element);
410
		}
411
		else if (element instanceof DescriptionBase) {
412
			return getDescriptionText((DescriptionBase) element);
413
		}
414
		else if(element instanceof CategoricalData){
415
			return getCategoricalDataText((CategoricalData) element);
416
		}
417
		else if (element instanceof CommonTaxonName) {
418
			return getCommonNameText((CommonTaxonName) element);			
419
		}
420
		else if (element instanceof Distribution) {
421
			return getDistributionText((Distribution) element);			
422
		}	
423
		else if (element instanceof IndividualsAssociation) {
424
			return getIndividualsAssociationText((IndividualsAssociation) element);
425
		}
426
		else if (element instanceof QuantitativeData) {
427
			return getQuantitativeDataText((QuantitativeData) element);
428
		}
429
		else if (element instanceof TaxonInteraction) {
430
			return getTaxonInteractionText((TaxonInteraction) element);
431
		}
432
		else if (element instanceof TextData) {
433
			return getElementText((TextData) element);
434
		}
435
		else{
436
			return element.toString();
437
		}
438
	}
439
}
(9-9/31)