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
{
20 protected TextData
doBuild(Map
<StatisticalMeasure
,Float
> measures
, MeasurementUnit mUnit
, List
<Language
> languages
){
21 StringBuilder QuantitativeDescription
= new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
22 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
26 if ((mUnit
!=null)&&(mUnit
.getLabel()!=null)){
27 unit
= mUnit
.getLabel();
30 // the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
31 NaturalLanguageTerm nltFrom
= NaturalLanguageTerm
.FROM();
32 String from
= nltFrom
.getPreferredRepresentation(languages
).getLabel();
33 NaturalLanguageTerm nltTo
= NaturalLanguageTerm
.TO();
34 String to
= nltTo
.getPreferredRepresentation(languages
).getLabel();
35 NaturalLanguageTerm nltUp_To
= NaturalLanguageTerm
.UP_TO();
36 String up_To
= nltUp_To
.getPreferredRepresentation(languages
).getLabel();
37 NaturalLanguageTerm nltMost_Frequently
= NaturalLanguageTerm
.MOST_FREQUENTLY();
38 String most_Frequently
= nltMost_Frequently
.getPreferredRepresentation(languages
).getLabel();
39 NaturalLanguageTerm nltOn_Average
= NaturalLanguageTerm
.ON_AVERAGE();
40 String on_Average
= nltOn_Average
.getPreferredRepresentation(languages
).getLabel();
41 NaturalLanguageTerm nltMore_Or_Less
= NaturalLanguageTerm
.MORE_OR_LESS();
42 String more_Or_Less
= nltMore_Or_Less
.getPreferredRepresentation(languages
).getLabel();
43 String space
= " "; // should "space" be considered as a linking word and thus be stored in NaturalLanguageTerm.class ?
45 // the booleans and floats are updated according to the presence or absence of values
47 Boolean max
, min
, upperb
, lowerb
, average
, sd
;
49 String averagevalue
= getValue(measures
,StatisticalMeasure
.AVERAGE());
50 if (averagevalue
!=null) average
=true; else average
=false;
51 String sdvalue
= getValue(measures
,StatisticalMeasure
.STANDARD_DEVIATION());
52 if (sdvalue
!=null) sd
=true; else sd
=false;
53 String minvalue
= getValue(measures
,StatisticalMeasure
.MIN());
54 if (minvalue
!=null) min
=true; else min
=false;
55 String maxvalue
= getValue(measures
,StatisticalMeasure
.MAX());
56 if (maxvalue
!=null) max
=true; else max
=false;
57 String lowerbvalue
= getValue(measures
,StatisticalMeasure
.TYPICAL_LOWER_BOUNDARY());
58 if (lowerbvalue
!=null) lowerb
=true; else lowerb
=false;
59 String upperbvalue
= getValue(measures
,StatisticalMeasure
.TYPICAL_UPPER_BOUNDARY());
60 if (upperbvalue
!=null) upperb
=true; else upperb
=false;
63 // depending on the different associations of values, a sentence is built
65 QuantitativeDescription
.append(space
+ from
+ space
+ minvalue
+ space
+ to
+ space
+ maxvalue
+ space
+ unit
);
68 QuantitativeDescription
.append(space
+ from
+ space
+ minvalue
+ space
+ unit
);
71 QuantitativeDescription
.append(space
+ up_To
+ space
+ maxvalue
+ space
+ unit
);
73 if ((max
||min
)&&(lowerb
||upperb
)) {
74 QuantitativeDescription
.append(","); // merge with below ?
76 if ((lowerb
||upperb
)&&(min
||max
)) {
77 QuantitativeDescription
.append(space
+ most_Frequently
);
79 if (upperb
&& lowerb
) {
80 QuantitativeDescription
.append(space
+ from
+ space
+ lowerbvalue
+ space
+ to
+ space
+ upperbvalue
+ space
+ unit
);
83 QuantitativeDescription
.append(space
+ from
+ space
+ lowerbvalue
+ space
+ unit
);
86 QuantitativeDescription
.append(space
+ up_To
+ space
+ upperbvalue
+ space
+ unit
);
88 if (((max
||min
)&&(average
))||((lowerb
||upperb
)&&(average
))) {
89 QuantitativeDescription
.append(",");
92 QuantitativeDescription
.append(space
+ averagevalue
+ space
+ unit
+ space
+ on_Average
);
94 QuantitativeDescription
.append("("+ more_Or_Less
+ space
+ sdvalue
+ ")");
97 textData
.putText(QuantitativeDescription
.toString(), languages
.get(0)); // which language should be put here ?
98 textData
.setFormat(TextFormat
.NewInstance(null, "HTML",null )); // the data format is set (not yet real HTML)
103 private String
getValue(Map
<StatisticalMeasure
,Float
> measures
, Object key
) {
106 if(measures
.containsKey(key
)) {
107 floatValue
= measures
.get(key
);
108 intValue
=floatValue
.intValue();
109 if (floatValue
.equals(intValue
.floatValue())) return intValue
.toString();
110 else return floatValue
.toString();
115 protected String
buildFeature(Feature feature
, boolean doItBetter
){
116 if (feature
==null || feature
.getLabel()==null) return "";
118 if (doItBetter
) { // remove the text between brackets
119 String str
= feature
.getLabel();
120 StringBuilder strbuilder
= new StringBuilder();
122 strbuilder
.append(StringUtils
.substringBefore(str
, "<"));
124 while (!(str
=StringUtils
.substringAfter(str
, ">")).equals(""));
125 return StringUtils
.substringBeforeLast(strbuilder
.toString()," ");
128 String betterString
= StringUtils
.replaceChars(feature
.getLabel(), "<>",""); // only remove the brackets
130 // return StringUtils.substringBeforeLast(betterString," ");