Project

General

Profile

Download (6.39 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2015 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.format.description;
10

    
11
import java.math.BigDecimal;
12
import java.util.List;
13
import java.util.Map;
14

    
15
import org.apache.commons.lang3.StringUtils;
16

    
17
import eu.etaxonomy.cdm.model.common.Language;
18
import eu.etaxonomy.cdm.model.description.Feature;
19
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
20
import eu.etaxonomy.cdm.model.description.NaturalLanguageTerm;
21
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
22
import eu.etaxonomy.cdm.model.description.TextData;
23
import eu.etaxonomy.cdm.model.description.TextFormat;
24

    
25
/**
26
 * @author m.venin
27
 * @since 2010
28
 */
29
public class MicroFormatQuantitativeDescriptionBuilder extends AbstractQuantitativeDescriptionBuilder {
30

    
31
	private String spanEnd = "</span>";
32

    
33
	@Override
34
	protected TextData doBuild(Map<StatisticalMeasure,BigDecimal> measures, MeasurementUnit mUnit,
35
	        List<Language> languages){
36
		StringBuilder QuantitativeDescription = new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
37
		TextData textData = TextData.NewInstance(); // TextData that will contain the description and the language corresponding
38

    
39
		// booleans indicating whether a kind of value is present or not and the float that will eventually hold the value
40
		boolean average = false;
41
		String averagevalue = null;
42
		boolean sd = false;
43
		String sdvalue = null;
44
		boolean min = false;
45
		String minvalue = null;
46
		boolean max = false;
47
		String maxvalue = null;
48
		boolean lowerb = false;
49
		String lowerbvalue = null;
50
		boolean upperb = false;
51
		String upperbvalue = null;
52

    
53
		String unit = "(unknown unit)";
54
		if ((mUnit!=null)&&(mUnit.getLabel()!=null)){
55
			unit = spanClass("unit") + mUnit.getLabel() + spanEnd;
56
		}
57

    
58
		// the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
59
		NaturalLanguageTerm nltFrom = NaturalLanguageTerm.FROM();
60
		String from = nltFrom.getPreferredRepresentation(languages).getLabel();
61
		NaturalLanguageTerm nltTo = NaturalLanguageTerm.TO();
62
		String to = nltTo.getPreferredRepresentation(languages).getLabel();
63
		NaturalLanguageTerm nltUp_To = NaturalLanguageTerm.UP_TO();
64
		String up_To = nltUp_To.getPreferredRepresentation(languages).getLabel();
65
		NaturalLanguageTerm nltMost_Frequently = NaturalLanguageTerm.MOST_FREQUENTLY();
66
		String most_Frequently = nltMost_Frequently.getPreferredRepresentation(languages).getLabel();
67
		NaturalLanguageTerm nltOn_Average = NaturalLanguageTerm.ON_AVERAGE();
68
		String on_Average = nltOn_Average.getPreferredRepresentation(languages).getLabel();
69
		NaturalLanguageTerm nltMore_Or_Less = NaturalLanguageTerm.MORE_OR_LESS();
70
		String more_Or_Less = nltMore_Or_Less.getPreferredRepresentation(languages).getLabel();
71
		String space = " "; // should "space" be considered as a linking word and thus be stored in NaturalLanguageTerm.class ?
72

    
73
		// the booleans and floats are updated according to the presence or absence of values
74
		if (measures.containsKey(StatisticalMeasure.AVERAGE())) {
75
			average = true;
76
			averagevalue = spanClass("measurement") + measures.get(StatisticalMeasure.AVERAGE()) + spanEnd;
77
		} else if(measures.containsKey(StatisticalMeasure.STANDARD_DEVIATION())) {
78
			sd = true;
79
			sdvalue = spanClass("measurement") + measures.get(StatisticalMeasure.STANDARD_DEVIATION()) + spanEnd;
80
		} else if (measures.containsKey(StatisticalMeasure.MIN())) {
81
			min = true;
82
			minvalue = spanClass("measurement") + measures.get(StatisticalMeasure.MIN()) + spanEnd;
83
		} else if (measures.containsKey(StatisticalMeasure.MAX())) {
84
			max = true;
85
			maxvalue = spanClass("measurement") + measures.get(StatisticalMeasure.MAX()) + spanEnd;
86
		} else if (measures.containsKey(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY())) {
87
			lowerb = true;
88
			lowerbvalue = spanClass("measurement") + measures.get(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY()) + spanEnd;
89
		} else if (measures.containsKey(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY())) {
90
			upperb = true;
91
			upperbvalue = spanClass("measurement") + measures.get(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY()) + spanEnd;
92
		}
93

    
94
		QuantitativeDescription.append(spanClass("state"));
95
		// depending on the different associations of values, a sentence is built
96
		if (max && min) {
97
			QuantitativeDescription.append(space + from + space + minvalue + space + to + space + maxvalue + space + unit);
98
		}
99
		else if (min) {
100
			QuantitativeDescription.append(space + from + space + minvalue + space + unit);
101
		}
102
		else if (max) {
103
			QuantitativeDescription.append(space + up_To + space + maxvalue + space + unit);
104
		}
105
		if ((max||min)&&(lowerb||upperb)) {
106
			QuantitativeDescription.append(","); // merge with below ?
107
		}
108
		if ((lowerb||upperb)&&(min||max)) {
109
			QuantitativeDescription.append(space + most_Frequently + space);
110
		}
111
		if (upperb && lowerb) {
112
			QuantitativeDescription.append(space + from + space + lowerbvalue + space + to + space + upperbvalue + space + unit);
113
		}
114
		else if (lowerb) {
115
			QuantitativeDescription.append(space + from + space + lowerbvalue + space + unit);
116
		}
117
		else if (upperb) {
118
			QuantitativeDescription.append(space + up_To + space + upperbvalue + space + unit);
119
		}
120
		if (((max||min)&&(average))||((lowerb||upperb)&&(average))) {
121
			QuantitativeDescription.append(",");
122
		}
123
		if (average) {
124
			QuantitativeDescription.append(space + averagevalue + space + unit + space + on_Average);
125
			if (sd) {
126
				QuantitativeDescription.append("("+ more_Or_Less + space + sdvalue + ")");
127
			}
128
		}
129
		QuantitativeDescription.append(spanEnd);
130
		textData.putText(languages.get(0), QuantitativeDescription.toString()); // which language should be put here ?
131
		textData.setFormat(TextFormat.NewInstance(null, "HTML",null )); // the data format is set (not yet real HTML)
132

    
133
		return textData;
134
	}
135

    
136
	protected String buildFeature(Feature feature, boolean doItBetter){
137
		if (feature==null || feature.getLabel()==null) {
138
            return "";
139
        } else {
140
			if (doItBetter) {
141
				String betterString = StringUtils.substringBefore(feature.getLabel(), "<");
142
				return StringUtils.removeEnd(betterString, " ");
143
			} else {
144
                return feature.getLabel();
145
            }
146
		}
147
	}
148

    
149
	private String spanClass(String classString){
150
		return("<span class=\""+classString+"\">");
151
	}
152
}
(9-9/10)