Add find- and countByTitle() methods to primer and amplification service
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / DefaultQuantitativeDescriptionBuilder.java
index 77f177c3c3f4000630287f26cfef73f5fd8ef350..5f5aec8492411472f164845e5dd4cd9ff9edc9aa 100644 (file)
@@ -13,28 +13,21 @@ import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
 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, 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 = "(unknown unit)";
+               String unit = "";
                if ((mUnit!=null)&&(mUnit.getLabel()!=null)){
                        unit = mUnit.getLabel();
                }
@@ -52,30 +45,26 @@ public class DefaultQuantitativeDescriptionBuilder extends AbstractQuantitativeD
                String on_Average = nltOn_Average.getPreferredRepresentation(languages).getLabel();
                NaturalLanguageTerm nltMore_Or_Less = NaturalLanguageTerm.MORE_OR_LESS();
                String more_Or_Less = nltMore_Or_Less.getPreferredRepresentation(languages).getLabel();
-               String space = " "; // should "space" be considered as a linking word and thus be stored in NaturalLanguageTerm.class ?
+               
                
                // the booleans and floats are updated according to the presence or absence of values
-                       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());
-                       }
-                       
-                       
+
+               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);
@@ -87,10 +76,10 @@ public class DefaultQuantitativeDescriptionBuilder extends AbstractQuantitativeD
                        QuantitativeDescription.append(space + up_To + space + maxvalue + space + unit);
                }
                if ((max||min)&&(lowerb||upperb)) {
-                       QuantitativeDescription.append(","); // merge with below ?
+                       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);
@@ -102,7 +91,7 @@ public class DefaultQuantitativeDescriptionBuilder extends AbstractQuantitativeD
                        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);
@@ -110,22 +99,33 @@ public class DefaultQuantitativeDescriptionBuilder extends AbstractQuantitativeD
                                QuantitativeDescription.append("("+ more_Or_Less + space + sdvalue + ")");
                        }
                }
-               textData.putText(QuantitativeDescription.toString(), languages.get(0)); // which language should be put here ?
-               textData.setFormat(TextFormat.NewInstance(null, "HTML",null )); // the data format is set (not yet real HTML)
+               textData.putText(languages.get(0), QuantitativeDescription.toString()); // which language should be put here ?
+               textData.setFormat(TextFormat.NewInstance(null, "Text",null ));
                
                return textData;
        }
        
-       protected String buildFeature(Feature feature, boolean doItBetter){
-               if (feature==null || feature.getLabel()==null) return "";
-               else {
-                       if (doItBetter) {
-                               String betterString = StringUtils.substringBefore(feature.getLabel(), "<");
-                               return StringUtils.removeEnd(betterString, " ");
-                       }
-                       else    return feature.getLabel();
+       
+       
+       /**
+        * 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;
        }
-
        
 }