4732515336d2fcd3935829e9947c4be9ed6148b8
[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 package eu.etaxonomy.taxeditor.model;
10
11 import java.lang.reflect.InvocationTargetException;
12 import java.lang.reflect.Method;
13 import java.util.Arrays;
14 import java.util.List;
15 import java.util.Set;
16
17 import org.apache.commons.lang3.StringUtils;
18 import org.hibernate.LazyInitializationException;
19
20 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
21 import eu.etaxonomy.cdm.common.CdmUtils;
22 import eu.etaxonomy.cdm.format.common.ExtendedTimePeriodFormatter;
23 import eu.etaxonomy.cdm.format.description.CategoricalDataFormatter;
24 import eu.etaxonomy.cdm.format.description.QuantitativeDataFormatter;
25 import eu.etaxonomy.cdm.model.agent.AgentBase;
26 import eu.etaxonomy.cdm.model.common.CdmBase;
27 import eu.etaxonomy.cdm.model.common.ExtendedTimePeriod;
28 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
29 import eu.etaxonomy.cdm.model.common.Language;
30 import eu.etaxonomy.cdm.model.common.LanguageString;
31 import eu.etaxonomy.cdm.model.common.LanguageStringBase;
32 import eu.etaxonomy.cdm.model.common.Marker;
33 import eu.etaxonomy.cdm.model.common.MarkerType;
34 import eu.etaxonomy.cdm.model.common.RelationshipBase;
35 import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
36 import eu.etaxonomy.cdm.model.common.TimePeriod;
37 import eu.etaxonomy.cdm.model.description.CategoricalData;
38 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
39 import eu.etaxonomy.cdm.model.description.DescriptionBase;
40 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
41 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
42 import eu.etaxonomy.cdm.model.description.Distribution;
43 import eu.etaxonomy.cdm.model.description.Feature;
44 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
45 import eu.etaxonomy.cdm.model.description.KeyStatement;
46 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
47 import eu.etaxonomy.cdm.model.description.QuantitativeData;
48 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
49 import eu.etaxonomy.cdm.model.description.TaxonDescription;
50 import eu.etaxonomy.cdm.model.description.TaxonInteraction;
51 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
52 import eu.etaxonomy.cdm.model.description.TemporalData;
53 import eu.etaxonomy.cdm.model.description.TextData;
54 import eu.etaxonomy.cdm.model.location.NamedArea;
55 import eu.etaxonomy.cdm.model.media.Media;
56 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
57 import eu.etaxonomy.cdm.model.name.HybridRelationship;
58 import eu.etaxonomy.cdm.model.name.NameRelationship;
59 import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
60 import eu.etaxonomy.cdm.model.name.NomenclaturalSource;
61 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
62 import eu.etaxonomy.cdm.model.name.TaxonName;
63 import eu.etaxonomy.cdm.model.name.TextualTypeDesignation;
64 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
65 import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
66 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
67 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
68 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
69 import eu.etaxonomy.cdm.model.permission.Group;
70 import eu.etaxonomy.cdm.model.permission.User;
71 import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
72 import eu.etaxonomy.cdm.model.taxon.Classification;
73 import eu.etaxonomy.cdm.model.taxon.Taxon;
74 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
75 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
76 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
77 import eu.etaxonomy.cdm.model.term.Representation;
78 import eu.etaxonomy.taxeditor.store.CdmStore;
79
80 /**
81 * DescriptionHelper class.
82 *
83 * @author p.ciardelli
84 * @author n.hoffmann
85 */
86 public class DescriptionHelper {
87
88 public static final String NO_LABEL_STRING = "[no label]";
89
90 /**
91 * Returns whatever the element's title cache equivalent is,
92 * depending on its class.
93 *
94 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
95 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
96 * @return a {@link java.lang.String} object.
97 */
98 public static String getCache(DescriptionElementBase element,
99 Language language) {
100
101 String mainElementLabel= null;
102 DescriptionBase<?> descr = element.getInDescription();
103 descr = CdmBase.deproxy(descr, DescriptionBase.class);
104
105 if (descr != null){
106 if (descr.isInstanceOf(TaxonDescription.class)){
107 Taxon taxon = CdmBase.deproxy(descr, TaxonDescription.class).getTaxon();
108 if (taxon != null){
109 mainElementLabel = taxon.getTitleCache();
110 }
111 }else if (descr.isInstanceOf(SpecimenDescription.class)){
112 SpecimenOrObservationBase<?> specimen = CdmBase.deproxy(descr, SpecimenDescription.class).getDescribedSpecimenOrObservation();
113 if (specimen != null){
114 mainElementLabel = specimen.getTitleCache();
115 }
116 }else if (descr.isInstanceOf(TaxonNameDescription.class)){
117 TaxonName name = CdmBase.deproxy(descr, TaxonNameDescription.class).getTaxonName();
118 if (name != null){
119 mainElementLabel = name.getTitleCache();
120 }
121 }
122 }
123
124 String cache = null;
125 if (element instanceof TextData) {
126 //cache = ((TextData) element).getText(language);
127 cache = "Text Data";
128 }
129 if (element instanceof CommonTaxonName) {
130 cache = ((CommonTaxonName) element).getName();
131 }
132 if (element instanceof TaxonInteraction) {
133 Taxon taxon2 = ((TaxonInteraction) element).getTaxon2();
134 if(taxon2 != null && taxon2.getName() != null){
135 cache = taxon2.getName().getTitleCache();
136 }else{
137 cache = "No taxon chosen";
138 }
139 }
140 if (element instanceof Distribution) {
141 Distribution distribution = (Distribution) element;
142
143 NamedArea area = distribution.getArea();
144 if(area != null){
145 cache = area.getLabel();
146
147 PresenceAbsenceTerm status = distribution.getStatus();
148 if (status == null){
149 cache += ", no status";
150 }else {
151 cache += ", " + status.getLabel();
152 }
153 }
154 }
155 String result = cache == null ? "" : cache;
156 if (isNotBlank(mainElementLabel)){
157 result = CdmUtils.concat(" ", result, "(" + mainElementLabel + ")");
158 }
159 return result;
160 }
161
162 /**
163 * Returns whatever the element's title cache equivalent is,
164 * depending on its class, using the default language.
165 *
166 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
167 * @return a {@link java.lang.String} object.
168 */
169 public static String getCache(DescriptionElementBase element) {
170 return getCache(element, CdmStore.getDefaultLanguage());
171 }
172
173 /**
174 * Set whatever the element's title cache equivalent is,
175 * depending on its class.
176 *
177 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
178 * @param value a {@link java.lang.String} object.
179 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
180 */
181 public static void setCache(DescriptionElementBase element,
182 String value, Language language) {
183 if (element instanceof TextData) {
184 ((TextData) element).putText(language, value);
185 return;
186 }else if (element instanceof CommonTaxonName) {
187 ((CommonTaxonName) element).setName(value);
188 return;
189 }else if (element instanceof TaxonInteraction) {
190
191 }else if(element instanceof Distribution){
192 MessagingUtils.warn(DescriptionHelper.class, "trying to set cache on distribution, don't know what to do at the moment.");
193 return;
194 }else{
195 MessagingUtils.warn(DescriptionHelper.class, "No matching subclass found for DescriptionElementBase object, 'cache' not set.");
196 }
197 }
198
199 /**
200 * Set whatever the element's title cache equivalent is,
201 * depending on its class, using the default language.
202 *
203 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
204 * @param value a {@link java.lang.String} object.
205 */
206 public static void setCache(DescriptionElementBase element,
207 String value) {
208 setCache(element, value, CdmStore.getDefaultLanguage());
209 }
210
211 public static String getObjectDescription(Object element) {
212 String resultString = null;
213 if (element instanceof IdentifiableEntity) {
214 try{
215 resultString = ((IdentifiableEntity<?>) element).getTitleCache();
216 }catch(LazyInitializationException e){
217 String result = "No Session to initialize title cache for IdentifiableEntity";
218 MessagingUtils.error(DescriptionHelper.class, result, e);
219 resultString = "TODO: " + result;
220 }
221 }else if (element instanceof OriginalSourceBase) {
222 try{
223 OriginalSourceBase<?> originalSource = (OriginalSourceBase<?>) element;
224 // ISourceable sourcedObject = originalSource.getSourcedObj();
225 //due to #5743 the bidirectionality for sourced object had to be removed
226
227 String sourceObjectTitle = "sourced object data not available (#5743)";
228
229 //it is now possible for NomenclaturalSource as they link to the sourced name
230 if (originalSource instanceof NomenclaturalSource){
231 TaxonName sourcedName = ((NomenclaturalSource)originalSource).getSourcedName();
232 sourceObjectTitle = sourcedName == null ? "Source not attached to a name" :
233 "Nom. source for " + sourcedName.getTitleCache();
234 }
235
236 // if(sourcedObject instanceof IIdentifiableEntity){
237 // sourceObjectTitle = ((IdentifiableEntity) sourcedObject).getTitleCache();
238 // }else if(sourcedObject instanceof DescriptionElementBase){
239 // sourceObjectTitle = "Element for description: " + ((DescriptionElementBase) sourcedObject).getInDescription().getTitleCache();
240 // }else{
241 // throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
242 // }
243
244 resultString = CdmUtils.concat("; ", new String[]{originalSource.getIdNamespace(), originalSource.getIdInSource(), sourceObjectTitle});
245 }catch(LazyInitializationException e){
246 String result = "Error initializing title cache for ISourceable of an OriginalSourceBase";
247 MessagingUtils.error(DescriptionHelper.class, result, e);
248 resultString = "TODO: " + result;
249 }
250 }else if (element instanceof LanguageStringBase) {
251 resultString = ((LanguageStringBase) element).getText();
252 }else if (element instanceof DescriptionElementBase) {
253 resultString = getCache((DescriptionElementBase) element);
254 }else if (element instanceof RelationshipBase<?, ?, ?>) {
255 resultString = getCache((RelationshipBase<?, ?, ?>) element);
256 }else if (element instanceof TypeDesignationBase<?>) {
257 resultString = getCache((TypeDesignationBase<?>) element);
258 }else if (element instanceof HomotypicalGroup) {
259 resultString = getCache((HomotypicalGroup) element);
260 }else if (element instanceof TaxonNode) {
261 resultString = getCache((TaxonNode) element);
262 }else if (element instanceof DeterminationEvent) {
263 resultString = getCache((DeterminationEvent) element);
264 }else if (element instanceof GatheringEvent){
265 resultString = getCache((GatheringEvent) element);
266 }else if (element instanceof Marker) {
267 Marker marker = (Marker) element;
268 MarkerType type = marker.getMarkerType();
269 resultString = (type == null ? "- no marker type -" : marker.getMarkerType().getLabel()) + " (" + marker.getFlag() + ")";
270 }else if (element instanceof User) {
271 User user = (User) element;
272 resultString = user.getUsername();
273 }else if (element instanceof Group) {
274 Group group = (Group) element;
275 resultString = group.getName();
276 }else if (element instanceof KeyStatement) {
277 KeyStatement keyStatement = (KeyStatement) element;
278 resultString = getCache(keyStatement);
279 }else{
280 // TODO write return texts for HomotypicalGroup, etc.
281 resultString = element.toString();
282 }
283
284 if (resultString == null){
285 resultString = element.toString();
286 }
287 return resultString;
288 }
289
290 private static String getCache(GatheringEvent gatheringEvent){
291 String ALTITUDE_PREFIX = "alt. ";
292 final String METER = "m";
293
294 String result = "";
295
296 //collector
297 AgentBase<?> collector = CdmBase.deproxy(gatheringEvent.getCollector());
298 String collectorStr = collector == null? null : collector.getTitleCache();
299 result = CdmUtils.concat(", ", result, collectorStr);
300
301 //gathering period
302 TimePeriod gatheringPeriod = gatheringEvent.getTimeperiod();
303 result = CdmUtils.concat(", ", result, (gatheringPeriod == null? null : gatheringPeriod.toString()));
304
305 //country
306 String strCountry = null;
307 NamedArea country = gatheringEvent.getCountry();
308 Representation repCountry = country == null ? null : country.getRepresentation(Language.DEFAULT());
309 strCountry = repCountry == null ? null: repCountry.getLabel();
310 result = CdmUtils.concat(", ", result, strCountry);
311
312 //locality
313 LanguageString locality = gatheringEvent.getLocality();
314 if (locality != null) {
315 result = CdmUtils.concat(", ", result, locality.getText());
316 }
317
318 //elevation
319 String elevationStr;
320 if (isNotBlank(gatheringEvent.getAbsoluteElevationText())){
321 elevationStr = gatheringEvent.getAbsoluteElevationText();
322 }else{
323 String text = gatheringEvent.getAbsoluteElevationText();
324 Integer min = gatheringEvent.getAbsoluteElevation();
325 Integer max = gatheringEvent.getAbsoluteElevationMax();
326 elevationStr = DerivedUnitFacade.distanceString(min, max, text, METER);
327 }
328 if (isNotBlank(elevationStr)){
329 result = CdmUtils.concat(", " , result, ALTITUDE_PREFIX);
330 result += elevationStr;
331 }
332
333 //exact locality
334 if (gatheringEvent.getExactLocation() != null){
335 String exactLocation = gatheringEvent.getExactLocation().toSexagesimalString(false, false);
336 result = CdmUtils.concat(", ", result, exactLocation);
337 }
338
339 return result;
340 }
341
342 private static String getCache(DeterminationEvent detEvent) {
343 //taxon
344 String taxonStr = null;
345 TaxonName taxonName = detEvent.getTaxonName();
346 TaxonBase<?> taxon = detEvent.getTaxon();
347 if (taxonName != null){
348 taxonStr = taxonName.getTitleCache();
349 }
350 if (isBlank(taxonStr) && taxon != null){
351 taxonStr = taxon.getTitleCache();
352 }
353 if (isBlank(taxonStr)){
354 taxonStr = "no or unlabled taxon";
355 }
356
357 //unit
358 SpecimenOrObservationBase<?> unit = detEvent.getIdentifiedUnit();
359 String unitStr;
360 if (unit != null){
361 unitStr = unit.getTitleCache();
362 if (isBlank(unitStr)){
363 unitStr = "Unlabled unit";
364 }
365 }else{
366 unitStr = "no unit";
367 }
368
369 String result = CdmUtils.concat(" determined as ", unitStr, taxonStr);
370
371 return result;
372 }
373
374 private static String getCache(TaxonNode taxonNode) {
375 String result = "";
376 Classification classification = taxonNode.getClassification();
377 if (classification != null){
378 String classificationStr = classification.getName() == null ? "" : classification.getName().getText();
379 result = CdmUtils.concat("" , result, classificationStr);
380 if (isBlank(result)){
381 result = classification.toString();
382 }
383 }
384 String parentStr;
385 TaxonNode parentNode = taxonNode.getParent();
386 if (parentNode == null){
387 parentStr = "no parent";
388 }else{
389 Taxon parentTaxon = parentNode.getTaxon();
390 if (parentTaxon == null){
391 parentStr = "no parent taxon";
392 }else{
393 TaxonName parentName = parentTaxon.getName();
394 if (parentName == null){
395 parentStr = "child of " + parentTaxon.getTitleCache();
396 }else{
397 parentStr = "child of " + parentName.getTitleCache();
398 }
399 }
400 }
401 result = CdmUtils.concat(": ", result, parentStr);
402
403 return result;
404 }
405
406 private static String getCache(TypeDesignationBase<?> designation) {
407 designation = CdmBase.deproxy(designation);
408 //from
409 String fromString = null;
410 Set<TaxonName> from = designation.getTypifiedNames();
411 if(from != null){
412 for (TaxonName name : from){
413 fromString = CdmUtils.concat(",", fromString, name.getTitleCache());
414 }
415 }
416 //to
417 IdentifiableEntity<?> to = null;
418 String toStr = "";
419 if (designation.isInstanceOf(SpecimenTypeDesignation.class)){
420 to = ((SpecimenTypeDesignation)designation).getTypeSpecimen();
421 }else if (designation.isInstanceOf(NameTypeDesignation.class)){
422 to = ((NameTypeDesignation)designation).getTypeName();
423 }else if (designation.isInstanceOf(TextualTypeDesignation.class)){
424 toStr = ((TextualTypeDesignation)designation).getPreferredText(CdmStore.getDefaultLanguage());
425 }else{
426 throw new RuntimeException("Type Designation class not supported: " + designation.getClass().getName());
427 }
428 toStr = to == null? toStr : to.getTitleCache();
429 //status
430 String typeLabel = null;
431 TypeDesignationStatusBase<?> status = designation.getTypeStatus();
432 if (status != null){
433 Representation typeRepr = status.getPreferredRepresentation(CdmStore.getDefaultLanguage());
434 if (typeRepr != null){
435 typeLabel = typeRepr.getAbbreviatedLabel();
436 }
437 if (isBlank(typeLabel) && typeRepr != null){
438 typeLabel = typeRepr.getLabel();
439 }
440 if (isBlank(typeLabel) ){
441 typeLabel = status.getSymbol();
442 }
443 if (isBlank(typeLabel)){
444 typeLabel = status.getTitleCache();
445 }
446 }
447 if (isBlank(typeLabel)){
448 typeLabel = "->";
449 }
450 //concat
451 String result = CdmUtils.concat(" ", new String[]{fromString, typeLabel, toStr});
452 return result;
453 }
454
455 private static String getCache(RelationshipBase<?, ?, ?> rel) {
456 rel = CdmBase.deproxy(rel);
457 RelationshipTermBase<?> type = rel.getType();
458 IdentifiableEntity<?> from;
459 IdentifiableEntity<?> to;
460 if (rel.isInstanceOf(NameRelationship.class)){
461 from = ((NameRelationship)rel).getFromName();
462 to = ((NameRelationship)rel).getToName();
463 }else if (rel.isInstanceOf(HybridRelationship.class)){
464 from = ((HybridRelationship)rel).getParentName();
465 to = ((HybridRelationship)rel).getHybridName();
466 }else if (rel.isInstanceOf(TaxonRelationship.class)){
467 from = ((TaxonRelationship)rel).getFromTaxon();
468 to = ((TaxonRelationship)rel).getToTaxon();
469 }else{
470 try {
471 Method fromMethod = rel.getClass().getMethod("getRelatedFrom");
472 Method toMethod = rel.getClass().getMethod("getRelatedFrom");
473 fromMethod.setAccessible(true);
474 toMethod.setAccessible(true);
475 from = (IdentifiableEntity<?>)fromMethod.invoke(rel);
476 to = (IdentifiableEntity<?>)toMethod.invoke(rel);
477 } catch (NoSuchMethodException | SecurityException | IllegalAccessException
478 | IllegalArgumentException | InvocationTargetException e) {
479 throw new RuntimeException(e);
480 }
481 }
482 String typeLabel = null;
483 if (type != null){
484 Representation typeRepr = type.getPreferredRepresentation(CdmStore.getDefaultLanguage());
485 if (typeRepr != null){
486 typeLabel = typeRepr.getAbbreviatedLabel();
487 }
488 if (isBlank(typeLabel) && typeRepr != null){
489 typeLabel = typeRepr.getLabel();
490 }
491 if (isBlank(typeLabel) ){
492 typeLabel = type.getSymbol();
493 }
494 if (isBlank(typeLabel)){
495 typeLabel = type.getTitleCache();
496 }
497 }
498 if (isBlank(typeLabel)){
499 typeLabel = "->";
500 }
501 String result = CdmUtils.concat(" ", new String[]{from == null ? null : from.getTitleCache(),
502 typeLabel, to == null? null : to.getTitleCache()});
503 return result;
504 }
505
506
507 private static String getCache(HomotypicalGroup hg) {
508 String result = "";
509 for (TaxonName tnb : hg.getTypifiedNames()){
510 result = CdmUtils.concat(", ", result, tnb.getTitleCache());
511 }
512 if (isBlank(result)){
513 result = "No typified names";
514 }
515 return result;
516 }
517
518 private static String getCache(KeyStatement ks) {
519 String result = "";
520
521 result = "KeyStatement";
522
523 return result;
524 }
525
526 public static String getObjectClassname(Object element) {
527 return element.getClass().getSimpleName();
528 }
529
530 public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
531 String result = null;
532 if(element.getFeatureNode() != null && element.getFeatureNode().getTerm() != null){
533 result = element.getFeatureNode().getTerm().getLabel(CdmStore.getDefaultLanguage());
534 } else{
535 return "No label set";
536 }
537 if (result == null){
538 result = element.getFeatureNode().getTerm().getLabel();
539 }
540 return result;
541 }
542
543 public static String getQuantitativeDataText(QuantitativeData element) {
544 QuantitativeDataFormatter formatter = new QuantitativeDataFormatter(null, null);
545 String label = formatter.format(element, getLanguageList());
546 return label;
547 }
548
549 public static String getCategoricalDataText(CategoricalData element) {
550 CategoricalDataFormatter formatter = new CategoricalDataFormatter(null, null);
551 return formatter.format(element, getLanguageList());
552 }
553
554 private static List<Language> getLanguageList(){
555 return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
556 }
557
558 public static String getDistributionText(Distribution element) {
559
560 String text = "EMPTY";
561
562 Distribution distribution = element;
563
564 NamedArea area = distribution.getArea();
565 if(area != null){
566
567 text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
568
569 PresenceAbsenceTerm status = distribution.getStatus();
570
571 if (status != null) {
572 text += ", " + status.getLabel();
573 }else{
574 text += ", NO STATUS";
575 }
576 }
577 return text;
578 }
579
580 public static String getImageText(Media media) {
581 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
582 if (title != null) {
583 return title.getText();
584 }
585 return "No title.";
586 }
587
588 public static String getElementText(TextData element) {
589 String text = null;
590 if(element.getFeature().equals(Feature.CITATION())){
591 text = "";
592 for(DescriptionElementSource source : element.getSources()){
593 if(source.getCitation() != null){
594 text += source.getCitation().getTitleCache();
595 }
596 if(source.getNameUsedInSource() != null){
597 text += " [" + source.getNameUsedInSource().getTitleCache() + "]";
598 }
599 }
600 if(isBlank(text)){
601 text = "No sources provided";
602 }
603 }else{
604 List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
605 LanguageString languageString = element.getPreferredLanguageString(languages);
606 text = languageString != null ? languageString.getText() : null;
607 }
608 return isBlank(text) ? "No text provided" : text;
609 }
610
611 public static String getTaxonInteractionText(TaxonInteraction element) {
612 String text = "";
613 Taxon taxon2 = element.getTaxon2();
614 if(taxon2 != null && taxon2.getName() != null){
615 text = taxon2.getName().getTitleCache();
616 }else{
617 text = "No taxon chosen";
618 }
619
620 return text;
621 }
622
623 public static String getCommonNameText(CommonTaxonName commonName) {
624 String name = commonName.getName();
625 if (name == null || name.length() == 0) {
626 name = "No name provided";
627 }
628 Language language = commonName.getLanguage();
629 if (language == null) {
630 name += " (No language provided)";
631 } else {
632 name += " (" + language.getLabel(CdmStore.getDefaultLanguage()) + ")";
633 }
634 return name;
635 }
636
637 public static String getDescriptionText(DescriptionBase element) {
638 String text = element.getTitleCache();
639 if (text == null || text.length() == 0) {
640 text = "No label provided";
641 }
642
643 return text;
644 }
645
646 public static String getIndividualsAssociationText(IndividualsAssociation element) {
647 SpecimenOrObservationBase<?> derivedUnit = element.getAssociatedSpecimenOrObservation();
648 if(derivedUnit != null){
649 return derivedUnit.getTitleCache();
650 }
651 return "No unit chosen";
652 }
653
654 public static String getLabel(Object element){
655 if (element instanceof FeatureNodeContainer){
656 return getFeatureNodeContainerText((FeatureNodeContainer) element);
657 }
658 else if (element instanceof DescriptionBase) {
659 return getDescriptionText((DescriptionBase<?>) element);
660 }
661 else if(element instanceof CategoricalData){
662 String categoricalDataText = getCategoricalDataText((CategoricalData) element);
663 if(categoricalDataText==null || categoricalDataText.isEmpty()){
664 categoricalDataText = NO_LABEL_STRING;
665 }
666 return categoricalDataText;
667 }
668 else if (element instanceof CommonTaxonName) {
669 return getCommonNameText((CommonTaxonName) element);
670 }
671 else if (element instanceof Distribution) {
672 return getDistributionText((Distribution) element);
673 }
674 else if (element instanceof IndividualsAssociation) {
675 return getIndividualsAssociationText((IndividualsAssociation) element);
676 }
677 else if (element instanceof QuantitativeData) {
678 String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
679 if(isBlank(quantitativeDataText)){
680 quantitativeDataText = NO_LABEL_STRING;
681 }
682 return quantitativeDataText;
683 }
684 else if (element instanceof TaxonInteraction) {
685 return getTaxonInteractionText((TaxonInteraction) element);
686 }
687 else if (element instanceof TextData) {
688 return getElementText((TextData) element);
689 }
690 else if (element instanceof TemporalData) {
691 String lable = getTemporalDataText((TemporalData) element);
692 if(isBlank(lable)){
693 lable = NO_LABEL_STRING;
694 }
695 return lable;
696 }
697 else{
698 return element.toString();
699 }
700 }
701
702 private static String getTemporalDataText(TemporalData element) {
703 ExtendedTimePeriod period = element.getPeriod();
704 ExtendedTimePeriodFormatter formatter = ExtendedTimePeriodFormatter.NewDefaultInstance();
705 return period == null ? NO_LABEL_STRING : formatter.format(period);
706 }
707
708 private static boolean isNotBlank(String str){
709 return StringUtils.isNotBlank(str);
710 }
711
712 private static boolean isBlank(String str){
713 return StringUtils.isBlank(str);
714 }
715 }