Project

General

Profile

Download (23.8 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.service.DefaultCategoricalDescriptionBuilder;
22
import eu.etaxonomy.cdm.api.service.DefaultQuantitativeDescriptionBuilder;
23
import eu.etaxonomy.cdm.api.service.DescriptionBuilder;
24
import eu.etaxonomy.cdm.common.CdmUtils;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.common.Group;
27
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
28
import eu.etaxonomy.cdm.model.common.Language;
29
import eu.etaxonomy.cdm.model.common.LanguageString;
30
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
31
import eu.etaxonomy.cdm.model.common.Marker;
32
import eu.etaxonomy.cdm.model.common.MarkerType;
33
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
34
import eu.etaxonomy.cdm.model.common.RelationshipBase;
35
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
36
import eu.etaxonomy.cdm.model.common.Representation;
37
import eu.etaxonomy.cdm.model.common.User;
38
import eu.etaxonomy.cdm.model.description.CategoricalData;
39
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
40
import eu.etaxonomy.cdm.model.description.DescriptionBase;
41
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
42
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
43
import eu.etaxonomy.cdm.model.description.Distribution;
44
import eu.etaxonomy.cdm.model.description.Feature;
45
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
46
import eu.etaxonomy.cdm.model.description.KeyStatement;
47
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
48
import eu.etaxonomy.cdm.model.description.QuantitativeData;
49
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
50
import eu.etaxonomy.cdm.model.description.TaxonDescription;
51
import eu.etaxonomy.cdm.model.description.TaxonInteraction;
52
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
53
import eu.etaxonomy.cdm.model.description.TextData;
54
import eu.etaxonomy.cdm.model.location.NamedArea;
55
import eu.etaxonomy.cdm.model.media.Media;
56
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
57
import eu.etaxonomy.cdm.model.name.HybridRelationship;
58
import eu.etaxonomy.cdm.model.name.NameRelationship;
59
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
60
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
61
import eu.etaxonomy.cdm.model.name.TaxonName;
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.SpecimenOrObservationBase;
66
import eu.etaxonomy.cdm.model.taxon.Classification;
67
import eu.etaxonomy.cdm.model.taxon.Taxon;
68
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
69
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
70
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
71
import eu.etaxonomy.taxeditor.store.CdmStore;
72

    
73
/**
74
 * <p>DescriptionHelper class.</p>
75
 *
76
 * @author p.ciardelli
77
 * @author n.hoffmann
78
 */
79
public class DescriptionHelper {
80

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

    
92
		String mainElementLabel= null;
93
		DescriptionBase<?> descr = element.getInDescription();
94
		descr = CdmBase.deproxy(descr, DescriptionBase.class);
95

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

    
115

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

    
132
		}
133
		if (element instanceof Distribution) {
134
			Distribution distribution = (Distribution) element;
135

    
136
			NamedArea area = distribution.getArea();
137
			if(area != null){
138
				cache =  area.getLabel();
139

    
140
				PresenceAbsenceTerm status = distribution.getStatus();
141
				if (status == null){
142
					cache += ", no status";
143
				}else {
144
					cache += ", " + status.getLabel();
145
				}
146
			}
147
		}
148
		String result = cache == null ? "" : cache;
149
		if (isNotBlank(mainElementLabel)){
150
			result = CdmUtils.concat(" ", result, "(" + mainElementLabel + ")");
151
		}
152
		return result;
153
	}
154

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

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

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

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

    
204

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

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

    
273
		if (resultString == null){
274
		    resultString = element.toString();
275
		}
276
		return resultString;
277
	}
278

    
279

    
280
	private static String getCache(DeterminationEvent detEvent) {
281
		//taxon
282
		String taxonStr = null;
283
		TaxonName taxonName = detEvent.getTaxonName();
284
		TaxonBase<?> taxon = detEvent.getTaxon();
285
		if (taxonName != null){
286
			taxonStr = taxonName.getTitleCache();
287
		}
288
		if (isBlank(taxonStr) && taxon != null){
289
			taxonStr = taxon.getTitleCache();
290
		}
291
		if (isBlank(taxonStr)){
292
			taxonStr = "no or unlabled taxon";
293
		}
294

    
295

    
296
		//unit
297
		SpecimenOrObservationBase<?> unit = detEvent.getIdentifiedUnit();
298
		String unitStr;
299
		if (unit != null){
300
			unitStr = unit.getTitleCache();
301
			if (isBlank(unitStr)){
302
				unitStr = "Unlabled unit";
303
			}
304
		}else{
305
			unitStr = "no unit";
306
		}
307

    
308
		String result = CdmUtils.concat(" determined as ", unitStr, taxonStr);
309

    
310
		return result;
311
	}
312

    
313
	private static String getCache(TaxonNode taxonNode) {
314
		String result = "";
315
		Classification classification = taxonNode.getClassification();
316
		if (classification != null){
317
			String classificationStr = classification.getName() == null ? "" : classification.getName().getText();
318
			result = CdmUtils.concat("" , result, classificationStr);
319
			if (isBlank(result)){
320
				result = classification.toString();
321
			}
322
		}
323
		String parentStr;
324
		TaxonNode parentNode = taxonNode.getParent();
325
		if (parentNode == null){
326
			parentStr = "no parent";
327
		}else{
328
			Taxon parentTaxon = parentNode.getTaxon();
329
			if (parentTaxon == null){
330
				parentStr = "no parent taxon";
331
			}else{
332
				TaxonName parentName = parentTaxon.getName();
333
				if (parentName == null){
334
					parentStr = "child of " + parentTaxon.getTitleCache();
335
				}else{
336
					parentStr = "child of " + parentName.getTitleCache();
337
				}
338
			}
339
		}
340
		result = CdmUtils.concat(": ", result, parentStr);
341

    
342
		return result;
343
	}
344

    
345
	private static String getCache(TypeDesignationBase<?> designation) {
346
		designation = CdmBase.deproxy(designation);
347
		TypeDesignationStatusBase<?> status = designation.getTypeStatus();
348
		Set<TaxonName> from;
349
		IdentifiableEntity<?> to;
350
		from = designation.getTypifiedNames();
351
		if (designation.isInstanceOf(SpecimenTypeDesignation.class)){
352
			to = ((SpecimenTypeDesignation)designation).getTypeSpecimen();
353
		}else if (designation.isInstanceOf(NameTypeDesignation.class)){
354
			to = ((NameTypeDesignation)designation).getTypeName();
355
		}else{
356
			throw new RuntimeException("Type Designation class not supported: " +  designation.getClass().getName());
357
		}
358
		String typeLabel = null;
359
		if (status != null){
360
			Representation typeRepr = status.getPreferredRepresentation(CdmStore.getDefaultLanguage());
361
			if (typeRepr != null){
362
				typeLabel = typeRepr.getAbbreviatedLabel();
363
			}
364
			if (isBlank(typeLabel) && typeRepr != null){
365
				typeLabel = typeRepr.getLabel();
366
			}
367
			if (isBlank(typeLabel) ){
368
				typeLabel = status.getSymbol();
369
			}
370
			if (isBlank(typeLabel)){
371
				typeLabel = status.getTitleCache();
372
			}
373
		}
374
		if (isBlank(typeLabel)){
375
			typeLabel = "->";
376
		}
377
		String fromString = "";
378
		for (TaxonName name : from){
379
			CdmUtils.concat(",", fromString, name.getTitleCache());
380
		}
381
		String result = CdmUtils.concat(" ", new String[]{from == null ? null : fromString,
382
				typeLabel, to == null? null : to.getTitleCache()});
383
		return result;
384
	}
385

    
386
	private static String getCache(RelationshipBase<?, ?, ?> rel) {
387
		rel = CdmBase.deproxy(rel);
388
		RelationshipTermBase<?> type = rel.getType();
389
		IdentifiableEntity<?> from;
390
		IdentifiableEntity<?> to;
391
		if (rel.isInstanceOf(NameRelationship.class)){
392
			from = ((NameRelationship)rel).getFromName();
393
			to = ((NameRelationship)rel).getToName();
394
		}else if (rel.isInstanceOf(HybridRelationship.class)){
395
			from = ((HybridRelationship)rel).getParentName();
396
			to = ((HybridRelationship)rel).getHybridName();
397
		}else if (rel.isInstanceOf(TaxonRelationship.class)){
398
			from = ((TaxonRelationship)rel).getFromTaxon();
399
			to = ((TaxonRelationship)rel).getToTaxon();
400
		}else{
401
			try {
402
				Method fromMethod = rel.getClass().getMethod("getRelatedFrom");
403
				Method toMethod = rel.getClass().getMethod("getRelatedFrom");
404
				fromMethod.setAccessible(true);
405
				toMethod.setAccessible(true);
406
				from = (IdentifiableEntity<?>)fromMethod.invoke(rel);
407
				to = (IdentifiableEntity<?>)toMethod.invoke(rel);
408
			} catch (NoSuchMethodException | SecurityException | IllegalAccessException
409
				| IllegalArgumentException | InvocationTargetException e) {
410
				throw new RuntimeException(e);
411
			}
412
		}
413
		String typeLabel = null;
414
		if (type != null){
415
			Representation typeRepr = type.getPreferredRepresentation(CdmStore.getDefaultLanguage());
416
			if (typeRepr != null){
417
				typeLabel = typeRepr.getAbbreviatedLabel();
418
			}
419
			if (isBlank(typeLabel) && typeRepr != null){
420
				typeLabel = typeRepr.getLabel();
421
			}
422
			if (isBlank(typeLabel) ){
423
				typeLabel = type.getSymbol();
424
			}
425
			if (isBlank(typeLabel)){
426
				typeLabel = type.getTitleCache();
427
			}
428
		}
429
		if (isBlank(typeLabel)){
430
			typeLabel = "->";
431
		}
432
		String result = CdmUtils.concat(" ", new String[]{from == null ? null : from.getTitleCache(),
433
				typeLabel, to == null? null : to.getTitleCache()});
434
		return result;
435
	}
436

    
437

    
438
	private static String getCache(HomotypicalGroup hg) {
439
		String result = "";
440
		for (TaxonName tnb : hg.getTypifiedNames()){
441
			result = CdmUtils.concat(", ", result, tnb.getTitleCache());
442
		}
443
		if (isBlank(result)){
444
			result = "No typified names";
445
		}
446
		return result;
447
	}
448

    
449
	private static String getCache(KeyStatement ks) {
450
        String result = "";
451

    
452
        result = "KeyStatement";
453

    
454
        return result;
455
    }
456

    
457
	/**
458
	 * <p>getObjectClassname</p>
459
	 *
460
	 * @param element a {@link java.lang.Object} object.
461
	 * @return a {@link java.lang.String} object.
462
	 */
463
	public static String getObjectClassname(Object element) {
464
		return element.getClass().getSimpleName();
465
	}
466

    
467
	/**
468
	 * <p>getFeatureNodeContainerText</p>
469
	 *
470
	 * @param element a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
471
	 * @return a {@link java.lang.String} object.
472
	 */
473
	public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
474
		String result = null;
475
		if(element.getFeatureNode() != null && element.getFeatureNode().getFeature() != null){
476
			result = element.getFeatureNode().getFeature().getLabel(CdmStore.getDefaultLanguage());
477
		} else{
478
			return "No label set";
479
		}
480
		if (result == null){
481
			result = element.getFeatureNode().getFeature().getLabel();
482
		}
483
		return result;
484
	}
485

    
486
	/**
487
	 * <p>getQuantitativeDataText</p>
488
	 *
489
	 * @param element a {@link eu.etaxonomy.cdm.model.description.QuantitativeData} object.
490
	 * @return a {@link java.lang.String} object.
491
	 */
492
	public static String getQuantitativeDataText(QuantitativeData element) {
493
		TextData textData = quantitativeDescriptionBuilder.build(element, getLanguageList());
494

    
495
		return textData.getText(CdmStore.getDefaultLanguage());
496
	}
497

    
498
	/**
499
	 * <p>getCategoricalDataText</p>
500
	 *
501
	 * @param element a {@link eu.etaxonomy.cdm.model.description.CategoricalData} object.
502
	 * @return a {@link java.lang.String} object.
503
	 */
504
	public static String getCategoricalDataText(CategoricalData element) {
505
		TextData textData = categoricalDescriptionBuilder.build(element, getLanguageList());
506

    
507
		return textData.getText(CdmStore.getDefaultLanguage());
508
	}
509

    
510
	private static List<Language> getLanguageList(){
511
		return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
512
	}
513

    
514
	private static DescriptionBuilder<QuantitativeData> quantitativeDescriptionBuilder = new DefaultQuantitativeDescriptionBuilder();
515
	private static DescriptionBuilder<CategoricalData> categoricalDescriptionBuilder = new DefaultCategoricalDescriptionBuilder();
516

    
517

    
518
	/**
519
	 * <p>getDistributionText</p>
520
	 *
521
	 * @param element a {@link eu.etaxonomy.cdm.model.description.Distribution} object.
522
	 * @return a {@link java.lang.String} object.
523
	 */
524
	public static String getDistributionText(Distribution element) {
525

    
526
		String text = "EMPTY";
527

    
528
		Distribution distribution = element;
529

    
530
		NamedArea area = distribution.getArea();
531
		if(area != null){
532

    
533
			text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
534

    
535
			PresenceAbsenceTerm status = distribution.getStatus();
536

    
537
			if (status != null) {
538
				text += ", " + status.getLabel();
539
			}else{
540
				text += ", NO STATUS";
541
			}
542
		}
543
		return text;
544
	}
545

    
546
	/**
547
	 * <p>getImageText</p>
548
	 *
549
	 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
550
	 * @return a {@link java.lang.String} object.
551
	 */
552
	public static  String getImageText(Media media) {
553
		 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
554
		if (title != null) {
555
			return title.getText();
556
		}
557
		return "No title.";
558
	}
559

    
560

    
561

    
562
	/**
563
	 * <p>getElementText</p>
564
	 *
565
	 * @param element a {@link eu.etaxonomy.cdm.model.description.TextData} object.
566
	 * @return a {@link java.lang.String} object.
567
	 */
568
	public static  String getElementText(TextData element) {
569
		String text = null;
570
		if(element.getFeature().equals(Feature.CITATION())){
571
			text = "";
572
			for(DescriptionElementSource source : element.getSources()){
573
				if(source.getCitation() != null){
574
					text += source.getCitation().getTitleCache();
575
				}
576
				if(source.getNameUsedInSource() != null){
577
					text += " [ " + source.getNameUsedInSource().getTitleCache() + " ]";
578
				}
579
			}
580
			if(isBlank(text)){
581
				text = "No sources provided";
582
			}
583
		}else{
584
			List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
585
			LanguageString languageString = element.getPreferredLanguageString(languages);
586
			text = languageString != null ? languageString.getText() : null;
587
		}
588
		return isBlank(text) ? "No text provided" : text;
589
	}
590

    
591
	/**
592
	 * <p>getTaxonInteractionText</p>
593
	 *
594
	 * @param element a {@link eu.etaxonomy.cdm.model.description.TaxonInteraction} object.
595
	 * @return a {@link java.lang.String} object.
596
	 */
597
	public static  String getTaxonInteractionText(TaxonInteraction element) {
598
		String text = "";
599
		Taxon taxon2 = element.getTaxon2();
600
		if(taxon2 != null && taxon2.getName() != null){
601
			text = taxon2.getName().getTitleCache();
602
		}else{
603
			text = "No taxon chosen";
604
		}
605

    
606
		return text;
607
	}
608

    
609
	/**
610
	 * <p>getCommonNameText</p>
611
	 *
612
	 * @param commonName a {@link eu.etaxonomy.cdm.model.description.CommonTaxonName} object.
613
	 * @return a {@link java.lang.String} object.
614
	 */
615
	public static  String getCommonNameText(CommonTaxonName commonName) {
616
		String name = commonName.getName();
617
		if (name == null || name.length() == 0) {
618
			name = "No name provided";
619
		}
620
		Language language = commonName.getLanguage();
621
		if (language == null) {
622
			name += " (No language provided)";
623
		} else {
624
			name += " (" + language.getDescription() + ")";
625
		}
626
		return name;
627
	}
628

    
629
	/**
630
	 * <p>getDescriptionText</p>
631
	 *
632
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
633
	 * @return a {@link java.lang.String} object.
634
	 */
635
	public static  String getDescriptionText(DescriptionBase element) {
636
		String text = element.getTitleCache();
637
		if (text == null || text.length() == 0) {
638
			text = "No label provided";
639
		}
640

    
641
		return text;
642
	}
643

    
644
	/**
645
	 * <p>getIndividualsAssociationText</p>
646
	 *
647
	 * @param element a {@link eu.etaxonomy.cdm.model.description.IndividualsAssociation} object.
648
	 * @return a {@link java.lang.String} object.
649
	 */
650
	public static  String getIndividualsAssociationText(IndividualsAssociation element) {
651
		SpecimenOrObservationBase derivedUnit = element.getAssociatedSpecimenOrObservation();
652
		if(derivedUnit != null){
653
			return derivedUnit.getTitleCache();
654
		}
655
		return "No unit chosen";
656
	}
657

    
658
	/**
659
	 * <p>getLabel</p>
660
	 *
661
	 * @param element a {@link java.lang.Object} object.
662
	 * @return a {@link java.lang.String} object.
663
	 */
664
	public static String getLabel(Object element){
665
	    String noLabelString = "[no label]";
666
		if (element instanceof FeatureNodeContainer){
667
			return getFeatureNodeContainerText((FeatureNodeContainer) element);
668
		}
669
		else if (element instanceof DescriptionBase) {
670
			return getDescriptionText((DescriptionBase) element);
671
		}
672
		else if(element instanceof CategoricalData){
673
			String categoricalDataText = getCategoricalDataText((CategoricalData) element);
674
			if(categoricalDataText==null || categoricalDataText.isEmpty()){
675
			    categoricalDataText = noLabelString;
676
			}
677
            return categoricalDataText;
678
		}
679
		else if (element instanceof CommonTaxonName) {
680
			return getCommonNameText((CommonTaxonName) element);
681
		}
682
		else if (element instanceof Distribution) {
683
			return getDistributionText((Distribution) element);
684
		}
685
		else if (element instanceof IndividualsAssociation) {
686
			return getIndividualsAssociationText((IndividualsAssociation) element);
687
		}
688
		else if (element instanceof QuantitativeData) {
689
			String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
690
			if(quantitativeDataText==null || quantitativeDataText.isEmpty()){
691
                quantitativeDataText = noLabelString;
692
			}
693
            return quantitativeDataText;
694
		}
695
		else if (element instanceof TaxonInteraction) {
696
			return getTaxonInteractionText((TaxonInteraction) element);
697
		}
698
		else if (element instanceof TextData) {
699
			return getElementText((TextData) element);
700
		}
701
		else{
702
			return element.toString();
703
		}
704
	}
705
	
706
	private static boolean isNotBlank(String str){
707
		return StringUtils.isNotBlank(str);
708
	}
709
	
710
	private static boolean isBlank(String str){
711
		return StringUtils.isBlank(str);
712
	}
713
}
(14-14/41)