282cced8491e5859989cff07750f5d0be671d9a6
[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 // 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);
25 boolean sd = false;
26 float sdvalue = new Float(0);
27 boolean min = false;
28 float minvalue = new Float(0);
29 boolean max = false;
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);
35
36 String unit = "(unknown unit)";
37 if ((mUnit!=null)&&(mUnit.getLabel()!=null)){
38 unit = mUnit.getLabel();
39 }
40
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 ?
55
56 // the booleans and floats are updated according to the presence or absence of values
57 if (measures.containsKey(StatisticalMeasure.AVERAGE())) {
58 average = true;
59 averagevalue = measures.get(StatisticalMeasure.AVERAGE());
60 }
61 if(measures.containsKey(StatisticalMeasure.STANDARD_DEVIATION())) {
62 sd = true;
63 sdvalue = measures.get(StatisticalMeasure.STANDARD_DEVIATION());
64 }
65 if (measures.containsKey(StatisticalMeasure.MIN())) {
66 min = true;
67 minvalue = measures.get(StatisticalMeasure.MIN());
68 }
69 if (measures.containsKey(StatisticalMeasure.MAX())) {
70 max = true;
71 maxvalue = measures.get(StatisticalMeasure.MAX());
72 }
73 if (measures.containsKey(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY())) {
74 lowerb = true;
75 lowerbvalue = measures.get(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY());
76 }
77 if (measures.containsKey(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY())) {
78 upperb = true;
79 upperbvalue = measures.get(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY());
80 }
81
82
83 // depending on the different associations of values, a sentence is built
84 if (max && min) {
85 QuantitativeDescription.append(space + from + space + minvalue + space + to + space + maxvalue + space + unit);
86 }
87 else if (min) {
88 QuantitativeDescription.append(space + from + space + minvalue + space + unit);
89 }
90 else if (max) {
91 QuantitativeDescription.append(space + up_To + space + maxvalue + space + unit);
92 }
93 if ((max||min)&&(lowerb||upperb)) {
94 QuantitativeDescription.append(","); // merge with below ?
95 }
96 if ((lowerb||upperb)&&(min||max)) {
97 QuantitativeDescription.append(space + most_Frequently);
98 }
99 if (upperb && lowerb) {
100 QuantitativeDescription.append(space + from + space + lowerbvalue + space + to + space + upperbvalue + space + unit);
101 }
102 else if (lowerb) {
103 QuantitativeDescription.append(space + from + space + lowerbvalue + space + unit);
104 }
105 else if (upperb) {
106 QuantitativeDescription.append(space + up_To + space + upperbvalue + space + unit);
107 }
108 if (((max||min)&&(average))||((lowerb||upperb)&&(average))) {
109 QuantitativeDescription.append(",");
110 }
111 if (average) {
112 QuantitativeDescription.append(space + averagevalue + space + unit + space + on_Average);
113 if (sd) {
114 QuantitativeDescription.append("("+ more_Or_Less + space + sdvalue + ")");
115 }
116 }
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)
119
120 return textData;
121 }
122
123 protected String buildFeature(Feature feature, boolean doItBetter){
124 if (feature==null || feature.getLabel()==null) return "";
125 else {
126 if (doItBetter) { // remove the text between brackets
127 String str= feature.getLabel();
128 StringBuilder strbuilder = new StringBuilder();
129 do {
130 strbuilder.append(StringUtils.substringBefore(str, "<"));
131 }
132 while (!(str=StringUtils.substringAfter(str, ">")).equals(""));
133 return StringUtils.substringBeforeLast(strbuilder.toString()," ");
134 }
135 else{
136 String betterString = StringUtils.replaceChars(feature.getLabel(), "<>",""); // only remove the brackets
137 return StringUtils.substringBeforeLast(betterString," ");
138 }
139 }
140 }
141
142
143 }