optimizing memory consumption of massindexer and catching data related exceptions
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / MicroFormatQuantitativeDescriptionBuilder.java
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 public class MicroFormatQuantitativeDescriptionBuilder extends AbstractQuantitativeDescriptionBuilder {
17
18 private String spanEnd = "</span>";
19
20 @Override
21 protected TextData doBuild(Map<StatisticalMeasure,Float> measures, MeasurementUnit mUnit, List<Language> languages){
22 StringBuilder QuantitativeDescription = new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
23 TextData textData = TextData.NewInstance(); // TextData that will contain the description and the language corresponding
24
25 // booleans indicating whether a kind of value is present or not and the float that will eventually hold the value
26 boolean average = false;
27 String averagevalue = null;
28 boolean sd = false;
29 String sdvalue = null;
30 boolean min = false;
31 String minvalue = null;
32 boolean max = false;
33 String maxvalue = null;
34 boolean lowerb = false;
35 String lowerbvalue = null;
36 boolean upperb = false;
37 String upperbvalue = null;
38
39 String unit = "(unknown unit)";
40 if ((mUnit!=null)&&(mUnit.getLabel()!=null)){
41 unit = spanClass("unit") + mUnit.getLabel() + spanEnd;
42 }
43
44 // the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
45 NaturalLanguageTerm nltFrom = NaturalLanguageTerm.FROM();
46 String from = nltFrom.getPreferredRepresentation(languages).getLabel();
47 NaturalLanguageTerm nltTo = NaturalLanguageTerm.TO();
48 String to = nltTo.getPreferredRepresentation(languages).getLabel();
49 NaturalLanguageTerm nltUp_To = NaturalLanguageTerm.UP_TO();
50 String up_To = nltUp_To.getPreferredRepresentation(languages).getLabel();
51 NaturalLanguageTerm nltMost_Frequently = NaturalLanguageTerm.MOST_FREQUENTLY();
52 String most_Frequently = nltMost_Frequently.getPreferredRepresentation(languages).getLabel();
53 NaturalLanguageTerm nltOn_Average = NaturalLanguageTerm.ON_AVERAGE();
54 String on_Average = nltOn_Average.getPreferredRepresentation(languages).getLabel();
55 NaturalLanguageTerm nltMore_Or_Less = NaturalLanguageTerm.MORE_OR_LESS();
56 String more_Or_Less = nltMore_Or_Less.getPreferredRepresentation(languages).getLabel();
57 String space = " "; // should "space" be considered as a linking word and thus be stored in NaturalLanguageTerm.class ?
58
59 // the booleans and floats are updated according to the presence or absence of values
60 if (measures.containsKey(StatisticalMeasure.AVERAGE())) {
61 average = true;
62 averagevalue = spanClass("measurement") + measures.get(StatisticalMeasure.AVERAGE()) + spanEnd;
63 } else if(measures.containsKey(StatisticalMeasure.STANDARD_DEVIATION())) {
64 sd = true;
65 sdvalue = spanClass("measurement") + measures.get(StatisticalMeasure.STANDARD_DEVIATION()) + spanEnd;
66 } else if (measures.containsKey(StatisticalMeasure.MIN())) {
67 min = true;
68 minvalue = spanClass("measurement") + measures.get(StatisticalMeasure.MIN()) + spanEnd;
69 } else if (measures.containsKey(StatisticalMeasure.MAX())) {
70 max = true;
71 maxvalue = spanClass("measurement") + measures.get(StatisticalMeasure.MAX()) + spanEnd;
72 } else if (measures.containsKey(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY())) {
73 lowerb = true;
74 lowerbvalue = spanClass("measurement") + measures.get(StatisticalMeasure.TYPICAL_LOWER_BOUNDARY()) + spanEnd;
75 } else if (measures.containsKey(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY())) {
76 upperb = true;
77 upperbvalue = spanClass("measurement") + measures.get(StatisticalMeasure.TYPICAL_UPPER_BOUNDARY()) + spanEnd;
78 }
79
80 QuantitativeDescription.append(spanClass("state"));
81 // depending on the different associations of values, a sentence is built
82 if (max && min) {
83 QuantitativeDescription.append(space + from + space + minvalue + space + to + space + maxvalue + space + unit);
84 }
85 else if (min) {
86 QuantitativeDescription.append(space + from + space + minvalue + space + unit);
87 }
88 else if (max) {
89 QuantitativeDescription.append(space + up_To + space + maxvalue + space + unit);
90 }
91 if ((max||min)&&(lowerb||upperb)) {
92 QuantitativeDescription.append(","); // merge with below ?
93 }
94 if ((lowerb||upperb)&&(min||max)) {
95 QuantitativeDescription.append(space + most_Frequently + space);
96 }
97 if (upperb && lowerb) {
98 QuantitativeDescription.append(space + from + space + lowerbvalue + space + to + space + upperbvalue + space + unit);
99 }
100 else if (lowerb) {
101 QuantitativeDescription.append(space + from + space + lowerbvalue + space + unit);
102 }
103 else if (upperb) {
104 QuantitativeDescription.append(space + up_To + space + upperbvalue + space + unit);
105 }
106 if (((max||min)&&(average))||((lowerb||upperb)&&(average))) {
107 QuantitativeDescription.append(",");
108 }
109 if (average) {
110 QuantitativeDescription.append(space + averagevalue + space + unit + space + on_Average);
111 if (sd) {
112 QuantitativeDescription.append("("+ more_Or_Less + space + sdvalue + ")");
113 }
114 }
115 QuantitativeDescription.append(spanEnd);
116 textData.putText(languages.get(0), QuantitativeDescription.toString()); // which language should be put here ?
117 textData.setFormat(TextFormat.NewInstance(null, "HTML",null )); // the data format is set (not yet real HTML)
118
119 return textData;
120 }
121
122 protected String buildFeature(Feature feature, boolean doItBetter){
123 if (feature==null || feature.getLabel()==null) return "";
124 else {
125 if (doItBetter) {
126 String betterString = StringUtils.substringBefore(feature.getLabel(), "<");
127 return StringUtils.removeEnd(betterString, " ");
128 }
129 else return feature.getLabel();
130 }
131 }
132
133 private String spanClass(String classString){
134 return("<span class=\""+classString+"\">");
135 }
136
137
138 }