1 package com.calipso.reportgenerator.common; 2 3 import com.calipso.reportgenerator.reportdefinitions.MetricSourceDefinition; 4 import com.calipso.reportgenerator.reportdefinitions.MetricDefinition; 5 import com.calipso.reportgenerator.reportdefinitions.types.CalculationType; 6 import com.calipso.reportgenerator.reportcalculator.arithmetic.ArithmeticExpression; 7 import com.calipso.reportgenerator.reportcalculator.SharedString; 8 import com.calipso.reportgenerator.reportcalculator.SharedFloat; 9 import com.calipso.reportgenerator.common.LanguageTraslator; 10 import com.calipso.reportgenerator.common.ReportFieldSpec; 11 12 import java.util.*; 13 import java.math.BigDecimal ; 14 15 17 public class ReportMetricSpec extends ReportFieldSpec { 18 19 private boolean visible; 20 private CalculationType aggregateType; 21 private String aggregateFunction; 22 private String groupFooterFunction; 23 private CalculationType groupFooterType; 24 private ArithmeticExpression arithmeticExpression; 25 private Map variableIndexes; 26 private boolean accumulable; 27 private boolean externalCalculated; 28 29 33 public ReportMetricSpec(String name) { 34 super(name); 35 } 36 37 38 44 public void fillFrom(MetricSourceDefinition metricSourceDefinition, boolean isMultilanguaje, Map localization) { 45 setCaption(resolveCaption(isMultilanguaje, localization, metricSourceDefinition.getDescription())); 46 setCalculated(metricSourceDefinition.getCalculated()); 47 setExternalData(metricSourceDefinition.getExternalData()); 48 setAggregateFunction(metricSourceDefinition.getAggregateFunction()); 49 setAggregateType(metricSourceDefinition.getAggregateType()); 50 setGroupFooterFunction(metricSourceDefinition.getGroupFooterFunction()); 51 setGroupFooterType(metricSourceDefinition.getGroupFooterType()); 52 setExternalCalculated(metricSourceDefinition.getExternalCalculated()); 53 setInReportDefinition(true); 54 } 55 56 62 public void fillFrom(MetricDefinition metricDefinition, boolean isMultilanguaje, Map localization) { 63 if (metricDefinition.getDescription() != null && !("").equals(metricDefinition.getDescription())) { 64 setCaption(resolveCaption(isMultilanguaje, localization, metricDefinition.getDescription())); 65 } 66 setVisible(metricDefinition.getVisible()); 67 setAccumulable(metricDefinition.getAccumulable()); 68 } 69 74 public boolean getAccumulable() { 75 return accumulable; 76 } 77 78 82 public void setAccumulable(boolean accumulable) { 83 this.accumulable = accumulable; 84 } 85 86 90 public boolean getVisible() { 91 return visible; 92 } 93 94 98 public void setVisible(boolean visible) { 99 this.visible = visible; 100 } 101 102 106 public String getAggregateFunction() { 107 return aggregateFunction; 108 } 109 110 114 public void setAggregateFunction(String aggregateFunction) { 115 this.aggregateFunction = aggregateFunction; 116 } 117 118 119 123 public CalculationType getAggregateType() { 124 return aggregateType; 125 } 126 127 public void setAggregateType(CalculationType aggregateType) { 128 this.aggregateType = aggregateType; 129 } 130 131 public String getGroupFooterFunction() { 132 return groupFooterFunction; 133 } 134 135 public void setGroupFooterFunction(String groupFooterFunction) { 136 this.groupFooterFunction = groupFooterFunction; 137 } 138 139 public CalculationType getGroupFooterType() { 140 return groupFooterType; 141 } 142 143 public void setGroupFooterType(CalculationType groupFooterType) { 144 this.groupFooterType = groupFooterType; 145 } 146 147 151 public boolean getExternalCalculated() { 152 return externalCalculated; 153 } 154 155 159 private void setExternalCalculated(boolean externalCalculated) { 160 this.externalCalculated = externalCalculated; 161 } 162 163 167 protected ArithmeticExpression getExpression() { 168 if (arithmeticExpression == null) { 169 arithmeticExpression = ArithmeticExpression.newFrom(getAggregateFunction()); 170 variableIndexes = variableIndexesFrom(arithmeticExpression); 171 } 172 return arithmeticExpression; 173 } 174 175 179 public Map getVariableIndexes() { 180 return variableIndexes; 181 } 182 183 188 private Map variableIndexesFrom(ArithmeticExpression arithmeticExpression) { 189 Collection variables = new ArrayList(); 190 arithmeticExpression.getVariables(variables); 191 Iterator iterator = variables.iterator(); 192 Map indexes = new HashMap(); 193 while (iterator.hasNext()) { 194 String variable = (String ) iterator.next(); 195 indexes.put(variable, new Integer (getDataSourceIndexFromName(variable))); 196 } 197 return indexes; 198 } 199 200 206 public Object getValue(Object [] rowValues) throws InfoException { 207 if (getCalculated()) { 208 Float aFloat = new Float (getExpression().value(getContext(rowValues))); 209 if ((aFloat.equals(new Float (Float.NaN))) || (aFloat.equals(new Float (Float.POSITIVE_INFINITY))) || (aFloat.equals(new Float (Float.NEGATIVE_INFINITY)))){ 210 return null; 211 }else{ 212 return SharedFloat.newFrom(aFloat); 213 } 214 } 215 else { 216 return floatFrom(rowValues[getReportSourceIndex()]); 217 } 218 } 219 220 public Float getValue(Map context){ 221 return new Float (getExpression().value(context)); 222 } 223 224 229 private Map getContext(Object [] rowValues) throws InfoException { 230 Map context = new HashMap(); 231 fillContext(rowValues, context); 232 return context; 233 } 234 235 240 private void fillContext(Object [] values, Map context) throws InfoException { 241 Set entries = getVariableIndexes().entrySet(); 242 Iterator iterator = entries.iterator(); 243 while (iterator.hasNext()) { 244 Map.Entry entry = (Map.Entry) iterator.next(); 245 context.put(entry.getKey(), floatFrom(values[((Integer ) entry.getValue()).intValue()])); 246 } 247 } 248 249 254 private Object floatFrom(Object value) throws InfoException { 255 if(value instanceof Float ){ 256 return SharedFloat.newFrom((Float ) value); 257 }else if (value instanceof SharedFloat){ 258 return value; } else if ((value instanceof String )||(value instanceof SharedString)){ 260 String strValue = value.toString(); 261 if (strValue.equalsIgnoreCase("")){ 262 strValue = "0"; 263 } 264 return SharedFloat.newFrom(Float.valueOf(strValue.trim())); 265 } else if(value instanceof Integer ) { 266 return SharedFloat.newFrom(Float.valueOf(value.toString())); 267 } else if(value instanceof Long ) { 268 return SharedFloat.newFrom(Float.valueOf(value.toString())); 269 } else if(value instanceof BigDecimal ) { 270 return SharedFloat.newFrom(((BigDecimal )value).floatValue()); 271 } else if(value instanceof Double ) { 272 return SharedFloat.newFrom(((Double )value).floatValue()); 273 }else if(value instanceof java.sql.Date ) { 274 return SharedFloat.newFrom(Float.valueOf((value).toString())); 275 }else if(value == null) { 276 return null; 277 } 278 279 throw new InfoException(LanguageTraslator.traslate("279")); 280 } 281 } 282 | Popular Tags |