1 package eu
.etaxonomy
.cdm
.format
.description
;
3 import java
.math
.BigDecimal
;
7 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
8 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
9 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
10 import eu
.etaxonomy
.cdm
.model
.description
.MeasurementUnit
;
11 import eu
.etaxonomy
.cdm
.model
.description
.NaturalLanguageTerm
;
12 import eu
.etaxonomy
.cdm
.model
.description
.StatisticalMeasure
;
13 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
18 public class DefaultQuantitativeDescriptionBuilder
extends QuantitativeDescriptionBuilderBase
{
20 private String space
= " ";
23 protected TextData
doBuild(Map
<StatisticalMeasure
,List
<BigDecimal
>> measures
, MeasurementUnit mUnit
, List
<Language
> languages
){
24 StringBuilder quantitativeDescription
= new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
25 TextData textData
= TextData
.NewInstance(); // TextData that will contain the description and the language corresponding
26 // booleans indicating whether a kind of value is present or not and the float that will eventually hold the value
29 mUnit
= HibernateProxyHelper
.deproxy(mUnit
, MeasurementUnit
.class);
31 if ((mUnit
!=null)&&(mUnit
.getIdInVocabulary()!=null)){
32 unit
= mUnit
.getIdInVocabulary();
33 }else if ((mUnit
!=null)&&(mUnit
.getLabel()!=null)){
34 unit
= mUnit
.getIdInVocabulary();
37 // the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
38 NaturalLanguageTerm nltFrom
= NaturalLanguageTerm
.FROM();
39 String from
= nltFrom
.getPreferredRepresentation(languages
).getLabel();
40 NaturalLanguageTerm nltTo
= NaturalLanguageTerm
.TO();
41 String to
= nltTo
.getPreferredRepresentation(languages
).getLabel();
42 NaturalLanguageTerm nltUp_To
= NaturalLanguageTerm
.UP_TO();
43 String up_To
= nltUp_To
.getPreferredRepresentation(languages
).getLabel();
44 NaturalLanguageTerm nltMost_Frequently
= NaturalLanguageTerm
.MOST_FREQUENTLY();
45 String most_Frequently
= nltMost_Frequently
.getPreferredRepresentation(languages
).getLabel();
46 NaturalLanguageTerm nltOn_Average
= NaturalLanguageTerm
.ON_AVERAGE();
47 String on_Average
= nltOn_Average
.getPreferredRepresentation(languages
).getLabel();
48 NaturalLanguageTerm nltMore_Or_Less
= NaturalLanguageTerm
.MORE_OR_LESS();
49 String more_Or_Less
= nltMore_Or_Less
.getPreferredRepresentation(languages
).getLabel();
51 // the booleans and floats are updated according to the presence or absence of values
53 Boolean max
, min
, upperb
, lowerb
, average
, sd
, exact
;
55 String averagevalue
= getValues(measures
, StatisticalMeasure
.AVERAGE());
56 if (averagevalue
!=null) {
61 String sdvalue
= getValues(measures
, StatisticalMeasure
.STANDARD_DEVIATION());
67 String minvalue
= getValues(measures
, StatisticalMeasure
.MIN());
73 String maxvalue
= getValues(measures
, StatisticalMeasure
.MAX());
79 String lowerbvalue
= getValues(measures
, StatisticalMeasure
.TYPICAL_LOWER_BOUNDARY());
80 if (lowerbvalue
!=null) {
85 String upperbvalue
= getValues(measures
, StatisticalMeasure
.TYPICAL_UPPER_BOUNDARY());
86 if (upperbvalue
!=null) {
92 String exactValue
= getValues(measures
, StatisticalMeasure
.EXACT_VALUE());
93 if (exactValue
!= null) {
100 // depending on the different associations of values, a sentence is built
102 quantitativeDescription
.append(space
+ from
+ space
+ minvalue
+ space
+ to
+ space
+ maxvalue
+ space
+ unit
);
105 quantitativeDescription
.append(space
+ from
+ space
+ minvalue
+ space
+ unit
);
108 quantitativeDescription
.append(space
+ up_To
+ space
+ maxvalue
+ space
+ unit
);
110 if ((max
||min
)&&(lowerb
||upperb
)) {
111 quantitativeDescription
.append(separator
); // merge with below ?
113 if ((lowerb
||upperb
)&&(min
||max
)) {
114 quantitativeDescription
.append(space
+ most_Frequently
);
116 if (upperb
&& lowerb
) {
117 quantitativeDescription
.append(space
+ from
+ space
+ lowerbvalue
+ space
+ to
+ space
+ upperbvalue
+ space
+ unit
);
120 quantitativeDescription
.append(space
+ from
+ space
+ lowerbvalue
+ space
+ unit
);
123 quantitativeDescription
.append(space
+ up_To
+ space
+ upperbvalue
+ space
+ unit
);
126 quantitativeDescription
.append(space
+ exactValue
+ space
+ unit
);
128 if (((max
||min
)&&(average
))||((lowerb
||upperb
)&&(average
))) {
129 quantitativeDescription
.append(separator
);
132 quantitativeDescription
.append(space
+ averagevalue
+ space
+ unit
+ space
+ on_Average
);
134 quantitativeDescription
.append("("+ more_Or_Less
+ space
+ sdvalue
+ ")");
139 textData
.putText((languages
== null || languages
.isEmpty())? Language
.DEFAULT() : languages
.get(0), quantitativeDescription
.toString()); // which language should we put here ?
140 // textData.setFormat(TextFormat.NewInstance(null, "Text",null ));
148 * Returns the value of a given type of measure as a String. If the value is an integer it is printed
149 * as an integer instead of a float.
150 * If no value of this type is present, returns null.
152 * @param measures the map with the values
153 * @param key the desired measure
156 private String
getValues(Map
<StatisticalMeasure
,List
<BigDecimal
>> measures
, Object key
) {
157 if(measures
.containsKey(key
)) {
158 List
<BigDecimal
> floatValues
;
160 floatValues
= measures
.get(key
);
161 for (BigDecimal value
: floatValues
) {
162 result
+= CdmUtils
.concat("; ", result
, value
.toString());