Remove sourced object form referencing objects view #5743, #5746
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / DescriptionHelper.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.model;
12
13 import java.util.Arrays;
14 import java.util.List;
15
16 import org.hibernate.LazyInitializationException;
17
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;
48
49 /**
50 * <p>DescriptionHelper class.</p>
51 *
52 * @author p.ciardelli
53 * @author n.hoffmann
54 * @created 02.04.2009
55 * @version 1.0
56 */
57 public class DescriptionHelper {
58
59 /**
60 * Returns whatever the element's title cache equivalent is,
61 * depending on its class.
62 *
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.
66 */
67 public static String getCache(DescriptionElementBase element,
68 Language language) {
69 String cache = null;
70 if (element instanceof TextData) {
71 cache = ((TextData) element).getText(language);
72 }
73 if (element instanceof CommonTaxonName) {
74 cache = ((CommonTaxonName) element).getName();
75 }
76 if (element instanceof TaxonInteraction) {
77 Taxon taxon2 = ((TaxonInteraction) element).getTaxon2();
78 if(taxon2 != null && taxon2.getName() != null){
79 cache = taxon2.getName().getTitleCache();
80 }else{
81 cache = "No taxon chosen";
82 }
83
84 }
85 if (element instanceof Distribution) {
86 Distribution distribution = (Distribution) element;
87
88 NamedArea area = distribution.getArea();
89 if(area != null){
90 cache = area.getLabel();
91
92 PresenceAbsenceTerm status = distribution.getStatus();
93 if (status == null){
94 cache += ", no status";
95 }else {
96 cache += ", " + status.getLabel();
97 }
98 }
99 }
100 return cache == null ? "" : cache;
101 }
102
103 /**
104 * Returns whatever the element's title cache equivalent is,
105 * depending on its class, using the default language.
106 *
107 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
108 * @return a {@link java.lang.String} object.
109 */
110 public static String getCache(DescriptionElementBase element) {
111 return getCache(element, CdmStore.getDefaultLanguage());
112 }
113
114 /**
115 * Set whatever the element's title cache equivalent is,
116 * depending on its class.
117 *
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.
121 */
122 public static void setCache(DescriptionElementBase element,
123 String value, Language language) {
124 if (element instanceof TextData) {
125 ((TextData) element).putText(language, value);
126 return;
127 }
128 if (element instanceof CommonTaxonName) {
129 ((CommonTaxonName) element).setName(value);
130 return;
131 }
132 if (element instanceof TaxonInteraction) {
133
134 }
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.");
137 return;
138 }
139 MessagingUtils.warn(DescriptionHelper.class, "No matching subclass found for DescriptionElementBase object, 'cache' not set.");
140 }
141
142 /**
143 * Set whatever the element's title cache equivalent is,
144 * depending on its class, using the default language.
145 *
146 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
147 * @param value a {@link java.lang.String} object.
148 */
149 public static void setCache(DescriptionElementBase element,
150 String value) {
151 setCache(element, value, CdmStore.getDefaultLanguage());
152 }
153
154 /* (non-Javadoc)
155 * @see eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.IReferencingObjectsService#getObjectDescription(java.lang.Object)
156 */
157 /**
158 * <p>getObjectDescription</p>
159 *
160 * @param element a {@link java.lang.Object} object.
161 * @return a {@link java.lang.String} object.
162 */
163 public static String getObjectDescription(Object element) {
164 if (element instanceof IdentifiableEntity) {
165 try{
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;
171 }
172 }
173 if (element instanceof OriginalSourceBase) {
174 try{
175 OriginalSourceBase originalSource = (OriginalSourceBase) element;
176 // ISourceable sourcedObject = originalSource.getSourcedObj();
177 //due to #5743 the bidirectionality for sourced object had to be removed
178 String sourceObjectTitle = "sourced object data not available (#5743)";
179 // if(sourcedObject instanceof IIdentifiableEntity){
180 // sourceObjectTitle = ((IdentifiableEntity) sourcedObject).getTitleCache();
181 // }else if(sourcedObject instanceof DescriptionElementBase){
182 // sourceObjectTitle = "Element for description: " + ((DescriptionElementBase) sourcedObject).getInDescription().getTitleCache();
183 // }else{
184 // throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
185 // }
186
187 return CdmUtils.concat("; ", new String[]{originalSource.getIdNamespace(), originalSource.getIdInSource(), sourceObjectTitle});
188 }catch(LazyInitializationException e){
189 String result = "Error initializing title cache for ISourceable of an OriginalSourceBase";
190 MessagingUtils.error(DescriptionHelper.class, result, e);
191 return "TODO: " + result;
192 }
193 }
194 if (element instanceof LanguageStringBase) {
195 return ((LanguageStringBase) element).getText();
196 }
197 if (element instanceof DescriptionElementBase) {
198 return getCache((DescriptionElementBase) element);
199 }
200 if (element instanceof Marker) {
201 Marker marker = (Marker) element;
202 MarkerType type = marker.getMarkerType();
203 return (type == null ? "- no marker type -" : marker.getMarkerType().getLabel()) + " (" + marker.getFlag() + ")";
204 }
205 // TODO write return texts for NameRelationship, HomotypicalGroup, SpecimenTypeDesignation, etc.
206 return element.toString();
207 }
208
209 /**
210 * <p>getObjectClassname</p>
211 *
212 * @param element a {@link java.lang.Object} object.
213 * @return a {@link java.lang.String} object.
214 */
215 public static String getObjectClassname(Object element) {
216 return element.getClass().getSimpleName();
217 }
218
219 /**
220 * <p>getFeatureNodeContainerText</p>
221 *
222 * @param element a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
223 * @return a {@link java.lang.String} object.
224 */
225 public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
226 String result = null;
227 if(element.getFeatureNode() != null && element.getFeatureNode().getFeature() != null){
228 result = element.getFeatureNode().getFeature().getLabel(CdmStore.getDefaultLanguage());
229 } else{
230 return "No label set";
231 }
232 if (result == null){
233 result = element.getFeatureNode().getFeature().getLabel();
234 }
235 return result;
236 }
237
238 /**
239 * <p>getQuantitativeDataText</p>
240 *
241 * @param element a {@link eu.etaxonomy.cdm.model.description.QuantitativeData} object.
242 * @return a {@link java.lang.String} object.
243 */
244 public static String getQuantitativeDataText(QuantitativeData element) {
245 TextData textData = quantitativeDescriptionBuilder.build(element, getLanguageList());
246
247 return textData.getText(CdmStore.getDefaultLanguage());
248 }
249
250 /**
251 * <p>getCategoricalDataText</p>
252 *
253 * @param element a {@link eu.etaxonomy.cdm.model.description.CategoricalData} object.
254 * @return a {@link java.lang.String} object.
255 */
256 public static String getCategoricalDataText(CategoricalData element) {
257 TextData textData = categoricalDescriptionBuilder.build(element, getLanguageList());
258
259 return textData.getText(CdmStore.getDefaultLanguage());
260 }
261
262 private static List<Language> getLanguageList(){
263 return Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
264 }
265
266 private static DescriptionBuilder<QuantitativeData> quantitativeDescriptionBuilder = new DefaultQuantitativeDescriptionBuilder();
267 private static DescriptionBuilder<CategoricalData> categoricalDescriptionBuilder = new DefaultCategoricalDescriptionBuilder();
268
269
270 /**
271 * <p>getDistributionText</p>
272 *
273 * @param element a {@link eu.etaxonomy.cdm.model.description.Distribution} object.
274 * @return a {@link java.lang.String} object.
275 */
276 public static String getDistributionText(Distribution element) {
277
278 String text = "EMPTY";
279
280 Distribution distribution = element;
281
282 NamedArea area = distribution.getArea();
283 if(area != null){
284
285 text = NamedArea.labelWithLevel(area, CdmStore.getDefaultLanguage());
286
287 PresenceAbsenceTerm status = distribution.getStatus();
288
289 if (status != null) {
290 text += ", " + status.getLabel();
291 }else{
292 text += ", NO STATUS";
293 }
294 }
295 return text;
296 }
297
298 /**
299 * <p>getImageText</p>
300 *
301 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
302 * @return a {@link java.lang.String} object.
303 */
304 public static String getImageText(Media media) {
305 LanguageString title = media.getTitle(CdmStore.getDefaultLanguage());
306 if (title != null) {
307 return title.getText();
308 }
309 return "No title.";
310 }
311
312
313
314 /**
315 * <p>getElementText</p>
316 *
317 * @param element a {@link eu.etaxonomy.cdm.model.description.TextData} object.
318 * @return a {@link java.lang.String} object.
319 */
320 public static String getElementText(TextData element) {
321 String text = null;
322 if(element.getFeature().equals(Feature.CITATION())){
323 text = "";
324 for(DescriptionElementSource source : element.getSources()){
325 if(source.getCitation() != null){
326 text += source.getCitation().getTitleCache();
327 }
328 if(source.getNameUsedInSource() != null){
329 text += " [ " + source.getNameUsedInSource().getTitleCache() + " ]";
330 }
331 }
332 if(CdmUtils.isEmpty(text)){
333 text = "No sources provided";
334 }
335 }else{
336 List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
337 LanguageString languageString = element.getPreferredLanguageString(languages);
338 text = languageString != null ? languageString.getText() : null;
339 }
340 return CdmUtils.isEmpty(text) ? "No text provided" : text;
341 }
342
343 /**
344 * <p>getTaxonInteractionText</p>
345 *
346 * @param element a {@link eu.etaxonomy.cdm.model.description.TaxonInteraction} object.
347 * @return a {@link java.lang.String} object.
348 */
349 public static String getTaxonInteractionText(TaxonInteraction element) {
350 String text = "";
351 Taxon taxon2 = element.getTaxon2();
352 if(taxon2 != null && taxon2.getName() != null){
353 text = taxon2.getName().getTitleCache();
354 }else{
355 text = "No taxon chosen";
356 }
357
358 return text;
359 }
360
361 /**
362 * <p>getCommonNameText</p>
363 *
364 * @param commonName a {@link eu.etaxonomy.cdm.model.description.CommonTaxonName} object.
365 * @return a {@link java.lang.String} object.
366 */
367 public static String getCommonNameText(CommonTaxonName commonName) {
368 String name = commonName.getName();
369 if (name == null || name.length() == 0) {
370 name = "No name provided";
371 }
372 Language language = commonName.getLanguage();
373 if (language == null) {
374 name += " (No language provided)";
375 } else {
376 name += " (" + language.getDescription() + ")";
377 }
378 return name;
379 }
380
381 /**
382 * <p>getDescriptionText</p>
383 *
384 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
385 * @return a {@link java.lang.String} object.
386 */
387 public static String getDescriptionText(DescriptionBase element) {
388 String text = element.getTitleCache();
389 if (text == null || text.length() == 0) {
390 text = "No label provided";
391 }
392
393 return text;
394 }
395
396 /**
397 * <p>getIndividualsAssociationText</p>
398 *
399 * @param element a {@link eu.etaxonomy.cdm.model.description.IndividualsAssociation} object.
400 * @return a {@link java.lang.String} object.
401 */
402 public static String getIndividualsAssociationText(IndividualsAssociation element) {
403 SpecimenOrObservationBase derivedUnit = element.getAssociatedSpecimenOrObservation();
404 if(derivedUnit != null){
405 return derivedUnit.getTitleCache();
406 }
407 return "No unit chosen";
408 }
409
410 /**
411 * <p>getLabel</p>
412 *
413 * @param element a {@link java.lang.Object} object.
414 * @return a {@link java.lang.String} object.
415 */
416 public static String getLabel(Object element){
417 String noLabelString = "[no label]";
418 if (element instanceof FeatureNodeContainer){
419 return getFeatureNodeContainerText((FeatureNodeContainer) element);
420 }
421 else if (element instanceof DescriptionBase) {
422 return getDescriptionText((DescriptionBase) element);
423 }
424 else if(element instanceof CategoricalData){
425 String categoricalDataText = getCategoricalDataText((CategoricalData) element);
426 if(categoricalDataText==null || categoricalDataText.isEmpty()){
427 categoricalDataText = noLabelString;
428 }
429 return categoricalDataText;
430 }
431 else if (element instanceof CommonTaxonName) {
432 return getCommonNameText((CommonTaxonName) element);
433 }
434 else if (element instanceof Distribution) {
435 return getDistributionText((Distribution) element);
436 }
437 else if (element instanceof IndividualsAssociation) {
438 return getIndividualsAssociationText((IndividualsAssociation) element);
439 }
440 else if (element instanceof QuantitativeData) {
441 String quantitativeDataText = getQuantitativeDataText((QuantitativeData) element);
442 if(quantitativeDataText==null || quantitativeDataText.isEmpty()){
443 quantitativeDataText = noLabelString;
444 }
445 return quantitativeDataText;
446 }
447 else if (element instanceof TaxonInteraction) {
448 return getTaxonInteractionText((TaxonInteraction) element);
449 }
450 else if (element instanceof TextData) {
451 return getElementText((TextData) element);
452 }
453 else{
454 return element.toString();
455 }
456 }
457 }