show language label in common name label in factual data view
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / DescriptionHelper.java
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.Group;
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.common.User;
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.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.SpecimenTypeDesignation;
62 import eu.etaxonomy.cdm.model.name.TaxonName;
63 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
64 import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
65 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
66 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
67 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
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 for (TaxonName name : from){
437 CdmUtils.concat(",", fromString, name.getTitleCache());
438 }
439 String result = CdmUtils.concat(" ", new String[]{from == null ? null : fromString,
440 typeLabel, to == null? null : to.getTitleCache()});
441 return result;
442 }
443
444 private static String getCache(RelationshipBase<?, ?, ?> rel) {
445 rel = CdmBase.deproxy(rel);
446 RelationshipTermBase<?> type = rel.getType();
447 IdentifiableEntity<?> from;
448 IdentifiableEntity<?> to;
449 if (rel.isInstanceOf(NameRelationship.class)){
450 from = ((NameRelationship)rel).getFromName();
451 to = ((NameRelationship)rel).getToName();
452 }else if (rel.isInstanceOf(HybridRelationship.class)){
453 from = ((HybridRelationship)rel).getParentName();
454 to = ((HybridRelationship)rel).getHybridName();
455 }else if (rel.isInstanceOf(TaxonRelationship.class)){
456 from = ((TaxonRelationship)rel).getFromTaxon();
457 to = ((TaxonRelationship)rel).getToTaxon();
458 }else{
459 try {
460 Method fromMethod = rel.getClass().getMethod("getRelatedFrom");
461 Method toMethod = rel.getClass().getMethod("getRelatedFrom");
462 fromMethod.setAccessible(true);
463 toMethod.setAccessible(true);
464 from = (IdentifiableEntity<?>)fromMethod.invoke(rel);
465 to = (IdentifiableEntity<?>)toMethod.invoke(rel);
466 } catch (NoSuchMethodException | SecurityException | IllegalAccessException
467 | IllegalArgumentException | InvocationTargetException e) {
468 throw new RuntimeException(e);
469 }
470 }
471 String typeLabel = null;
472 if (type != null){
473 Representation typeRepr = type.getPreferredRepresentation(CdmStore.getDefaultLanguage());
474 if (typeRepr != null){
475 typeLabel = typeRepr.getAbbreviatedLabel();
476 }
477 if (isBlank(typeLabel) && typeRepr != null){
478 typeLabel = typeRepr.getLabel();
479 }
480 if (isBlank(typeLabel) ){
481 typeLabel = type.getSymbol();
482 }
483 if (isBlank(typeLabel)){
484 typeLabel = type.getTitleCache();
485 }
486 }
487 if (isBlank(typeLabel)){
488 typeLabel = "->";
489 }
490 String result = CdmUtils.concat(" ", new String[]{from == null ? null : from.getTitleCache(),
491 typeLabel, to == null? null : to.getTitleCache()});
492 return result;
493 }
494
495
496 private static String getCache(HomotypicalGroup hg) {
497 String result = "";
498 for (TaxonName tnb : hg.getTypifiedNames()){
499 result = CdmUtils.concat(", ", result, tnb.getTitleCache());
500 }
501 if (isBlank(result)){
502 result = "No typified names";
503 }
504 return result;
505 }
506
507 private static String getCache(KeyStatement ks) {
508 String result = "";
509
510 result = "KeyStatement";
511
512 return result;
513 }
514
515 /**
516 * <p>getObjectClassname</p>
517 *
518 * @param element a {@link java.lang.Object} object.
519 * @return a {@link java.lang.String} object.
520 */
521 public static String getObjectClassname(Object element) {
522 return element.getClass().getSimpleName();
523 }
524
525 /**
526 * <p>getFeatureNodeContainerText</p>
527 *
528 * @param element a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
529 * @return a {@link java.lang.String} object.
530 */
531 public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
532 String result = null;
533 if(element.getFeatureNode() != null && element.getFeatureNode().getTerm() != null){
534 result = element.getFeatureNode().getTerm().getLabel(CdmStore.getDefaultLanguage());
535 } else{
536 return "No label set";
537 }
538 if (result == null){
539 result = element.getFeatureNode().getTerm().getLabel();
540 }
541 return result;
542 }
543
544 /**
545 * <p>getQuantitativeDataText</p>
546 *
547 * @param element a {@link eu.etaxonomy.cdm.model.description.QuantitativeData} object.
548 * @return a {@link java.lang.String} object.
549 */
550 public static String getQuantitativeDataText(QuantitativeData element) {
551 TextData textData = quantitativeDescriptionBuilder.build(element, getLanguageList());
552
553 return textData.getText(CdmStore.getDefaultLanguage());
554 }
555
556 /**
557 * <p>getCategoricalDataText</p>
558 *
559 * @param element a {@link eu.etaxonomy.cdm.model.description.CategoricalData} object.
560 * @return a {@link java.lang.String} object.
561 */
562 public static String getCategoricalDataText(CategoricalData element) {
563 TextData textData = categoricalDescriptionBuilder.build(element, getLanguageList());
564
565 return textData.getText(CdmStore.getDefaultLanguage());
566 }
567
568 private static List<Language> getLanguageList(){
569 return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
570 }
571
572 private static DescriptionBuilder<QuantitativeData> quantitativeDescriptionBuilder = new DefaultQuantitativeDescriptionBuilder();
573 private static DescriptionBuilder<CategoricalData> categoricalDescriptionBuilder = new DefaultCategoricalDescriptionBuilder();
574
575
576 /**
577 * <p>getDistributionText</p>
578 *
579 * @param element a {@link eu.etaxonomy.cdm.model.description.Distribution} object.
580 * @return a {@link java.lang.String} object.
581 */
582 public static String getDistributionText(Distribution element) {
583
584 String text = "EMPTY";
585
586 Distribution distribution = element;
587
588 NamedArea area = distribution.getArea();
589 if(area != null){
590
591 text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
592
593 PresenceAbsenceTerm status = distribution.getStatus();
594
595 if (status != null) {
596 text += ", " + status.getLabel();
597 }else{
598 text += ", NO STATUS";
599 }
600 }
601 return text;
602 }
603
604 /**
605 * <p>getImageText</p>
606 *
607 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
608 * @return a {@link java.lang.String} object.
609 */
610 public static String getImageText(Media media) {
611 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
612 if (title != null) {
613 return title.getText();
614 }
615 return "No title.";
616 }
617
618
619
620 /**
621 * <p>getElementText</p>
622 *
623 * @param element a {@link eu.etaxonomy.cdm.model.description.TextData} object.
624 * @return a {@link java.lang.String} object.
625 */
626 public static String getElementText(TextData element) {
627 String text = null;
628 if(element.getFeature().equals(Feature.CITATION())){
629 text = "";
630 for(DescriptionElementSource source : element.getSources()){
631 if(source.getCitation() != null){
632 text += source.getCitation().getTitleCache();
633 }
634 if(source.getNameUsedInSource() != null){
635 text += " [ " + source.getNameUsedInSource().getTitleCache() + " ]";
636 }
637 }
638 if(isBlank(text)){
639 text = "No sources provided";
640 }
641 }else{
642 List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
643 LanguageString languageString = element.getPreferredLanguageString(languages);
644 text = languageString != null ? languageString.getText() : null;
645 }
646 return isBlank(text) ? "No text provided" : text;
647 }
648
649 /**
650 * <p>getTaxonInteractionText</p>
651 *
652 * @param element a {@link eu.etaxonomy.cdm.model.description.TaxonInteraction} object.
653 * @return a {@link java.lang.String} object.
654 */
655 public static String getTaxonInteractionText(TaxonInteraction element) {
656 String text = "";
657 Taxon taxon2 = element.getTaxon2();
658 if(taxon2 != null && taxon2.getName() != null){
659 text = taxon2.getName().getTitleCache();
660 }else{
661 text = "No taxon chosen";
662 }
663
664 return text;
665 }
666
667 /**
668 * <p>getCommonNameText</p>
669 *
670 * @param commonName a {@link eu.etaxonomy.cdm.model.description.CommonTaxonName} object.
671 * @return a {@link java.lang.String} object.
672 */
673 public static String getCommonNameText(CommonTaxonName commonName) {
674 String name = commonName.getName();
675 if (name == null || name.length() == 0) {
676 name = "No name provided";
677 }
678 Language language = commonName.getLanguage();
679 if (language == null) {
680 name += " (No language provided)";
681 } else {
682 name += " (" + language.getLabel(CdmStore.getDefaultLanguage()) + ")";
683 }
684 return name;
685 }
686
687 /**
688 * <p>getDescriptionText</p>
689 *
690 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
691 * @return a {@link java.lang.String} object.
692 */
693 public static String getDescriptionText(DescriptionBase element) {
694 String text = element.getTitleCache();
695 if (text == null || text.length() == 0) {
696 text = "No label provided";
697 }
698
699 return text;
700 }
701
702 /**
703 * <p>getIndividualsAssociationText</p>
704 *
705 * @param element a {@link eu.etaxonomy.cdm.model.description.IndividualsAssociation} object.
706 * @return a {@link java.lang.String} object.
707 */
708 public static String getIndividualsAssociationText(IndividualsAssociation element) {
709 SpecimenOrObservationBase derivedUnit = element.getAssociatedSpecimenOrObservation();
710 if(derivedUnit != null){
711 return derivedUnit.getTitleCache();
712 }
713 return "No unit chosen";
714 }
715
716 /**
717 * <p>getLabel</p>
718 *
719 * @param element a {@link java.lang.Object} object.
720 * @return a {@link java.lang.String} object.
721 */
722 public static String getLabel(Object element){
723 String noLabelString = "[no label]";
724 if (element instanceof FeatureNodeContainer){
725 return getFeatureNodeContainerText((FeatureNodeContainer) element);
726 }
727 else if (element instanceof DescriptionBase) {
728 return getDescriptionText((DescriptionBase) element);
729 }
730 else if(element instanceof CategoricalData){
731 String categoricalDataText = getCategoricalDataText((CategoricalData) element);
732 if(categoricalDataText==null || categoricalDataText.isEmpty()){
733 categoricalDataText = noLabelString;
734 }
735 return categoricalDataText;
736 }
737 else if (element instanceof CommonTaxonName) {
738 return getCommonNameText((CommonTaxonName) element);
739 }
740 else if (element instanceof Distribution) {
741 return getDistributionText((Distribution) element);
742 }
743 else if (element instanceof IndividualsAssociation) {
744 return getIndividualsAssociationText((IndividualsAssociation) element);
745 }
746 else if (element instanceof QuantitativeData) {
747 String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
748 if(quantitativeDataText==null || quantitativeDataText.isEmpty()){
749 quantitativeDataText = noLabelString;
750 }
751 return quantitativeDataText;
752 }
753 else if (element instanceof TaxonInteraction) {
754 return getTaxonInteractionText((TaxonInteraction) element);
755 }
756 else if (element instanceof TextData) {
757 return getElementText((TextData) element);
758 }
759 else{
760 return element.toString();
761 }
762 }
763
764 private static boolean isNotBlank(String str){
765 return StringUtils.isNotBlank(str);
766 }
767
768 private static boolean isBlank(String str){
769 return StringUtils.isBlank(str);
770 }
771 }