Project

General

Profile

Download (14.7 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.PresenceAbsenceTermBase;
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
import eu.etaxonomy.taxeditor.store.StoreUtil;
49

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

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

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

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

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

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

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

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

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

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

    
410
	/**
411
	 * <p>getLabel</p>
412
	 *
413
	 * @param element a {@link java.lang.Object} object.
414
	 * @return a {@link java.lang.String} object.
415
	 */
416
	public static String getLabel(Object element){
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
			return getCategoricalDataText((CategoricalData) element);
425
		}
426
		else if (element instanceof CommonTaxonName) {
427
			return getCommonNameText((CommonTaxonName) element);			
428
		}
429
		else if (element instanceof Distribution) {
430
			return getDistributionText((Distribution) element);			
431
		}	
432
		else if (element instanceof IndividualsAssociation) {
433
			return getIndividualsAssociationText((IndividualsAssociation) element);
434
		}
435
		else if (element instanceof QuantitativeData) {
436
			return getQuantitativeDataText((QuantitativeData) element);
437
		}
438
		else if (element instanceof TaxonInteraction) {
439
			return getTaxonInteractionText((TaxonInteraction) element);
440
		}
441
		else if (element instanceof TextData) {
442
			return getElementText((TextData) element);
443
		}
444
		else{
445
			return element.toString();
446
		}
447
	}
448
}
(10-10/33)