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 String sourceObjectTitle
= "";
178 if(sourcedObject
instanceof IIdentifiableEntity
){
179 sourceObjectTitle
= ((IdentifiableEntity
) sourcedObject
).getTitleCache();
180 }else if(sourcedObject
instanceof DescriptionElementBase
){
181 sourceObjectTitle
= "Element for description: " + ((DescriptionElementBase
) sourcedObject
).getInDescription().getTitleCache();
183 throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
186 return CdmUtils
.concat("; ", new String
[]{originalSource
.getIdNamespace(), originalSource
.getIdInSource(), sourceObjectTitle
});
187 }catch(LazyInitializationException e
){
188 String result
= "Error initializing title cache for ISourceable of an OriginalSourceBase";
189 MessagingUtils
.error(DescriptionHelper
.class, result
, e
);
190 return "TODO: " + result
;
193 if (element
instanceof LanguageStringBase
) {
194 return ((LanguageStringBase
) element
).getText();
196 if (element
instanceof DescriptionElementBase
) {
197 return getCache((DescriptionElementBase
) element
);
199 if (element
instanceof Marker
) {
200 Marker marker
= (Marker
) element
;
201 MarkerType type
= marker
.getMarkerType();
202 return (type
== null ?
"- no marker type -" : marker
.getMarkerType().getLabel()) + " (" + marker
.getFlag() + ")";
204 // TODO write return texts for NameRelationship, HomotypicalGroup, SpecimenTypeDesignation, etc.
205 return element
.toString();
209 * <p>getObjectClassname</p>
211 * @param element a {@link java.lang.Object} object.
212 * @return a {@link java.lang.String} object.
214 public static String
getObjectClassname(Object element
) {
215 return element
.getClass().getSimpleName();
219 * <p>getFeatureNodeContainerText</p>
221 * @param element a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
222 * @return a {@link java.lang.String} object.
224 public static String
getFeatureNodeContainerText(FeatureNodeContainer element
) {
225 String result
= null;
226 if(element
.getFeatureNode() != null && element
.getFeatureNode().getFeature() != null){
227 result
= element
.getFeatureNode().getFeature().getLabel(CdmStore
.getDefaultLanguage());
229 return "No label set";
232 result
= element
.getFeatureNode().getFeature().getLabel();
238 * <p>getQuantitativeDataText</p>
240 * @param element a {@link eu.etaxonomy.cdm.model.description.QuantitativeData} object.
241 * @return a {@link java.lang.String} object.
243 public static String
getQuantitativeDataText(QuantitativeData element
) {
244 TextData textData
= quantitativeDescriptionBuilder
.build(element
, getLanguageList());
246 return textData
.getText(CdmStore
.getDefaultLanguage());
250 * <p>getCategoricalDataText</p>
252 * @param element a {@link eu.etaxonomy.cdm.model.description.CategoricalData} object.
253 * @return a {@link java.lang.String} object.
255 public static String
getCategoricalDataText(CategoricalData element
) {
256 TextData textData
= categoricalDescriptionBuilder
.build(element
, getLanguageList());
258 return textData
.getText(CdmStore
.getDefaultLanguage());
261 private static List
<Language
> getLanguageList(){
262 return Arrays
.asList(new Language
[]{CdmStore
.getDefaultLanguage()});
265 private static DescriptionBuilder
<QuantitativeData
> quantitativeDescriptionBuilder
= new DefaultQuantitativeDescriptionBuilder();
266 private static DescriptionBuilder
<CategoricalData
> categoricalDescriptionBuilder
= new DefaultCategoricalDescriptionBuilder();
270 * <p>getDistributionText</p>
272 * @param element a {@link eu.etaxonomy.cdm.model.description.Distribution} object.
273 * @return a {@link java.lang.String} object.
275 public static String
getDistributionText(Distribution element
) {
277 String text
= "EMPTY";
279 Distribution distribution
= element
;
281 NamedArea area
= distribution
.getArea();
284 text
= NamedArea
.labelWithLevel(area
, CdmStore
.getDefaultLanguage());
286 PresenceAbsenceTerm status
= distribution
.getStatus();
288 if (status
!= null) {
289 text
+= ", " + status
.getLabel();
291 text
+= ", NO STATUS";
298 * <p>getImageText</p>
300 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
301 * @return a {@link java.lang.String} object.
303 public static String
getImageText(Media media
) {
304 LanguageString title
= media
.getTitle(CdmStore
.getDefaultLanguage());
306 return title
.getText();
314 * <p>getElementText</p>
316 * @param element a {@link eu.etaxonomy.cdm.model.description.TextData} object.
317 * @return a {@link java.lang.String} object.
319 public static String
getElementText(TextData element
) {
321 if(element
.getFeature().equals(Feature
.CITATION())){
323 for(DescriptionElementSource source
: element
.getSources()){
324 if(source
.getCitation() != null){
325 text
+= source
.getCitation().getTitleCache();
327 if(source
.getNameUsedInSource() != null){
328 text
+= " [ " + source
.getNameUsedInSource().getTitleCache() + " ]";
331 if(CdmUtils
.isEmpty(text
)){
332 text
= "No sources provided";
335 List
<Language
> languages
= Arrays
.asList(new Language
[]{CdmStore
.getDefaultLanguage()});
336 LanguageString languageString
= element
.getPreferredLanguageString(languages
);
337 text
= languageString
!= null ? languageString
.getText() : null;
339 return CdmUtils
.isEmpty(text
) ?
"No text provided" : text
;
343 * <p>getTaxonInteractionText</p>
345 * @param element a {@link eu.etaxonomy.cdm.model.description.TaxonInteraction} object.
346 * @return a {@link java.lang.String} object.
348 public static String
getTaxonInteractionText(TaxonInteraction element
) {
350 Taxon taxon2
= element
.getTaxon2();
351 if(taxon2
!= null && taxon2
.getName() != null){
352 text
= taxon2
.getName().getTitleCache();
354 text
= "No taxon chosen";
361 * <p>getCommonNameText</p>
363 * @param commonName a {@link eu.etaxonomy.cdm.model.description.CommonTaxonName} object.
364 * @return a {@link java.lang.String} object.
366 public static String
getCommonNameText(CommonTaxonName commonName
) {
367 String name
= commonName
.getName();
368 if (name
== null || name
.length() == 0) {
369 name
= "No name provided";
371 Language language
= commonName
.getLanguage();
372 if (language
== null) {
373 name
+= " (No language provided)";
375 name
+= " (" + language
.getDescription() + ")";
381 * <p>getDescriptionText</p>
383 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
384 * @return a {@link java.lang.String} object.
386 public static String
getDescriptionText(DescriptionBase element
) {
387 String text
= element
.getTitleCache();
388 if (text
== null || text
.length() == 0) {
389 text
= "No label provided";
392 return "Description: " + text
;
396 * <p>getIndividualsAssociationText</p>
398 * @param element a {@link eu.etaxonomy.cdm.model.description.IndividualsAssociation} object.
399 * @return a {@link java.lang.String} object.
401 public static String
getIndividualsAssociationText(IndividualsAssociation element
) {
402 SpecimenOrObservationBase derivedUnit
= element
.getAssociatedSpecimenOrObservation();
403 if(derivedUnit
!= null){
404 return derivedUnit
.getTitleCache();
406 return "No text provided";
412 * @param element a {@link java.lang.Object} object.
413 * @return a {@link java.lang.String} object.
415 public static String
getLabel(Object element
){
416 if (element
instanceof FeatureNodeContainer
){
417 return getFeatureNodeContainerText((FeatureNodeContainer
) element
);
419 else if (element
instanceof DescriptionBase
) {
420 return getDescriptionText((DescriptionBase
) element
);
422 else if(element
instanceof CategoricalData
){
423 return getCategoricalDataText((CategoricalData
) element
);
425 else if (element
instanceof CommonTaxonName
) {
426 return getCommonNameText((CommonTaxonName
) element
);
428 else if (element
instanceof Distribution
) {
429 return getDistributionText((Distribution
) element
);
431 else if (element
instanceof IndividualsAssociation
) {
432 return getIndividualsAssociationText((IndividualsAssociation
) element
);
434 else if (element
instanceof QuantitativeData
) {
435 return getQuantitativeDataText((QuantitativeData
) element
);
437 else if (element
instanceof TaxonInteraction
) {
438 return getTaxonInteractionText((TaxonInteraction
) element
);
440 else if (element
instanceof TextData
) {
441 return getElementText((TextData
) element
);
444 return element
.toString();