Project

General

Profile

Download (24.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.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.format.description.QuantitativeDataFormatter;
27
import eu.etaxonomy.cdm.model.agent.AgentBase;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.common.ExtendedTimePeriod;
30
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
31
import eu.etaxonomy.cdm.model.common.Language;
32
import eu.etaxonomy.cdm.model.common.LanguageString;
33
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
34
import eu.etaxonomy.cdm.model.common.Marker;
35
import eu.etaxonomy.cdm.model.common.MarkerType;
36
import eu.etaxonomy.cdm.model.common.RelationshipBase;
37
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
38
import eu.etaxonomy.cdm.model.common.TimePeriod;
39
import eu.etaxonomy.cdm.model.description.CategoricalData;
40
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
41
import eu.etaxonomy.cdm.model.description.DescriptionBase;
42
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
43
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
44
import eu.etaxonomy.cdm.model.description.Distribution;
45
import eu.etaxonomy.cdm.model.description.Feature;
46
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
47
import eu.etaxonomy.cdm.model.description.KeyStatement;
48
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
49
import eu.etaxonomy.cdm.model.description.QuantitativeData;
50
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
51
import eu.etaxonomy.cdm.model.description.TaxonDescription;
52
import eu.etaxonomy.cdm.model.description.TaxonInteraction;
53
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
54
import eu.etaxonomy.cdm.model.description.TemporalData;
55
import eu.etaxonomy.cdm.model.description.TextData;
56
import eu.etaxonomy.cdm.model.location.NamedArea;
57
import eu.etaxonomy.cdm.model.media.Media;
58
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
59
import eu.etaxonomy.cdm.model.name.HybridRelationship;
60
import eu.etaxonomy.cdm.model.name.NameRelationship;
61
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
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
	/**
90
	 * Returns whatever the element's title cache equivalent is,
91
	 * depending on its class.
92
	 *
93
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
94
	 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
95
	 * @return a {@link java.lang.String} object.
96
	 */
97
	public static String getCache(DescriptionElementBase element,
98
			Language language) {
99

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

    
104
		if (descr != null){
105
			if (descr.isInstanceOf(TaxonDescription.class)){
106
				Taxon taxon = CdmBase.deproxy(descr, TaxonDescription.class).getTaxon();
107
				if (taxon != null){
108
					mainElementLabel = taxon.getTitleCache();
109
				}
110
			}else if (descr.isInstanceOf(SpecimenDescription.class)){
111
				SpecimenOrObservationBase<?> specimen = CdmBase.deproxy(descr, SpecimenDescription.class).getDescribedSpecimenOrObservation();
112
				if (specimen != null){
113
					mainElementLabel = specimen.getTitleCache();
114
				}
115
			}else if (descr.isInstanceOf(TaxonNameDescription.class)){
116
				TaxonName name = CdmBase.deproxy(descr, TaxonNameDescription.class).getTaxonName();
117
				if (name != null){
118
					mainElementLabel = name.getTitleCache();
119
				}
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
		}
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
				String sourceObjectTitle = "sourced object data not available (#5743)";
228
//				if(sourcedObject instanceof IIdentifiableEntity){
229
//					sourceObjectTitle = ((IdentifiableEntity) sourcedObject).getTitleCache();
230
//				}else if(sourcedObject instanceof DescriptionElementBase){
231
//					sourceObjectTitle = "Element for description: " + ((DescriptionElementBase) sourcedObject).getInDescription().getTitleCache();
232
//				}else{
233
//					throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
234
//				}
235

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

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

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

    
286
		String result = "";
287

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

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

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

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

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

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

    
331
		return result;
332
	}
333

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

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

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

    
363
		return result;
364
	}
365

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

    
395
		return result;
396
	}
397

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

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

    
498

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

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

    
513
        result = "KeyStatement";
514

    
515
        return result;
516
    }
517

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

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

    
535
	public static String getQuantitativeDataText(QuantitativeData element) {
536
	    QuantitativeDataFormatter formatter = new QuantitativeDataFormatter(null, null);
537
	    String label = formatter.doFormat(element);
538

    
539
		return label;
540
	}
541

    
542
	public static String getCategoricalDataText(CategoricalData element) {
543
		TextData textData = categoricalDescriptionBuilder.build(element, getLanguageList());
544

    
545
		return textData.getText(CdmStore.getDefaultLanguage());
546
	}
547

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

    
552
	private static DescriptionBuilder<QuantitativeData> quantitativeDescriptionBuilder = new DefaultQuantitativeDescriptionBuilder();
553
	private static DescriptionBuilder<CategoricalData> categoricalDescriptionBuilder = new DefaultCategoricalDescriptionBuilder();
554
    public static final String NO_LABEL_STRING = "[no label]";
555

    
556

    
557
	public static String getDistributionText(Distribution element) {
558

    
559
		String text = "EMPTY";
560

    
561
		Distribution distribution = element;
562

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

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

    
568
			PresenceAbsenceTerm status = distribution.getStatus();
569

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

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

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

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

    
619
		return text;
620
	}
621

    
622
	public static  String getCommonNameText(CommonTaxonName commonName) {
623
		String name = commonName.getName();
624
		if (name == null || name.length() == 0) {
625
			name = "No name provided";
626
		}
627
		Language language = commonName.getLanguage();
628
		if (language == null) {
629
			name += " (No language provided)";
630
		} else {
631
			name += " (" + language.getLabel(CdmStore.getDefaultLanguage()) + ")";
632
		}
633
		return name;
634
	}
635

    
636
	public static  String getDescriptionText(DescriptionBase element) {
637
		String text = element.getTitleCache();
638
		if (text == null || text.length() == 0) {
639
			text = "No label provided";
640
		}
641

    
642
		return text;
643
	}
644

    
645
	public static  String getIndividualsAssociationText(IndividualsAssociation element) {
646
		SpecimenOrObservationBase<?> derivedUnit = element.getAssociatedSpecimenOrObservation();
647
		if(derivedUnit != null){
648
			return derivedUnit.getTitleCache();
649
		}
650
		return "No unit chosen";
651
	}
652

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

    
701
    private static String getTemporalDataText(TemporalData element) {
702
        String result = "";
703

    
704
        ExtendedTimePeriod period = element.getPeriod();
705

    
706
        if (period != null){
707
            result = period.getTimePeriod();
708
        }
709

    
710
        return result;
711
    }
712

    
713
    private static boolean isNotBlank(String str){
714
		return StringUtils.isNotBlank(str);
715
	}
716

    
717
	private static boolean isBlank(String str){
718
		return StringUtils.isBlank(str);
719
	}
720
}
(14-14/41)