ref #10394 cleanup code after fix
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / format / description / DefaultQuantitativeDescriptionBuilder.java
1 package eu.etaxonomy.cdm.format.description;
2
3 import java.math.BigDecimal;
4 import java.util.List;
5 import java.util.Map;
6
7 import eu.etaxonomy.cdm.common.CdmUtils;
8 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
9 import eu.etaxonomy.cdm.model.common.Language;
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
15 /**
16 * @author m.venin
17 */
18 public class DefaultQuantitativeDescriptionBuilder extends QuantitativeDescriptionBuilderBase {
19
20 private String space = " ";
21
22 @Override
23 protected TextData doBuild(Map<StatisticalMeasure,List<BigDecimal>> measures, MeasurementUnit mUnit, List<Language> languages){
24 StringBuilder quantitativeDescription = new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
25 TextData textData = TextData.NewInstance(); // TextData that will contain the description and the language corresponding
26 // booleans indicating whether a kind of value is present or not and the float that will eventually hold the value
27
28 String unit = "";
29 mUnit = HibernateProxyHelper.deproxy(mUnit, MeasurementUnit.class);
30
31 if ((mUnit!=null)&&(mUnit.getIdInVocabulary()!=null)){
32 unit = mUnit.getIdInVocabulary();
33 }else if ((mUnit!=null)&&(mUnit.getLabel()!=null)){
34 unit = mUnit.getIdInVocabulary();
35 }
36
37 // the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
38 NaturalLanguageTerm nltFrom = NaturalLanguageTerm.FROM();
39 String from = nltFrom.getPreferredRepresentation(languages).getLabel();
40 NaturalLanguageTerm nltTo = NaturalLanguageTerm.TO();
41 String to = nltTo.getPreferredRepresentation(languages).getLabel();
42 NaturalLanguageTerm nltUp_To = NaturalLanguageTerm.UP_TO();
43 String up_To = nltUp_To.getPreferredRepresentation(languages).getLabel();
44 NaturalLanguageTerm nltMost_Frequently = NaturalLanguageTerm.MOST_FREQUENTLY();
45 String most_Frequently = nltMost_Frequently.getPreferredRepresentation(languages).getLabel();
46 NaturalLanguageTerm nltOn_Average = NaturalLanguageTerm.ON_AVERAGE();
47 String on_Average = nltOn_Average.getPreferredRepresentation(languages).getLabel();
48 NaturalLanguageTerm nltMore_Or_Less = NaturalLanguageTerm.MORE_OR_LESS();
49 String more_Or_Less = nltMore_Or_Less.getPreferredRepresentation(languages).getLabel();
50
51 // the booleans and floats are updated according to the presence or absence of values
52
53 Boolean max, min, upperb, lowerb, average, sd, exact;
54
55 String averagevalue = getValues(measures, StatisticalMeasure.AVERAGE());
56 if (averagevalue!=null) {
57 average=true;
58 } else {
59 average=false;
60 }
61 String sdvalue = getValues(measures, StatisticalMeasure.STANDARD_DEVIATION());
62 if (sdvalue!=null) {
63 sd=true;
64 } else {
65 sd=false;
66 }
67 String minvalue = getValues(measures, StatisticalMeasure.MIN());
68 if (minvalue!=null) {
69 min=true;
70 } else {
71 min=false;
72 }
73 String maxvalue = getValues(measures, StatisticalMeasure.MAX());
74 if (maxvalue!=null) {
75 max=true;
76 } else {
77 max=false;
78 }
79 String lowerbvalue = getValues(measures, StatisticalMeasure.TYPICAL_LOWER_BOUNDARY());
80 if (lowerbvalue!=null) {
81 lowerb=true;
82 } else {
83 lowerb=false;
84 }
85 String upperbvalue = getValues(measures, StatisticalMeasure.TYPICAL_UPPER_BOUNDARY());
86 if (upperbvalue!=null) {
87 upperb=true;
88 } else {
89 upperb=false;
90 }
91
92 String exactValue = getValues(measures, StatisticalMeasure.EXACT_VALUE());
93 if (exactValue != null) {
94 exact = true;
95 }else {
96 exact = false;
97 }
98
99
100 // depending on the different associations of values, a sentence is built
101 if (max && min) {
102 quantitativeDescription.append(space + from + space + minvalue + space + to + space + maxvalue + space + unit);
103 }
104 else if (min) {
105 quantitativeDescription.append(space + from + space + minvalue + space + unit);
106 }
107 else if (max) {
108 quantitativeDescription.append(space + up_To + space + maxvalue + space + unit);
109 }
110 if ((max||min)&&(lowerb||upperb)) {
111 quantitativeDescription.append(separator); // merge with below ?
112 }
113 if ((lowerb||upperb)&&(min||max)) {
114 quantitativeDescription.append(space + most_Frequently);
115 }
116 if (upperb && lowerb) {
117 quantitativeDescription.append(space + from + space + lowerbvalue + space + to + space + upperbvalue + space + unit);
118 }
119 else if (lowerb) {
120 quantitativeDescription.append(space + from + space + lowerbvalue + space + unit);
121 }
122 else if (upperb) {
123 quantitativeDescription.append(space + up_To + space + upperbvalue + space + unit);
124 }
125 if (exact) {
126 quantitativeDescription.append(space + exactValue + space + unit);
127 }
128 if (((max||min)&&(average))||((lowerb||upperb)&&(average))) {
129 quantitativeDescription.append(separator);
130 }
131 if (average) {
132 quantitativeDescription.append(space + averagevalue + space + unit + space + on_Average);
133 if (sd) {
134 quantitativeDescription.append("("+ more_Or_Less + space + sdvalue + ")");
135 }
136 }
137
138
139 textData.putText((languages == null || languages.isEmpty())? Language.DEFAULT() : languages.get(0), quantitativeDescription.toString()); // which language should we put here ?
140 // textData.setFormat(TextFormat.NewInstance(null, "Text",null ));
141
142 return textData;
143 }
144
145
146
147 /**
148 * Returns the value of a given type of measure as a String. If the value is an integer it is printed
149 * as an integer instead of a float.
150 * If no value of this type is present, returns null.
151 *
152 * @param measures the map with the values
153 * @param key the desired measure
154 * @return
155 */
156 private String getValues(Map<StatisticalMeasure,List<BigDecimal>> measures, Object key) {
157 if(measures.containsKey(key)) {
158 List<BigDecimal> floatValues;
159 String result = "";
160 floatValues = measures.get(key);
161 for (BigDecimal value: floatValues) {
162 result += CdmUtils.concat("; ", result, value.toString());
163 }
164 return result;
165 }
166 return null;
167 }
168
169 }