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
22 // booleans indicating whether a kind of value is present or not and the float that will eventually hold the value
23 boolean average
= false;
24 float averagevalue
= new Float(0);
26 float sdvalue
= new Float(0);
28 float minvalue
= new Float(0);
30 float maxvalue
= new Float(0);
31 boolean lowerb
= false;
32 float lowerbvalue
= new Float(0);
33 boolean upperb
= false;
34 float upperbvalue
= new Float(0);
36 String unit
= "(unknown unit)";
37 if ((mUnit
!=null)&&(mUnit
.getLabel()!=null)){
38 unit
= mUnit
.getLabel();
41 // the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
42 NaturalLanguageTerm nltFrom
= NaturalLanguageTerm
.FROM();
43 String from
= nltFrom
.getPreferredRepresentation(languages
).getLabel();
44 NaturalLanguageTerm nltTo
= NaturalLanguageTerm
.TO();
45 String to
= nltTo
.getPreferredRepresentation(languages
).getLabel();
46 NaturalLanguageTerm nltUp_To
= NaturalLanguageTerm
.UP_TO();
47 String up_To
= nltUp_To
.getPreferredRepresentation(languages
).getLabel();
48 NaturalLanguageTerm nltMost_Frequently
= NaturalLanguageTerm
.MOST_FREQUENTLY();
49 String most_Frequently
= nltMost_Frequently
.getPreferredRepresentation(languages
).getLabel();
50 NaturalLanguageTerm nltOn_Average
= NaturalLanguageTerm
.ON_AVERAGE();
51 String on_Average
= nltOn_Average
.getPreferredRepresentation(languages
).getLabel();
52 NaturalLanguageTerm nltMore_Or_Less
= NaturalLanguageTerm
.MORE_OR_LESS();
53 String more_Or_Less
= nltMore_Or_Less
.getPreferredRepresentation(languages
).getLabel();
54 String space
= " "; // should "space" be considered as a linking word and thus be stored in NaturalLanguageTerm.class ?
56 // the booleans and floats are updated according to the presence or absence of values
57 if (measures
.containsKey(StatisticalMeasure
.AVERAGE())) {
59 averagevalue
= measures
.get(StatisticalMeasure
.AVERAGE());
61 if(measures
.containsKey(StatisticalMeasure
.STANDARD_DEVIATION())) {
63 sdvalue
= measures
.get(StatisticalMeasure
.STANDARD_DEVIATION());
65 if (measures
.containsKey(StatisticalMeasure
.MIN())) {
67 minvalue
= measures
.get(StatisticalMeasure
.MIN());
69 if (measures
.containsKey(StatisticalMeasure
.MAX())) {
71 maxvalue
= measures
.get(StatisticalMeasure
.MAX());
73 if (measures
.containsKey(StatisticalMeasure
.TYPICAL_LOWER_BOUNDARY())) {
75 lowerbvalue
= measures
.get(StatisticalMeasure
.TYPICAL_LOWER_BOUNDARY());
77 if (measures
.containsKey(StatisticalMeasure
.TYPICAL_UPPER_BOUNDARY())) {
79 upperbvalue
= measures
.get(StatisticalMeasure
.TYPICAL_UPPER_BOUNDARY());
83 // depending on the different associations of values, a sentence is built
85 QuantitativeDescription
.append(space
+ from
+ space
+ minvalue
+ space
+ to
+ space
+ maxvalue
+ space
+ unit
);
88 QuantitativeDescription
.append(space
+ from
+ space
+ minvalue
+ space
+ unit
);
91 QuantitativeDescription
.append(space
+ up_To
+ space
+ maxvalue
+ space
+ unit
);
93 if ((max
||min
)&&(lowerb
||upperb
)) {
94 QuantitativeDescription
.append(","); // merge with below ?
96 if ((lowerb
||upperb
)&&(min
||max
)) {
97 QuantitativeDescription
.append(space
+ most_Frequently
);
99 if (upperb
&& lowerb
) {
100 QuantitativeDescription
.append(space
+ from
+ space
+ lowerbvalue
+ space
+ to
+ space
+ upperbvalue
+ space
+ unit
);
103 QuantitativeDescription
.append(space
+ from
+ space
+ lowerbvalue
+ space
+ unit
);
106 QuantitativeDescription
.append(space
+ up_To
+ space
+ upperbvalue
+ space
+ unit
);
108 if (((max
||min
)&&(average
))||((lowerb
||upperb
)&&(average
))) {
109 QuantitativeDescription
.append(",");
112 QuantitativeDescription
.append(space
+ averagevalue
+ space
+ unit
+ space
+ on_Average
);
114 QuantitativeDescription
.append("("+ more_Or_Less
+ space
+ sdvalue
+ ")");
117 textData
.putText(QuantitativeDescription
.toString(), languages
.get(0)); // which language should be put here ?
118 textData
.setFormat(TextFormat
.NewInstance(null, "HTML",null )); // the data format is set (not yet real HTML)
123 protected String
buildFeature(Feature feature
, boolean doItBetter
){
124 if (feature
==null || feature
.getLabel()==null) return "";
126 if (doItBetter
) { // remove the text between brackets
127 String str
= feature
.getLabel();
128 StringBuilder strbuilder
= new StringBuilder();
130 strbuilder
.append(StringUtils
.substringBefore(str
, "<"));
132 while (!(str
=StringUtils
.substringAfter(str
, ">")).equals(""));
133 return StringUtils
.substringBeforeLast(strbuilder
.toString()," ");
136 String betterString
= StringUtils
.replaceChars(feature
.getLabel(), "<>",""); // only remove the brackets
137 return StringUtils
.substringBeforeLast(betterString
," ");