Project

General

Profile

Download (26.3 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.model;
11

    
12
import java.lang.reflect.InvocationTargetException;
13
import java.lang.reflect.Method;
14
import java.util.Arrays;
15
import java.util.List;
16
import java.util.Set;
17

    
18
import org.apache.commons.lang.StringUtils;
19
import org.hibernate.LazyInitializationException;
20

    
21
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.format.description.DefaultCategoricalDescriptionBuilder;
24
import eu.etaxonomy.cdm.format.description.DefaultQuantitativeDescriptionBuilder;
25
import eu.etaxonomy.cdm.format.description.DescriptionBuilder;
26
import eu.etaxonomy.cdm.model.agent.AgentBase;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
29
import eu.etaxonomy.cdm.model.common.Language;
30
import eu.etaxonomy.cdm.model.common.LanguageString;
31
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
32
import eu.etaxonomy.cdm.model.common.Marker;
33
import eu.etaxonomy.cdm.model.common.MarkerType;
34
import eu.etaxonomy.cdm.model.common.RelationshipBase;
35
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
36
import eu.etaxonomy.cdm.model.common.TimePeriod;
37
import eu.etaxonomy.cdm.model.description.CategoricalData;
38
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
39
import eu.etaxonomy.cdm.model.description.DescriptionBase;
40
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
41
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
42
import eu.etaxonomy.cdm.model.description.Distribution;
43
import eu.etaxonomy.cdm.model.description.Feature;
44
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
45
import eu.etaxonomy.cdm.model.description.KeyStatement;
46
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
47
import eu.etaxonomy.cdm.model.description.QuantitativeData;
48
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
49
import eu.etaxonomy.cdm.model.description.TaxonDescription;
50
import eu.etaxonomy.cdm.model.description.TaxonInteraction;
51
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
52
import eu.etaxonomy.cdm.model.description.TextData;
53
import eu.etaxonomy.cdm.model.location.NamedArea;
54
import eu.etaxonomy.cdm.model.media.Media;
55
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
56
import eu.etaxonomy.cdm.model.name.HybridRelationship;
57
import eu.etaxonomy.cdm.model.name.NameRelationship;
58
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
59
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
60
import eu.etaxonomy.cdm.model.name.TaxonName;
61
import eu.etaxonomy.cdm.model.name.TextualTypeDesignation;
62
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
63
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
64
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
65
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
66
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
67
import eu.etaxonomy.cdm.model.permission.Group;
68
import eu.etaxonomy.cdm.model.permission.User;
69
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
70
import eu.etaxonomy.cdm.model.taxon.Classification;
71
import eu.etaxonomy.cdm.model.taxon.Taxon;
72
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
73
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
74
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
75
import eu.etaxonomy.cdm.model.term.Representation;
76
import eu.etaxonomy.taxeditor.store.CdmStore;
77

    
78
/**
79
 * <p>DescriptionHelper class.</p>
80
 *
81
 * @author p.ciardelli
82
 * @author n.hoffmann
83
 */
84
public class DescriptionHelper {
85

    
86
	/**
87
	 * Returns whatever the element's title cache equivalent is,
88
	 * depending on its class.
89
	 *
90
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
91
	 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
92
	 * @return a {@link java.lang.String} object.
93
	 */
94
	public static String getCache(DescriptionElementBase element,
95
			Language language) {
96

    
97
		String mainElementLabel= null;
98
		DescriptionBase<?> descr = element.getInDescription();
99
		descr = CdmBase.deproxy(descr, DescriptionBase.class);
100

    
101
		if (descr != null){
102
			if (descr.isInstanceOf(TaxonDescription.class)){
103
				Taxon taxon = CdmBase.deproxy(descr, TaxonDescription.class).getTaxon();
104
				if (taxon != null){
105
					mainElementLabel = taxon.getTitleCache();
106
				}
107
			}else if (descr.isInstanceOf(SpecimenDescription.class)){
108
				SpecimenOrObservationBase<?> specimen = CdmBase.deproxy(descr, SpecimenDescription.class).getDescribedSpecimenOrObservation();
109
				if (specimen != null){
110
					mainElementLabel = specimen.getTitleCache();
111
				}
112
			}else if (descr.isInstanceOf(TaxonNameDescription.class)){
113
				TaxonName name = CdmBase.deproxy(descr, TaxonNameDescription.class).getTaxonName();
114
				if (name != null){
115
					mainElementLabel = name.getTitleCache();
116
				}
117
			}
118
		}
119

    
120

    
121
		String cache = null;
122
		if (element instanceof TextData) {
123
			//cache = ((TextData) element).getText(language);
124
		    cache = "Text Data";
125
		}
126
		if (element instanceof CommonTaxonName) {
127
			cache = ((CommonTaxonName) element).getName();
128
		}
129
		if (element instanceof TaxonInteraction) {
130
			Taxon taxon2 = ((TaxonInteraction) element).getTaxon2();
131
			if(taxon2 != null && taxon2.getName() != null){
132
				cache = taxon2.getName().getTitleCache();
133
			}else{
134
				cache = "No taxon chosen";
135
			}
136

    
137
		}
138
		if (element instanceof Distribution) {
139
			Distribution distribution = (Distribution) element;
140

    
141
			NamedArea area = distribution.getArea();
142
			if(area != null){
143
				cache =  area.getLabel();
144

    
145
				PresenceAbsenceTerm status = distribution.getStatus();
146
				if (status == null){
147
					cache += ", no status";
148
				}else {
149
					cache += ", " + status.getLabel();
150
				}
151
			}
152
		}
153
		String result = cache == null ? "" : cache;
154
		if (isNotBlank(mainElementLabel)){
155
			result = CdmUtils.concat(" ", result, "(" + mainElementLabel + ")");
156
		}
157
		return result;
158
	}
159

    
160
	/**
161
	 * Returns whatever the element's title cache equivalent is,
162
	 * depending on its class, using the default language.
163
	 *
164
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
165
	 * @return a {@link java.lang.String} object.
166
	 */
167
	public static String getCache(DescriptionElementBase element) {
168
		return getCache(element, CdmStore.getDefaultLanguage());
169
	}
170

    
171
	/**
172
	 * Set whatever the element's title cache equivalent is,
173
	 * depending on its class.
174
	 *
175
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
176
	 * @param value a {@link java.lang.String} object.
177
	 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
178
	 */
179
	public static void setCache(DescriptionElementBase element,
180
			String value, Language language) {
181
		if (element instanceof TextData) {
182
			((TextData) element).putText(language, value);
183
			return;
184
		}else if (element instanceof CommonTaxonName) {
185
			((CommonTaxonName) element).setName(value);
186
			return;
187
		}else if (element instanceof TaxonInteraction) {
188

    
189
		}else if(element instanceof Distribution){
190
			MessagingUtils.warn(DescriptionHelper.class, "trying to set cache on distribution, don't know what to do at the moment.");
191
			return;
192
		}else{
193
			MessagingUtils.warn(DescriptionHelper.class, "No matching subclass found for DescriptionElementBase object, 'cache' not set.");
194
		}
195
	}
196

    
197
	/**
198
	 * Set whatever the element's title cache equivalent is,
199
	 * depending on its class, using the default language.
200
	 *
201
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
202
	 * @param value a {@link java.lang.String} object.
203
	 */
204
	public static void setCache(DescriptionElementBase element,
205
			String value) {
206
		setCache(element, value, CdmStore.getDefaultLanguage());
207
	}
208

    
209

    
210
	/**
211
	 * <p>getObjectDescription</p>
212
	 *
213
	 * @param element a {@link java.lang.Object} object.
214
	 * @return a {@link java.lang.String} object.
215
	 */
216
	public static String getObjectDescription(Object element) {
217
	    String resultString = null;
218
		if (element instanceof IdentifiableEntity) {
219
			try{
220
			    resultString = ((IdentifiableEntity) element).getTitleCache();
221
			}catch(LazyInitializationException e){
222
				String result = "No Session to initialize title cache for IdentifiableEntity";
223
				MessagingUtils.error(DescriptionHelper.class, result, e);
224
				resultString = "TODO: " + result;
225
			}
226
		}else if (element instanceof OriginalSourceBase) {
227
			try{
228
				OriginalSourceBase<?> originalSource = (OriginalSourceBase<?>) element;
229
//				ISourceable sourcedObject = originalSource.getSourcedObj();
230
				//due to #5743 the bidirectionality for sourced object had to be removed
231
				String sourceObjectTitle = "sourced object data not available (#5743)";
232
//				if(sourcedObject instanceof IIdentifiableEntity){
233
//					sourceObjectTitle = ((IdentifiableEntity) sourcedObject).getTitleCache();
234
//				}else if(sourcedObject instanceof DescriptionElementBase){
235
//					sourceObjectTitle = "Element for description: " + ((DescriptionElementBase) sourcedObject).getInDescription().getTitleCache();
236
//				}else{
237
//					throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
238
//				}
239

    
240
				resultString = CdmUtils.concat("; ", new String[]{originalSource.getIdNamespace(), originalSource.getIdInSource(), sourceObjectTitle});
241
			}catch(LazyInitializationException e){
242
				String result = "Error initializing title cache for ISourceable of an OriginalSourceBase";
243
				MessagingUtils.error(DescriptionHelper.class, result, e);
244
				resultString = "TODO: " + result;
245
			}
246
		}else if (element instanceof LanguageStringBase) {
247
		    resultString = ((LanguageStringBase) element).getText();
248
		}else if (element instanceof DescriptionElementBase) {
249
		    resultString = getCache((DescriptionElementBase) element);
250
		}else if (element instanceof RelationshipBase<?, ?, ?>) {
251
		    resultString = getCache((RelationshipBase<?, ?, ?>) element);
252
		}else if (element instanceof TypeDesignationBase<?>) {
253
		    resultString = getCache((TypeDesignationBase<?>) element);
254
		}else if (element instanceof HomotypicalGroup) {
255
		    resultString = getCache((HomotypicalGroup) element);
256
		}else if (element instanceof TaxonNode) {
257
		    resultString = getCache((TaxonNode) element);
258
		}else if (element instanceof DeterminationEvent) {
259
		    resultString = getCache((DeterminationEvent) element);
260
		}else if (element instanceof GatheringEvent){
261
			resultString = getCache((GatheringEvent) element);
262
		}else if (element instanceof Marker) {
263
			Marker marker = (Marker) element;
264
			MarkerType type = marker.getMarkerType();
265
			resultString = (type == null ? "- no marker type -" : marker.getMarkerType().getLabel()) + " (" + marker.getFlag() + ")";
266
		}else if (element instanceof User) {
267
			User user = (User) element;
268
			resultString = user.getUsername();
269
		}else if (element instanceof Group) {
270
			Group group = (Group) element;
271
			resultString = group.getName();
272
		}else if (element instanceof KeyStatement) {
273
            KeyStatement keyStatement = (KeyStatement) element;
274
            resultString = getCache(keyStatement);
275
        }else{
276
			// TODO write return texts for HomotypicalGroup, etc.
277
            resultString = element.toString();
278
		}
279

    
280
		if (resultString == null){
281
		    resultString = element.toString();
282
		}
283
		return resultString;
284
	}
285

    
286
	private static String getCache(GatheringEvent gatheringEvent){
287
		String ALTITUDE_PREFIX = "alt. ";
288
		final String METER = "m";
289

    
290
		String result = "";
291

    
292
		//collector
293
		AgentBase<?> collector = CdmBase.deproxy(gatheringEvent.getCollector());
294
		String collectorStr = collector == null? null : collector.getTitleCache();
295
		result = CdmUtils.concat(", ", result, collectorStr);
296

    
297
		//gathering period
298
		TimePeriod gatheringPeriod = gatheringEvent.getTimeperiod();
299
		result = CdmUtils.concat(", ", result, (gatheringPeriod == null? null : gatheringPeriod.toString()));
300

    
301
		//country
302
		String strCountry = null;
303
		NamedArea country = gatheringEvent.getCountry();
304
		Representation repCountry = country == null ? null : country.getRepresentation(Language.DEFAULT());
305
		strCountry = repCountry == null ? null: repCountry.getLabel();
306
		result = CdmUtils.concat(", ", result, strCountry);
307

    
308
		//locality
309
		LanguageString locality = gatheringEvent.getLocality();
310
		if (locality != null) {
311
			result = CdmUtils.concat(", ", result, locality.getText());
312
		}
313

    
314
		//elevation
315
		String elevationStr;
316
		if (isNotBlank(gatheringEvent.getAbsoluteElevationText())){
317
			elevationStr = gatheringEvent.getAbsoluteElevationText();
318
		}else{
319
			String text = gatheringEvent.getAbsoluteElevationText();
320
			Integer min = gatheringEvent.getAbsoluteElevation();
321
			Integer max = gatheringEvent.getAbsoluteElevationMax();
322
			elevationStr = DerivedUnitFacade.distanceString(min, max, text, METER);
323
		}
324
		if (isNotBlank(elevationStr)){
325
			result = CdmUtils.concat(", " , result, ALTITUDE_PREFIX);
326
			result += elevationStr;
327
		}
328

    
329
		//exact locality
330
		if (gatheringEvent.getExactLocation() != null){
331
			String exactLocation = gatheringEvent.getExactLocation().toSexagesimalString(false, false);
332
			result = CdmUtils.concat(", ", result, exactLocation);
333
		}
334

    
335
		return result;
336
	}
337

    
338

    
339
	private static String getCache(DeterminationEvent detEvent) {
340
		//taxon
341
		String taxonStr = null;
342
		TaxonName taxonName = detEvent.getTaxonName();
343
		TaxonBase<?> taxon = detEvent.getTaxon();
344
		if (taxonName != null){
345
			taxonStr = taxonName.getTitleCache();
346
		}
347
		if (isBlank(taxonStr) && taxon != null){
348
			taxonStr = taxon.getTitleCache();
349
		}
350
		if (isBlank(taxonStr)){
351
			taxonStr = "no or unlabled taxon";
352
		}
353

    
354

    
355
		//unit
356
		SpecimenOrObservationBase<?> unit = detEvent.getIdentifiedUnit();
357
		String unitStr;
358
		if (unit != null){
359
			unitStr = unit.getTitleCache();
360
			if (isBlank(unitStr)){
361
				unitStr = "Unlabled unit";
362
			}
363
		}else{
364
			unitStr = "no unit";
365
		}
366

    
367
		String result = CdmUtils.concat(" determined as ", unitStr, taxonStr);
368

    
369
		return result;
370
	}
371

    
372
	private static String getCache(TaxonNode taxonNode) {
373
		String result = "";
374
		Classification classification = taxonNode.getClassification();
375
		if (classification != null){
376
			String classificationStr = classification.getName() == null ? "" : classification.getName().getText();
377
			result = CdmUtils.concat("" , result, classificationStr);
378
			if (isBlank(result)){
379
				result = classification.toString();
380
			}
381
		}
382
		String parentStr;
383
		TaxonNode parentNode = taxonNode.getParent();
384
		if (parentNode == null){
385
			parentStr = "no parent";
386
		}else{
387
			Taxon parentTaxon = parentNode.getTaxon();
388
			if (parentTaxon == null){
389
				parentStr = "no parent taxon";
390
			}else{
391
				TaxonName parentName = parentTaxon.getName();
392
				if (parentName == null){
393
					parentStr = "child of " + parentTaxon.getTitleCache();
394
				}else{
395
					parentStr = "child of " + parentName.getTitleCache();
396
				}
397
			}
398
		}
399
		result = CdmUtils.concat(": ", result, parentStr);
400

    
401
		return result;
402
	}
403

    
404
	private static String getCache(TypeDesignationBase<?> designation) {
405
		designation = CdmBase.deproxy(designation);
406
		//from
407
		String fromString = null;
408
		Set<TaxonName> from = designation.getTypifiedNames();
409
		if(from != null){
410
		    for (TaxonName name : from){
411
		        fromString = CdmUtils.concat(",", fromString, name.getTitleCache());
412
		    }
413
		}
414
		//to
415
		IdentifiableEntity<?> to = null;
416
		String toStr = "";
417
		if (designation.isInstanceOf(SpecimenTypeDesignation.class)){
418
			to = ((SpecimenTypeDesignation)designation).getTypeSpecimen();
419
		}else if (designation.isInstanceOf(NameTypeDesignation.class)){
420
			to = ((NameTypeDesignation)designation).getTypeName();
421
		}else if (designation.isInstanceOf(TextualTypeDesignation.class)){
422
		    toStr = ((TextualTypeDesignation)designation).getPreferredText(CdmStore.getDefaultLanguage());
423
		}else{
424
			throw new RuntimeException("Type Designation class not supported: " +  designation.getClass().getName());
425
		}
426
		toStr = to == null? toStr : to.getTitleCache();
427
		//status
428
		String typeLabel = null;
429
		TypeDesignationStatusBase<?> status = designation.getTypeStatus();
430
		if (status != null){
431
			Representation typeRepr = status.getPreferredRepresentation(CdmStore.getDefaultLanguage());
432
			if (typeRepr != null){
433
				typeLabel = typeRepr.getAbbreviatedLabel();
434
			}
435
			if (isBlank(typeLabel) && typeRepr != null){
436
				typeLabel = typeRepr.getLabel();
437
			}
438
			if (isBlank(typeLabel) ){
439
				typeLabel = status.getSymbol();
440
			}
441
			if (isBlank(typeLabel)){
442
				typeLabel = status.getTitleCache();
443
			}
444
		}
445
		if (isBlank(typeLabel)){
446
			typeLabel = "->";
447
		}
448
		//concat
449
		String result = CdmUtils.concat(" ", new String[]{fromString, typeLabel, toStr});
450
		return result;
451
	}
452

    
453
	private static String getCache(RelationshipBase<?, ?, ?> rel) {
454
		rel = CdmBase.deproxy(rel);
455
		RelationshipTermBase<?> type = rel.getType();
456
		IdentifiableEntity<?> from;
457
		IdentifiableEntity<?> to;
458
		if (rel.isInstanceOf(NameRelationship.class)){
459
			from = ((NameRelationship)rel).getFromName();
460
			to = ((NameRelationship)rel).getToName();
461
		}else if (rel.isInstanceOf(HybridRelationship.class)){
462
			from = ((HybridRelationship)rel).getParentName();
463
			to = ((HybridRelationship)rel).getHybridName();
464
		}else if (rel.isInstanceOf(TaxonRelationship.class)){
465
			from = ((TaxonRelationship)rel).getFromTaxon();
466
			to = ((TaxonRelationship)rel).getToTaxon();
467
		}else{
468
			try {
469
				Method fromMethod = rel.getClass().getMethod("getRelatedFrom");
470
				Method toMethod = rel.getClass().getMethod("getRelatedFrom");
471
				fromMethod.setAccessible(true);
472
				toMethod.setAccessible(true);
473
				from = (IdentifiableEntity<?>)fromMethod.invoke(rel);
474
				to = (IdentifiableEntity<?>)toMethod.invoke(rel);
475
			} catch (NoSuchMethodException | SecurityException | IllegalAccessException
476
				| IllegalArgumentException | InvocationTargetException e) {
477
				throw new RuntimeException(e);
478
			}
479
		}
480
		String typeLabel = null;
481
		if (type != null){
482
			Representation typeRepr = type.getPreferredRepresentation(CdmStore.getDefaultLanguage());
483
			if (typeRepr != null){
484
				typeLabel = typeRepr.getAbbreviatedLabel();
485
			}
486
			if (isBlank(typeLabel) && typeRepr != null){
487
				typeLabel = typeRepr.getLabel();
488
			}
489
			if (isBlank(typeLabel) ){
490
				typeLabel = type.getSymbol();
491
			}
492
			if (isBlank(typeLabel)){
493
				typeLabel = type.getTitleCache();
494
			}
495
		}
496
		if (isBlank(typeLabel)){
497
			typeLabel = "->";
498
		}
499
		String result = CdmUtils.concat(" ", new String[]{from == null ? null : from.getTitleCache(),
500
				typeLabel, to == null? null : to.getTitleCache()});
501
		return result;
502
	}
503

    
504

    
505
	private static String getCache(HomotypicalGroup hg) {
506
		String result = "";
507
		for (TaxonName tnb : hg.getTypifiedNames()){
508
			result = CdmUtils.concat(", ", result, tnb.getTitleCache());
509
		}
510
		if (isBlank(result)){
511
			result = "No typified names";
512
		}
513
		return result;
514
	}
515

    
516
	private static String getCache(KeyStatement ks) {
517
        String result = "";
518

    
519
        result = "KeyStatement";
520

    
521
        return result;
522
    }
523

    
524
	/**
525
	 * <p>getObjectClassname</p>
526
	 *
527
	 * @param element a {@link java.lang.Object} object.
528
	 * @return a {@link java.lang.String} object.
529
	 */
530
	public static String getObjectClassname(Object element) {
531
		return element.getClass().getSimpleName();
532
	}
533

    
534
	/**
535
	 * <p>getFeatureNodeContainerText</p>
536
	 *
537
	 * @param element a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
538
	 * @return a {@link java.lang.String} object.
539
	 */
540
	public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
541
		String result = null;
542
		if(element.getFeatureNode() != null && element.getFeatureNode().getTerm() != null){
543
			result = element.getFeatureNode().getTerm().getLabel(CdmStore.getDefaultLanguage());
544
		} else{
545
			return "No label set";
546
		}
547
		if (result == null){
548
			result = element.getFeatureNode().getTerm().getLabel();
549
		}
550
		return result;
551
	}
552

    
553
	/**
554
	 * <p>getQuantitativeDataText</p>
555
	 *
556
	 * @param element a {@link eu.etaxonomy.cdm.model.description.QuantitativeData} object.
557
	 * @return a {@link java.lang.String} object.
558
	 */
559
	public static String getQuantitativeDataText(QuantitativeData element) {
560
		TextData textData = quantitativeDescriptionBuilder.build(element, getLanguageList());
561

    
562
		return textData.getText(CdmStore.getDefaultLanguage());
563
	}
564

    
565
	/**
566
	 * <p>getCategoricalDataText</p>
567
	 *
568
	 * @param element a {@link eu.etaxonomy.cdm.model.description.CategoricalData} object.
569
	 * @return a {@link java.lang.String} object.
570
	 */
571
	public static String getCategoricalDataText(CategoricalData element) {
572
		TextData textData = categoricalDescriptionBuilder.build(element, getLanguageList());
573

    
574
		return textData.getText(CdmStore.getDefaultLanguage());
575
	}
576

    
577
	private static List<Language> getLanguageList(){
578
		return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
579
	}
580

    
581
	private static DescriptionBuilder<QuantitativeData> quantitativeDescriptionBuilder = new DefaultQuantitativeDescriptionBuilder();
582
	private static DescriptionBuilder<CategoricalData> categoricalDescriptionBuilder = new DefaultCategoricalDescriptionBuilder();
583
    public static final String NO_LABEL_STRING = "[no label]";
584

    
585

    
586
	/**
587
	 * <p>getDistributionText</p>
588
	 *
589
	 * @param element a {@link eu.etaxonomy.cdm.model.description.Distribution} object.
590
	 * @return a {@link java.lang.String} object.
591
	 */
592
	public static String getDistributionText(Distribution element) {
593

    
594
		String text = "EMPTY";
595

    
596
		Distribution distribution = element;
597

    
598
		NamedArea area = distribution.getArea();
599
		if(area != null){
600

    
601
			text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
602

    
603
			PresenceAbsenceTerm status = distribution.getStatus();
604

    
605
			if (status != null) {
606
				text += ", " + status.getLabel();
607
			}else{
608
				text += ", NO STATUS";
609
			}
610
		}
611
		return text;
612
	}
613

    
614
	/**
615
	 * <p>getImageText</p>
616
	 *
617
	 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
618
	 * @return a {@link java.lang.String} object.
619
	 */
620
	public static  String getImageText(Media media) {
621
		 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
622
		if (title != null) {
623
			return title.getText();
624
		}
625
		return "No title.";
626
	}
627

    
628

    
629

    
630
	/**
631
	 * <p>getElementText</p>
632
	 *
633
	 * @param element a {@link eu.etaxonomy.cdm.model.description.TextData} object.
634
	 * @return a {@link java.lang.String} object.
635
	 */
636
	public static  String getElementText(TextData element) {
637
		String text = null;
638
		if(element.getFeature().equals(Feature.CITATION())){
639
			text = "";
640
			for(DescriptionElementSource source : element.getSources()){
641
				if(source.getCitation() != null){
642
					text += source.getCitation().getTitleCache();
643
				}
644
				if(source.getNameUsedInSource() != null){
645
					text += " [ " + source.getNameUsedInSource().getTitleCache() + " ]";
646
				}
647
			}
648
			if(isBlank(text)){
649
				text = "No sources provided";
650
			}
651
		}else{
652
			List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
653
			LanguageString languageString = element.getPreferredLanguageString(languages);
654
			text = languageString != null ? languageString.getText() : null;
655
		}
656
		return isBlank(text) ? "No text provided" : text;
657
	}
658

    
659
	/**
660
	 * <p>getTaxonInteractionText</p>
661
	 *
662
	 * @param element a {@link eu.etaxonomy.cdm.model.description.TaxonInteraction} object.
663
	 * @return a {@link java.lang.String} object.
664
	 */
665
	public static  String getTaxonInteractionText(TaxonInteraction element) {
666
		String text = "";
667
		Taxon taxon2 = element.getTaxon2();
668
		if(taxon2 != null && taxon2.getName() != null){
669
			text = taxon2.getName().getTitleCache();
670
		}else{
671
			text = "No taxon chosen";
672
		}
673

    
674
		return text;
675
	}
676

    
677
	/**
678
	 * <p>getCommonNameText</p>
679
	 *
680
	 * @param commonName a {@link eu.etaxonomy.cdm.model.description.CommonTaxonName} object.
681
	 * @return a {@link java.lang.String} object.
682
	 */
683
	public static  String getCommonNameText(CommonTaxonName commonName) {
684
		String name = commonName.getName();
685
		if (name == null || name.length() == 0) {
686
			name = "No name provided";
687
		}
688
		Language language = commonName.getLanguage();
689
		if (language == null) {
690
			name += " (No language provided)";
691
		} else {
692
			name += " (" + language.getLabel(CdmStore.getDefaultLanguage()) + ")";
693
		}
694
		return name;
695
	}
696

    
697
	/**
698
	 * <p>getDescriptionText</p>
699
	 *
700
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
701
	 * @return a {@link java.lang.String} object.
702
	 */
703
	public static  String getDescriptionText(DescriptionBase element) {
704
		String text = element.getTitleCache();
705
		if (text == null || text.length() == 0) {
706
			text = "No label provided";
707
		}
708

    
709
		return text;
710
	}
711

    
712
	/**
713
	 * <p>getIndividualsAssociationText</p>
714
	 *
715
	 * @param element a {@link eu.etaxonomy.cdm.model.description.IndividualsAssociation} object.
716
	 * @return a {@link java.lang.String} object.
717
	 */
718
	public static  String getIndividualsAssociationText(IndividualsAssociation element) {
719
		SpecimenOrObservationBase derivedUnit = element.getAssociatedSpecimenOrObservation();
720
		if(derivedUnit != null){
721
			return derivedUnit.getTitleCache();
722
		}
723
		return "No unit chosen";
724
	}
725

    
726
	/**
727
	 * <p>getLabel</p>
728
	 *
729
	 * @param element a {@link java.lang.Object} object.
730
	 * @return a {@link java.lang.String} object.
731
	 */
732
	public static String getLabel(Object element){
733
		if (element instanceof FeatureNodeContainer){
734
			return getFeatureNodeContainerText((FeatureNodeContainer) element);
735
		}
736
		else if (element instanceof DescriptionBase) {
737
			return getDescriptionText((DescriptionBase) element);
738
		}
739
		else if(element instanceof CategoricalData){
740
			String categoricalDataText = getCategoricalDataText((CategoricalData) element);
741
			if(categoricalDataText==null || categoricalDataText.isEmpty()){
742
			    categoricalDataText = NO_LABEL_STRING;
743
			}
744
            return categoricalDataText;
745
		}
746
		else if (element instanceof CommonTaxonName) {
747
			return getCommonNameText((CommonTaxonName) element);
748
		}
749
		else if (element instanceof Distribution) {
750
			return getDistributionText((Distribution) element);
751
		}
752
		else if (element instanceof IndividualsAssociation) {
753
			return getIndividualsAssociationText((IndividualsAssociation) element);
754
		}
755
		else if (element instanceof QuantitativeData) {
756
			String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
757
			if(quantitativeDataText==null || quantitativeDataText.isEmpty()){
758
                quantitativeDataText = NO_LABEL_STRING;
759
			}
760
            return quantitativeDataText;
761
		}
762
		else if (element instanceof TaxonInteraction) {
763
			return getTaxonInteractionText((TaxonInteraction) element);
764
		}
765
		else if (element instanceof TextData) {
766
			return getElementText((TextData) element);
767
		}
768
		else{
769
			return element.toString();
770
		}
771
	}
772

    
773
	private static boolean isNotBlank(String str){
774
		return StringUtils.isNotBlank(str);
775
	}
776

    
777
	private static boolean isBlank(String str){
778
		return StringUtils.isBlank(str);
779
	}
780
}
(14-14/41)