1 package eu
.etaxonomy
.cdm
.api
.service
;
6 import org
.apache
.commons
.lang
.StringUtils
;
8 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
9 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
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
;
14 import eu
.etaxonomy
.cdm
.model
.description
.TextFormat
;
16 public class DefaultQuantitativeDescriptionBuilder
extends AbstractQuantitativeDescriptionBuilder
{
19 protected TextData
doBuild(Map
<StatisticalMeasure
,Float
> measures
, MeasurementUnit mUnit
, List
<Language
> languages
){
20 StringBuilder QuantitativeDescription
= new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
21 TextData textData
= TextData
.NewInstance(); // TextData that will contain the description and the language corresponding
23 // booleans indicating whether a kind of value is present or not and the float that will eventually hold the value
24 boolean average
= false;
25 float averagevalue
= new Float(0);
27 float sdvalue
= new Float(0);
29 float minvalue
= new Float(0);
31 float maxvalue
= new Float(0);
32 boolean lowerb
= false;
33 float lowerbvalue
= new Float(0);
34 boolean upperb
= false;
35 float upperbvalue
= new Float(0);
37 String unit
= "(unknown unit)";
38 if ((mUnit
!=null)&&(mUnit
.getLabel()!=null)){
39 unit
= mUnit
.getLabel();
42 // the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
43 NaturalLanguageTerm nltFrom
= NaturalLanguageTerm
.FROM();
44 String from
= nltFrom
.getPreferredRepresentation(languages
).getLabel();
45 NaturalLanguageTerm nltTo
= NaturalLanguageTerm
.TO();
46 String to
= nltTo
.getPreferredRepresentation(languages
).getLabel();
47 NaturalLanguageTerm nltUp_To
= NaturalLanguageTerm
.UP_TO();
48 String up_To
= nltUp_To
.getPreferredRepresentation(languages
).getLabel();
49 NaturalLanguageTerm nltMost_Frequently
= NaturalLanguageTerm
.MOST_FREQUENTLY();
50 String most_Frequently
= nltMost_Frequently
.getPreferredRepresentation(languages
).getLabel();
51 NaturalLanguageTerm nltOn_Average
= NaturalLanguageTerm
.ON_AVERAGE();
52 String on_Average
= nltOn_Average
.getPreferredRepresentation(languages
).getLabel();
53 NaturalLanguageTerm nltMore_Or_Less
= NaturalLanguageTerm
.MORE_OR_LESS();
54 String more_Or_Less
= nltMore_Or_Less
.getPreferredRepresentation(languages
).getLabel();
55 String space
= " "; // should "space" be considered as a linking word and thus be stored in NaturalLanguageTerm.class ?
57 // the booleans and floats are updated according to the presence or absence of values
58 if (measures
.containsKey(StatisticalMeasure
.AVERAGE())) {
60 averagevalue
= measures
.get(StatisticalMeasure
.AVERAGE());
61 } else if(measures
.containsKey(StatisticalMeasure
.STANDARD_DEVIATION())) {
63 sdvalue
= measures
.get(StatisticalMeasure
.STANDARD_DEVIATION());
64 } else if (measures
.containsKey(StatisticalMeasure
.MIN())) {
66 minvalue
= measures
.get(StatisticalMeasure
.MIN());
67 } else if (measures
.containsKey(StatisticalMeasure
.MAX())) {
69 maxvalue
= measures
.get(StatisticalMeasure
.MAX());
70 } else if (measures
.containsKey(StatisticalMeasure
.TYPICAL_LOWER_BOUNDARY())) {
72 lowerbvalue
= measures
.get(StatisticalMeasure
.TYPICAL_LOWER_BOUNDARY());
73 } else if (measures
.containsKey(StatisticalMeasure
.TYPICAL_UPPER_BOUNDARY())) {
75 upperbvalue
= measures
.get(StatisticalMeasure
.TYPICAL_UPPER_BOUNDARY());
79 // depending on the different associations of values, a sentence is built
81 QuantitativeDescription
.append(space
+ from
+ space
+ minvalue
+ space
+ to
+ space
+ maxvalue
+ space
+ unit
);
84 QuantitativeDescription
.append(space
+ from
+ space
+ minvalue
+ space
+ unit
);
87 QuantitativeDescription
.append(space
+ up_To
+ space
+ maxvalue
+ space
+ unit
);
89 if ((max
||min
)&&(lowerb
||upperb
)) {
90 QuantitativeDescription
.append(","); // merge with below ?
92 if ((lowerb
||upperb
)&&(min
||max
)) {
93 QuantitativeDescription
.append(space
+ most_Frequently
+ space
);
95 if (upperb
&& lowerb
) {
96 QuantitativeDescription
.append(space
+ from
+ space
+ lowerbvalue
+ space
+ to
+ space
+ upperbvalue
+ space
+ unit
);
99 QuantitativeDescription
.append(space
+ from
+ space
+ lowerbvalue
+ space
+ unit
);
102 QuantitativeDescription
.append(space
+ up_To
+ space
+ upperbvalue
+ space
+ unit
);
104 if (((max
||min
)&&(average
))||((lowerb
||upperb
)&&(average
))) {
105 QuantitativeDescription
.append(",");
108 QuantitativeDescription
.append(space
+ averagevalue
+ space
+ unit
+ space
+ on_Average
);
110 QuantitativeDescription
.append("("+ more_Or_Less
+ space
+ sdvalue
+ ")");
113 textData
.putText(QuantitativeDescription
.toString(), languages
.get(0)); // which language should be put here ?
114 textData
.setFormat(TextFormat
.NewInstance(null, "HTML",null )); // the data format is set (not yet real HTML)
119 protected String
buildFeature(Feature feature
, boolean doItBetter
){
120 if (feature
==null || feature
.getLabel()==null) return "";
123 String betterString
= StringUtils
.substringBefore(feature
.getLabel(), "<");
124 return StringUtils
.removeEnd(betterString
, " ");
126 else return feature
.getLabel();