1 package com.calipso.reportgenerator.reportmanager; 2 3 import com.calipso.reportgenerator.reportdefinitions.DimensionSourceDefinition; 4 import com.calipso.reportgenerator.reportcalculator.IDataSource; 5 import com.calipso.reportgenerator.common.*; 6 7 13 14 public class ReportSourceDimension { 15 16 private int index; 17 private int dataIndex; 18 private DimensionSourceDefinition dimensionSourceDefinition; 19 private DateExpressionParser expressionParser; 20 private String datePattern; 21 22 28 public ReportSourceDimension(DimensionSourceDefinition dimensionSourceDefinition, int index, int dataIndex) { 29 this.dimensionSourceDefinition = dimensionSourceDefinition; 30 this.index = index; 31 if (dimensionSourceDefinition.getCalculated()) { 32 this.dataIndex = -1; 33 } 34 else { 35 this.dataIndex = dataIndex; 36 } 37 } 38 39 46 public ReportSourceDimension(DimensionSourceDefinition dimensionSourceDefinition, int index, int dataIndex, String datePattern) { 47 this(dimensionSourceDefinition, index, dataIndex); 48 this.datePattern = datePattern; 49 } 50 51 56 protected DateExpressionParser getExpressionParser() { 57 if (expressionParser == null) { 58 expressionParser = new DateExpressionParser(); 59 } 60 return expressionParser; 61 } 62 63 67 public int getIndex() { 68 return index; 69 } 70 71 72 77 public int getDataIndex() { 78 return dataIndex; 79 } 80 81 85 public DimensionSourceDefinition getDimensionSourceDefinition() { 86 return dimensionSourceDefinition; 87 } 88 89 private int getCalculatedDataIndex(String name, IDataSource dataSource) { 90 if (dataIndex == -1) { 91 for (int i = 0; i < dataSource.getColumCount(); i++) { 92 String colName = dataSource.getColumName(i); 93 if (colName.equals(name)) { 94 return i; 95 } 96 } 97 } 98 return dataIndex; 99 } 100 101 108 public Object getValue(IDataSource dataSource, int index) throws InfoException { 109 try { 110 if (getDimensionSourceDefinition().getCalculated()) { 111 String expression = getDimensionSourceDefinition().getExpression(); 112 getExpressionParser().setExpression(expression); 113 String refDimensionName = getExpressionParser().getFieldName(); 114 int calcDataIndex = getCalculatedDataIndex(refDimensionName, dataSource); 115 String dateString = dataSource.getValueAt(index, calcDataIndex).toString(); 116 String resultValue = DateExpressionResolver.Resolve(getExpressionParser().getDateFunction(), dateString, getDatePattern()); 117 return resultValue; 118 } 119 else { 120 return dataSource.getValueAt(index, this.dataIndex).toString(); 121 } 122 } catch (Exception e) { 123 throw new InfoException(LanguageTraslator.traslate("90"), e); 124 } 125 } 126 127 131 public String getDatePattern() { 132 return datePattern; 133 } 134 135 139 public void setDatePattern(String pattern) { 140 datePattern = pattern; 141 } 142 } 143 | Popular Tags |