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 MicroFormatQuantitativeDescriptionBuilder
extends AbstractQuantitativeDescriptionBuilder
{
18 private String spanEnd
= "</span>";
21 protected TextData
doBuild(Map
<StatisticalMeasure
,Float
> measures
, MeasurementUnit mUnit
, List
<Language
> languages
){
22 StringBuilder QuantitativeDescription
= new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
23 TextData textData
= TextData
.NewInstance(); // TextData that will contain the description and the language corresponding
25 // booleans indicating whether a kind of value is present or not and the float that will eventually hold the value
26 boolean average
= false;
27 String averagevalue
= null;
29 String sdvalue
= null;
31 String minvalue
= null;
33 String maxvalue
= null;
34 boolean lowerb
= false;
35 String lowerbvalue
= null;
36 boolean upperb
= false;
37 String upperbvalue
= null;
39 String unit
= "(unknown unit)";
40 if ((mUnit
!=null)&&(mUnit
.getLabel()!=null)){
41 unit
= spanClass("unit") + mUnit
.getLabel() + spanEnd
;
44 // the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
45 NaturalLanguageTerm nltFrom
= NaturalLanguageTerm
.FROM();
46 String from
= nltFrom
.getPreferredRepresentation(languages
).getLabel();
47 NaturalLanguageTerm nltTo
= NaturalLanguageTerm
.TO();
48 String to
= nltTo
.getPreferredRepresentation(languages
).getLabel();
49 NaturalLanguageTerm nltUp_To
= NaturalLanguageTerm
.UP_TO();
50 String up_To
= nltUp_To
.getPreferredRepresentation(languages
).getLabel();
51 NaturalLanguageTerm nltMost_Frequently
= NaturalLanguageTerm
.MOST_FREQUENTLY();
52 String most_Frequently
= nltMost_Frequently
.getPreferredRepresentation(languages
).getLabel();
53 NaturalLanguageTerm nltOn_Average
= NaturalLanguageTerm
.ON_AVERAGE();
54 String on_Average
= nltOn_Average
.getPreferredRepresentation(languages
).getLabel();
55 NaturalLanguageTerm nltMore_Or_Less
= NaturalLanguageTerm
.MORE_OR_LESS();
56 String more_Or_Less
= nltMore_Or_Less
.getPreferredRepresentation(languages
).getLabel();
57 String space
= " "; // should "space" be considered as a linking word and thus be stored in NaturalLanguageTerm.class ?
59 // the booleans and floats are updated according to the presence or absence of values
60 if (measures
.containsKey(StatisticalMeasure
.AVERAGE())) {
62 averagevalue
= spanClass("measurement") + measures
.get(StatisticalMeasure
.AVERAGE()) + spanEnd
;
63 } else if(measures
.containsKey(StatisticalMeasure
.STANDARD_DEVIATION())) {
65 sdvalue
= spanClass("measurement") + measures
.get(StatisticalMeasure
.STANDARD_DEVIATION()) + spanEnd
;
66 } else if (measures
.containsKey(StatisticalMeasure
.MIN())) {
68 minvalue
= spanClass("measurement") + measures
.get(StatisticalMeasure
.MIN()) + spanEnd
;
69 } else if (measures
.containsKey(StatisticalMeasure
.MAX())) {
71 maxvalue
= spanClass("measurement") + measures
.get(StatisticalMeasure
.MAX()) + spanEnd
;
72 } else if (measures
.containsKey(StatisticalMeasure
.TYPICAL_LOWER_BOUNDARY())) {
74 lowerbvalue
= spanClass("measurement") + measures
.get(StatisticalMeasure
.TYPICAL_LOWER_BOUNDARY()) + spanEnd
;
75 } else if (measures
.containsKey(StatisticalMeasure
.TYPICAL_UPPER_BOUNDARY())) {
77 upperbvalue
= spanClass("measurement") + measures
.get(StatisticalMeasure
.TYPICAL_UPPER_BOUNDARY()) + spanEnd
;
80 QuantitativeDescription
.append(spanClass("state"));
81 // depending on the different associations of values, a sentence is built
83 QuantitativeDescription
.append(space
+ from
+ space
+ minvalue
+ space
+ to
+ space
+ maxvalue
+ space
+ unit
);
86 QuantitativeDescription
.append(space
+ from
+ space
+ minvalue
+ space
+ unit
);
89 QuantitativeDescription
.append(space
+ up_To
+ space
+ maxvalue
+ space
+ unit
);
91 if ((max
||min
)&&(lowerb
||upperb
)) {
92 QuantitativeDescription
.append(","); // merge with below ?
94 if ((lowerb
||upperb
)&&(min
||max
)) {
95 QuantitativeDescription
.append(space
+ most_Frequently
+ space
);
97 if (upperb
&& lowerb
) {
98 QuantitativeDescription
.append(space
+ from
+ space
+ lowerbvalue
+ space
+ to
+ space
+ upperbvalue
+ space
+ unit
);
101 QuantitativeDescription
.append(space
+ from
+ space
+ lowerbvalue
+ space
+ unit
);
104 QuantitativeDescription
.append(space
+ up_To
+ space
+ upperbvalue
+ space
+ unit
);
106 if (((max
||min
)&&(average
))||((lowerb
||upperb
)&&(average
))) {
107 QuantitativeDescription
.append(",");
110 QuantitativeDescription
.append(space
+ averagevalue
+ space
+ unit
+ space
+ on_Average
);
112 QuantitativeDescription
.append("("+ more_Or_Less
+ space
+ sdvalue
+ ")");
115 QuantitativeDescription
.append(spanEnd
);
116 textData
.putText(languages
.get(0), QuantitativeDescription
.toString()); // which language should be put here ?
117 textData
.setFormat(TextFormat
.NewInstance(null, "HTML",null )); // the data format is set (not yet real HTML)
122 protected String
buildFeature(Feature feature
, boolean doItBetter
){
123 if (feature
==null || feature
.getLabel()==null) return "";
126 String betterString
= StringUtils
.substringBefore(feature
.getLabel(), "<");
127 return StringUtils
.removeEnd(betterString
, " ");
129 else return feature
.getLabel();
133 private String
spanClass(String classString
){
134 return("<span class=\""+classString
+"\">");