Project

General

Profile

Download (24.5 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
package eu.etaxonomy.taxeditor.model;
10

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

    
17
import org.apache.commons.lang3.StringUtils;
18
import org.hibernate.LazyInitializationException;
19

    
20
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
21
import eu.etaxonomy.cdm.common.CdmUtils;
22
import eu.etaxonomy.cdm.format.common.ExtendedTimePeriodFormatter;
23
import eu.etaxonomy.cdm.format.description.CategoricalDataFormatter;
24
import eu.etaxonomy.cdm.format.description.QuantitativeDataFormatter;
25
import eu.etaxonomy.cdm.model.agent.AgentBase;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.common.ExtendedTimePeriod;
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.TemporalData;
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.NomenclaturalSource;
61
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
62
import eu.etaxonomy.cdm.model.name.TaxonName;
63
import eu.etaxonomy.cdm.model.name.TextualTypeDesignation;
64
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
65
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
66
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
67
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
68
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
69
import eu.etaxonomy.cdm.model.permission.Group;
70
import eu.etaxonomy.cdm.model.permission.User;
71
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
72
import eu.etaxonomy.cdm.model.taxon.Classification;
73
import eu.etaxonomy.cdm.model.taxon.Taxon;
74
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
75
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
76
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
77
import eu.etaxonomy.cdm.model.term.Representation;
78
import eu.etaxonomy.taxeditor.store.CdmStore;
79

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

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

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

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

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

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

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

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

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

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

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

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

    
227
				String sourceObjectTitle = "sourced object data not available (#5743)";
228

    
229
				//it is now possible for NomenclaturalSource as they link to the sourced name
230
				if (originalSource instanceof NomenclaturalSource){
231
				    TaxonName sourcedName = ((NomenclaturalSource)originalSource).getSourcedName();
232
				    sourceObjectTitle = sourcedName == null ? "Source not attached to a name" :
233
				        "Nom. source for " + sourcedName.getTitleCache();
234
				}
235

    
236

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

    
277
		if (resultString == null){
278
		    resultString = element.toString();
279
		}
280
		return resultString;
281
	}
282

    
283
	private static String getCache(GatheringEvent gatheringEvent){
284
		String ALTITUDE_PREFIX = "alt. ";
285
		final String METER = "m";
286

    
287
		String result = "";
288

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

    
294
		//gathering period
295
		TimePeriod gatheringPeriod = gatheringEvent.getTimeperiod();
296
		result = CdmUtils.concat(", ", result, (gatheringPeriod == null? null : gatheringPeriod.toString()));
297

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

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

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

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

    
332
		return result;
333
	}
334

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

    
350
		//unit
351
		SpecimenOrObservationBase<?> unit = detEvent.getIdentifiedUnit();
352
		String unitStr;
353
		if (unit != null){
354
			unitStr = unit.getTitleCache();
355
			if (isBlank(unitStr)){
356
				unitStr = "Unlabled unit";
357
			}
358
		}else{
359
			unitStr = "no unit";
360
		}
361

    
362
		String result = CdmUtils.concat(" determined as ", unitStr, taxonStr);
363

    
364
		return result;
365
	}
366

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

    
396
		return result;
397
	}
398

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

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

    
499

    
500
	private static String getCache(HomotypicalGroup hg) {
501
		String result = "";
502
		for (TaxonName tnb : hg.getTypifiedNames()){
503
			result = CdmUtils.concat(", ", result, tnb.getTitleCache());
504
		}
505
		if (isBlank(result)){
506
			result = "No typified names";
507
		}
508
		return result;
509
	}
510

    
511
	private static String getCache(KeyStatement ks) {
512
        String result = "";
513

    
514
        result = "KeyStatement";
515

    
516
        return result;
517
    }
518

    
519
	public static String getObjectClassname(Object element) {
520
		return element.getClass().getSimpleName();
521
	}
522

    
523
	public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
524
		String result = null;
525
		if(element.getFeatureNode() != null && element.getFeatureNode().getTerm() != null){
526
			result = element.getFeatureNode().getTerm().getLabel(CdmStore.getDefaultLanguage());
527
		} else{
528
			return "No label set";
529
		}
530
		if (result == null){
531
			result = element.getFeatureNode().getTerm().getLabel();
532
		}
533
		return result;
534
	}
535

    
536
	public static String getQuantitativeDataText(QuantitativeData element) {
537
	    QuantitativeDataFormatter formatter = new QuantitativeDataFormatter(null, null);
538
	    String label = formatter.format(element, getLanguageList());
539
		return label;
540
	}
541

    
542
	public static String getCategoricalDataText(CategoricalData element) {
543
	    CategoricalDataFormatter formatter = new CategoricalDataFormatter(null, null);
544
		return formatter.format(element, getLanguageList());
545
	}
546

    
547
	private static List<Language> getLanguageList(){
548
		return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
549
	}
550

    
551
	public static String getDistributionText(Distribution element) {
552

    
553
		String text = "EMPTY";
554

    
555
		Distribution distribution = element;
556

    
557
		NamedArea area = distribution.getArea();
558
		if(area != null){
559

    
560
			text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
561

    
562
			PresenceAbsenceTerm status = distribution.getStatus();
563

    
564
			if (status != null) {
565
				text += ", " + status.getLabel();
566
			}else{
567
				text += ", NO STATUS";
568
			}
569
		}
570
		return text;
571
	}
572

    
573
	public static  String getImageText(Media media) {
574
		 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
575
		if (title != null) {
576
			return title.getText();
577
		}
578
		return "No title.";
579
	}
580

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

    
604
	public static  String getTaxonInteractionText(TaxonInteraction element) {
605
		String text = "";
606
		Taxon taxon2 = element.getTaxon2();
607
		if(taxon2 != null && taxon2.getName() != null){
608
			text = taxon2.getName().getTitleCache();
609
		}else{
610
			text = "No taxon chosen";
611
		}
612

    
613
		return text;
614
	}
615

    
616
	public static  String getCommonNameText(CommonTaxonName commonName) {
617
		String name = commonName.getName();
618
		if (name == null || name.length() == 0) {
619
			name = "No name provided";
620
		}
621
		Language language = commonName.getLanguage();
622
		if (language == null) {
623
			name += " (No language provided)";
624
		} else {
625
			name += " (" + language.getLabel(CdmStore.getDefaultLanguage()) + ")";
626
		}
627
		return name;
628
	}
629

    
630
	public static  String getDescriptionText(DescriptionBase element) {
631
		String text = element.getTitleCache();
632
		if (text == null || text.length() == 0) {
633
			text = "No label provided";
634
		}
635

    
636
		return text;
637
	}
638

    
639
	public static  String getIndividualsAssociationText(IndividualsAssociation element) {
640
		SpecimenOrObservationBase<?> derivedUnit = element.getAssociatedSpecimenOrObservation();
641
		if(derivedUnit != null){
642
			return derivedUnit.getTitleCache();
643
		}
644
		return "No unit chosen";
645
	}
646

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

    
695
    private static String getTemporalDataText(TemporalData element) {
696
        ExtendedTimePeriod period = element.getPeriod();
697
        ExtendedTimePeriodFormatter formatter = ExtendedTimePeriodFormatter.NewDefaultInstance();
698
        return period == null ? NO_LABEL_STRING : formatter.format(period);
699
    }
700

    
701
    private static boolean isNotBlank(String str){
702
		return StringUtils.isNotBlank(str);
703
	}
704

    
705
	private static boolean isBlank(String str){
706
		return StringUtils.isBlank(str);
707
	}
708
}
(14-14/41)