1 package com.calipso.reportgenerator.common; 2 3 import com.calipso.reportgenerator.reportdefinitions.DimensionSourceDefinition; 4 import com.calipso.reportgenerator.reportdefinitions.DimensionDefinition; 5 import com.calipso.reportgenerator.reportdefinitions.types.DimensionDefinitionLocationType; 6 import com.calipso.reportgenerator.reportdefinitions.types.DimensionDefinitionOrderType; 7 import com.calipso.reportgenerator.reportdefinitions.types.ReportDataType; 8 import com.calipso.reportgenerator.reportcalculator.SharedDate; 9 import com.calipso.reportgenerator.reportcalculator.SharedFloat; 10 import com.calipso.reportgenerator.reportcalculator.SharedInteger; 11 import com.calipso.common.DateEx; 12 import java.util.Date ; 13 import java.util.Map ; 14 15 19 public class ReportDimensionSpec extends ReportFieldSpec { 20 21 private String expression; 22 private String groupFooterCaption; 23 private DimensionDefinitionLocationType location; 24 private DimensionDefinitionOrderType order; 25 private ReportDataType dataType; 26 private boolean groups; 27 private String datePattern; 28 private String referencedDimensionName; 29 private int referencedDataIndex; 30 private String dateFunction; 31 private String rankMetricName; 32 private String dateTimePattern; 33 private boolean indexed; 34 36 40 public ReportDimensionSpec(String name) { 41 super(name); 42 referencedDataIndex = -1; 43 referencedDimensionName = null; 44 dateFunction = null; 45 } 46 47 48 54 public void fillFrom(DimensionSourceDefinition dimensionSourceDefinition, boolean isMultilanguaje, Map localization) { 55 setCaption(resolveCaption(isMultilanguaje,localization,dimensionSourceDefinition.getDescription())); 56 setCalculated(dimensionSourceDefinition.getCalculated()); 57 setExpression(dimensionSourceDefinition.getExpression()); 58 setExternalData(dimensionSourceDefinition.getExternalData()); 59 setDataType(dimensionSourceDefinition.getDataType()); 60 setIndexed(dimensionSourceDefinition.getIndexed()); 61 } 63 64 70 public void fillFrom(DimensionDefinition dimensionDefinition, boolean isMultilanguaje, Map localization) { 71 if ((dimensionDefinition.getDescription() != null) && !("").equals(dimensionDefinition.getDescription())) { 72 setCaption(resolveCaption(isMultilanguaje,localization,dimensionDefinition.getDescription())); 73 } 74 setGroupFooterCaption(resolveCaption(isMultilanguaje,localization,dimensionDefinition.getGroupFooterCaption())); 75 setLocation(dimensionDefinition.getLocation()); 76 setOrder(dimensionDefinition.getOrder()); 77 setGroups(dimensionDefinition.getGroups()); 78 setInReportDefinition(true); 79 setRankMetricName(dimensionDefinition.getRankMetricName()); 80 } 81 82 86 public String getExpression() { 87 return expression; 88 } 89 90 94 public void setExpression(String expression) { 95 this.expression = expression; 96 } 97 98 102 public String getGroupFooterCaption() { 103 return groupFooterCaption; 104 } 105 106 110 111 public void setGroupFooterCaption(String groupFooterCaption) { 112 this.groupFooterCaption = groupFooterCaption; 113 } 114 115 116 120 public DimensionDefinitionOrderType getOrder() { 121 if (order == null){ 122 return DimensionDefinitionOrderType.A; 123 } 124 return order; 125 } 126 127 128 132 public void setOrder(DimensionDefinitionOrderType order) { 133 this.order = order; 134 } 135 136 141 public DimensionDefinitionLocationType getLocation() { 142 return location; 143 } 144 145 149 public void setLocation(DimensionDefinitionLocationType location) { 150 this.location = location; 151 } 152 153 157 public ReportDataType getDataType() { 158 return dataType; 159 } 160 161 165 public void setDataType(ReportDataType dataType) { 166 this.dataType = dataType; 167 } 168 169 174 public boolean getGroups() { 175 return groups; 176 } 177 178 182 public void setGroups(boolean groups) { 183 this.groups = groups; 184 } 185 186 191 protected int getReferencedDataIndex(String referencedName){ 192 if (referencedDataIndex == -1){ 193 referencedDataIndex = getReportSourceIndexFromName(referencedName); 194 } 195 return referencedDataIndex; 196 } 197 198 private int getReportSourceIndexFromName(String referencedName) { 199 return ((Integer )getReportSourceIndexes().get(referencedName)).intValue(); 200 } 201 202 206 protected String getReferencedDimensionName() { 207 if (referencedDimensionName == null){ 208 DateExpressionParser expressionParser = new DateExpressionParser(); 209 expressionParser.setExpression(getExpression()); 210 referencedDimensionName = expressionParser.getFieldName(); 211 } 212 return referencedDimensionName; 213 } 214 215 219 protected String getDateFunction(){ 220 if (dateFunction == null){ 221 DateExpressionParser expressionParser = new DateExpressionParser(); 222 expressionParser.setExpression(getExpression()); 223 dateFunction = expressionParser.getDateFunction(); 224 } 225 return dateFunction; 226 } 227 228 259 260 265 public String getRankMetricName() { 266 return rankMetricName; 267 } 268 269 273 public void setRankMetricName(String rankMetricName) { 274 this.rankMetricName = rankMetricName; 275 } 276 277 286 public Object getValue(Object [] rowValues, ReportDataSourceSpec dataSourceSpec) throws InfoException { 287 try { 288 if (getCalculated()) { 289 String refDimensionName = getReferencedDimensionName(); 290 int calcDataIndex = getReferencedDataIndex(refDimensionName); 291 Object date = rowValues[calcDataIndex]; 292 if ((date instanceof SharedDate)){ 293 String resultValue = DateExpressionResolver.Resolve(getDateFunction(),((SharedDate)date).getDateEx().getDate()); 294 return SharedInteger.newFrom(Integer.valueOf(resultValue)); 295 } else if (date instanceof Date){ 296 String resultValue = DateExpressionResolver.Resolve(getDateFunction(),((Date)date)); 297 return SharedInteger.newFrom(Integer.valueOf(resultValue)); 298 } else{ 299 String dateString = date.toString(); 300 String resultValue = DateExpressionResolver.Resolve(getDateFunction(), dateString, dataSourceSpec.getPattern(this.getDataType().getType())); 301 return SharedInteger.newFrom(Integer.valueOf(resultValue)); 302 } 303 } else { 304 return convertValue(rowValues[getReportSourceIndex()], dataSourceSpec.getPattern(this.getDataType().getType())); 305 } 306 } catch (Exception e) { 307 throw new InfoException(LanguageTraslator.traslate("80"), e); 308 } 309 } 310 311 321 322 327 private Object convertValue(Object value, String datePattern) throws InfoException { 328 if (getDataType().getType() == ReportDataType.DATETIME_TYPE || getDataType().getType() == ReportDataType.DATE_TYPE){ 329 if(value instanceof SharedDate){ 330 return value; 331 }else if(value instanceof DateEx){ 332 return SharedDate.newFrom((DateEx)value); 333 }else{ 334 return SharedDate.newFrom(new DateEx(value, datePattern)); 335 } 336 } else { 337 return value; 338 } 339 } 340 341 342 343 356 357 364 365 371 public Object getValueFor(Object value) throws InfoException { 372 return getValueFor(value, dataType.getType()); 373 } 374 375 public static Object getValueFor(Object value, int dataType) throws InfoException{ 376 if(value instanceof String ){ 377 try { 378 String strValue = value.toString(); 379 switch(dataType){ 380 case ReportDataType.BOOLEAN_TYPE: 381 return new Boolean (strValue); 382 case ReportDataType.FLOAT_TYPE: 383 return SharedFloat.newFrom(new Float (strValue)); 384 case ReportDataType.STRING_TYPE: 385 return strValue; 386 case ReportDataType.INTEGER_TYPE: 387 return SharedInteger.newFrom(new Integer (strValue)); 388 case ReportDataType.DATETIME_TYPE: 389 case ReportDataType.DATE_TYPE: 390 return SharedDate.newFrom(new DateEx(value)); 391 } 392 } catch (Exception e) { 393 throw new InfoException(LanguageTraslator.traslate("361"), e); 394 } 395 } else if(value instanceof Date) { 396 if(dataType == ReportDataType.DATETIME_TYPE || dataType == ReportDataType.DATE_TYPE){ 397 return SharedDate.newFrom(new DateEx((Date)value)); 398 } else { 399 throw new InfoException(LanguageTraslator.traslate("361")); 400 } 401 } 402 return value; 403 } 404 405 public boolean isIndexed() { 406 return indexed; 407 } 408 409 public void setIndexed(boolean indexed) { 410 this.indexed = indexed; 411 } 412 413 } 414 415 | Popular Tags |