Project

General

Profile

Download (14.6 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.description.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.MarkerType;
31
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
32
import eu.etaxonomy.cdm.model.description.CategoricalData;
33
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
34
import eu.etaxonomy.cdm.model.description.DescriptionBase;
35
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
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
		if(element.getFeatureNode() != null && element.getFeatureNode().getFeature() != null){
227
			return element.getFeatureNode().getFeature().getLabel(CdmStore.getDefaultLanguage());
228
		}
229
		return "No label set";
230
	}
231

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

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

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

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

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

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

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

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