3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.model
;
13 import java
.util
.Arrays
;
14 import java
.util
.List
;
16 import org
.hibernate
.LazyInitializationException
;
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
.DescriptionElementSource
;
23 import eu
.etaxonomy
.cdm
.model
.common
.IIdentifiableEntity
;
24 import eu
.etaxonomy
.cdm
.model
.common
.ISourceable
;
25 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
26 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
27 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
28 import eu
.etaxonomy
.cdm
.model
.common
.LanguageStringBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
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
.Distribution
;
36 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
37 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
38 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTermBase
;
39 import eu
.etaxonomy
.cdm
.model
.description
.QuantitativeData
;
40 import eu
.etaxonomy
.cdm
.model
.description
.TaxonInteraction
;
41 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
42 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
43 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
44 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
46 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
47 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
50 * <p>DescriptionHelper class.</p>
57 public class DescriptionHelper
{
60 * Returns whatever the element's title cache equivalent is,
61 * depending on its class.
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.
67 public static String
getCache(DescriptionElementBase element
,
70 if (element
instanceof TextData
) {
71 cache
= ((TextData
) element
).getText(language
);
73 if (element
instanceof CommonTaxonName
) {
74 cache
= ((CommonTaxonName
) element
).getName();
76 if (element
instanceof TaxonInteraction
) {
77 Taxon taxon2
= ((TaxonInteraction
) element
).getTaxon2();
78 if(taxon2
!= null && taxon2
.getName() != null){
79 cache
= taxon2
.getName().getTitleCache();
81 cache
= "No taxon chosen";
85 if (element
instanceof Distribution
) {
86 Distribution distribution
= (Distribution
) element
;
88 NamedArea area
= distribution
.getArea();
90 cache
= area
.getLabel();
92 PresenceAbsenceTermBase
<?
> status
= distribution
.getStatus();
94 cache
+= ", no status";
96 cache
+= ", " + status
.getLabel();
100 return cache
== null ?
"" : cache
;
104 * Returns whatever the element's title cache equivalent is,
105 * depending on its class, using the default language.
107 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
108 * @return a {@link java.lang.String} object.
110 public static String
getCache(DescriptionElementBase element
) {
111 return getCache(element
, CdmStore
.getDefaultLanguage());
115 * Set whatever the element's title cache equivalent is,
116 * depending on its class.
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.
122 public static void setCache(DescriptionElementBase element
,
123 String value
, Language language
) {
124 if (element
instanceof TextData
) {
125 ((TextData
) element
).putText(language
, value
);
128 if (element
instanceof CommonTaxonName
) {
129 ((CommonTaxonName
) element
).setName(value
);
132 if (element
instanceof TaxonInteraction
) {
135 if(element
instanceof Distribution
){
136 StoreUtil
.warn(DescriptionHelper
.class, "trying to set cache on distribution, don't know what to do at the moment.");
139 StoreUtil
.warn(DescriptionHelper
.class, "No matching subclass found for DescriptionElementBase object, 'cache' not set.");
143 * Set whatever the element's title cache equivalent is,
144 * depending on its class, using the default language.
146 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
147 * @param value a {@link java.lang.String} object.
149 public static void setCache(DescriptionElementBase element
,
151 setCache(element
, value
, CdmStore
.getDefaultLanguage());
155 * @see eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.IReferencingObjectsService#getObjectDescription(java.lang.Object)
158 * <p>getObjectDescription</p>
160 * @param element a {@link java.lang.Object} object.
161 * @return a {@link java.lang.String} object.
163 public static String
getObjectDescription(Object element
) {
164 if (element
instanceof IdentifiableEntity
) {
166 return ((IdentifiableEntity
) element
).getTitleCache();
167 }catch(LazyInitializationException e
){
168 String result
= "No Session to initialize title cache for IdentifiableEntity";
169 StoreUtil
.error(DescriptionHelper
.class, result
, e
);
170 return "TODO: " + result
;
173 if (element
instanceof OriginalSourceBase
) {
175 OriginalSourceBase originalSource
= (OriginalSourceBase
) element
;
176 ISourceable sourcedObject
= originalSource
.getSourcedObj();
177 String sourceObjectTitle
= "";
178 if(sourcedObject
instanceof IIdentifiableEntity
){
179 sourceObjectTitle
= ((IdentifiableEntity
) sourcedObject
).getTitleCache();
180 }else if(sourcedObject
instanceof DescriptionElementBase
){
181 sourceObjectTitle
= "Element for description: " + ((DescriptionElementBase
) sourcedObject
).getInDescription().getTitleCache();
183 throw new IllegalStateException("Unknown ISourceable object for given OriginalSourceBase");
186 return CdmUtils
.concat("; ", new String
[]{originalSource
.getIdNamespace(), originalSource
.getIdInSource(), sourceObjectTitle
});
187 }catch(LazyInitializationException e
){
188 String result
= "Error initializing title cache for ISourceable of an OriginalSourceBase";
189 StoreUtil
.error(DescriptionHelper
.class, result
, e
);
190 return "TODO: " + result
;
193 if (element
instanceof LanguageStringBase
) {
194 return ((LanguageStringBase
) element
).getText();
196 if (element
instanceof DescriptionElementBase
) {
197 return getCache((DescriptionElementBase
) element
);
199 if (element
instanceof Marker
) {
200 Marker marker
= (Marker
) element
;
201 return marker
.getMarkerType().getLabel() + " (" + marker
.getFlag() + ")";
203 // TODO write return texts for NameRelationship, HomotypicalGroup, SpecimenTypeDesignation, etc.
204 return element
.toString();
208 * <p>getObjectClassname</p>
210 * @param element a {@link java.lang.Object} object.
211 * @return a {@link java.lang.String} object.
213 public static String
getObjectClassname(Object element
) {
214 return element
.getClass().getSimpleName();
218 * <p>getFeatureNodeContainerText</p>
220 * @param element a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
221 * @return a {@link java.lang.String} object.
223 public static String
getFeatureNodeContainerText(FeatureNodeContainer element
) {
224 if(element
.getFeatureNode() != null && element
.getFeatureNode().getFeature() != null){
225 return element
.getFeatureNode().getFeature().getLabel(CdmStore
.getDefaultLanguage());
227 return "No label set";
231 * <p>getQuantitativeDataText</p>
233 * @param element a {@link eu.etaxonomy.cdm.model.description.QuantitativeData} object.
234 * @return a {@link java.lang.String} object.
236 public static String
getQuantitativeDataText(QuantitativeData element
) {
237 TextData textData
= quantitativeDescriptionBuilder
.build(element
, getLanguageList());
239 return textData
.getText(CdmStore
.getDefaultLanguage());
243 * <p>getCategoricalDataText</p>
245 * @param element a {@link eu.etaxonomy.cdm.model.description.CategoricalData} object.
246 * @return a {@link java.lang.String} object.
248 public static String
getCategoricalDataText(CategoricalData element
) {
249 TextData textData
= categoricalDescriptionBuilder
.build(element
, getLanguageList());
251 return textData
.getText(CdmStore
.getDefaultLanguage());
254 private static List
<Language
> getLanguageList(){
255 return Arrays
.asList(new Language
[]{CdmStore
.getDefaultLanguage()});
258 private static DescriptionBuilder
<QuantitativeData
> quantitativeDescriptionBuilder
= new DefaultQuantitativeDescriptionBuilder();
259 private static DescriptionBuilder
<CategoricalData
> categoricalDescriptionBuilder
= new DefaultCategoricalDescriptionBuilder();
263 * <p>getDistributionText</p>
265 * @param element a {@link eu.etaxonomy.cdm.model.description.Distribution} object.
266 * @return a {@link java.lang.String} object.
268 public static String
getDistributionText(Distribution element
) {
270 String text
= "EMPTY";
272 Distribution distribution
= (Distribution
) element
;
274 NamedArea area
= distribution
.getArea();
277 text
= NamedArea
.labelWithLevel(area
, CdmStore
.getDefaultLanguage());
279 PresenceAbsenceTermBase
<?
> status
= distribution
.getStatus();
281 if (status
!= null) {
282 text
+= ", " + status
.getLabel();
284 text
+= ", NO STATUS";
291 * <p>getImageText</p>
293 * @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
294 * @return a {@link java.lang.String} object.
296 public static String
getImageText(Media media
) {
297 LanguageString title
= media
.getTitle(CdmStore
.getDefaultLanguage());
299 return title
.getText();
307 * <p>getElementText</p>
309 * @param element a {@link eu.etaxonomy.cdm.model.description.TextData} object.
310 * @return a {@link java.lang.String} object.
312 public static String
getElementText(TextData element
) {
314 if(element
.getFeature().equals(Feature
.CITATION())){
316 for(DescriptionElementSource source
: element
.getSources()){
317 if(source
.getCitation() != null){
318 text
+= source
.getCitation().getTitleCache();
320 if(source
.getNameUsedInSource() != null){
321 text
+= " [ " + source
.getNameUsedInSource().getTitleCache() + " ]";
324 if(CdmUtils
.isEmpty(text
)){
325 text
= "No sources provided";
328 List
<Language
> languages
= Arrays
.asList(new Language
[]{CdmStore
.getDefaultLanguage()});
329 LanguageString languageString
= element
.getPreferredLanguageString(languages
);
330 text
= languageString
!= null ? languageString
.getText() : null;
332 return CdmUtils
.isEmpty(text
) ?
"No text provided" : text
;
336 * <p>getTaxonInteractionText</p>
338 * @param element a {@link eu.etaxonomy.cdm.model.description.TaxonInteraction} object.
339 * @return a {@link java.lang.String} object.
341 public static String
getTaxonInteractionText(TaxonInteraction element
) {
343 Taxon taxon2
= ((TaxonInteraction
) element
).getTaxon2();
344 if(taxon2
!= null && taxon2
.getName() != null){
345 text
= taxon2
.getName().getTitleCache();
347 text
= "No taxon chosen";
354 * <p>getCommonNameText</p>
356 * @param commonName a {@link eu.etaxonomy.cdm.model.description.CommonTaxonName} object.
357 * @return a {@link java.lang.String} object.
359 public static String
getCommonNameText(CommonTaxonName commonName
) {
360 String name
= commonName
.getName();
361 if (name
== null || name
.length() == 0) {
362 name
= "No name provided";
364 Language language
= commonName
.getLanguage();
365 if (language
== null) {
366 name
+= " (No language provided)";
368 name
+= " (" + language
.getDescription() + ")";
374 * <p>getDescriptionText</p>
376 * @param element a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
377 * @return a {@link java.lang.String} object.
379 public static String
getDescriptionText(DescriptionBase element
) {
380 String text
= element
.getTitleCache();
381 if (text
== null || text
.length() == 0) {
382 text
= "No label provided";
385 return "Description: " + text
;
389 * <p>getIndividualsAssociationText</p>
391 * @param element a {@link eu.etaxonomy.cdm.model.description.IndividualsAssociation} object.
392 * @return a {@link java.lang.String} object.
394 public static String
getIndividualsAssociationText(IndividualsAssociation element
) {
395 SpecimenOrObservationBase derivedUnit
= element
.getAssociatedSpecimenOrObservation();
396 if(derivedUnit
!= null){
397 return derivedUnit
.getTitleCache();
399 return "No text provided";
405 * @param element a {@link java.lang.Object} object.
406 * @return a {@link java.lang.String} object.
408 public static String
getLabel(Object element
){
409 if (element
instanceof FeatureNodeContainer
){
410 return getFeatureNodeContainerText((FeatureNodeContainer
) element
);
412 else if (element
instanceof DescriptionBase
) {
413 return getDescriptionText((DescriptionBase
) element
);
415 else if(element
instanceof CategoricalData
){
416 return getCategoricalDataText((CategoricalData
) element
);
418 else if (element
instanceof CommonTaxonName
) {
419 return getCommonNameText((CommonTaxonName
) element
);
421 else if (element
instanceof Distribution
) {
422 return getDistributionText((Distribution
) element
);
424 else if (element
instanceof IndividualsAssociation
) {
425 return getIndividualsAssociationText((IndividualsAssociation
) element
);
427 else if (element
instanceof QuantitativeData
) {
428 return getQuantitativeDataText((QuantitativeData
) element
);
430 else if (element
instanceof TaxonInteraction
) {
431 return getTaxonInteractionText((TaxonInteraction
) element
);
433 else if (element
instanceof TextData
) {
434 return getElementText((TextData
) element
);
437 return element
.toString();