ref #6072 test missing entity in webservice
[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 eu.etaxonomy.cdm.model.common.Language;
7 import eu.etaxonomy.cdm.model.description.MeasurementUnit;
8 import eu.etaxonomy.cdm.model.description.NaturalLanguageTerm;
9 import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
10 import eu.etaxonomy.cdm.model.description.TextData;
11 import eu.etaxonomy.cdm.model.description.TextFormat;
12
13 /**
14 * @author m.venin
15 *
16 */
17 public class DefaultQuantitativeDescriptionBuilder extends AbstractQuantitativeDescriptionBuilder {
18
19 String space = " ";
20
21 @Override
22 protected TextData doBuild(Map<StatisticalMeasure,Float> measures, MeasurementUnit mUnit, List<Language> languages){
23 StringBuilder QuantitativeDescription = new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
24 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
27 String unit = "";
28 if ((mUnit!=null)&&(mUnit.getLabel()!=null)){
29 unit = mUnit.getLabel();
30 }
31
32 // the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
33 NaturalLanguageTerm nltFrom = NaturalLanguageTerm.FROM();
34 String from = nltFrom.getPreferredRepresentation(languages).getLabel();
35 NaturalLanguageTerm nltTo = NaturalLanguageTerm.TO();
36 String to = nltTo.getPreferredRepresentation(languages).getLabel();
37 NaturalLanguageTerm nltUp_To = NaturalLanguageTerm.UP_TO();
38 String up_To = nltUp_To.getPreferredRepresentation(languages).getLabel();
39 NaturalLanguageTerm nltMost_Frequently = NaturalLanguageTerm.MOST_FREQUENTLY();
40 String most_Frequently = nltMost_Frequently.getPreferredRepresentation(languages).getLabel();
41 NaturalLanguageTerm nltOn_Average = NaturalLanguageTerm.ON_AVERAGE();
42 String on_Average = nltOn_Average.getPreferredRepresentation(languages).getLabel();
43 NaturalLanguageTerm nltMore_Or_Less = NaturalLanguageTerm.MORE_OR_LESS();
44 String more_Or_Less = nltMore_Or_Less.getPreferredRepresentation(languages).getLabel();
45
46
47 // the booleans and floats are updated according to the presence or absence of values
48
49 Boolean max, min, upperb, lowerb, average, sd;
50
51 String averagevalue = getValue(measures,StatisticalMeasure.AVERAGE());
52 if (averagevalue!=null) average=true; else average=false;
53 String sdvalue = getValue(measures,StatisticalMeasure.STANDARD_DEVIATION());
54 if (sdvalue!=null) sd=true; else sd=false;
55 String minvalue = getValue(measures,StatisticalMeasure.MIN());
56 if (minvalue!=null) min=true; else min=false;
57 String maxvalue = getValue(measures,StatisticalMeasure.MAX());
58 if (maxvalue!=null) max=true; else max=false;
59 String lowerbvalue = getValue(measures,StatisticalMeasure.TYPICAL_LOWER_BOUNDARY());
60 if (lowerbvalue!=null) lowerb=true; else lowerb=false;
61 String upperbvalue = getValue(measures,StatisticalMeasure.TYPICAL_UPPER_BOUNDARY());
62 if (upperbvalue!=null) upperb=true; else upperb=false;
63
64
65 // depending on the different associations of values, a sentence is built
66 if (max && min) {
67 QuantitativeDescription.append(space + from + space + minvalue + space + to + space + maxvalue + space + unit);
68 }
69 else if (min) {
70 QuantitativeDescription.append(space + from + space + minvalue + space + unit);
71 }
72 else if (max) {
73 QuantitativeDescription.append(space + up_To + space + maxvalue + space + unit);
74 }
75 if ((max||min)&&(lowerb||upperb)) {
76 QuantitativeDescription.append(separator); // merge with below ?
77 }
78 if ((lowerb||upperb)&&(min||max)) {
79 QuantitativeDescription.append(space + most_Frequently);
80 }
81 if (upperb && lowerb) {
82 QuantitativeDescription.append(space + from + space + lowerbvalue + space + to + space + upperbvalue + space + unit);
83 }
84 else if (lowerb) {
85 QuantitativeDescription.append(space + from + space + lowerbvalue + space + unit);
86 }
87 else if (upperb) {
88 QuantitativeDescription.append(space + up_To + space + upperbvalue + space + unit);
89 }
90 if (((max||min)&&(average))||((lowerb||upperb)&&(average))) {
91 QuantitativeDescription.append(separator);
92 }
93 if (average) {
94 QuantitativeDescription.append(space + averagevalue + space + unit + space + on_Average);
95 if (sd) {
96 QuantitativeDescription.append("("+ more_Or_Less + space + sdvalue + ")");
97 }
98 }
99 textData.putText(languages.get(0), QuantitativeDescription.toString()); // which language should be put here ?
100 textData.setFormat(TextFormat.NewInstance(null, "Text",null ));
101
102 return textData;
103 }
104
105
106
107 /**
108 * Returns the value of a given type of measure as a String. If the value is an integer it is printed
109 * as an integer instead of a float.
110 * If no value of this type is present, returns null.
111 *
112 * @param measures the map with the values
113 * @param key the desired measure
114 * @return
115 */
116 private String getValue(Map<StatisticalMeasure,Float> measures, Object key) {
117 Float floatValue;
118 Integer intValue;
119 if(measures.containsKey(key)) {
120 floatValue = measures.get(key);
121 intValue=floatValue.intValue();
122 if (floatValue.equals(intValue.floatValue())) return intValue.toString();
123 else return floatValue.toString();
124 }
125 else return null;
126 }
127
128 }