ref #9541 new referencing objects implementation in TaxEditor
[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.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;
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 result = element.getFeatureNode().getTerm().getLabel(CdmStore.getDefaultLanguage());
133 } else{
134 return "No label set";
135 }
136 if (result == null){
137 result = element.getFeatureNode().getTerm().getLabel();
138 }
139 return result;
140 }
141
142 public static String getQuantitativeDataText(QuantitativeData element) {
143 QuantitativeDataFormatter formatter = new QuantitativeDataFormatter(null, null);
144 String label = formatter.format(element, getLanguageList());
145 return label;
146 }
147
148 public static String getCategoricalDataText(CategoricalData element) {
149 CategoricalDataFormatter formatter = new CategoricalDataFormatter(null, null);
150 return formatter.format(element, getLanguageList());
151 }
152
153 private static List<Language> getLanguageList(){
154 return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
155 }
156
157 public static String getDistributionText(Distribution element) {
158
159 String text = "EMPTY";
160
161 Distribution distribution = element;
162
163 NamedArea area = distribution.getArea();
164 if(area != null){
165
166 text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
167
168 PresenceAbsenceTerm status = distribution.getStatus();
169
170 if (status != null) {
171 text += ", " + status.getLabel();
172 }else{
173 text += ", NO STATUS";
174 }
175 }
176 return text;
177 }
178
179 public static String getImageText(Media media) {
180 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
181 if (title != null) {
182 return title.getText();
183 }
184 return "No title.";
185 }
186
187 public static String getElementText(TextData element) {
188 String text = null;
189 if(element.getFeature().equals(Feature.CITATION())){
190 text = "";
191 for(DescriptionElementSource source : element.getSources()){
192 if(source.getCitation() != null){
193 text += source.getCitation().getTitleCache();
194 }
195 if(source.getNameUsedInSource() != null){
196 text += " [" + source.getNameUsedInSource().getTitleCache() + "]";
197 }
198 }
199 if(isBlank(text)){
200 text = "No sources provided";
201 }
202 }else{
203 List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
204 LanguageString languageString = element.getPreferredLanguageString(languages);
205 text = languageString != null ? languageString.getText() : null;
206 }
207 return isBlank(text) ? "No text provided" : text;
208 }
209
210 public static String getTaxonInteractionText(TaxonInteraction element) {
211 String text = "";
212 Taxon taxon2 = element.getTaxon2();
213 if(taxon2 != null && taxon2.getName() != null){
214 text = taxon2.getName().getTitleCache();
215 }else{
216 text = "No taxon chosen";
217 }
218
219 return text;
220 }
221
222 public static String getCommonNameText(CommonTaxonName commonName) {
223 String name = commonName.getName();
224 if (name == null || name.length() == 0) {
225 name = "No name provided";
226 }
227 Language language = commonName.getLanguage();
228 if (language == null) {
229 name += " (No language provided)";
230 } else {
231 name += " (" + language.getLabel(CdmStore.getDefaultLanguage()) + ")";
232 }
233 return name;
234 }
235
236 public static String getDescriptionText(DescriptionBase element) {
237 String text = element.getTitleCache();
238 if (text == null || text.length() == 0) {
239 text = "No label provided";
240 }
241
242 return text;
243 }
244
245 public static String getIndividualsAssociationText(IndividualsAssociation element) {
246 SpecimenOrObservationBase<?> derivedUnit = element.getAssociatedSpecimenOrObservation();
247 if(derivedUnit != null){
248 return derivedUnit.getTitleCache();
249 }
250 return "No unit chosen";
251 }
252
253 public static String getLabel(Object element){
254 if (element instanceof FeatureNodeContainer){
255 return getFeatureNodeContainerText((FeatureNodeContainer) element);
256 }
257 else if (element instanceof DescriptionBase) {
258 return getDescriptionText((DescriptionBase<?>) element);
259 }
260 else if(element instanceof CategoricalData){
261 String categoricalDataText = getCategoricalDataText((CategoricalData) element);
262 if(categoricalDataText==null || categoricalDataText.isEmpty()){
263 categoricalDataText = NO_LABEL_STRING;
264 }
265 return categoricalDataText;
266 }
267 else if (element instanceof CommonTaxonName) {
268 return getCommonNameText((CommonTaxonName) element);
269 }
270 else if (element instanceof Distribution) {
271 return getDistributionText((Distribution) element);
272 }
273 else if (element instanceof IndividualsAssociation) {
274 return getIndividualsAssociationText((IndividualsAssociation) element);
275 }
276 else if (element instanceof QuantitativeData) {
277 String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
278 if(isBlank(quantitativeDataText)){
279 quantitativeDataText = NO_LABEL_STRING;
280 }
281 return quantitativeDataText;
282 }
283 else if (element instanceof TaxonInteraction) {
284 return getTaxonInteractionText((TaxonInteraction) element);
285 }
286 else if (element instanceof TextData) {
287 return getElementText((TextData) element);
288 }
289 else if (element instanceof TemporalData) {
290 String lable = getTemporalDataText((TemporalData) element);
291 if(isBlank(lable)){
292 lable = NO_LABEL_STRING;
293 }
294 return lable;
295 }
296 else{
297 return element.toString();
298 }
299 }
300
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);
305 }
306
307 private static boolean isNotBlank(String str){
308 return StringUtils.isNotBlank(str);
309 }
310
311 private static boolean isBlank(String str){
312 return StringUtils.isBlank(str);
313 }
314 }