KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > reportmanager > ExcelReportDataSource


1 package com.calipso.reportgenerator.reportmanager;
2
3 import com.calipso.reportgenerator.reportcalculator.IDataSource;
4 import com.calipso.reportgenerator.common.InfoException;
5 import java.io.*;
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Vector JavaDoc;
9 import java.util.Enumeration JavaDoc;
10
11 import org.apache.poi.hssf.usermodel.HSSFCell;
12 import org.apache.poi.hssf.usermodel.HSSFRow;
13 import org.apache.poi.hssf.usermodel.HSSFSheet;
14 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
15 import com.calipso.reportgenerator.common.*;
16 import com.calipso.reportgenerator.reportcalculator.*;
17 import com.calipso.reportgenerator.reportdefinitions.types.ReportDataType;
18 import com.calipso.common.DateEx;
19
20 /**
21  * Representa un origen de datos Excel
22  */

23
24 public class ExcelReportDataSource extends ReportDataSource {
25
26   private static final int STRING = 1;
27   private static final int NUMERIC = 0;
28   private IDataSource dataSource;
29   private org.apache.poi.poifs.filesystem.POIFSFileSystem fileSystem;
30   private String JavaDoc sheetName;
31   private String JavaDoc dataInitialCell;
32   private String JavaDoc dataEndingCell;
33   private ExcelSheetPosition initialPosition;
34   private ExcelSheetPosition endingPosition;
35   private int dimensionsCount;
36
37   /**
38    * Inicializa una instancia de <code>ExcelReportDataSource</code>
39    * @param reportSpec
40    * @param reportDataSourceSpec
41    */

42   public ExcelReportDataSource(ReportSpec reportSpec, ReportDataSourceSpec reportDataSourceSpec, ReportGeneratorConfiguration configuration) throws InfoException {
43     super(reportSpec, reportDataSourceSpec);
44     setGeneratorConfiguration(configuration);
45     //dimensionsCount = reportSpec.getNotCalculatedDimensions().size();
46
File file = new File(reportDataSourceSpec.getExpression());
47     sheetName = reportDataSourceSpec.getSheetName();
48     dataEndingCell = reportDataSourceSpec.getDataEndingCell();
49     dataInitialCell = reportDataSourceSpec.getDataInitialCell();
50     try {
51       InputStream inputStream = new FileInputStream(file);
52       fileSystem = new org.apache.poi.poifs.filesystem.POIFSFileSystem(inputStream);
53     } catch (Exception JavaDoc e) {
54       throw new InfoException(LanguageTraslator.traslate("473"),e);
55     }
56   }
57
58   public IDataSource getDataSource(Matrix matrix) throws InfoException {
59     /*if (dataSource == null) {
60       dataSource = newDataSource();*/

61       fillFromFile(matrix);
62     /*}*/
63     return dataSource;
64   }
65
66   private void fillFromFile(Matrix matrix) throws InfoException{
67     try {
68       HSSFWorkbook wb = new HSSFWorkbook(fileSystem);
69       HSSFSheet sheet;
70       if(sheetName!=null && !sheetName.equalsIgnoreCase("")){
71         sheet = wb.getSheet(sheetName);
72       }else{
73         sheet = wb.getSheetAt(0);
74       }
75       createInitialPositions(sheet);
76       //for(int i = sheet.getFirstRowNum() ; i <= sheet.getLastRowNum() ; i++) {
77
for(int i = initialPosition.getRow() ; i <= endingPosition.getRow() ; i++) {
78         /*List dataSourceRow = new ArrayList();
79         fillDataSourceRowFrom(getItemVector(sheet.getRow(i)), dataSourceRow);*/

80         Object JavaDoc[] row = getItemVector(sheet.getRow(i));
81         try {
82           if ((getFilter()== null)||((getFilter()!= null) && (getFilter().matches(row)))) {
83             //dataSource.addRow(dataSourceRow);
84
matrix.add(row);
85           }
86         } catch (InfoException e) {
87           throw new InfoException(LanguageTraslator.traslate("386"), e);
88         }
89       }
90     } catch (Exception JavaDoc e) {
91       throw new InfoException(LanguageTraslator.traslate("316"),e);
92     } catch(OutOfMemoryError JavaDoc e) {
93       throw new InfoException(LanguageTraslator.traslate("326"), e);
94     }
95   }
96
97   private void createInitialPositions(HSSFSheet sheet) throws InfoException {
98     if(dataInitialCell==null || dataInitialCell.equalsIgnoreCase("")){
99       int initialRow = sheet.getFirstRowNum();
100       short initialColumn = sheet.getRow(initialRow).getFirstCellNum();
101       initialPosition = new ExcelSheetPosition(initialRow, initialColumn);
102     }else{
103       initialPosition = new ExcelSheetPosition(getDataInitialCell());
104       initialPosition.setRow(initialPosition.getRow()-1);
105     }
106     if(dataEndingCell==null || dataEndingCell.equalsIgnoreCase("")){
107       int finalRow = sheet.getLastRowNum();
108       short finalColumn = (short)(sheet.getRow(finalRow).getLastCellNum()-1);
109       endingPosition = new ExcelSheetPosition(finalRow, finalColumn);
110     }else{
111       endingPosition = new ExcelSheetPosition(getDataEndingCell());
112       endingPosition.setRow(endingPosition.getRow()-1);
113     }
114   }
115
116   private String JavaDoc getDataEndingCell() {
117     return dataEndingCell;
118   }
119
120   private String JavaDoc getDataInitialCell() {
121     return dataInitialCell;
122   }
123
124 /* private void fillDataSourceRowFrom(Vector itemVector, List dataSourceRow) throws InfoException{
125     int i = 0;
126     //int size = getReportSpec().getDataSourceIndexes().size();
127     int dimensionSize = getReportSpec().getNotCalculatedDimensions().size();
128     for(Enumeration enumeration = itemVector.elements() ; enumeration.hasMoreElements() ; i++) {
129       Object object = enumeration.nextElement();
130       if(i < dimensionSize) {
131         String name = getReportSpec().getDataSourceIndexNameByIndex(i);
132         ReportDimensionSpec dimensionSpec = getReportSpec().getDimensionFromName(name);
133         //ReportDimensionSpec dimensionSpec = (ReportDimensionSpec)getReportSpec().getDimensionsByIndex().get(i);
134         if(object instanceof String) {
135           fillDataSourceItemFrom((String)object, dataSourceRow, dimensionSpec, i);
136         } else if(object instanceof Double){
137           fillDataSourceItemFrom((Double)object, dataSourceRow, dimensionSpec, i);
138         } else if(object instanceof Integer){
139           fillDataSourceItemFrom((Integer)object, dataSourceRow, dimensionSpec, i);
140         }
141
142       } else {
143         dataSourceRow.add(object.toString());
144       }
145     }
146   }
147
148   private void fillDataSourceItemFrom(Number value, List dataSourceRow, ReportDimensionSpec dimensionSpec, int index) throws InfoException{
149     switch(dimensionSpec.getDataType().getType()){
150       case ReportDataType.DATETIME_TYPE:
151       case ReportDataType.DATE_TYPE:
152         dataSourceRow.add(index, SharedDate.newFrom(new DateEx(value, getReportDataSourceSpec().getPattern(dimensionSpec.getDataType().getType()))));
153         //DecimalFormat numberFormat = new DecimalFormat("########");
154         //Format numberFormat = new DecimalFormat("#####################");
155         //dataSourceRow.add(index, SharedDate.newFrom(new DateEx(numberFormat.format(value), getReportDataSourceSpec().getPattern(dimensionSpec.getDataType().getType()))));
156         break;
157       case ReportDataType.STRING_TYPE:
158         dataSourceRow.add(index, SharedString.newFrom(value.toString()));
159         break;
160       case ReportDataType.FLOAT_TYPE:
161         dataSourceRow.add(index, SharedFloat.newFrom(new Float(value.floatValue())));
162         break;
163       case ReportDataType.INTEGER_TYPE:
164         dataSourceRow.add(index, SharedInteger.newFrom(new Integer(value.intValue())));
165         break;
166       case ReportDataType.BOOLEAN_TYPE:
167         throw new InfoException("374");
168     }
169   }
170
171
172
173
174   private void fillDataSourceItemFrom(String strValue, List dataSourceRow, ReportDimensionSpec dimensionSpec, int index) throws InfoException{
175     switch(dimensionSpec.getDataType().getType()){
176       case ReportDataType.DATETIME_TYPE:
177         dataSourceRow.add(index, SharedDate.newFrom(new DateEx(strValue, getReportDataSourceSpec().getDateTimePattern())));
178         break;
179       case ReportDataType.DATE_TYPE:
180         dataSourceRow.add(index, SharedDate.newFrom(new DateEx(strValue, getReportDataSourceSpec().getDatePattern())));
181         break;
182       case ReportDataType.STRING_TYPE:
183         dataSourceRow.add(index, SharedString.newFrom(strValue));
184         break;
185       case ReportDataType.FLOAT_TYPE:
186         dataSourceRow.add(index, SharedFloat.newFrom(new Float(strValue)));
187         break;
188       case ReportDataType.INTEGER_TYPE:
189         dataSourceRow.add(index, SharedInteger.newFrom(new Integer(strValue)));
190         break;
191       case ReportDataType.BOOLEAN_TYPE:
192         dataSourceRow.add(index, Boolean.valueOf(strValue));
193         break;
194     }
195   }*/

196
197   private Object JavaDoc[] getItemVector(HSSFRow row) throws InfoException {
198     int dimCount = getReportSpec().getDimensionsByIndex().size();
199     int colNum = dimCount + getReportSpec().getMetricsByIndex().size();
200     Object JavaDoc[] collection = new Object JavaDoc[colNum];
201     //for(int i = row.getFirstCellNum() ; i < row.getLastCellNum() ; i++) {
202
int j = initialPosition.getColumn();
203     for (int i = 0; i < (colNum); i++) {
204       if(i < dimCount){
205         //Es dimension
206
ReportDimensionSpec dimension = getReportSpec().getDimensionFromIndex(i);
207         if(dimension.getCalculated()){
208           collection[i] = dimension.getValue(collection, getReportDataSourceSpec());
209         }else{
210           HSSFCell cell = row.getCell((short)j);
211           collection[i] = getValueForDimension(getValueFromCell(cell), dimension, collection, i);
212           j++;
213         }
214       }else{
215         //Es metrica
216
ReportMetricSpec metric = getReportSpec().getMetricFromIndex(i - dimCount);
217         if(metric.getCalculated()){
218           collection[i] = metric.getValue(collection);
219         }else{
220           HSSFCell cell = row.getCell((short)j);
221           collection[i] = getValueForMetric(getValueFromCell(cell), metric, collection, i);
222           j++;
223         }
224       }
225     }
226     return collection;
227   }
228
229   public int getFilterVarMode() {
230     return ReportFilterBuilder.VARMODE_DATAINDEX;
231   }
232
233   private Object JavaDoc getValueFromCell(HSSFCell cell) {
234     if (cell != null) {
235       switch(cell.getCellType()) {
236         case STRING:
237           return (cell.getStringCellValue());
238         case NUMERIC:
239           Double JavaDoc aDouble = new Double JavaDoc(cell.getNumericCellValue());
240           if ( aDouble.doubleValue() == aDouble.intValue()) {
241             return (new Integer JavaDoc(aDouble.intValue()));
242           } else {
243             return (aDouble);
244           }
245         default:
246           return ("");
247       }
248     }else{
249       return ("");
250     }
251   }
252
253 }
254
Popular Tags