package eu.etaxonomy.cdm.api.service;
-import java.util.Iterator;
+import java.util.List;
import java.util.Map;
-import java.util.Set;
+
+import org.apache.commons.lang.StringUtils;
import eu.etaxonomy.cdm.model.common.Language;
import eu.etaxonomy.cdm.model.description.Feature;
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
import eu.etaxonomy.cdm.model.description.NaturalLanguageTerm;
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
-import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
import eu.etaxonomy.cdm.model.description.TextData;
import eu.etaxonomy.cdm.model.description.TextFormat;
+/**
+ * @author m.venin
+ *
+ */
public class DefaultQuantitativeDescriptionBuilder extends AbstractQuantitativeDescriptionBuilder {
+
+ String space = " ";
@Override
- protected TextData doBuild(Map<StatisticalMeasure,Float> measures, MeasurementUnit mUnit){
- StringBuilder QuantitativeDescription = new StringBuilder();
- TextData textData = TextData.NewInstance();
- Language language = Language.DEFAULT();
+ protected TextData doBuild(Map<StatisticalMeasure,Float> measures, MeasurementUnit mUnit, List<Language> languages){
+ StringBuilder QuantitativeDescription = new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
+ TextData textData = TextData.NewInstance(); // TextData that will contain the description and the language corresponding
+ // booleans indicating whether a kind of value is present or not and the float that will eventually hold the value
- boolean average = false;
- float averagevalue = new Float(0);
- boolean sd = false;
- float sdvalue = new Float(0);
- boolean min = false;
- float minvalue = new Float(0);
- boolean max = false;
- float maxvalue = new Float(0);
- boolean lowerb = false;
- float lowerbvalue = new Float(0);
- boolean upperb = false;
- float upperbvalue = new Float(0);
-
- String unit = mUnit.getLabel();
+ String unit = "";
+ if ((mUnit!=null)&&(mUnit.getLabel()!=null)){
+ unit = mUnit.getLabel();
+ }
+ // the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
NaturalLanguageTerm nltFrom = NaturalLanguageTerm.FROM();
- String from = nltFrom.getPreferredRepresentation(language).getLabel();
+ String from = nltFrom.getPreferredRepresentation(languages).getLabel();
NaturalLanguageTerm nltTo = NaturalLanguageTerm.TO();
- String to = nltTo.getPreferredRepresentation(language).getLabel();
+ String to = nltTo.getPreferredRepresentation(languages).getLabel();
NaturalLanguageTerm nltUp_To = NaturalLanguageTerm.UP_TO();
- String up_To = nltUp_To.getPreferredRepresentation(language).getLabel();
+ String up_To = nltUp_To.getPreferredRepresentation(languages).getLabel();
NaturalLanguageTerm nltMost_Frequently = NaturalLanguageTerm.MOST_FREQUENTLY();
- String most_Frequently = nltMost_Frequently.getPreferredRepresentation(language).getLabel();
+ String most_Frequently = nltMost_Frequently.getPreferredRepresentation(languages).getLabel();
NaturalLanguageTerm nltOn_Average = NaturalLanguageTerm.ON_AVERAGE();
- String on_Average = nltOn_Average.getPreferredRepresentation(language).getLabel();
+ String on_Average = nltOn_Average.getPreferredRepresentation(languages).getLabel();
NaturalLanguageTerm nltMore_Or_Less = NaturalLanguageTerm.MORE_OR_LESS();
- String more_Or_Less = nltMore_Or_Less.getPreferredRepresentation(language).getLabel();
- String space = " ";
+ String more_Or_Less = nltMore_Or_Less.getPreferredRepresentation(languages).getLabel();
- if (measures.containsKey(StatisticalMeasure.AVERAGE())) {
- average = true;
- averagevalue = measures.get(StatisticalMeasure.AVERAGE());
- } else if(measures.containsKey(StatisticalMeasure.STANDARD_DEVIATION())) {
- sd = true;
- sdvalue = measures.get(StatisticalMeasure.STANDARD_DEVIATION());
- } else if (measures.containsKey(StatisticalMeasure.MIN())) {
- min = true;
- minvalue = measures.get(StatisticalMeasure.MIN());
- } else if (measures.containsKey(StatisticalMeasure.MAX())) {
- max = true;
- maxvalue = measures.get(StatisticalMeasure.MAX());
- } else if (measures.containsKey(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY())) {
- lowerb = true;
- lowerbvalue = measures.get(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY());
- } else if (measures.containsKey(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY())) {
- upperb = true;
- upperbvalue = measures.get(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY());
- }
-
+
+ // the booleans and floats are updated according to the presence or absence of values
+
+ Boolean max, min, upperb, lowerb, average, sd;
+
+ String averagevalue = getValue(measures,StatisticalMeasure.AVERAGE());
+ if (averagevalue!=null) average=true; else average=false;
+ String sdvalue = getValue(measures,StatisticalMeasure.STANDARD_DEVIATION());
+ if (sdvalue!=null) sd=true; else sd=false;
+ String minvalue = getValue(measures,StatisticalMeasure.MIN());
+ if (minvalue!=null) min=true; else min=false;
+ String maxvalue = getValue(measures,StatisticalMeasure.MAX());
+ if (maxvalue!=null) max=true; else max=false;
+ String lowerbvalue = getValue(measures,StatisticalMeasure.TYPICAL_LOWER_BOUNDARY());
+ if (lowerbvalue!=null) lowerb=true; else lowerb=false;
+ String upperbvalue = getValue(measures,StatisticalMeasure.TYPICAL_UPPER_BOUNDARY());
+ if (upperbvalue!=null) upperb=true; else upperb=false;
+
+
+ // depending on the different associations of values, a sentence is built
if (max && min) {
QuantitativeDescription.append(space + from + space + minvalue + space + to + space + maxvalue + space + unit);
}
QuantitativeDescription.append(space + up_To + space + maxvalue + space + unit);
}
if ((max||min)&&(lowerb||upperb)) {
- QuantitativeDescription.append(","); // fusion avec dessous ?
+ QuantitativeDescription.append(separator); // merge with below ?
}
if ((lowerb||upperb)&&(min||max)) {
- QuantitativeDescription.append(space + most_Frequently + space);
+ QuantitativeDescription.append(space + most_Frequently);
}
if (upperb && lowerb) {
QuantitativeDescription.append(space + from + space + lowerbvalue + space + to + space + upperbvalue + space + unit);
}
else if (lowerb) {
- QuantitativeDescription.append(space + from + lowerbvalue + space + unit);
+ QuantitativeDescription.append(space + from + space + lowerbvalue + space + unit);
}
else if (upperb) {
QuantitativeDescription.append(space + up_To + space + upperbvalue + space + unit);
}
if (((max||min)&&(average))||((lowerb||upperb)&&(average))) {
- QuantitativeDescription.append(",");
+ QuantitativeDescription.append(separator);
}
if (average) {
QuantitativeDescription.append(space + averagevalue + space + unit + space + on_Average);
QuantitativeDescription.append("("+ more_Or_Less + space + sdvalue + ")");
}
}
- textData.putText(QuantitativeDescription.toString(), language);
- textData.setFormat(TextFormat.NewInstance(null, "HTML",null ));
+ textData.putText(languages.get(0), QuantitativeDescription.toString()); // which language should be put here ?
+ textData.setFormat(TextFormat.NewInstance(null, "Text",null ));
return textData;
}
+
+
+
+ /**
+ * Returns the value of a given type of measure as a String. If the value is an integer it is printed
+ * as an integer instead of a float.
+ * If no value of this type is present, returns null.
+ *
+ * @param measures the map with the values
+ * @param key the desired measure
+ * @return
+ */
+ private String getValue(Map<StatisticalMeasure,Float> measures, Object key) {
+ Float floatValue;
+ Integer intValue;
+ if(measures.containsKey(key)) {
+ floatValue = measures.get(key);
+ intValue=floatValue.intValue();
+ if (floatValue.equals(intValue.floatValue())) return intValue.toString();
+ else return floatValue.toString();
+ }
+ else return null;
+ }
+
}