Project

General

Profile

Download (5.34 KB) Statistics
| Branch: | Tag: | Revision:
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
/**
17
 * @author m.venin
18
 *
19
 */
20
public class DefaultQuantitativeDescriptionBuilder extends AbstractQuantitativeDescriptionBuilder {
21

    
22
	String space = " ";
23
	
24
	@Override
25
	protected TextData doBuild(Map<StatisticalMeasure,Float> measures, MeasurementUnit mUnit, List<Language> languages){
26
		StringBuilder QuantitativeDescription = new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
27
		TextData textData = TextData.NewInstance(); // TextData that will contain the description and the language corresponding
28
		// booleans indicating whether a kind of value is present or not and the float that will eventually hold the value
29
		
30
		String unit = "";
31
		if ((mUnit!=null)&&(mUnit.getLabel()!=null)){
32
			unit = mUnit.getLabel();
33
		}
34
		
35
		// the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
36
		NaturalLanguageTerm nltFrom = NaturalLanguageTerm.FROM();
37
		String from = nltFrom.getPreferredRepresentation(languages).getLabel();
38
		NaturalLanguageTerm nltTo = NaturalLanguageTerm.TO();
39
		String to = nltTo.getPreferredRepresentation(languages).getLabel();
40
		NaturalLanguageTerm nltUp_To = NaturalLanguageTerm.UP_TO();
41
		String up_To = nltUp_To.getPreferredRepresentation(languages).getLabel();
42
		NaturalLanguageTerm nltMost_Frequently = NaturalLanguageTerm.MOST_FREQUENTLY();
43
		String most_Frequently = nltMost_Frequently.getPreferredRepresentation(languages).getLabel();
44
		NaturalLanguageTerm nltOn_Average = NaturalLanguageTerm.ON_AVERAGE();
45
		String on_Average = nltOn_Average.getPreferredRepresentation(languages).getLabel();
46
		NaturalLanguageTerm nltMore_Or_Less = NaturalLanguageTerm.MORE_OR_LESS();
47
		String more_Or_Less = nltMore_Or_Less.getPreferredRepresentation(languages).getLabel();
48
		
49
		
50
		// the booleans and floats are updated according to the presence or absence of values
51

    
52
		Boolean max, min, upperb, lowerb, average, sd;
53
		
54
		String averagevalue = getValue(measures,StatisticalMeasure.AVERAGE());
55
		if (averagevalue!=null) average=true; else average=false;
56
		String sdvalue = getValue(measures,StatisticalMeasure.STANDARD_DEVIATION());
57
		if (sdvalue!=null) sd=true; else sd=false;
58
		String minvalue = getValue(measures,StatisticalMeasure.MIN());
59
		if (minvalue!=null) min=true; else min=false;
60
		String maxvalue = getValue(measures,StatisticalMeasure.MAX());
61
		if (maxvalue!=null) max=true; else max=false;
62
		String lowerbvalue = getValue(measures,StatisticalMeasure.TYPICAL_LOWER_BOUNDARY());
63
		if (lowerbvalue!=null) lowerb=true; else lowerb=false;
64
		String upperbvalue = getValue(measures,StatisticalMeasure.TYPICAL_UPPER_BOUNDARY());
65
		if (upperbvalue!=null) upperb=true; else upperb=false;
66
		
67
		
68
		// depending on the different associations of values, a sentence is built	
69
		if (max && min) {
70
			QuantitativeDescription.append(space + from + space + minvalue + space + to + space + maxvalue + space + unit);
71
		}
72
		else if (min) {
73
			QuantitativeDescription.append(space + from + space + minvalue + space + unit);
74
		}
75
		else if (max) {
76
			QuantitativeDescription.append(space + up_To + space + maxvalue + space + unit);
77
		}
78
		if ((max||min)&&(lowerb||upperb)) {
79
			QuantitativeDescription.append(separator); // merge with below ?
80
		}
81
		if ((lowerb||upperb)&&(min||max)) {
82
			QuantitativeDescription.append(space + most_Frequently);
83
		}
84
		if (upperb && lowerb) {
85
			QuantitativeDescription.append(space + from + space + lowerbvalue + space + to + space + upperbvalue + space + unit);
86
		}
87
		else if (lowerb) {
88
			QuantitativeDescription.append(space + from + space + lowerbvalue + space + unit);
89
		}
90
		else if (upperb) {
91
			QuantitativeDescription.append(space + up_To + space + upperbvalue + space + unit);
92
		}
93
		if (((max||min)&&(average))||((lowerb||upperb)&&(average))) {
94
			QuantitativeDescription.append(separator);
95
		}
96
		if (average) {
97
			QuantitativeDescription.append(space + averagevalue + space + unit + space + on_Average);
98
			if (sd) {
99
				QuantitativeDescription.append("("+ more_Or_Less + space + sdvalue + ")");
100
			}
101
		}
102
		textData.putText(languages.get(0), QuantitativeDescription.toString()); // which language should be put here ?
103
		textData.setFormat(TextFormat.NewInstance(null, "Text",null ));
104
		
105
		return textData;
106
	}
107
	
108
	
109
	
110
	/**
111
	 * Returns the value of a given type of measure as a String. If the value is an integer it is printed
112
	 * as an integer instead of a float.
113
	 * If no value of this type is present, returns null.
114
	 * 
115
	 * @param measures the map with the values
116
	 * @param key the desired measure
117
	 * @return
118
	 */
119
	private String getValue(Map<StatisticalMeasure,Float> measures, Object key) {
120
		Float floatValue;
121
		Integer intValue;
122
		if(measures.containsKey(key)) {
123
			floatValue = measures.get(key);
124
			intValue=floatValue.intValue();
125
			if (floatValue.equals(intValue.floatValue())) return intValue.toString();
126
			else return floatValue.toString();
127
		}
128
		else return null;
129
	}
130
	
131
}
(12-12/76)