2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.model
;
11 import java
.util
.Arrays
;
12 import java
.util
.List
;
14 import org
.apache
.commons
.lang3
.StringUtils
;
16 import eu
.etaxonomy
.cdm
.format
.common
.ExtendedTimePeriodFormatter
;
17 import eu
.etaxonomy
.cdm
.format
.description
.CategoricalDataFormatter
;
18 import eu
.etaxonomy
.cdm
.format
.description
.DescriptionElementFormatter
;
19 import eu
.etaxonomy
.cdm
.format
.description
.QuantitativeDataFormatter
;
20 import eu
.etaxonomy
.cdm
.model
.common
.ExtendedTimePeriod
;
21 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
22 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
23 import eu
.etaxonomy
.cdm
.model
.description
.CategoricalData
;
24 import eu
.etaxonomy
.cdm
.model
.description
.CommonTaxonName
;
25 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
26 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
27 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementSource
;
28 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
29 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
30 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
31 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
32 import eu
.etaxonomy
.cdm
.model
.description
.QuantitativeData
;
33 import eu
.etaxonomy
.cdm
.model
.description
.TaxonInteraction
;
34 import eu
.etaxonomy
.cdm
.model
.description
.TemporalData
;
35 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
36 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
37 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
38 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
40 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
43 * DescriptionHelper class.
48 public class DescriptionHelper
{
50 public static final String NO_LABEL_STRING
= "[no label]";
53 * Returns whatever the element's title cache equivalent is,
54 * depending on its class.
56 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
57 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
58 * @return a {@link java.lang.String} object.
60 public static String
getCache(DescriptionElementBase element
, Language language
) {
61 return DescriptionElementFormatter
.format(element
, language
);
65 * Returns whatever the element's title cache equivalent is,
66 * depending on its class, using the default language.
68 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
69 * @return a {@link java.lang.String} object.
71 public static String
getCache(DescriptionElementBase element
) {
72 return getCache(element
, CdmStore
.getDefaultLanguage());
76 * Set whatever the element's title cache equivalent is,
77 * depending on its class.
79 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
80 * @param value a {@link java.lang.String} object.
81 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
83 public static void setCache(DescriptionElementBase element
,
84 String value
, Language language
) {
85 if (element
instanceof TextData
) {
86 ((TextData
) element
).putText(language
, value
);
88 }else if (element
instanceof CommonTaxonName
) {
89 ((CommonTaxonName
) element
).setName(value
);
91 }else if (element
instanceof TaxonInteraction
) {
93 }else if(element
instanceof Distribution
){
94 MessagingUtils
.warn(DescriptionHelper
.class, "trying to set cache on distribution, don't know what to do at the moment.");
97 MessagingUtils
.warn(DescriptionHelper
.class, "No matching subclass found for DescriptionElementBase object, 'cache' not set.");
101 // public static String getObjectDescription(Object o){
102 // if (o instanceof CdmBase){
103 // return ReferencingObjectFormatter.format((CdmBase)o, CdmStore.getDefaultLanguage());
105 // return o.toString();
110 * Set whatever the element's title cache equivalent is,
111 * depending on its class, using the default language.
113 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
114 * @param value a {@link java.lang.String} object.
116 public static void setCache(DescriptionElementBase element
,
118 setCache(element
, value
, CdmStore
.getDefaultLanguage());
121 // public static String getObjectClassname(Object element) {
122 // if (element instanceof CdmBase){
123 // return ((CdmBase) element).getUserFriendlyTypeName();
125 // return element.getClass().getSimpleName();
129 public static String
getFeatureNodeContainerText(FeatureNodeContainer element
) {
130 String result
= null;
131 if(element
.getFeatureNode() != null && element
.getFeatureNode().getTerm() != null){
133 result
= element
.getFeatureNode().getTerm().getPreferredRepresentation(CdmStore
.getDefaultLanguage()).getLabel();
136 return "No label set";
139 result
= element
.getFeatureNode().getTerm().getLabel();
144 public static String
getQuantitativeDataText(QuantitativeData element
) {
145 QuantitativeDataFormatter formatter
= new QuantitativeDataFormatter(null, null);
146 String label
= formatter
.format(element
, getLanguageList());
150 public static String
getCategoricalDataText(CategoricalData element
) {
151 CategoricalDataFormatter formatter
= new CategoricalDataFormatter(null, null);
152 return formatter
.format(element
, getLanguageList());
155 private static List
<Language
> getLanguageList(){
156 return Arrays
.asList(new Language
[]{CdmStore
.getDefaultLanguage()});
159 public static String
getDistributionText(Distribution element
) {
161 String text
= "EMPTY";
163 Distribution distribution
= element
;
165 NamedArea area
= distribution
.getArea();
168 text
= NamedArea
.labelWithLevel(area
, CdmStore
.getDefaultLanguage());
170 PresenceAbsenceTerm status
= distribution
.getStatus();
172 if (status
!= null) {
173 text
+= ", " + status
.getPreferredRepresentation(CdmStore
.getDefaultLanguage()).getLabel();
175 text
+= ", NO STATUS";
181 public static String
getImageText(Media media
) {
182 LanguageString title
= media
.getTitle(CdmStore
.getDefaultLanguage());
184 return title
.getText();
189 public static String
getElementText(TextData element
) {
191 if(element
.getFeature().equals(Feature
.CITATION())){
193 for(DescriptionElementSource source
: element
.getSources()){
194 if(source
.getCitation() != null){
195 text
+= source
.getCitation().getTitleCache();
197 if(source
.getNameUsedInSource() != null){
198 text
+= " [" + source
.getNameUsedInSource().getTitleCache() + "]";
202 text
= "No sources provided";
205 List
<Language
> languages
= Arrays
.asList(new Language
[]{CdmStore
.getDefaultLanguage()});
206 LanguageString languageString
= element
.getPreferredLanguageString(languages
);
207 text
= languageString
!= null ? languageString
.getText() : null;
209 return isBlank(text
) ?
"No text provided" : text
;
212 public static String
getTaxonInteractionText(TaxonInteraction element
) {
214 Taxon taxon2
= element
.getTaxon2();
215 if(taxon2
!= null && taxon2
.getName() != null){
216 text
= taxon2
.getName().getTitleCache();
218 text
= "No taxon chosen";
224 public static String
getCommonNameText(CommonTaxonName commonName
) {
225 String name
= commonName
.getName();
226 if (name
== null || name
.length() == 0) {
227 name
= "No name provided";
229 Language language
= commonName
.getLanguage();
230 NamedArea area
= commonName
.getArea();
231 String areaLabel
= null;
232 String languageLabel
= null;
234 areaLabel
= area
.getPreferredRepresentation(CdmStore
.getDefaultLanguage()).getLabel();
236 if (language
!= null){
237 languageLabel
= language
.getPreferredRepresentation(CdmStore
.getDefaultLanguage()).getLabel();
239 if (areaLabel
!= null || languageLabel
!= null){
241 if (languageLabel
!= null){
242 name
+= languageLabel
;
244 if (areaLabel
!= null){
245 if (languageLabel
!= null){
257 public static String
getDescriptionText(DescriptionBase element
) {
258 String text
= element
.getTitleCache();
259 if (text
== null || text
.length() == 0) {
260 text
= "No label provided";
266 public static String
getIndividualsAssociationText(IndividualsAssociation element
) {
267 SpecimenOrObservationBase
<?
> derivedUnit
= element
.getAssociatedSpecimenOrObservation();
268 if(derivedUnit
!= null){
269 return derivedUnit
.getTitleCache();
271 return "No unit chosen";
274 public static String
getLabel(Object element
){
275 if (element
instanceof FeatureNodeContainer
){
276 return getFeatureNodeContainerText((FeatureNodeContainer
) element
);
278 else if (element
instanceof DescriptionBase
) {
279 return getDescriptionText((DescriptionBase
<?
>) element
);
281 else if(element
instanceof CategoricalData
){
282 String categoricalDataText
= getCategoricalDataText((CategoricalData
) element
);
283 if(categoricalDataText
==null || categoricalDataText
.isEmpty()){
284 categoricalDataText
= NO_LABEL_STRING
;
286 return categoricalDataText
;
288 else if (element
instanceof CommonTaxonName
) {
289 return getCommonNameText((CommonTaxonName
) element
);
291 else if (element
instanceof Distribution
) {
292 return getDistributionText((Distribution
) element
);
294 else if (element
instanceof IndividualsAssociation
) {
295 return getIndividualsAssociationText((IndividualsAssociation
) element
);
297 else if (element
instanceof QuantitativeData
) {
298 String quantitativeDataText
= getQuantitativeDataText((QuantitativeData
) element
);
299 if(isBlank(quantitativeDataText
)){
300 quantitativeDataText
= NO_LABEL_STRING
;
302 return quantitativeDataText
;
304 else if (element
instanceof TaxonInteraction
) {
305 return getTaxonInteractionText((TaxonInteraction
) element
);
307 else if (element
instanceof TextData
) {
308 return getElementText((TextData
) element
);
310 else if (element
instanceof TemporalData
) {
311 String lable
= getTemporalDataText((TemporalData
) element
);
313 lable
= NO_LABEL_STRING
;
318 return element
.toString();
322 private static String
getTemporalDataText(TemporalData element
) {
323 ExtendedTimePeriod period
= element
.getPeriod();
324 ExtendedTimePeriodFormatter formatter
= ExtendedTimePeriodFormatter
.NewDefaultInstance();
325 return period
== null ? NO_LABEL_STRING
: formatter
.format(period
);
328 private static boolean isNotBlank(String str
){
329 return StringUtils
.isNotBlank(str
);
332 private static boolean isBlank(String str
){
333 return StringUtils
.isBlank(str
);