Merge branch 'release/5.35.0'
[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 package eu.etaxonomy.taxeditor.model;
10
11 import java.util.Arrays;
12 import java.util.List;
13
14 import org.apache.commons.lang3.StringUtils;
15
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;
41
42 /**
43 * DescriptionHelper class.
44 *
45 * @author p.ciardelli
46 * @author n.hoffmann
47 */
48 public class DescriptionHelper {
49
50 public static final String NO_LABEL_STRING = "[no label]";
51
52 /**
53 * Returns whatever the element's title cache equivalent is,
54 * depending on its class.
55 *
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.
59 */
60 public static String getCache(DescriptionElementBase element, Language language) {
61 return DescriptionElementFormatter.format(element, language);
62 }
63
64 /**
65 * Returns whatever the element's title cache equivalent is,
66 * depending on its class, using the default language.
67 *
68 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
69 * @return a {@link java.lang.String} object.
70 */
71 public static String getCache(DescriptionElementBase element) {
72 return getCache(element, CdmStore.getDefaultLanguage());
73 }
74
75 /**
76 * Set whatever the element's title cache equivalent is,
77 * depending on its class.
78 *
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.
82 */
83 public static void setCache(DescriptionElementBase element,
84 String value, Language language) {
85 if (element instanceof TextData) {
86 ((TextData) element).putText(language, value);
87 return;
88 }else if (element instanceof CommonTaxonName) {
89 ((CommonTaxonName) element).setName(value);
90 return;
91 }else if (element instanceof TaxonInteraction) {
92
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.");
95 return;
96 }else{
97 MessagingUtils.warn(DescriptionHelper.class, "No matching subclass found for DescriptionElementBase object, 'cache' not set.");
98 }
99 }
100
101 // public static String getObjectDescription(Object o){
102 // if (o instanceof CdmBase){
103 // return ReferencingObjectFormatter.format((CdmBase)o, CdmStore.getDefaultLanguage());
104 // }else{
105 // return o.toString();
106 // }
107 // }
108
109 /**
110 * Set whatever the element's title cache equivalent is,
111 * depending on its class, using the default language.
112 *
113 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
114 * @param value a {@link java.lang.String} object.
115 */
116 public static void setCache(DescriptionElementBase element,
117 String value) {
118 setCache(element, value, CdmStore.getDefaultLanguage());
119 }
120
121 // public static String getObjectClassname(Object element) {
122 // if (element instanceof CdmBase){
123 // return ((CdmBase) element).getUserFriendlyTypeName();
124 // }else{
125 // return element.getClass().getSimpleName();
126 // }
127 // }
128
129 public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
130 String result = null;
131 if(element.getFeatureNode() != null && element.getFeatureNode().getTerm() != null){
132
133 result = element.getFeatureNode().getTerm().getPreferredRepresentation(CdmStore.getDefaultLanguage()).getLabel();
134
135 } else{
136 return "No label set";
137 }
138 if (result == null){
139 result = element.getFeatureNode().getTerm().getLabel();
140 }
141 return result;
142 }
143
144 public static String getQuantitativeDataText(QuantitativeData element) {
145 QuantitativeDataFormatter formatter = new QuantitativeDataFormatter(null, null);
146 String label = formatter.format(element, getLanguageList());
147 return label;
148 }
149
150 public static String getCategoricalDataText(CategoricalData element) {
151 CategoricalDataFormatter formatter = new CategoricalDataFormatter(null, null);
152 return formatter.format(element, getLanguageList());
153 }
154
155 private static List<Language> getLanguageList(){
156 return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
157 }
158
159 public static String getDistributionText(Distribution element) {
160
161 String text = "EMPTY";
162
163 Distribution distribution = element;
164
165 NamedArea area = distribution.getArea();
166 if(area != null){
167
168 text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
169
170 PresenceAbsenceTerm status = distribution.getStatus();
171
172 if (status != null) {
173 text += ", " + status.getPreferredRepresentation(CdmStore.getDefaultLanguage()).getLabel();
174 }else{
175 text += ", NO STATUS";
176 }
177 }
178 return text;
179 }
180
181 public static String getImageText(Media media) {
182 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
183 if (title != null) {
184 return title.getText();
185 }
186 return "No title.";
187 }
188
189 public static String getElementText(TextData element) {
190 String text = null;
191 if(element.getFeature().equals(Feature.CITATION())){
192 text = "";
193 for(DescriptionElementSource source : element.getSources()){
194 if(source.getCitation() != null){
195 text += source.getCitation().getTitleCache();
196 }
197 if(source.getNameUsedInSource() != null){
198 text += " [" + source.getNameUsedInSource().getTitleCache() + "]";
199 }
200 }
201 if(isBlank(text)){
202 text = "No sources provided";
203 }
204 }else{
205 List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
206 LanguageString languageString = element.getPreferredLanguageString(languages);
207 text = languageString != null ? languageString.getText() : null;
208 }
209 return isBlank(text) ? "No text provided" : text;
210 }
211
212 public static String getTaxonInteractionText(TaxonInteraction element) {
213 String text = "";
214 Taxon taxon2 = element.getTaxon2();
215 if(taxon2 != null && taxon2.getName() != null){
216 text = taxon2.getName().getTitleCache();
217 }else{
218 text = "No taxon chosen";
219 }
220
221 return text;
222 }
223
224 public static String getCommonNameText(CommonTaxonName commonName) {
225 String name = commonName.getName();
226 if (name == null || name.length() == 0) {
227 name = "No name provided";
228 }
229 Language language = commonName.getLanguage();
230 NamedArea area = commonName.getArea();
231 String areaLabel = null;
232 String languageLabel = null;
233 if (area != null){
234 areaLabel = area.getPreferredRepresentation(CdmStore.getDefaultLanguage()).getLabel();
235 }
236 if (language != null){
237 languageLabel = language.getPreferredRepresentation(CdmStore.getDefaultLanguage()).getLabel();
238 }
239 if (areaLabel != null || languageLabel != null){
240 name += " (" ;
241 if (languageLabel != null){
242 name += languageLabel;
243 }
244 if (areaLabel != null){
245 if (languageLabel != null){
246 name += ", ";
247 }
248 name += areaLabel;
249 }
250 name += ")";
251 }
252
253
254 return name;
255 }
256
257 public static String getDescriptionText(DescriptionBase element) {
258 String text = element.getTitleCache();
259 if (text == null || text.length() == 0) {
260 text = "No label provided";
261 }
262
263 return text;
264 }
265
266 public static String getIndividualsAssociationText(IndividualsAssociation element) {
267 SpecimenOrObservationBase<?> derivedUnit = element.getAssociatedSpecimenOrObservation();
268 if(derivedUnit != null){
269 return derivedUnit.getTitleCache();
270 }
271 return "No unit chosen";
272 }
273
274 public static String getLabel(Object element){
275 if (element instanceof FeatureNodeContainer){
276 return getFeatureNodeContainerText((FeatureNodeContainer) element);
277 }
278 else if (element instanceof DescriptionBase) {
279 return getDescriptionText((DescriptionBase<?>) element);
280 }
281 else if(element instanceof CategoricalData){
282 String categoricalDataText = getCategoricalDataText((CategoricalData) element);
283 if(categoricalDataText==null || categoricalDataText.isEmpty()){
284 categoricalDataText = NO_LABEL_STRING;
285 }
286 return categoricalDataText;
287 }
288 else if (element instanceof CommonTaxonName) {
289 return getCommonNameText((CommonTaxonName) element);
290 }
291 else if (element instanceof Distribution) {
292 return getDistributionText((Distribution) element);
293 }
294 else if (element instanceof IndividualsAssociation) {
295 return getIndividualsAssociationText((IndividualsAssociation) element);
296 }
297 else if (element instanceof QuantitativeData) {
298 String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
299 if(isBlank(quantitativeDataText)){
300 quantitativeDataText = NO_LABEL_STRING;
301 }
302 return quantitativeDataText;
303 }
304 else if (element instanceof TaxonInteraction) {
305 return getTaxonInteractionText((TaxonInteraction) element);
306 }
307 else if (element instanceof TextData) {
308 return getElementText((TextData) element);
309 }
310 else if (element instanceof TemporalData) {
311 String lable = getTemporalDataText((TemporalData) element);
312 if(isBlank(lable)){
313 lable = NO_LABEL_STRING;
314 }
315 return lable;
316 }
317 else{
318 return element.toString();
319 }
320 }
321
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);
326 }
327
328 private static boolean isNotBlank(String str){
329 return StringUtils.isNotBlank(str);
330 }
331
332 private static boolean isBlank(String str){
333 return StringUtils.isBlank(str);
334 }
335 }