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