ref #8990 fix NPE in getTemporalDataText
[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
217 /**
218 * <p>getObjectDescription</p>
219 *
220 * @param element a {@link java.lang.Object} object.
221 * @return a {@link java.lang.String} object.
222 */
223 public static String getObjectDescription(Object element) {
224 String resultString = null;
225 if (element instanceof IdentifiableEntity) {
226 try{
227 resultString = ((IdentifiableEntity) element).getTitleCache();
228 }catch(LazyInitializationException e){
229 String result = "No Session to initialize title cache for IdentifiableEntity";
230 MessagingUtils.error(DescriptionHelper.class, result, e);
231 resultString = "TODO: " + result;
232 }
233 }else if (element instanceof OriginalSourceBase) {
234 try{
235 OriginalSourceBase<?> originalSource = (OriginalSourceBase<?>) element;
236 // ISourceable sourcedObject = originalSource.getSourcedObj();
237 //due to #5743 the bidirectionality for sourced object had to be removed
238 String sourceObjectTitle = "sourced object data not available (#5743)";
239 // if(sourcedObject instanceof IIdentifiableEntity){
240 // sourceObjectTitle = ((IdentifiableEntity) sourcedObject).getTitleCache();
241 // }else if(sourcedObject instanceof DescriptionElementBase){
242 // sourceObjectTitle = "Element for description: " + ((DescriptionElementBase) sourcedObject).getInDescription().getTitleCache();
243 // }else{
244 // throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
245 // }
246
247 resultString = CdmUtils.concat("; ", new String[]{originalSource.getIdNamespace(), originalSource.getIdInSource(), sourceObjectTitle});
248 }catch(LazyInitializationException e){
249 String result = "Error initializing title cache for ISourceable of an OriginalSourceBase";
250 MessagingUtils.error(DescriptionHelper.class, result, e);
251 resultString = "TODO: " + result;
252 }
253 }else if (element instanceof LanguageStringBase) {
254 resultString = ((LanguageStringBase) element).getText();
255 }else if (element instanceof DescriptionElementBase) {
256 resultString = getCache((DescriptionElementBase) element);
257 }else if (element instanceof RelationshipBase<?, ?, ?>) {
258 resultString = getCache((RelationshipBase<?, ?, ?>) element);
259 }else if (element instanceof TypeDesignationBase<?>) {
260 resultString = getCache((TypeDesignationBase<?>) element);
261 }else if (element instanceof HomotypicalGroup) {
262 resultString = getCache((HomotypicalGroup) element);
263 }else if (element instanceof TaxonNode) {
264 resultString = getCache((TaxonNode) element);
265 }else if (element instanceof DeterminationEvent) {
266 resultString = getCache((DeterminationEvent) element);
267 }else if (element instanceof GatheringEvent){
268 resultString = getCache((GatheringEvent) element);
269 }else if (element instanceof Marker) {
270 Marker marker = (Marker) element;
271 MarkerType type = marker.getMarkerType();
272 resultString = (type == null ? "- no marker type -" : marker.getMarkerType().getLabel()) + " (" + marker.getFlag() + ")";
273 }else if (element instanceof User) {
274 User user = (User) element;
275 resultString = user.getUsername();
276 }else if (element instanceof Group) {
277 Group group = (Group) element;
278 resultString = group.getName();
279 }else if (element instanceof KeyStatement) {
280 KeyStatement keyStatement = (KeyStatement) element;
281 resultString = getCache(keyStatement);
282 }else{
283 // TODO write return texts for HomotypicalGroup, etc.
284 resultString = element.toString();
285 }
286
287 if (resultString == null){
288 resultString = element.toString();
289 }
290 return resultString;
291 }
292
293 private static String getCache(GatheringEvent gatheringEvent){
294 String ALTITUDE_PREFIX = "alt. ";
295 final String METER = "m";
296
297 String result = "";
298
299 //collector
300 AgentBase<?> collector = CdmBase.deproxy(gatheringEvent.getCollector());
301 String collectorStr = collector == null? null : collector.getTitleCache();
302 result = CdmUtils.concat(", ", result, collectorStr);
303
304 //gathering period
305 TimePeriod gatheringPeriod = gatheringEvent.getTimeperiod();
306 result = CdmUtils.concat(", ", result, (gatheringPeriod == null? null : gatheringPeriod.toString()));
307
308 //country
309 String strCountry = null;
310 NamedArea country = gatheringEvent.getCountry();
311 Representation repCountry = country == null ? null : country.getRepresentation(Language.DEFAULT());
312 strCountry = repCountry == null ? null: repCountry.getLabel();
313 result = CdmUtils.concat(", ", result, strCountry);
314
315 //locality
316 LanguageString locality = gatheringEvent.getLocality();
317 if (locality != null) {
318 result = CdmUtils.concat(", ", result, locality.getText());
319 }
320
321 //elevation
322 String elevationStr;
323 if (isNotBlank(gatheringEvent.getAbsoluteElevationText())){
324 elevationStr = gatheringEvent.getAbsoluteElevationText();
325 }else{
326 String text = gatheringEvent.getAbsoluteElevationText();
327 Integer min = gatheringEvent.getAbsoluteElevation();
328 Integer max = gatheringEvent.getAbsoluteElevationMax();
329 elevationStr = DerivedUnitFacade.distanceString(min, max, text, METER);
330 }
331 if (isNotBlank(elevationStr)){
332 result = CdmUtils.concat(", " , result, ALTITUDE_PREFIX);
333 result += elevationStr;
334 }
335
336 //exact locality
337 if (gatheringEvent.getExactLocation() != null){
338 String exactLocation = gatheringEvent.getExactLocation().toSexagesimalString(false, false);
339 result = CdmUtils.concat(", ", result, exactLocation);
340 }
341
342 return result;
343 }
344
345
346 private static String getCache(DeterminationEvent detEvent) {
347 //taxon
348 String taxonStr = null;
349 TaxonName taxonName = detEvent.getTaxonName();
350 TaxonBase<?> taxon = detEvent.getTaxon();
351 if (taxonName != null){
352 taxonStr = taxonName.getTitleCache();
353 }
354 if (isBlank(taxonStr) && taxon != null){
355 taxonStr = taxon.getTitleCache();
356 }
357 if (isBlank(taxonStr)){
358 taxonStr = "no or unlabled taxon";
359 }
360
361
362 //unit
363 SpecimenOrObservationBase<?> unit = detEvent.getIdentifiedUnit();
364 String unitStr;
365 if (unit != null){
366 unitStr = unit.getTitleCache();
367 if (isBlank(unitStr)){
368 unitStr = "Unlabled unit";
369 }
370 }else{
371 unitStr = "no unit";
372 }
373
374 String result = CdmUtils.concat(" determined as ", unitStr, taxonStr);
375
376 return result;
377 }
378
379 private static String getCache(TaxonNode taxonNode) {
380 String result = "";
381 Classification classification = taxonNode.getClassification();
382 if (classification != null){
383 String classificationStr = classification.getName() == null ? "" : classification.getName().getText();
384 result = CdmUtils.concat("" , result, classificationStr);
385 if (isBlank(result)){
386 result = classification.toString();
387 }
388 }
389 String parentStr;
390 TaxonNode parentNode = taxonNode.getParent();
391 if (parentNode == null){
392 parentStr = "no parent";
393 }else{
394 Taxon parentTaxon = parentNode.getTaxon();
395 if (parentTaxon == null){
396 parentStr = "no parent taxon";
397 }else{
398 TaxonName parentName = parentTaxon.getName();
399 if (parentName == null){
400 parentStr = "child of " + parentTaxon.getTitleCache();
401 }else{
402 parentStr = "child of " + parentName.getTitleCache();
403 }
404 }
405 }
406 result = CdmUtils.concat(": ", result, parentStr);
407
408 return result;
409 }
410
411 private static String getCache(TypeDesignationBase<?> designation) {
412 designation = CdmBase.deproxy(designation);
413 //from
414 String fromString = null;
415 Set<TaxonName> from = designation.getTypifiedNames();
416 if(from != null){
417 for (TaxonName name : from){
418 fromString = CdmUtils.concat(",", fromString, name.getTitleCache());
419 }
420 }
421 //to
422 IdentifiableEntity<?> to = null;
423 String toStr = "";
424 if (designation.isInstanceOf(SpecimenTypeDesignation.class)){
425 to = ((SpecimenTypeDesignation)designation).getTypeSpecimen();
426 }else if (designation.isInstanceOf(NameTypeDesignation.class)){
427 to = ((NameTypeDesignation)designation).getTypeName();
428 }else if (designation.isInstanceOf(TextualTypeDesignation.class)){
429 toStr = ((TextualTypeDesignation)designation).getPreferredText(CdmStore.getDefaultLanguage());
430 }else{
431 throw new RuntimeException("Type Designation class not supported: " + designation.getClass().getName());
432 }
433 toStr = to == null? toStr : to.getTitleCache();
434 //status
435 String typeLabel = null;
436 TypeDesignationStatusBase<?> status = designation.getTypeStatus();
437 if (status != null){
438 Representation typeRepr = status.getPreferredRepresentation(CdmStore.getDefaultLanguage());
439 if (typeRepr != null){
440 typeLabel = typeRepr.getAbbreviatedLabel();
441 }
442 if (isBlank(typeLabel) && typeRepr != null){
443 typeLabel = typeRepr.getLabel();
444 }
445 if (isBlank(typeLabel) ){
446 typeLabel = status.getSymbol();
447 }
448 if (isBlank(typeLabel)){
449 typeLabel = status.getTitleCache();
450 }
451 }
452 if (isBlank(typeLabel)){
453 typeLabel = "->";
454 }
455 //concat
456 String result = CdmUtils.concat(" ", new String[]{fromString, typeLabel, toStr});
457 return result;
458 }
459
460 private static String getCache(RelationshipBase<?, ?, ?> rel) {
461 rel = CdmBase.deproxy(rel);
462 RelationshipTermBase<?> type = rel.getType();
463 IdentifiableEntity<?> from;
464 IdentifiableEntity<?> to;
465 if (rel.isInstanceOf(NameRelationship.class)){
466 from = ((NameRelationship)rel).getFromName();
467 to = ((NameRelationship)rel).getToName();
468 }else if (rel.isInstanceOf(HybridRelationship.class)){
469 from = ((HybridRelationship)rel).getParentName();
470 to = ((HybridRelationship)rel).getHybridName();
471 }else if (rel.isInstanceOf(TaxonRelationship.class)){
472 from = ((TaxonRelationship)rel).getFromTaxon();
473 to = ((TaxonRelationship)rel).getToTaxon();
474 }else{
475 try {
476 Method fromMethod = rel.getClass().getMethod("getRelatedFrom");
477 Method toMethod = rel.getClass().getMethod("getRelatedFrom");
478 fromMethod.setAccessible(true);
479 toMethod.setAccessible(true);
480 from = (IdentifiableEntity<?>)fromMethod.invoke(rel);
481 to = (IdentifiableEntity<?>)toMethod.invoke(rel);
482 } catch (NoSuchMethodException | SecurityException | IllegalAccessException
483 | IllegalArgumentException | InvocationTargetException e) {
484 throw new RuntimeException(e);
485 }
486 }
487 String typeLabel = null;
488 if (type != null){
489 Representation typeRepr = type.getPreferredRepresentation(CdmStore.getDefaultLanguage());
490 if (typeRepr != null){
491 typeLabel = typeRepr.getAbbreviatedLabel();
492 }
493 if (isBlank(typeLabel) && typeRepr != null){
494 typeLabel = typeRepr.getLabel();
495 }
496 if (isBlank(typeLabel) ){
497 typeLabel = type.getSymbol();
498 }
499 if (isBlank(typeLabel)){
500 typeLabel = type.getTitleCache();
501 }
502 }
503 if (isBlank(typeLabel)){
504 typeLabel = "->";
505 }
506 String result = CdmUtils.concat(" ", new String[]{from == null ? null : from.getTitleCache(),
507 typeLabel, to == null? null : to.getTitleCache()});
508 return result;
509 }
510
511
512 private static String getCache(HomotypicalGroup hg) {
513 String result = "";
514 for (TaxonName tnb : hg.getTypifiedNames()){
515 result = CdmUtils.concat(", ", result, tnb.getTitleCache());
516 }
517 if (isBlank(result)){
518 result = "No typified names";
519 }
520 return result;
521 }
522
523 private static String getCache(KeyStatement ks) {
524 String result = "";
525
526 result = "KeyStatement";
527
528 return result;
529 }
530
531 /**
532 * <p>getObjectClassname</p>
533 *
534 * @param element a {@link java.lang.Object} object.
535 * @return a {@link java.lang.String} object.
536 */
537 public static String getObjectClassname(Object element) {
538 return element.getClass().getSimpleName();
539 }
540
541 /**
542 * <p>getFeatureNodeContainerText</p>
543 *
544 * @param element a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
545 * @return a {@link java.lang.String} object.
546 */
547 public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
548 String result = null;
549 if(element.getFeatureNode() != null && element.getFeatureNode().getTerm() != null){
550 result = element.getFeatureNode().getTerm().getLabel(CdmStore.getDefaultLanguage());
551 } else{
552 return "No label set";
553 }
554 if (result == null){
555 result = element.getFeatureNode().getTerm().getLabel();
556 }
557 return result;
558 }
559
560 /**
561 * <p>getQuantitativeDataText</p>
562 *
563 * @param element a {@link eu.etaxonomy.cdm.model.description.QuantitativeData} object.
564 * @return a {@link java.lang.String} object.
565 */
566 public static String getQuantitativeDataText(QuantitativeData element) {
567 QuantitativeDataFormatter formatter = new QuantitativeDataFormatter(null, null);
568 String label = formatter.doFormat(element);
569
570 return label;
571 }
572
573 /**
574 * <p>getCategoricalDataText</p>
575 *
576 * @param element a {@link eu.etaxonomy.cdm.model.description.CategoricalData} object.
577 * @return a {@link java.lang.String} object.
578 */
579 public static String getCategoricalDataText(CategoricalData element) {
580 TextData textData = categoricalDescriptionBuilder.build(element, getLanguageList());
581
582 return textData.getText(CdmStore.getDefaultLanguage());
583 }
584
585 private static List<Language> getLanguageList(){
586 return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
587 }
588
589 private static DescriptionBuilder<QuantitativeData> quantitativeDescriptionBuilder = new DefaultQuantitativeDescriptionBuilder();
590 private static DescriptionBuilder<CategoricalData> categoricalDescriptionBuilder = new DefaultCategoricalDescriptionBuilder();
591 public static final String NO_LABEL_STRING = "[no label]";
592
593
594 /**
595 * <p>getDistributionText</p>
596 *
597 * @param element a {@link eu.etaxonomy.cdm.model.description.Distribution} object.
598 * @return a {@link java.lang.String} object.
599 */
600 public static String getDistributionText(Distribution element) {
601
602 String text = "EMPTY";
603
604 Distribution distribution = element;
605
606 NamedArea area = distribution.getArea();
607 if(area != null){
608
609 text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
610
611 PresenceAbsenceTerm status = distribution.getStatus();
612
613 if (status != null) {
614 text += ", " + status.getLabel();
615 }else{
616 text += ", NO STATUS";
617 }
618 }
619 return text;
620 }
621
622 /**
623 * <p>getImageText</p>
624 *
625 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
626 * @return a {@link java.lang.String} object.
627 */
628 public static String getImageText(Media media) {
629 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
630 if (title != null) {
631 return title.getText();
632 }
633 return "No title.";
634 }
635
636
637
638 /**
639 * <p>getElementText</p>
640 *
641 * @param element a {@link eu.etaxonomy.cdm.model.description.TextData} object.
642 * @return a {@link java.lang.String} object.
643 */
644 public static String getElementText(TextData element) {
645 String text = null;
646 if(element.getFeature().equals(Feature.CITATION())){
647 text = "";
648 for(DescriptionElementSource source : element.getSources()){
649 if(source.getCitation() != null){
650 text += source.getCitation().getTitleCache();
651 }
652 if(source.getNameUsedInSource() != null){
653 text += " [ " + source.getNameUsedInSource().getTitleCache() + " ]";
654 }
655 }
656 if(isBlank(text)){
657 text = "No sources provided";
658 }
659 }else{
660 List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
661 LanguageString languageString = element.getPreferredLanguageString(languages);
662 text = languageString != null ? languageString.getText() : null;
663 }
664 return isBlank(text) ? "No text provided" : text;
665 }
666
667 /**
668 * <p>getTaxonInteractionText</p>
669 *
670 * @param element a {@link eu.etaxonomy.cdm.model.description.TaxonInteraction} object.
671 * @return a {@link java.lang.String} object.
672 */
673 public static String getTaxonInteractionText(TaxonInteraction element) {
674 String text = "";
675 Taxon taxon2 = element.getTaxon2();
676 if(taxon2 != null && taxon2.getName() != null){
677 text = taxon2.getName().getTitleCache();
678 }else{
679 text = "No taxon chosen";
680 }
681
682 return text;
683 }
684
685 /**
686 * <p>getCommonNameText</p>
687 *
688 * @param commonName a {@link eu.etaxonomy.cdm.model.description.CommonTaxonName} object.
689 * @return a {@link java.lang.String} object.
690 */
691 public static String getCommonNameText(CommonTaxonName commonName) {
692 String name = commonName.getName();
693 if (name == null || name.length() == 0) {
694 name = "No name provided";
695 }
696 Language language = commonName.getLanguage();
697 if (language == null) {
698 name += " (No language provided)";
699 } else {
700 name += " (" + language.getLabel(CdmStore.getDefaultLanguage()) + ")";
701 }
702 return name;
703 }
704
705 /**
706 * <p>getDescriptionText</p>
707 *
708 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
709 * @return a {@link java.lang.String} object.
710 */
711 public static String getDescriptionText(DescriptionBase element) {
712 String text = element.getTitleCache();
713 if (text == null || text.length() == 0) {
714 text = "No label provided";
715 }
716
717 return text;
718 }
719
720 /**
721 * <p>getIndividualsAssociationText</p>
722 *
723 * @param element a {@link eu.etaxonomy.cdm.model.description.IndividualsAssociation} object.
724 * @return a {@link java.lang.String} object.
725 */
726 public static String getIndividualsAssociationText(IndividualsAssociation element) {
727 SpecimenOrObservationBase derivedUnit = element.getAssociatedSpecimenOrObservation();
728 if(derivedUnit != null){
729 return derivedUnit.getTitleCache();
730 }
731 return "No unit chosen";
732 }
733
734 /**
735 * <p>getLabel</p>
736 *
737 * @param element a {@link java.lang.Object} object.
738 * @return a {@link java.lang.String} object.
739 */
740 public static String getLabel(Object element){
741 if (element instanceof FeatureNodeContainer){
742 return getFeatureNodeContainerText((FeatureNodeContainer) element);
743 }
744 else if (element instanceof DescriptionBase) {
745 return getDescriptionText((DescriptionBase) element);
746 }
747 else if(element instanceof CategoricalData){
748 String categoricalDataText = getCategoricalDataText((CategoricalData) element);
749 if(categoricalDataText==null || categoricalDataText.isEmpty()){
750 categoricalDataText = NO_LABEL_STRING;
751 }
752 return categoricalDataText;
753 }
754 else if (element instanceof CommonTaxonName) {
755 return getCommonNameText((CommonTaxonName) element);
756 }
757 else if (element instanceof Distribution) {
758 return getDistributionText((Distribution) element);
759 }
760 else if (element instanceof IndividualsAssociation) {
761 return getIndividualsAssociationText((IndividualsAssociation) element);
762 }
763 else if (element instanceof QuantitativeData) {
764 String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
765 if(quantitativeDataText==null || quantitativeDataText.isEmpty()){
766 quantitativeDataText = NO_LABEL_STRING;
767 }
768 return quantitativeDataText;
769 }
770 else if (element instanceof TaxonInteraction) {
771 return getTaxonInteractionText((TaxonInteraction) element);
772 }
773 else if (element instanceof TextData) {
774 return getElementText((TextData) element);
775 }
776 else if (element instanceof TemporalData) {
777 return getTemporalDataText((TemporalData) element);
778 }
779 else{
780 return element.toString();
781 }
782 }
783
784 /**
785 * @param element
786 * @return
787 */
788 private static String getTemporalDataText(TemporalData element) {
789 ExtendedTimePeriod period = element.getPeriod();
790 Partial end = period.getEnd()==null?null:period.getEnd().without(DateTimeFieldType.year());
791 Partial extremeEnd = period.getExtremeEnd()== null? null: period.getExtremeEnd().without(DateTimeFieldType.year());
792
793 Partial start = period.getStart()==null? null: period.getStart().without(DateTimeFieldType.year());
794 Partial extremeStart = period.getExtremeStart()==null? null : period.getExtremeStart().without(DateTimeFieldType.year());
795
796 String result = "";
797 DateTimeFormatter formatter = ExtendedTimePeriodPartialFormatter.NewInstance();
798 if (extremeStart != null){
799 result = "(" + extremeStart.toString(formatter) +"-)";
800 }
801 if (start != null){
802 result += start.toString(formatter);
803 }
804 if (end != null){
805 result += "-" + end.toString(formatter);
806 }
807
808 if (extremeEnd != null){
809 result += "(-" + extremeEnd.toString(formatter)+")";
810 }
811 return result;
812
813 // return element.getPeriod().toString();
814 }
815
816 private static boolean isNotBlank(String str){
817 return StringUtils.isNotBlank(str);
818 }
819
820 private static boolean isBlank(String str){
821 return StringUtils.isBlank(str);
822 }
823 }