Project

General

Profile

Download (24.7 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.lang3.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.common.ExtendedTimePeriodFormatter;
24
import eu.etaxonomy.cdm.format.description.CategoricalDataFormatter;
25
import eu.etaxonomy.cdm.format.description.QuantitativeDataFormatter;
26
import eu.etaxonomy.cdm.model.agent.AgentBase;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.ExtendedTimePeriod;
29
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
30
import eu.etaxonomy.cdm.model.common.Language;
31
import eu.etaxonomy.cdm.model.common.LanguageString;
32
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
33
import eu.etaxonomy.cdm.model.common.Marker;
34
import eu.etaxonomy.cdm.model.common.MarkerType;
35
import eu.etaxonomy.cdm.model.common.RelationshipBase;
36
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
37
import eu.etaxonomy.cdm.model.common.TimePeriod;
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.TemporalData;
54
import eu.etaxonomy.cdm.model.description.TextData;
55
import eu.etaxonomy.cdm.model.location.NamedArea;
56
import eu.etaxonomy.cdm.model.media.Media;
57
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
58
import eu.etaxonomy.cdm.model.name.HybridRelationship;
59
import eu.etaxonomy.cdm.model.name.NameRelationship;
60
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
61
import eu.etaxonomy.cdm.model.name.NomenclaturalSource;
62
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
63
import eu.etaxonomy.cdm.model.name.TaxonName;
64
import eu.etaxonomy.cdm.model.name.TextualTypeDesignation;
65
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
66
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
67
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
68
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
69
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
70
import eu.etaxonomy.cdm.model.permission.Group;
71
import eu.etaxonomy.cdm.model.permission.User;
72
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
73
import eu.etaxonomy.cdm.model.taxon.Classification;
74
import eu.etaxonomy.cdm.model.taxon.Taxon;
75
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
76
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
77
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
78
import eu.etaxonomy.cdm.model.term.Representation;
79
import eu.etaxonomy.taxeditor.store.CdmStore;
80

    
81
/**
82
 * <p>DescriptionHelper class.</p>
83
 *
84
 * @author p.ciardelli
85
 * @author n.hoffmann
86
 */
87
public class DescriptionHelper {
88

    
89
    public static final String NO_LABEL_STRING = "[no label]";
90

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

    
102
		String mainElementLabel= null;
103
		DescriptionBase<?> descr = element.getInDescription();
104
		descr = CdmBase.deproxy(descr, DescriptionBase.class);
105

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

    
125
		String cache = null;
126
		if (element instanceof TextData) {
127
			//cache = ((TextData) element).getText(language);
128
		    cache = "Text Data";
129
		}
130
		if (element instanceof CommonTaxonName) {
131
			cache = ((CommonTaxonName) element).getName();
132
		}
133
		if (element instanceof TaxonInteraction) {
134
			Taxon taxon2 = ((TaxonInteraction) element).getTaxon2();
135
			if(taxon2 != null && taxon2.getName() != null){
136
				cache = taxon2.getName().getTitleCache();
137
			}else{
138
				cache = "No taxon chosen";
139
			}
140
		}
141
		if (element instanceof Distribution) {
142
			Distribution distribution = (Distribution) element;
143

    
144
			NamedArea area = distribution.getArea();
145
			if(area != null){
146
				cache =  area.getLabel();
147

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

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

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

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

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

    
212
	public static String getObjectDescription(Object element) {
213
	    String resultString = null;
214
		if (element instanceof IdentifiableEntity) {
215
			try{
216
			    resultString = ((IdentifiableEntity) element).getTitleCache();
217
			}catch(LazyInitializationException e){
218
				String result = "No Session to initialize title cache for IdentifiableEntity";
219
				MessagingUtils.error(DescriptionHelper.class, result, e);
220
				resultString = "TODO: " + result;
221
			}
222
		}else if (element instanceof OriginalSourceBase) {
223
			try{
224
				OriginalSourceBase<?> originalSource = (OriginalSourceBase<?>) element;
225
//				ISourceable sourcedObject = originalSource.getSourcedObj();
226
				//due to #5743 the bidirectionality for sourced object had to be removed
227

    
228
				String sourceObjectTitle = "sourced object data not available (#5743)";
229
				if (originalSource instanceof NomenclaturalSource){
230
				    sourceObjectTitle = ((NomenclaturalSource)originalSource).getSourcedName().getTitleCache();
231
				}
232

    
233

    
234
//				if(sourcedObject instanceof IIdentifiableEntity){
235
//					sourceObjectTitle = ((IdentifiableEntity) sourcedObject).getTitleCache();
236
//				}else if(sourcedObject instanceof DescriptionElementBase){
237
//					sourceObjectTitle = "Element for description: " + ((DescriptionElementBase) sourcedObject).getInDescription().getTitleCache();
238
//				}else{
239
//					throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
240
//				}
241

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

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

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

    
292
		String result = "";
293

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

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

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

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

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

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

    
337
		return result;
338
	}
339

    
340
	private static String getCache(DeterminationEvent detEvent) {
341
		//taxon
342
		String taxonStr = null;
343
		TaxonName taxonName = detEvent.getTaxonName();
344
		TaxonBase<?> taxon = detEvent.getTaxon();
345
		if (taxonName != null){
346
			taxonStr = taxonName.getTitleCache();
347
		}
348
		if (isBlank(taxonStr) && taxon != null){
349
			taxonStr = taxon.getTitleCache();
350
		}
351
		if (isBlank(taxonStr)){
352
			taxonStr = "no or unlabled taxon";
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
	public static String getObjectClassname(Object element) {
525
		return element.getClass().getSimpleName();
526
	}
527

    
528
	public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
529
		String result = null;
530
		if(element.getFeatureNode() != null && element.getFeatureNode().getTerm() != null){
531
			result = element.getFeatureNode().getTerm().getLabel(CdmStore.getDefaultLanguage());
532
		} else{
533
			return "No label set";
534
		}
535
		if (result == null){
536
			result = element.getFeatureNode().getTerm().getLabel();
537
		}
538
		return result;
539
	}
540

    
541
	public static String getQuantitativeDataText(QuantitativeData element) {
542
	    QuantitativeDataFormatter formatter = new QuantitativeDataFormatter(null, null);
543
	    String label = formatter.format(element, getLanguageList());
544
		return label;
545
	}
546

    
547
	public static String getCategoricalDataText(CategoricalData element) {
548
	    CategoricalDataFormatter formatter = new CategoricalDataFormatter(null, null);
549
		return formatter.format(element, getLanguageList());
550
	}
551

    
552
	private static List<Language> getLanguageList(){
553
		return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
554
	}
555

    
556
	public static String getDistributionText(Distribution element) {
557

    
558
		String text = "EMPTY";
559

    
560
		Distribution distribution = element;
561

    
562
		NamedArea area = distribution.getArea();
563
		if(area != null){
564

    
565
			text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
566

    
567
			PresenceAbsenceTerm status = distribution.getStatus();
568

    
569
			if (status != null) {
570
				text += ", " + status.getLabel();
571
			}else{
572
				text += ", NO STATUS";
573
			}
574
		}
575
		return text;
576
	}
577

    
578
	public static  String getImageText(Media media) {
579
		 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
580
		if (title != null) {
581
			return title.getText();
582
		}
583
		return "No title.";
584
	}
585

    
586
	public static  String getElementText(TextData element) {
587
		String text = null;
588
		if(element.getFeature().equals(Feature.CITATION())){
589
			text = "";
590
			for(DescriptionElementSource source : element.getSources()){
591
				if(source.getCitation() != null){
592
					text += source.getCitation().getTitleCache();
593
				}
594
				if(source.getNameUsedInSource() != null){
595
					text += " [" + source.getNameUsedInSource().getTitleCache() + "]";
596
				}
597
			}
598
			if(isBlank(text)){
599
				text = "No sources provided";
600
			}
601
		}else{
602
			List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
603
			LanguageString languageString = element.getPreferredLanguageString(languages);
604
			text = languageString != null ? languageString.getText() : null;
605
		}
606
		return isBlank(text) ? "No text provided" : text;
607
	}
608

    
609
	public static  String getTaxonInteractionText(TaxonInteraction element) {
610
		String text = "";
611
		Taxon taxon2 = element.getTaxon2();
612
		if(taxon2 != null && taxon2.getName() != null){
613
			text = taxon2.getName().getTitleCache();
614
		}else{
615
			text = "No taxon chosen";
616
		}
617

    
618
		return text;
619
	}
620

    
621
	public static  String getCommonNameText(CommonTaxonName commonName) {
622
		String name = commonName.getName();
623
		if (name == null || name.length() == 0) {
624
			name = "No name provided";
625
		}
626
		Language language = commonName.getLanguage();
627
		if (language == null) {
628
			name += " (No language provided)";
629
		} else {
630
			name += " (" + language.getLabel(CdmStore.getDefaultLanguage()) + ")";
631
		}
632
		return name;
633
	}
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
	public static  String getIndividualsAssociationText(IndividualsAssociation element) {
645
		SpecimenOrObservationBase<?> derivedUnit = element.getAssociatedSpecimenOrObservation();
646
		if(derivedUnit != null){
647
			return derivedUnit.getTitleCache();
648
		}
649
		return "No unit chosen";
650
	}
651

    
652
	public static String getLabel(Object element){
653
		if (element instanceof FeatureNodeContainer){
654
			return getFeatureNodeContainerText((FeatureNodeContainer) element);
655
		}
656
		else if (element instanceof DescriptionBase) {
657
			return getDescriptionText((DescriptionBase<?>) element);
658
		}
659
		else if(element instanceof CategoricalData){
660
			String categoricalDataText = getCategoricalDataText((CategoricalData) element);
661
			if(categoricalDataText==null || categoricalDataText.isEmpty()){
662
			    categoricalDataText = NO_LABEL_STRING;
663
			}
664
            return categoricalDataText;
665
		}
666
		else if (element instanceof CommonTaxonName) {
667
			return getCommonNameText((CommonTaxonName) element);
668
		}
669
		else if (element instanceof Distribution) {
670
			return getDistributionText((Distribution) element);
671
		}
672
		else if (element instanceof IndividualsAssociation) {
673
			return getIndividualsAssociationText((IndividualsAssociation) element);
674
		}
675
		else if (element instanceof QuantitativeData) {
676
			String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
677
			if(isBlank(quantitativeDataText)){
678
                quantitativeDataText = NO_LABEL_STRING;
679
			}
680
            return quantitativeDataText;
681
		}
682
		else if (element instanceof TaxonInteraction) {
683
			return getTaxonInteractionText((TaxonInteraction) element);
684
		}
685
		else if (element instanceof TextData) {
686
			return getElementText((TextData) element);
687
		}
688
		else if (element instanceof TemporalData) {
689
            String lable = getTemporalDataText((TemporalData) element);
690
            if(isBlank(lable)){
691
                lable = NO_LABEL_STRING;
692
            }
693
            return lable;
694
        }
695
		else{
696
			return element.toString();
697
		}
698
	}
699

    
700
    private static String getTemporalDataText(TemporalData element) {
701
        ExtendedTimePeriod period = element.getPeriod();
702
        ExtendedTimePeriodFormatter formatter = ExtendedTimePeriodFormatter.NewDefaultInstance();
703
        return period == null ? NO_LABEL_STRING : formatter.format(period);
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)