include trunk updates
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / DefaultQuantitativeDescriptionBuilder.java
1 package eu.etaxonomy.cdm.api.service;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import org.apache.commons.lang.StringUtils;
7
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;
15
16 public class DefaultQuantitativeDescriptionBuilder extends AbstractQuantitativeDescriptionBuilder {
17
18 @Override
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
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);
26 boolean sd = false;
27 float sdvalue = new Float(0);
28 boolean min = false;
29 float minvalue = new Float(0);
30 boolean max = false;
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);
36
37 String unit = "(unknown unit)";
38 if ((mUnit!=null)&&(mUnit.getLabel()!=null)){
39 unit = mUnit.getLabel();
40 }
41
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 ?
56
57 // the booleans and floats are updated according to the presence or absence of values
58 if (measures.containsKey(StatisticalMeasure.AVERAGE())) {
59 average = true;
60 averagevalue = measures.get(StatisticalMeasure.AVERAGE());
61 } else if(measures.containsKey(StatisticalMeasure.STANDARD_DEVIATION())) {
62 sd = true;
63 sdvalue = measures.get(StatisticalMeasure.STANDARD_DEVIATION());
64 } else if (measures.containsKey(StatisticalMeasure.MIN())) {
65 min = true;
66 minvalue = measures.get(StatisticalMeasure.MIN());
67 } else if (measures.containsKey(StatisticalMeasure.MAX())) {
68 max = true;
69 maxvalue = measures.get(StatisticalMeasure.MAX());
70 } else if (measures.containsKey(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY())) {
71 lowerb = true;
72 lowerbvalue = measures.get(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY());
73 } else if (measures.containsKey(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY())) {
74 upperb = true;
75 upperbvalue = measures.get(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY());
76 }
77
78
79 // depending on the different associations of values, a sentence is built
80 if (max && min) {
81 QuantitativeDescription.append(space + from + space + minvalue + space + to + space + maxvalue + space + unit);
82 }
83 else if (min) {
84 QuantitativeDescription.append(space + from + space + minvalue + space + unit);
85 }
86 else if (max) {
87 QuantitativeDescription.append(space + up_To + space + maxvalue + space + unit);
88 }
89 if ((max||min)&&(lowerb||upperb)) {
90 QuantitativeDescription.append(","); // merge with below ?
91 }
92 if ((lowerb||upperb)&&(min||max)) {
93 QuantitativeDescription.append(space + most_Frequently + space);
94 }
95 if (upperb && lowerb) {
96 QuantitativeDescription.append(space + from + space + lowerbvalue + space + to + space + upperbvalue + space + unit);
97 }
98 else if (lowerb) {
99 QuantitativeDescription.append(space + from + space + lowerbvalue + space + unit);
100 }
101 else if (upperb) {
102 QuantitativeDescription.append(space + up_To + space + upperbvalue + space + unit);
103 }
104 if (((max||min)&&(average))||((lowerb||upperb)&&(average))) {
105 QuantitativeDescription.append(",");
106 }
107 if (average) {
108 QuantitativeDescription.append(space + averagevalue + space + unit + space + on_Average);
109 if (sd) {
110 QuantitativeDescription.append("("+ more_Or_Less + space + sdvalue + ")");
111 }
112 }
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)
115
116 return textData;
117 }
118
119 protected String buildFeature(Feature feature, boolean doItBetter){
120 if (feature==null || feature.getLabel()==null) return "";
121 else {
122 if (doItBetter) {
123 String betterString = StringUtils.substringBefore(feature.getLabel(), "<");
124 return StringUtils.removeEnd(betterString, " ");
125 }
126 else return feature.getLabel();
127 }
128 }
129
130
131 }