Project

General

Profile

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

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

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

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

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

    
119

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

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

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

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

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

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

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

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

    
208

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

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

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

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

    
289
		String result = "";
290

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

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

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

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

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

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

    
334
		return result;
335
	}
336

    
337

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

    
353

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

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

    
368
		return result;
369
	}
370

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

    
400
		return result;
401
	}
402

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

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

    
497

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

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

    
512
        result = "KeyStatement";
513

    
514
        return result;
515
    }
516

    
517
	/**
518
	 * <p>getObjectClassname</p>
519
	 *
520
	 * @param element a {@link java.lang.Object} object.
521
	 * @return a {@link java.lang.String} object.
522
	 */
523
	public static String getObjectClassname(Object element) {
524
		return element.getClass().getSimpleName();
525
	}
526

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

    
546
	/**
547
	 * <p>getQuantitativeDataText</p>
548
	 *
549
	 * @param element a {@link eu.etaxonomy.cdm.model.description.QuantitativeData} object.
550
	 * @return a {@link java.lang.String} object.
551
	 */
552
	public static String getQuantitativeDataText(QuantitativeData element) {
553
		TextData textData = quantitativeDescriptionBuilder.build(element, getLanguageList());
554

    
555
		return textData.getText(CdmStore.getDefaultLanguage());
556
	}
557

    
558
	/**
559
	 * <p>getCategoricalDataText</p>
560
	 *
561
	 * @param element a {@link eu.etaxonomy.cdm.model.description.CategoricalData} object.
562
	 * @return a {@link java.lang.String} object.
563
	 */
564
	public static String getCategoricalDataText(CategoricalData element) {
565
		TextData textData = categoricalDescriptionBuilder.build(element, getLanguageList());
566

    
567
		return textData.getText(CdmStore.getDefaultLanguage());
568
	}
569

    
570
	private static List<Language> getLanguageList(){
571
		return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
572
	}
573

    
574
	private static DescriptionBuilder<QuantitativeData> quantitativeDescriptionBuilder = new DefaultQuantitativeDescriptionBuilder();
575
	private static DescriptionBuilder<CategoricalData> categoricalDescriptionBuilder = new DefaultCategoricalDescriptionBuilder();
576
    public static final String NO_LABEL_STRING = "[no label]";
577

    
578

    
579
	/**
580
	 * <p>getDistributionText</p>
581
	 *
582
	 * @param element a {@link eu.etaxonomy.cdm.model.description.Distribution} object.
583
	 * @return a {@link java.lang.String} object.
584
	 */
585
	public static String getDistributionText(Distribution element) {
586

    
587
		String text = "EMPTY";
588

    
589
		Distribution distribution = element;
590

    
591
		NamedArea area = distribution.getArea();
592
		if(area != null){
593

    
594
			text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
595

    
596
			PresenceAbsenceTerm status = distribution.getStatus();
597

    
598
			if (status != null) {
599
				text += ", " + status.getLabel();
600
			}else{
601
				text += ", NO STATUS";
602
			}
603
		}
604
		return text;
605
	}
606

    
607
	/**
608
	 * <p>getImageText</p>
609
	 *
610
	 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
611
	 * @return a {@link java.lang.String} object.
612
	 */
613
	public static  String getImageText(Media media) {
614
		 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
615
		if (title != null) {
616
			return title.getText();
617
		}
618
		return "No title.";
619
	}
620

    
621

    
622

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

    
652
	/**
653
	 * <p>getTaxonInteractionText</p>
654
	 *
655
	 * @param element a {@link eu.etaxonomy.cdm.model.description.TaxonInteraction} object.
656
	 * @return a {@link java.lang.String} object.
657
	 */
658
	public static  String getTaxonInteractionText(TaxonInteraction element) {
659
		String text = "";
660
		Taxon taxon2 = element.getTaxon2();
661
		if(taxon2 != null && taxon2.getName() != null){
662
			text = taxon2.getName().getTitleCache();
663
		}else{
664
			text = "No taxon chosen";
665
		}
666

    
667
		return text;
668
	}
669

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

    
690
	/**
691
	 * <p>getDescriptionText</p>
692
	 *
693
	 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
694
	 * @return a {@link java.lang.String} object.
695
	 */
696
	public static  String getDescriptionText(DescriptionBase element) {
697
		String text = element.getTitleCache();
698
		if (text == null || text.length() == 0) {
699
			text = "No label provided";
700
		}
701

    
702
		return text;
703
	}
704

    
705
	/**
706
	 * <p>getIndividualsAssociationText</p>
707
	 *
708
	 * @param element a {@link eu.etaxonomy.cdm.model.description.IndividualsAssociation} object.
709
	 * @return a {@link java.lang.String} object.
710
	 */
711
	public static  String getIndividualsAssociationText(IndividualsAssociation element) {
712
		SpecimenOrObservationBase derivedUnit = element.getAssociatedSpecimenOrObservation();
713
		if(derivedUnit != null){
714
			return derivedUnit.getTitleCache();
715
		}
716
		return "No unit chosen";
717
	}
718

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

    
766
	private static boolean isNotBlank(String str){
767
		return StringUtils.isNotBlank(str);
768
	}
769

    
770
	private static boolean isBlank(String str){
771
		return StringUtils.isBlank(str);
772
	}
773
}
(14-14/41)