2768fdb7d5911e2a5fe4846769edba68ea72b40d
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / DescriptionHelper.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.model;
11
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 import java.util.Arrays;
15 import java.util.List;
16 import java.util.Set;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.hibernate.LazyInitializationException;
20
21 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
22 import eu.etaxonomy.cdm.common.CdmUtils;
23 import eu.etaxonomy.cdm.format.description.DefaultCategoricalDescriptionBuilder;
24 import eu.etaxonomy.cdm.format.description.DefaultQuantitativeDescriptionBuilder;
25 import eu.etaxonomy.cdm.format.description.DescriptionBuilder;
26 import eu.etaxonomy.cdm.format.description.QuantitativeDataFormatter;
27 import eu.etaxonomy.cdm.model.agent.AgentBase;
28 import eu.etaxonomy.cdm.model.common.CdmBase;
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 /**
89 * Returns whatever the element's title cache equivalent is,
90 * depending on its class.
91 *
92 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
93 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
94 * @return a {@link java.lang.String} object.
95 */
96 public static String getCache(DescriptionElementBase element,
97 Language language) {
98
99 String mainElementLabel= null;
100 DescriptionBase<?> descr = element.getInDescription();
101 descr = CdmBase.deproxy(descr, DescriptionBase.class);
102
103 if (descr != null){
104 if (descr.isInstanceOf(TaxonDescription.class)){
105 Taxon taxon = CdmBase.deproxy(descr, TaxonDescription.class).getTaxon();
106 if (taxon != null){
107 mainElementLabel = taxon.getTitleCache();
108 }
109 }else if (descr.isInstanceOf(SpecimenDescription.class)){
110 SpecimenOrObservationBase<?> specimen = CdmBase.deproxy(descr, SpecimenDescription.class).getDescribedSpecimenOrObservation();
111 if (specimen != null){
112 mainElementLabel = specimen.getTitleCache();
113 }
114 }else if (descr.isInstanceOf(TaxonNameDescription.class)){
115 TaxonName name = CdmBase.deproxy(descr, TaxonNameDescription.class).getTaxonName();
116 if (name != null){
117 mainElementLabel = name.getTitleCache();
118 }
119 }
120 }
121
122
123 String cache = null;
124 if (element instanceof TextData) {
125 //cache = ((TextData) element).getText(language);
126 cache = "Text Data";
127 }
128 if (element instanceof CommonTaxonName) {
129 cache = ((CommonTaxonName) element).getName();
130 }
131 if (element instanceof TaxonInteraction) {
132 Taxon taxon2 = ((TaxonInteraction) element).getTaxon2();
133 if(taxon2 != null && taxon2.getName() != null){
134 cache = taxon2.getName().getTitleCache();
135 }else{
136 cache = "No taxon chosen";
137 }
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
212 /**
213 * <p>getObjectDescription</p>
214 *
215 * @param element a {@link java.lang.Object} object.
216 * @return a {@link java.lang.String} object.
217 */
218 public static String getObjectDescription(Object element) {
219 String resultString = null;
220 if (element instanceof IdentifiableEntity) {
221 try{
222 resultString = ((IdentifiableEntity) element).getTitleCache();
223 }catch(LazyInitializationException e){
224 String result = "No Session to initialize title cache for IdentifiableEntity";
225 MessagingUtils.error(DescriptionHelper.class, result, e);
226 resultString = "TODO: " + result;
227 }
228 }else if (element instanceof OriginalSourceBase) {
229 try{
230 OriginalSourceBase<?> originalSource = (OriginalSourceBase<?>) element;
231 // ISourceable sourcedObject = originalSource.getSourcedObj();
232 //due to #5743 the bidirectionality for sourced object had to be removed
233 String sourceObjectTitle = "sourced object data not available (#5743)";
234 // if(sourcedObject instanceof IIdentifiableEntity){
235 // sourceObjectTitle = ((IdentifiableEntity) sourcedObject).getTitleCache();
236 // }else if(sourcedObject instanceof DescriptionElementBase){
237 // sourceObjectTitle = "Element for description: " + ((DescriptionElementBase) sourcedObject).getInDescription().getTitleCache();
238 // }else{
239 // throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
240 // }
241
242 resultString = CdmUtils.concat("; ", new String[]{originalSource.getIdNamespace(), originalSource.getIdInSource(), sourceObjectTitle});
243 }catch(LazyInitializationException e){
244 String result = "Error initializing title cache for ISourceable of an OriginalSourceBase";
245 MessagingUtils.error(DescriptionHelper.class, result, e);
246 resultString = "TODO: " + result;
247 }
248 }else if (element instanceof LanguageStringBase) {
249 resultString = ((LanguageStringBase) element).getText();
250 }else if (element instanceof DescriptionElementBase) {
251 resultString = getCache((DescriptionElementBase) element);
252 }else if (element instanceof RelationshipBase<?, ?, ?>) {
253 resultString = getCache((RelationshipBase<?, ?, ?>) element);
254 }else if (element instanceof TypeDesignationBase<?>) {
255 resultString = getCache((TypeDesignationBase<?>) element);
256 }else if (element instanceof HomotypicalGroup) {
257 resultString = getCache((HomotypicalGroup) element);
258 }else if (element instanceof TaxonNode) {
259 resultString = getCache((TaxonNode) element);
260 }else if (element instanceof DeterminationEvent) {
261 resultString = getCache((DeterminationEvent) element);
262 }else if (element instanceof GatheringEvent){
263 resultString = getCache((GatheringEvent) element);
264 }else if (element instanceof Marker) {
265 Marker marker = (Marker) element;
266 MarkerType type = marker.getMarkerType();
267 resultString = (type == null ? "- no marker type -" : marker.getMarkerType().getLabel()) + " (" + marker.getFlag() + ")";
268 }else if (element instanceof User) {
269 User user = (User) element;
270 resultString = user.getUsername();
271 }else if (element instanceof Group) {
272 Group group = (Group) element;
273 resultString = group.getName();
274 }else if (element instanceof KeyStatement) {
275 KeyStatement keyStatement = (KeyStatement) element;
276 resultString = getCache(keyStatement);
277 }else{
278 // TODO write return texts for HomotypicalGroup, etc.
279 resultString = element.toString();
280 }
281
282 if (resultString == null){
283 resultString = element.toString();
284 }
285 return resultString;
286 }
287
288 private static String getCache(GatheringEvent gatheringEvent){
289 String ALTITUDE_PREFIX = "alt. ";
290 final String METER = "m";
291
292 String result = "";
293
294 //collector
295 AgentBase<?> collector = CdmBase.deproxy(gatheringEvent.getCollector());
296 String collectorStr = collector == null? null : collector.getTitleCache();
297 result = CdmUtils.concat(", ", result, collectorStr);
298
299 //gathering period
300 TimePeriod gatheringPeriod = gatheringEvent.getTimeperiod();
301 result = CdmUtils.concat(", ", result, (gatheringPeriod == null? null : gatheringPeriod.toString()));
302
303 //country
304 String strCountry = null;
305 NamedArea country = gatheringEvent.getCountry();
306 Representation repCountry = country == null ? null : country.getRepresentation(Language.DEFAULT());
307 strCountry = repCountry == null ? null: repCountry.getLabel();
308 result = CdmUtils.concat(", ", result, strCountry);
309
310 //locality
311 LanguageString locality = gatheringEvent.getLocality();
312 if (locality != null) {
313 result = CdmUtils.concat(", ", result, locality.getText());
314 }
315
316 //elevation
317 String elevationStr;
318 if (isNotBlank(gatheringEvent.getAbsoluteElevationText())){
319 elevationStr = gatheringEvent.getAbsoluteElevationText();
320 }else{
321 String text = gatheringEvent.getAbsoluteElevationText();
322 Integer min = gatheringEvent.getAbsoluteElevation();
323 Integer max = gatheringEvent.getAbsoluteElevationMax();
324 elevationStr = DerivedUnitFacade.distanceString(min, max, text, METER);
325 }
326 if (isNotBlank(elevationStr)){
327 result = CdmUtils.concat(", " , result, ALTITUDE_PREFIX);
328 result += elevationStr;
329 }
330
331 //exact locality
332 if (gatheringEvent.getExactLocation() != null){
333 String exactLocation = gatheringEvent.getExactLocation().toSexagesimalString(false, false);
334 result = CdmUtils.concat(", ", result, exactLocation);
335 }
336
337 return result;
338 }
339
340
341 private static String getCache(DeterminationEvent detEvent) {
342 //taxon
343 String taxonStr = null;
344 TaxonName taxonName = detEvent.getTaxonName();
345 TaxonBase<?> taxon = detEvent.getTaxon();
346 if (taxonName != null){
347 taxonStr = taxonName.getTitleCache();
348 }
349 if (isBlank(taxonStr) && taxon != null){
350 taxonStr = taxon.getTitleCache();
351 }
352 if (isBlank(taxonStr)){
353 taxonStr = "no or unlabled taxon";
354 }
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 /**
527 * <p>getObjectClassname</p>
528 *
529 * @param element a {@link java.lang.Object} object.
530 * @return a {@link java.lang.String} object.
531 */
532 public static String getObjectClassname(Object element) {
533 return element.getClass().getSimpleName();
534 }
535
536 /**
537 * <p>getFeatureNodeContainerText</p>
538 *
539 * @param element a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
540 * @return a {@link java.lang.String} object.
541 */
542 public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
543 String result = null;
544 if(element.getFeatureNode() != null && element.getFeatureNode().getTerm() != null){
545 result = element.getFeatureNode().getTerm().getLabel(CdmStore.getDefaultLanguage());
546 } else{
547 return "No label set";
548 }
549 if (result == null){
550 result = element.getFeatureNode().getTerm().getLabel();
551 }
552 return result;
553 }
554
555 /**
556 * <p>getQuantitativeDataText</p>
557 *
558 * @param element a {@link eu.etaxonomy.cdm.model.description.QuantitativeData} object.
559 * @return a {@link java.lang.String} object.
560 */
561 public static String getQuantitativeDataText(QuantitativeData element) {
562 QuantitativeDataFormatter formatter = new QuantitativeDataFormatter(null, null);
563 String label = formatter.doFormat(element);
564
565 return label;
566 }
567
568 /**
569 * <p>getCategoricalDataText</p>
570 *
571 * @param element a {@link eu.etaxonomy.cdm.model.description.CategoricalData} object.
572 * @return a {@link java.lang.String} object.
573 */
574 public static String getCategoricalDataText(CategoricalData element) {
575 TextData textData = categoricalDescriptionBuilder.build(element, getLanguageList());
576
577 return textData.getText(CdmStore.getDefaultLanguage());
578 }
579
580 private static List<Language> getLanguageList(){
581 return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
582 }
583
584 private static DescriptionBuilder<QuantitativeData> quantitativeDescriptionBuilder = new DefaultQuantitativeDescriptionBuilder();
585 private static DescriptionBuilder<CategoricalData> categoricalDescriptionBuilder = new DefaultCategoricalDescriptionBuilder();
586 public static final String NO_LABEL_STRING = "[no label]";
587
588
589 /**
590 * <p>getDistributionText</p>
591 *
592 * @param element a {@link eu.etaxonomy.cdm.model.description.Distribution} object.
593 * @return a {@link java.lang.String} object.
594 */
595 public static String getDistributionText(Distribution element) {
596
597 String text = "EMPTY";
598
599 Distribution distribution = element;
600
601 NamedArea area = distribution.getArea();
602 if(area != null){
603
604 text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
605
606 PresenceAbsenceTerm status = distribution.getStatus();
607
608 if (status != null) {
609 text += ", " + status.getLabel();
610 }else{
611 text += ", NO STATUS";
612 }
613 }
614 return text;
615 }
616
617 /**
618 * <p>getImageText</p>
619 *
620 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
621 * @return a {@link java.lang.String} object.
622 */
623 public static String getImageText(Media media) {
624 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
625 if (title != null) {
626 return title.getText();
627 }
628 return "No title.";
629 }
630
631
632
633 /**
634 * <p>getElementText</p>
635 *
636 * @param element a {@link eu.etaxonomy.cdm.model.description.TextData} object.
637 * @return a {@link java.lang.String} object.
638 */
639 public static String getElementText(TextData element) {
640 String text = null;
641 if(element.getFeature().equals(Feature.CITATION())){
642 text = "";
643 for(DescriptionElementSource source : element.getSources()){
644 if(source.getCitation() != null){
645 text += source.getCitation().getTitleCache();
646 }
647 if(source.getNameUsedInSource() != null){
648 text += " [ " + source.getNameUsedInSource().getTitleCache() + " ]";
649 }
650 }
651 if(isBlank(text)){
652 text = "No sources provided";
653 }
654 }else{
655 List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
656 LanguageString languageString = element.getPreferredLanguageString(languages);
657 text = languageString != null ? languageString.getText() : null;
658 }
659 return isBlank(text) ? "No text provided" : text;
660 }
661
662 /**
663 * <p>getTaxonInteractionText</p>
664 *
665 * @param element a {@link eu.etaxonomy.cdm.model.description.TaxonInteraction} object.
666 * @return a {@link java.lang.String} object.
667 */
668 public static String getTaxonInteractionText(TaxonInteraction element) {
669 String text = "";
670 Taxon taxon2 = element.getTaxon2();
671 if(taxon2 != null && taxon2.getName() != null){
672 text = taxon2.getName().getTitleCache();
673 }else{
674 text = "No taxon chosen";
675 }
676
677 return text;
678 }
679
680 /**
681 * <p>getCommonNameText</p>
682 *
683 * @param commonName a {@link eu.etaxonomy.cdm.model.description.CommonTaxonName} object.
684 * @return a {@link java.lang.String} object.
685 */
686 public static String getCommonNameText(CommonTaxonName commonName) {
687 String name = commonName.getName();
688 if (name == null || name.length() == 0) {
689 name = "No name provided";
690 }
691 Language language = commonName.getLanguage();
692 if (language == null) {
693 name += " (No language provided)";
694 } else {
695 name += " (" + language.getLabel(CdmStore.getDefaultLanguage()) + ")";
696 }
697 return name;
698 }
699
700 /**
701 * <p>getDescriptionText</p>
702 *
703 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
704 * @return a {@link java.lang.String} object.
705 */
706 public static String getDescriptionText(DescriptionBase element) {
707 String text = element.getTitleCache();
708 if (text == null || text.length() == 0) {
709 text = "No label provided";
710 }
711
712 return text;
713 }
714
715 /**
716 * <p>getIndividualsAssociationText</p>
717 *
718 * @param element a {@link eu.etaxonomy.cdm.model.description.IndividualsAssociation} object.
719 * @return a {@link java.lang.String} object.
720 */
721 public static String getIndividualsAssociationText(IndividualsAssociation element) {
722 SpecimenOrObservationBase derivedUnit = element.getAssociatedSpecimenOrObservation();
723 if(derivedUnit != null){
724 return derivedUnit.getTitleCache();
725 }
726 return "No unit chosen";
727 }
728
729 /**
730 * <p>getLabel</p>
731 *
732 * @param element a {@link java.lang.Object} object.
733 * @return a {@link java.lang.String} object.
734 */
735 public static String getLabel(Object element){
736 if (element instanceof FeatureNodeContainer){
737 return getFeatureNodeContainerText((FeatureNodeContainer) element);
738 }
739 else if (element instanceof DescriptionBase) {
740 return getDescriptionText((DescriptionBase) element);
741 }
742 else if(element instanceof CategoricalData){
743 String categoricalDataText = getCategoricalDataText((CategoricalData) element);
744 if(categoricalDataText==null || categoricalDataText.isEmpty()){
745 categoricalDataText = NO_LABEL_STRING;
746 }
747 return categoricalDataText;
748 }
749 else if (element instanceof CommonTaxonName) {
750 return getCommonNameText((CommonTaxonName) element);
751 }
752 else if (element instanceof Distribution) {
753 return getDistributionText((Distribution) element);
754 }
755 else if (element instanceof IndividualsAssociation) {
756 return getIndividualsAssociationText((IndividualsAssociation) element);
757 }
758 else if (element instanceof QuantitativeData) {
759 String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
760 if(quantitativeDataText==null || quantitativeDataText.isEmpty()){
761 quantitativeDataText = NO_LABEL_STRING;
762 }
763 return quantitativeDataText;
764 }
765 else if (element instanceof TaxonInteraction) {
766 return getTaxonInteractionText((TaxonInteraction) element);
767 }
768 else if (element instanceof TextData) {
769 return getElementText((TextData) element);
770 }
771 else if (element instanceof TemporalData) {
772 return getTemporalDataText((TemporalData) element);
773 }
774 else{
775 return element.toString();
776 }
777 }
778
779 /**
780 * @param element
781 * @return
782 */
783 private static String getTemporalDataText(TemporalData element) {
784
785 return element.getPeriod().toString();
786 }
787
788 private static boolean isNotBlank(String str){
789 return StringUtils.isNotBlank(str);
790 }
791
792 private static boolean isBlank(String str){
793 return StringUtils.isBlank(str);
794 }
795 }