Project

General

Profile

Download (15 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.IIdentifiableEntity;
23
import eu.etaxonomy.cdm.model.common.ISourceable;
24
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
25
import eu.etaxonomy.cdm.model.common.Language;
26
import eu.etaxonomy.cdm.model.common.LanguageString;
27
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
28
import eu.etaxonomy.cdm.model.common.Marker;
29
import eu.etaxonomy.cdm.model.common.MarkerType;
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.DescriptionElementSource;
36
import eu.etaxonomy.cdm.model.description.Distribution;
37
import eu.etaxonomy.cdm.model.description.Feature;
38
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
39
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
40
import eu.etaxonomy.cdm.model.description.QuantitativeData;
41
import eu.etaxonomy.cdm.model.description.TaxonInteraction;
42
import eu.etaxonomy.cdm.model.description.TextData;
43
import eu.etaxonomy.cdm.model.location.NamedArea;
44
import eu.etaxonomy.cdm.model.media.Media;
45
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
46
import eu.etaxonomy.cdm.model.taxon.Taxon;
47
import eu.etaxonomy.taxeditor.store.CdmStore;
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
				PresenceAbsenceTerm 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
			MessagingUtils.warn(DescriptionHelper.class, "trying to set cache on distribution, don't know what to do at the moment.");
137
			return;
138
		}
139
		MessagingUtils.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
				MessagingUtils.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
				MessagingUtils.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
			MarkerType type = marker.getMarkerType();
202
			return (type == null ? "- no marker type -" : marker.getMarkerType().getLabel()) + " (" + marker.getFlag() + ")";
203
		}
204
		// TODO write return texts for NameRelationship, HomotypicalGroup, SpecimenTypeDesignation, etc.
205
		return element.toString();
206
	}
207

    
208
	/**
209
	 * <p>getObjectClassname</p>
210
	 *
211
	 * @param element a {@link java.lang.Object} object.
212
	 * @return a {@link java.lang.String} object.
213
	 */
214
	public static String getObjectClassname(Object element) {
215
		return element.getClass().getSimpleName();
216
	}
217

    
218
	/**
219
	 * <p>getFeatureNodeContainerText</p>
220
	 *
221
	 * @param element a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
222
	 * @return a {@link java.lang.String} object.
223
	 */
224
	public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
225
		String result = null;
226
		if(element.getFeatureNode() != null && element.getFeatureNode().getFeature() != null){
227
			result = element.getFeatureNode().getFeature().getLabel(CdmStore.getDefaultLanguage());
228
		} else{
229
			return "No label set";
230
		}
231
		if (result == null){
232
			result = element.getFeatureNode().getFeature().getLabel();
233
		}
234
		return result;
235
	}
236

    
237
	/**
238
	 * <p>getQuantitativeDataText</p>
239
	 *
240
	 * @param element a {@link eu.etaxonomy.cdm.model.description.QuantitativeData} object.
241
	 * @return a {@link java.lang.String} object.
242
	 */
243
	public static String getQuantitativeDataText(QuantitativeData element) {
244
		TextData textData = quantitativeDescriptionBuilder.build(element, getLanguageList());
245

    
246
		return textData.getText(CdmStore.getDefaultLanguage());
247
	}
248

    
249
	/**
250
	 * <p>getCategoricalDataText</p>
251
	 *
252
	 * @param element a {@link eu.etaxonomy.cdm.model.description.CategoricalData} object.
253
	 * @return a {@link java.lang.String} object.
254
	 */
255
	public static String getCategoricalDataText(CategoricalData element) {
256
		TextData textData = categoricalDescriptionBuilder.build(element, getLanguageList());
257

    
258
		return textData.getText(CdmStore.getDefaultLanguage());
259
	}
260

    
261
	private static List<Language> getLanguageList(){
262
		return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
263
	}
264

    
265
	private static DescriptionBuilder<QuantitativeData> quantitativeDescriptionBuilder = new DefaultQuantitativeDescriptionBuilder();
266
	private static DescriptionBuilder<CategoricalData> categoricalDescriptionBuilder = new DefaultCategoricalDescriptionBuilder();
267

    
268

    
269
	/**
270
	 * <p>getDistributionText</p>
271
	 *
272
	 * @param element a {@link eu.etaxonomy.cdm.model.description.Distribution} object.
273
	 * @return a {@link java.lang.String} object.
274
	 */
275
	public static String getDistributionText(Distribution element) {
276

    
277
		String text = "EMPTY";
278

    
279
		Distribution distribution = element;
280

    
281
		NamedArea area = distribution.getArea();
282
		if(area != null){
283

    
284
			text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
285

    
286
			PresenceAbsenceTerm status = distribution.getStatus();
287

    
288
			if (status != null) {
289
				text += ", " + status.getLabel();
290
			}else{
291
				text += ", NO STATUS";
292
			}
293
		}
294
		return text;
295
	}
296

    
297
	/**
298
	 * <p>getImageText</p>
299
	 *
300
	 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
301
	 * @return a {@link java.lang.String} object.
302
	 */
303
	public static  String getImageText(Media media) {
304
		 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
305
		if (title != null) {
306
			return title.getText();
307
		}
308
		return "No title.";
309
	}
310

    
311

    
312

    
313
	/**
314
	 * <p>getElementText</p>
315
	 *
316
	 * @param element a {@link eu.etaxonomy.cdm.model.description.TextData} object.
317
	 * @return a {@link java.lang.String} object.
318
	 */
319
	public static  String getElementText(TextData element) {
320
		String text = null;
321
		if(element.getFeature().equals(Feature.CITATION())){
322
			text = "";
323
			for(DescriptionElementSource source : element.getSources()){
324
				if(source.getCitation() != null){
325
					text += source.getCitation().getTitleCache();
326
				}
327
				if(source.getNameUsedInSource() != null){
328
					text += " [ " + source.getNameUsedInSource().getTitleCache() + " ]";
329
				}
330
			}
331
			if(CdmUtils.isEmpty(text)){
332
				text = "No sources provided";
333
			}
334
		}else{
335
			List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
336
			LanguageString languageString = element.getPreferredLanguageString(languages);
337
			text = languageString != null ? languageString.getText() : null;
338
		}
339
		return CdmUtils.isEmpty(text) ? "No text provided" : text;
340
	}
341

    
342
	/**
343
	 * <p>getTaxonInteractionText</p>
344
	 *
345
	 * @param element a {@link eu.etaxonomy.cdm.model.description.TaxonInteraction} object.
346
	 * @return a {@link java.lang.String} object.
347
	 */
348
	public static  String getTaxonInteractionText(TaxonInteraction element) {
349
		String text = "";
350
		Taxon taxon2 = element.getTaxon2();
351
		if(taxon2 != null && taxon2.getName() != null){
352
			text = taxon2.getName().getTitleCache();
353
		}else{
354
			text = "No taxon chosen";
355
		}
356

    
357
		return text;
358
	}
359

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

    
380
	/**
381
	 * <p>getDescriptionText</p>
382
	 *
383
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
384
	 * @return a {@link java.lang.String} object.
385
	 */
386
	public static  String getDescriptionText(DescriptionBase element) {
387
		String text = element.getTitleCache();
388
		if (text == null || text.length() == 0) {
389
			text = "No label provided";
390
		}
391

    
392
		return "Factual Data: " + text;
393
	}
394

    
395
	/**
396
	 * <p>getIndividualsAssociationText</p>
397
	 *
398
	 * @param element a {@link eu.etaxonomy.cdm.model.description.IndividualsAssociation} object.
399
	 * @return a {@link java.lang.String} object.
400
	 */
401
	public static  String getIndividualsAssociationText(IndividualsAssociation element) {
402
		SpecimenOrObservationBase derivedUnit = element.getAssociatedSpecimenOrObservation();
403
		if(derivedUnit != null){
404
			return derivedUnit.getTitleCache();
405
		}
406
		return "No unit chosen";
407
	}
408

    
409
	/**
410
	 * <p>getLabel</p>
411
	 *
412
	 * @param element a {@link java.lang.Object} object.
413
	 * @return a {@link java.lang.String} object.
414
	 */
415
	public static String getLabel(Object element){
416
	    String noLabelString = "[no label]";
417
		if (element instanceof FeatureNodeContainer){
418
			return getFeatureNodeContainerText((FeatureNodeContainer) element);
419
		}
420
		else if (element instanceof DescriptionBase) {
421
			return getDescriptionText((DescriptionBase) element);
422
		}
423
		else if(element instanceof CategoricalData){
424
			String categoricalDataText = getCategoricalDataText((CategoricalData) element);
425
			if(categoricalDataText==null || categoricalDataText.isEmpty()){
426
			    categoricalDataText = noLabelString;
427
			}
428
            return categoricalDataText;
429
		}
430
		else if (element instanceof CommonTaxonName) {
431
			return getCommonNameText((CommonTaxonName) element);
432
		}
433
		else if (element instanceof Distribution) {
434
			return getDistributionText((Distribution) element);
435
		}
436
		else if (element instanceof IndividualsAssociation) {
437
			return getIndividualsAssociationText((IndividualsAssociation) element);
438
		}
439
		else if (element instanceof QuantitativeData) {
440
			String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
441
			if(quantitativeDataText==null || quantitativeDataText.isEmpty()){
442
                quantitativeDataText = noLabelString;
443
			}
444
            return quantitativeDataText;
445
		}
446
		else if (element instanceof TaxonInteraction) {
447
			return getTaxonInteractionText((TaxonInteraction) element);
448
		}
449
		else if (element instanceof TextData) {
450
			return getElementText((TextData) element);
451
		}
452
		else{
453
			return element.toString();
454
		}
455
	}
456
}
(12-12/38)