3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.model
;
13 import java
.util
.Arrays
;
14 import java
.util
.List
;
16 import org
.hibernate
.LazyInitializationException
;
18 import eu
.etaxonomy
.cdm
.api
.service
.DefaultCategoricalDescriptionBuilder
;
19 import eu
.etaxonomy
.cdm
.api
.service
.DefaultQuantitativeDescriptionBuilder
;
20 import eu
.etaxonomy
.cdm
.api
.service
.DescriptionBuilder
;
21 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
22 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
23 import eu
.etaxonomy
.cdm
.model
.common
.ISourceable
;
24 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
25 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
26 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
27 import eu
.etaxonomy
.cdm
.model
.common
.LanguageStringBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
29 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
30 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceBase
;
31 import eu
.etaxonomy
.cdm
.model
.description
.CategoricalData
;
32 import eu
.etaxonomy
.cdm
.model
.description
.CommonTaxonName
;
33 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
34 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
35 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementSource
;
36 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
37 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
38 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
39 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
40 import eu
.etaxonomy
.cdm
.model
.description
.QuantitativeData
;
41 import eu
.etaxonomy
.cdm
.model
.description
.TaxonInteraction
;
42 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
43 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
44 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
45 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
47 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
50 * <p>DescriptionHelper class.</p>
57 public class DescriptionHelper
{
60 * Returns whatever the element's title cache equivalent is,
61 * depending on its class.
63 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
64 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
65 * @return a {@link java.lang.String} object.
67 public static String
getCache(DescriptionElementBase element
,
70 if (element
instanceof TextData
) {
71 cache
= ((TextData
) element
).getText(language
);
73 if (element
instanceof CommonTaxonName
) {
74 cache
= ((CommonTaxonName
) element
).getName();
76 if (element
instanceof TaxonInteraction
) {
77 Taxon taxon2
= ((TaxonInteraction
) element
).getTaxon2();
78 if(taxon2
!= null && taxon2
.getName() != null){
79 cache
= taxon2
.getName().getTitleCache();
81 cache
= "No taxon chosen";
85 if (element
instanceof Distribution
) {
86 Distribution distribution
= (Distribution
) element
;
88 NamedArea area
= distribution
.getArea();
90 cache
= area
.getLabel();
92 PresenceAbsenceTerm status
= distribution
.getStatus();
94 cache
+= ", no status";
96 cache
+= ", " + status
.getLabel();
100 return cache
== null ?
"" : cache
;
104 * Returns whatever the element's title cache equivalent is,
105 * depending on its class, using the default language.
107 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
108 * @return a {@link java.lang.String} object.
110 public static String
getCache(DescriptionElementBase element
) {
111 return getCache(element
, CdmStore
.getDefaultLanguage());
115 * Set whatever the element's title cache equivalent is,
116 * depending on its class.
118 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
119 * @param value a {@link java.lang.String} object.
120 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
122 public static void setCache(DescriptionElementBase element
,
123 String value
, Language language
) {
124 if (element
instanceof TextData
) {
125 ((TextData
) element
).putText(language
, value
);
128 if (element
instanceof CommonTaxonName
) {
129 ((CommonTaxonName
) element
).setName(value
);
132 if (element
instanceof TaxonInteraction
) {
135 if(element
instanceof Distribution
){
136 MessagingUtils
.warn(DescriptionHelper
.class, "trying to set cache on distribution, don't know what to do at the moment.");
139 MessagingUtils
.warn(DescriptionHelper
.class, "No matching subclass found for DescriptionElementBase object, 'cache' not set.");
143 * Set whatever the element's title cache equivalent is,
144 * depending on its class, using the default language.
146 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
147 * @param value a {@link java.lang.String} object.
149 public static void setCache(DescriptionElementBase element
,
151 setCache(element
, value
, CdmStore
.getDefaultLanguage());
155 * @see eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.IReferencingObjectsService#getObjectDescription(java.lang.Object)
158 * <p>getObjectDescription</p>
160 * @param element a {@link java.lang.Object} object.
161 * @return a {@link java.lang.String} object.
163 public static String
getObjectDescription(Object element
) {
164 if (element
instanceof IdentifiableEntity
) {
166 return ((IdentifiableEntity
) element
).getTitleCache();
167 }catch(LazyInitializationException e
){
168 String result
= "No Session to initialize title cache for IdentifiableEntity";
169 MessagingUtils
.error(DescriptionHelper
.class, result
, e
);
170 return "TODO: " + result
;
173 if (element
instanceof OriginalSourceBase
) {
175 OriginalSourceBase originalSource
= (OriginalSourceBase
) element
;
176 // ISourceable sourcedObject = originalSource.getSourcedObj();
177 //due to #5743 the bidirectionality for sourced object had to be removed
178 String sourceObjectTitle
= "sourced object data not available (#5743)";
179 // if(sourcedObject instanceof IIdentifiableEntity){
180 // sourceObjectTitle = ((IdentifiableEntity) sourcedObject).getTitleCache();
181 // }else if(sourcedObject instanceof DescriptionElementBase){
182 // sourceObjectTitle = "Element for description: " + ((DescriptionElementBase) sourcedObject).getInDescription().getTitleCache();
184 // throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
187 return CdmUtils
.concat("; ", new String
[]{originalSource
.getIdNamespace(), originalSource
.getIdInSource(), sourceObjectTitle
});
188 }catch(LazyInitializationException e
){
189 String result
= "Error initializing title cache for ISourceable of an OriginalSourceBase";
190 MessagingUtils
.error(DescriptionHelper
.class, result
, e
);
191 return "TODO: " + result
;
194 if (element
instanceof LanguageStringBase
) {
195 return ((LanguageStringBase
) element
).getText();
197 if (element
instanceof DescriptionElementBase
) {
198 return getCache((DescriptionElementBase
) element
);
200 if (element
instanceof Marker
) {
201 Marker marker
= (Marker
) element
;
202 MarkerType type
= marker
.getMarkerType();
203 return (type
== null ?
"- no marker type -" : marker
.getMarkerType().getLabel()) + " (" + marker
.getFlag() + ")";
205 // TODO write return texts for NameRelationship, HomotypicalGroup, SpecimenTypeDesignation, etc.
206 return element
.toString();
210 * <p>getObjectClassname</p>
212 * @param element a {@link java.lang.Object} object.
213 * @return a {@link java.lang.String} object.
215 public static String
getObjectClassname(Object element
) {
216 return element
.getClass().getSimpleName();
220 * <p>getFeatureNodeContainerText</p>
222 * @param element a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
223 * @return a {@link java.lang.String} object.
225 public static String
getFeatureNodeContainerText(FeatureNodeContainer element
) {
226 String result
= null;
227 if(element
.getFeatureNode() != null && element
.getFeatureNode().getFeature() != null){
228 result
= element
.getFeatureNode().getFeature().getLabel(CdmStore
.getDefaultLanguage());
230 return "No label set";
233 result
= element
.getFeatureNode().getFeature().getLabel();
239 * <p>getQuantitativeDataText</p>
241 * @param element a {@link eu.etaxonomy.cdm.model.description.QuantitativeData} object.
242 * @return a {@link java.lang.String} object.
244 public static String
getQuantitativeDataText(QuantitativeData element
) {
245 TextData textData
= quantitativeDescriptionBuilder
.build(element
, getLanguageList());
247 return textData
.getText(CdmStore
.getDefaultLanguage());
251 * <p>getCategoricalDataText</p>
253 * @param element a {@link eu.etaxonomy.cdm.model.description.CategoricalData} object.
254 * @return a {@link java.lang.String} object.
256 public static String
getCategoricalDataText(CategoricalData element
) {
257 TextData textData
= categoricalDescriptionBuilder
.build(element
, getLanguageList());
259 return textData
.getText(CdmStore
.getDefaultLanguage());
262 private static List
<Language
> getLanguageList(){
263 return Arrays
.asList(new Language
[]{CdmStore
.getDefaultLanguage()});
266 private static DescriptionBuilder
<QuantitativeData
> quantitativeDescriptionBuilder
= new DefaultQuantitativeDescriptionBuilder();
267 private static DescriptionBuilder
<CategoricalData
> categoricalDescriptionBuilder
= new DefaultCategoricalDescriptionBuilder();
271 * <p>getDistributionText</p>
273 * @param element a {@link eu.etaxonomy.cdm.model.description.Distribution} object.
274 * @return a {@link java.lang.String} object.
276 public static String
getDistributionText(Distribution element
) {
278 String text
= "EMPTY";
280 Distribution distribution
= element
;
282 NamedArea area
= distribution
.getArea();
285 text
= NamedArea
.labelWithLevel(area
, CdmStore
.getDefaultLanguage());
287 PresenceAbsenceTerm status
= distribution
.getStatus();
289 if (status
!= null) {
290 text
+= ", " + status
.getLabel();
292 text
+= ", NO STATUS";
299 * <p>getImageText</p>
301 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
302 * @return a {@link java.lang.String} object.
304 public static String
getImageText(Media media
) {
305 LanguageString title
= media
.getTitle(CdmStore
.getDefaultLanguage());
307 return title
.getText();
315 * <p>getElementText</p>
317 * @param element a {@link eu.etaxonomy.cdm.model.description.TextData} object.
318 * @return a {@link java.lang.String} object.
320 public static String
getElementText(TextData element
) {
322 if(element
.getFeature().equals(Feature
.CITATION())){
324 for(DescriptionElementSource source
: element
.getSources()){
325 if(source
.getCitation() != null){
326 text
+= source
.getCitation().getTitleCache();
328 if(source
.getNameUsedInSource() != null){
329 text
+= " [ " + source
.getNameUsedInSource().getTitleCache() + " ]";
332 if(CdmUtils
.isEmpty(text
)){
333 text
= "No sources provided";
336 List
<Language
> languages
= Arrays
.asList(new Language
[]{CdmStore
.getDefaultLanguage()});
337 LanguageString languageString
= element
.getPreferredLanguageString(languages
);
338 text
= languageString
!= null ? languageString
.getText() : null;
340 return CdmUtils
.isEmpty(text
) ?
"No text provided" : text
;
344 * <p>getTaxonInteractionText</p>
346 * @param element a {@link eu.etaxonomy.cdm.model.description.TaxonInteraction} object.
347 * @return a {@link java.lang.String} object.
349 public static String
getTaxonInteractionText(TaxonInteraction element
) {
351 Taxon taxon2
= element
.getTaxon2();
352 if(taxon2
!= null && taxon2
.getName() != null){
353 text
= taxon2
.getName().getTitleCache();
355 text
= "No taxon chosen";
362 * <p>getCommonNameText</p>
364 * @param commonName a {@link eu.etaxonomy.cdm.model.description.CommonTaxonName} object.
365 * @return a {@link java.lang.String} object.
367 public static String
getCommonNameText(CommonTaxonName commonName
) {
368 String name
= commonName
.getName();
369 if (name
== null || name
.length() == 0) {
370 name
= "No name provided";
372 Language language
= commonName
.getLanguage();
373 if (language
== null) {
374 name
+= " (No language provided)";
376 name
+= " (" + language
.getDescription() + ")";
382 * <p>getDescriptionText</p>
384 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
385 * @return a {@link java.lang.String} object.
387 public static String
getDescriptionText(DescriptionBase element
) {
388 String text
= element
.getTitleCache();
389 if (text
== null || text
.length() == 0) {
390 text
= "No label provided";
397 * <p>getIndividualsAssociationText</p>
399 * @param element a {@link eu.etaxonomy.cdm.model.description.IndividualsAssociation} object.
400 * @return a {@link java.lang.String} object.
402 public static String
getIndividualsAssociationText(IndividualsAssociation element
) {
403 SpecimenOrObservationBase derivedUnit
= element
.getAssociatedSpecimenOrObservation();
404 if(derivedUnit
!= null){
405 return derivedUnit
.getTitleCache();
407 return "No unit chosen";
413 * @param element a {@link java.lang.Object} object.
414 * @return a {@link java.lang.String} object.
416 public static String
getLabel(Object element
){
417 String noLabelString
= "[no label]";
418 if (element
instanceof FeatureNodeContainer
){
419 return getFeatureNodeContainerText((FeatureNodeContainer
) element
);
421 else if (element
instanceof DescriptionBase
) {
422 return getDescriptionText((DescriptionBase
) element
);
424 else if(element
instanceof CategoricalData
){
425 String categoricalDataText
= getCategoricalDataText((CategoricalData
) element
);
426 if(categoricalDataText
==null || categoricalDataText
.isEmpty()){
427 categoricalDataText
= noLabelString
;
429 return categoricalDataText
;
431 else if (element
instanceof CommonTaxonName
) {
432 return getCommonNameText((CommonTaxonName
) element
);
434 else if (element
instanceof Distribution
) {
435 return getDistributionText((Distribution
) element
);
437 else if (element
instanceof IndividualsAssociation
) {
438 return getIndividualsAssociationText((IndividualsAssociation
) element
);
440 else if (element
instanceof QuantitativeData
) {
441 String quantitativeDataText
= getQuantitativeDataText((QuantitativeData
) element
);
442 if(quantitativeDataText
==null || quantitativeDataText
.isEmpty()){
443 quantitativeDataText
= noLabelString
;
445 return quantitativeDataText
;
447 else if (element
instanceof TaxonInteraction
) {
448 return getTaxonInteractionText((TaxonInteraction
) element
);
450 else if (element
instanceof TextData
) {
451 return getElementText((TextData
) element
);
454 return element
.toString();