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
.DescriptionElementFormatter
;
17 import eu
.etaxonomy
.cdm
.format
.common
.ExtendedTimePeriodFormatter
;
18 import eu
.etaxonomy
.cdm
.format
.description
.CategoricalDataFormatter
;
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){
132 result
= element
.getFeatureNode().getTerm().getLabel(CdmStore
.getDefaultLanguage());
134 return "No label set";
137 result
= element
.getFeatureNode().getTerm().getLabel();
142 public static String
getQuantitativeDataText(QuantitativeData element
) {
143 QuantitativeDataFormatter formatter
= new QuantitativeDataFormatter(null, null);
144 String label
= formatter
.format(element
, getLanguageList());
148 public static String
getCategoricalDataText(CategoricalData element
) {
149 CategoricalDataFormatter formatter
= new CategoricalDataFormatter(null, null);
150 return formatter
.format(element
, getLanguageList());
153 private static List
<Language
> getLanguageList(){
154 return Arrays
.asList(new Language
[]{CdmStore
.getDefaultLanguage()});
157 public static String
getDistributionText(Distribution element
) {
159 String text
= "EMPTY";
161 Distribution distribution
= element
;
163 NamedArea area
= distribution
.getArea();
166 text
= NamedArea
.labelWithLevel(area
, CdmStore
.getDefaultLanguage());
168 PresenceAbsenceTerm status
= distribution
.getStatus();
170 if (status
!= null) {
171 text
+= ", " + status
.getLabel();
173 text
+= ", NO STATUS";
179 public static String
getImageText(Media media
) {
180 LanguageString title
= media
.getTitle(CdmStore
.getDefaultLanguage());
182 return title
.getText();
187 public static String
getElementText(TextData element
) {
189 if(element
.getFeature().equals(Feature
.CITATION())){
191 for(DescriptionElementSource source
: element
.getSources()){
192 if(source
.getCitation() != null){
193 text
+= source
.getCitation().getTitleCache();
195 if(source
.getNameUsedInSource() != null){
196 text
+= " [" + source
.getNameUsedInSource().getTitleCache() + "]";
200 text
= "No sources provided";
203 List
<Language
> languages
= Arrays
.asList(new Language
[]{CdmStore
.getDefaultLanguage()});
204 LanguageString languageString
= element
.getPreferredLanguageString(languages
);
205 text
= languageString
!= null ? languageString
.getText() : null;
207 return isBlank(text
) ?
"No text provided" : text
;
210 public static String
getTaxonInteractionText(TaxonInteraction element
) {
212 Taxon taxon2
= element
.getTaxon2();
213 if(taxon2
!= null && taxon2
.getName() != null){
214 text
= taxon2
.getName().getTitleCache();
216 text
= "No taxon chosen";
222 public static String
getCommonNameText(CommonTaxonName commonName
) {
223 String name
= commonName
.getName();
224 if (name
== null || name
.length() == 0) {
225 name
= "No name provided";
227 Language language
= commonName
.getLanguage();
228 if (language
== null) {
229 name
+= " (No language provided)";
231 name
+= " (" + language
.getLabel(CdmStore
.getDefaultLanguage()) + ")";
236 public static String
getDescriptionText(DescriptionBase element
) {
237 String text
= element
.getTitleCache();
238 if (text
== null || text
.length() == 0) {
239 text
= "No label provided";
245 public static String
getIndividualsAssociationText(IndividualsAssociation element
) {
246 SpecimenOrObservationBase
<?
> derivedUnit
= element
.getAssociatedSpecimenOrObservation();
247 if(derivedUnit
!= null){
248 return derivedUnit
.getTitleCache();
250 return "No unit chosen";
253 public static String
getLabel(Object element
){
254 if (element
instanceof FeatureNodeContainer
){
255 return getFeatureNodeContainerText((FeatureNodeContainer
) element
);
257 else if (element
instanceof DescriptionBase
) {
258 return getDescriptionText((DescriptionBase
<?
>) element
);
260 else if(element
instanceof CategoricalData
){
261 String categoricalDataText
= getCategoricalDataText((CategoricalData
) element
);
262 if(categoricalDataText
==null || categoricalDataText
.isEmpty()){
263 categoricalDataText
= NO_LABEL_STRING
;
265 return categoricalDataText
;
267 else if (element
instanceof CommonTaxonName
) {
268 return getCommonNameText((CommonTaxonName
) element
);
270 else if (element
instanceof Distribution
) {
271 return getDistributionText((Distribution
) element
);
273 else if (element
instanceof IndividualsAssociation
) {
274 return getIndividualsAssociationText((IndividualsAssociation
) element
);
276 else if (element
instanceof QuantitativeData
) {
277 String quantitativeDataText
= getQuantitativeDataText((QuantitativeData
) element
);
278 if(isBlank(quantitativeDataText
)){
279 quantitativeDataText
= NO_LABEL_STRING
;
281 return quantitativeDataText
;
283 else if (element
instanceof TaxonInteraction
) {
284 return getTaxonInteractionText((TaxonInteraction
) element
);
286 else if (element
instanceof TextData
) {
287 return getElementText((TextData
) element
);
289 else if (element
instanceof TemporalData
) {
290 String lable
= getTemporalDataText((TemporalData
) element
);
292 lable
= NO_LABEL_STRING
;
297 return element
.toString();
301 private static String
getTemporalDataText(TemporalData element
) {
302 ExtendedTimePeriod period
= element
.getPeriod();
303 ExtendedTimePeriodFormatter formatter
= ExtendedTimePeriodFormatter
.NewDefaultInstance();
304 return period
== null ? NO_LABEL_STRING
: formatter
.format(period
);
307 private static boolean isNotBlank(String str
){
308 return StringUtils
.isNotBlank(str
);
311 private static boolean isBlank(String str
){
312 return StringUtils
.isBlank(str
);