Merge branch 'release/5.18.0'
[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.lang3.StringUtils;
19 import org.hibernate.LazyInitializationException;
20
21 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
22 import eu.etaxonomy.cdm.common.CdmUtils;
23 import eu.etaxonomy.cdm.format.common.ExtendedTimePeriodFormatter;
24 import eu.etaxonomy.cdm.format.description.CategoricalDataFormatter;
25 import eu.etaxonomy.cdm.format.description.QuantitativeDataFormatter;
26 import eu.etaxonomy.cdm.model.agent.AgentBase;
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.common.ExtendedTimePeriod;
29 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
30 import eu.etaxonomy.cdm.model.common.Language;
31 import eu.etaxonomy.cdm.model.common.LanguageString;
32 import eu.etaxonomy.cdm.model.common.LanguageStringBase;
33 import eu.etaxonomy.cdm.model.common.Marker;
34 import eu.etaxonomy.cdm.model.common.MarkerType;
35 import eu.etaxonomy.cdm.model.common.RelationshipBase;
36 import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
37 import eu.etaxonomy.cdm.model.common.TimePeriod;
38 import eu.etaxonomy.cdm.model.description.CategoricalData;
39 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
40 import eu.etaxonomy.cdm.model.description.DescriptionBase;
41 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
42 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
43 import eu.etaxonomy.cdm.model.description.Distribution;
44 import eu.etaxonomy.cdm.model.description.Feature;
45 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
46 import eu.etaxonomy.cdm.model.description.KeyStatement;
47 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
48 import eu.etaxonomy.cdm.model.description.QuantitativeData;
49 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
50 import eu.etaxonomy.cdm.model.description.TaxonDescription;
51 import eu.etaxonomy.cdm.model.description.TaxonInteraction;
52 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
53 import eu.etaxonomy.cdm.model.description.TemporalData;
54 import eu.etaxonomy.cdm.model.description.TextData;
55 import eu.etaxonomy.cdm.model.location.NamedArea;
56 import eu.etaxonomy.cdm.model.media.Media;
57 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
58 import eu.etaxonomy.cdm.model.name.HybridRelationship;
59 import eu.etaxonomy.cdm.model.name.NameRelationship;
60 import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
61 import eu.etaxonomy.cdm.model.name.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 * <p>DescriptionHelper class.</p>
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 String sourceObjectTitle = "sourced object data not available (#5743)";
227 // if(sourcedObject instanceof IIdentifiableEntity){
228 // sourceObjectTitle = ((IdentifiableEntity) sourcedObject).getTitleCache();
229 // }else if(sourcedObject instanceof DescriptionElementBase){
230 // sourceObjectTitle = "Element for description: " + ((DescriptionElementBase) sourcedObject).getInDescription().getTitleCache();
231 // }else{
232 // throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
233 // }
234
235 resultString = CdmUtils.concat("; ", new String[]{originalSource.getIdNamespace(), originalSource.getIdInSource(), sourceObjectTitle});
236 }catch(LazyInitializationException e){
237 String result = "Error initializing title cache for ISourceable of an OriginalSourceBase";
238 MessagingUtils.error(DescriptionHelper.class, result, e);
239 resultString = "TODO: " + result;
240 }
241 }else if (element instanceof LanguageStringBase) {
242 resultString = ((LanguageStringBase) element).getText();
243 }else if (element instanceof DescriptionElementBase) {
244 resultString = getCache((DescriptionElementBase) element);
245 }else if (element instanceof RelationshipBase<?, ?, ?>) {
246 resultString = getCache((RelationshipBase<?, ?, ?>) element);
247 }else if (element instanceof TypeDesignationBase<?>) {
248 resultString = getCache((TypeDesignationBase<?>) element);
249 }else if (element instanceof HomotypicalGroup) {
250 resultString = getCache((HomotypicalGroup) element);
251 }else if (element instanceof TaxonNode) {
252 resultString = getCache((TaxonNode) element);
253 }else if (element instanceof DeterminationEvent) {
254 resultString = getCache((DeterminationEvent) element);
255 }else if (element instanceof GatheringEvent){
256 resultString = getCache((GatheringEvent) element);
257 }else if (element instanceof Marker) {
258 Marker marker = (Marker) element;
259 MarkerType type = marker.getMarkerType();
260 resultString = (type == null ? "- no marker type -" : marker.getMarkerType().getLabel()) + " (" + marker.getFlag() + ")";
261 }else if (element instanceof User) {
262 User user = (User) element;
263 resultString = user.getUsername();
264 }else if (element instanceof Group) {
265 Group group = (Group) element;
266 resultString = group.getName();
267 }else if (element instanceof KeyStatement) {
268 KeyStatement keyStatement = (KeyStatement) element;
269 resultString = getCache(keyStatement);
270 }else{
271 // TODO write return texts for HomotypicalGroup, etc.
272 resultString = element.toString();
273 }
274
275 if (resultString == null){
276 resultString = element.toString();
277 }
278 return resultString;
279 }
280
281 private static String getCache(GatheringEvent gatheringEvent){
282 String ALTITUDE_PREFIX = "alt. ";
283 final String METER = "m";
284
285 String result = "";
286
287 //collector
288 AgentBase<?> collector = CdmBase.deproxy(gatheringEvent.getCollector());
289 String collectorStr = collector == null? null : collector.getTitleCache();
290 result = CdmUtils.concat(", ", result, collectorStr);
291
292 //gathering period
293 TimePeriod gatheringPeriod = gatheringEvent.getTimeperiod();
294 result = CdmUtils.concat(", ", result, (gatheringPeriod == null? null : gatheringPeriod.toString()));
295
296 //country
297 String strCountry = null;
298 NamedArea country = gatheringEvent.getCountry();
299 Representation repCountry = country == null ? null : country.getRepresentation(Language.DEFAULT());
300 strCountry = repCountry == null ? null: repCountry.getLabel();
301 result = CdmUtils.concat(", ", result, strCountry);
302
303 //locality
304 LanguageString locality = gatheringEvent.getLocality();
305 if (locality != null) {
306 result = CdmUtils.concat(", ", result, locality.getText());
307 }
308
309 //elevation
310 String elevationStr;
311 if (isNotBlank(gatheringEvent.getAbsoluteElevationText())){
312 elevationStr = gatheringEvent.getAbsoluteElevationText();
313 }else{
314 String text = gatheringEvent.getAbsoluteElevationText();
315 Integer min = gatheringEvent.getAbsoluteElevation();
316 Integer max = gatheringEvent.getAbsoluteElevationMax();
317 elevationStr = DerivedUnitFacade.distanceString(min, max, text, METER);
318 }
319 if (isNotBlank(elevationStr)){
320 result = CdmUtils.concat(", " , result, ALTITUDE_PREFIX);
321 result += elevationStr;
322 }
323
324 //exact locality
325 if (gatheringEvent.getExactLocation() != null){
326 String exactLocation = gatheringEvent.getExactLocation().toSexagesimalString(false, false);
327 result = CdmUtils.concat(", ", result, exactLocation);
328 }
329
330 return result;
331 }
332
333 private static String getCache(DeterminationEvent detEvent) {
334 //taxon
335 String taxonStr = null;
336 TaxonName taxonName = detEvent.getTaxonName();
337 TaxonBase<?> taxon = detEvent.getTaxon();
338 if (taxonName != null){
339 taxonStr = taxonName.getTitleCache();
340 }
341 if (isBlank(taxonStr) && taxon != null){
342 taxonStr = taxon.getTitleCache();
343 }
344 if (isBlank(taxonStr)){
345 taxonStr = "no or unlabled taxon";
346 }
347
348 //unit
349 SpecimenOrObservationBase<?> unit = detEvent.getIdentifiedUnit();
350 String unitStr;
351 if (unit != null){
352 unitStr = unit.getTitleCache();
353 if (isBlank(unitStr)){
354 unitStr = "Unlabled unit";
355 }
356 }else{
357 unitStr = "no unit";
358 }
359
360 String result = CdmUtils.concat(" determined as ", unitStr, taxonStr);
361
362 return result;
363 }
364
365 private static String getCache(TaxonNode taxonNode) {
366 String result = "";
367 Classification classification = taxonNode.getClassification();
368 if (classification != null){
369 String classificationStr = classification.getName() == null ? "" : classification.getName().getText();
370 result = CdmUtils.concat("" , result, classificationStr);
371 if (isBlank(result)){
372 result = classification.toString();
373 }
374 }
375 String parentStr;
376 TaxonNode parentNode = taxonNode.getParent();
377 if (parentNode == null){
378 parentStr = "no parent";
379 }else{
380 Taxon parentTaxon = parentNode.getTaxon();
381 if (parentTaxon == null){
382 parentStr = "no parent taxon";
383 }else{
384 TaxonName parentName = parentTaxon.getName();
385 if (parentName == null){
386 parentStr = "child of " + parentTaxon.getTitleCache();
387 }else{
388 parentStr = "child of " + parentName.getTitleCache();
389 }
390 }
391 }
392 result = CdmUtils.concat(": ", result, parentStr);
393
394 return result;
395 }
396
397 private static String getCache(TypeDesignationBase<?> designation) {
398 designation = CdmBase.deproxy(designation);
399 //from
400 String fromString = null;
401 Set<TaxonName> from = designation.getTypifiedNames();
402 if(from != null){
403 for (TaxonName name : from){
404 fromString = CdmUtils.concat(",", fromString, name.getTitleCache());
405 }
406 }
407 //to
408 IdentifiableEntity<?> to = null;
409 String toStr = "";
410 if (designation.isInstanceOf(SpecimenTypeDesignation.class)){
411 to = ((SpecimenTypeDesignation)designation).getTypeSpecimen();
412 }else if (designation.isInstanceOf(NameTypeDesignation.class)){
413 to = ((NameTypeDesignation)designation).getTypeName();
414 }else if (designation.isInstanceOf(TextualTypeDesignation.class)){
415 toStr = ((TextualTypeDesignation)designation).getPreferredText(CdmStore.getDefaultLanguage());
416 }else{
417 throw new RuntimeException("Type Designation class not supported: " + designation.getClass().getName());
418 }
419 toStr = to == null? toStr : to.getTitleCache();
420 //status
421 String typeLabel = null;
422 TypeDesignationStatusBase<?> status = designation.getTypeStatus();
423 if (status != null){
424 Representation typeRepr = status.getPreferredRepresentation(CdmStore.getDefaultLanguage());
425 if (typeRepr != null){
426 typeLabel = typeRepr.getAbbreviatedLabel();
427 }
428 if (isBlank(typeLabel) && typeRepr != null){
429 typeLabel = typeRepr.getLabel();
430 }
431 if (isBlank(typeLabel) ){
432 typeLabel = status.getSymbol();
433 }
434 if (isBlank(typeLabel)){
435 typeLabel = status.getTitleCache();
436 }
437 }
438 if (isBlank(typeLabel)){
439 typeLabel = "->";
440 }
441 //concat
442 String result = CdmUtils.concat(" ", new String[]{fromString, typeLabel, toStr});
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 public static String getObjectClassname(Object element) {
518 return element.getClass().getSimpleName();
519 }
520
521 public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
522 String result = null;
523 if(element.getFeatureNode() != null && element.getFeatureNode().getTerm() != null){
524 result = element.getFeatureNode().getTerm().getLabel(CdmStore.getDefaultLanguage());
525 } else{
526 return "No label set";
527 }
528 if (result == null){
529 result = element.getFeatureNode().getTerm().getLabel();
530 }
531 return result;
532 }
533
534 public static String getQuantitativeDataText(QuantitativeData element) {
535 QuantitativeDataFormatter formatter = new QuantitativeDataFormatter(null, null);
536 String label = formatter.format(element, getLanguageList());
537 return label;
538 }
539
540 public static String getCategoricalDataText(CategoricalData element) {
541 CategoricalDataFormatter formatter = new CategoricalDataFormatter(null, null);
542 return formatter.format(element, getLanguageList());
543 }
544
545 private static List<Language> getLanguageList(){
546 return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
547 }
548
549 public static String getDistributionText(Distribution element) {
550
551 String text = "EMPTY";
552
553 Distribution distribution = element;
554
555 NamedArea area = distribution.getArea();
556 if(area != null){
557
558 text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
559
560 PresenceAbsenceTerm status = distribution.getStatus();
561
562 if (status != null) {
563 text += ", " + status.getLabel();
564 }else{
565 text += ", NO STATUS";
566 }
567 }
568 return text;
569 }
570
571 public static String getImageText(Media media) {
572 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
573 if (title != null) {
574 return title.getText();
575 }
576 return "No title.";
577 }
578
579 public static String getElementText(TextData element) {
580 String text = null;
581 if(element.getFeature().equals(Feature.CITATION())){
582 text = "";
583 for(DescriptionElementSource source : element.getSources()){
584 if(source.getCitation() != null){
585 text += source.getCitation().getTitleCache();
586 }
587 if(source.getNameUsedInSource() != null){
588 text += " [" + source.getNameUsedInSource().getTitleCache() + "]";
589 }
590 }
591 if(isBlank(text)){
592 text = "No sources provided";
593 }
594 }else{
595 List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
596 LanguageString languageString = element.getPreferredLanguageString(languages);
597 text = languageString != null ? languageString.getText() : null;
598 }
599 return isBlank(text) ? "No text provided" : text;
600 }
601
602 public static String getTaxonInteractionText(TaxonInteraction element) {
603 String text = "";
604 Taxon taxon2 = element.getTaxon2();
605 if(taxon2 != null && taxon2.getName() != null){
606 text = taxon2.getName().getTitleCache();
607 }else{
608 text = "No taxon chosen";
609 }
610
611 return text;
612 }
613
614 public static String getCommonNameText(CommonTaxonName commonName) {
615 String name = commonName.getName();
616 if (name == null || name.length() == 0) {
617 name = "No name provided";
618 }
619 Language language = commonName.getLanguage();
620 if (language == null) {
621 name += " (No language provided)";
622 } else {
623 name += " (" + language.getLabel(CdmStore.getDefaultLanguage()) + ")";
624 }
625 return name;
626 }
627
628 public static String getDescriptionText(DescriptionBase element) {
629 String text = element.getTitleCache();
630 if (text == null || text.length() == 0) {
631 text = "No label provided";
632 }
633
634 return text;
635 }
636
637 public static String getIndividualsAssociationText(IndividualsAssociation element) {
638 SpecimenOrObservationBase<?> derivedUnit = element.getAssociatedSpecimenOrObservation();
639 if(derivedUnit != null){
640 return derivedUnit.getTitleCache();
641 }
642 return "No unit chosen";
643 }
644
645 public static String getLabel(Object element){
646 if (element instanceof FeatureNodeContainer){
647 return getFeatureNodeContainerText((FeatureNodeContainer) element);
648 }
649 else if (element instanceof DescriptionBase) {
650 return getDescriptionText((DescriptionBase<?>) element);
651 }
652 else if(element instanceof CategoricalData){
653 String categoricalDataText = getCategoricalDataText((CategoricalData) element);
654 if(categoricalDataText==null || categoricalDataText.isEmpty()){
655 categoricalDataText = NO_LABEL_STRING;
656 }
657 return categoricalDataText;
658 }
659 else if (element instanceof CommonTaxonName) {
660 return getCommonNameText((CommonTaxonName) element);
661 }
662 else if (element instanceof Distribution) {
663 return getDistributionText((Distribution) element);
664 }
665 else if (element instanceof IndividualsAssociation) {
666 return getIndividualsAssociationText((IndividualsAssociation) element);
667 }
668 else if (element instanceof QuantitativeData) {
669 String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
670 if(isBlank(quantitativeDataText)){
671 quantitativeDataText = NO_LABEL_STRING;
672 }
673 return quantitativeDataText;
674 }
675 else if (element instanceof TaxonInteraction) {
676 return getTaxonInteractionText((TaxonInteraction) element);
677 }
678 else if (element instanceof TextData) {
679 return getElementText((TextData) element);
680 }
681 else if (element instanceof TemporalData) {
682 String lable = getTemporalDataText((TemporalData) element);
683 if(isBlank(lable)){
684 lable = NO_LABEL_STRING;
685 }
686 return lable;
687 }
688 else{
689 return element.toString();
690 }
691 }
692
693 private static String getTemporalDataText(TemporalData element) {
694 ExtendedTimePeriod period = element.getPeriod();
695 ExtendedTimePeriodFormatter formatter = ExtendedTimePeriodFormatter.NewDefaultInstance();
696 return period == null ? NO_LABEL_STRING : formatter.format(period);
697 }
698
699 private static boolean isNotBlank(String str){
700 return StringUtils.isNotBlank(str);
701 }
702
703 private static boolean isBlank(String str){
704 return StringUtils.isBlank(str);
705 }
706 }