001: package com.calipso.reportgenerator.reportmanager;
002:
003: import com.calipso.reportgenerator.reportcalculator.IDataSource;
004: import com.calipso.reportgenerator.common.InfoException;
005: import java.io.*;
006: import java.util.ArrayList;
007: import java.util.List;
008: import java.util.Vector;
009: import java.util.Enumeration;
010:
011: import org.apache.poi.hssf.usermodel.HSSFCell;
012: import org.apache.poi.hssf.usermodel.HSSFRow;
013: import org.apache.poi.hssf.usermodel.HSSFSheet;
014: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
015: import com.calipso.reportgenerator.common.*;
016: import com.calipso.reportgenerator.reportcalculator.*;
017: import com.calipso.reportgenerator.reportdefinitions.types.ReportDataType;
018: import com.calipso.common.DateEx;
019:
020: /**
021: * Representa un origen de datos Excel
022: */
023:
024: public class ExcelReportDataSource extends ReportDataSource {
025:
026: private static final int STRING = 1;
027: private static final int NUMERIC = 0;
028: private IDataSource dataSource;
029: private org.apache.poi.poifs.filesystem.POIFSFileSystem fileSystem;
030: private String sheetName;
031: private String dataInitialCell;
032: private String dataEndingCell;
033: private ExcelSheetPosition initialPosition;
034: private ExcelSheetPosition endingPosition;
035: private int dimensionsCount;
036:
037: /**
038: * Inicializa una instancia de <code>ExcelReportDataSource</code>
039: * @param reportSpec
040: * @param reportDataSourceSpec
041: */
042: public ExcelReportDataSource(ReportSpec reportSpec,
043: ReportDataSourceSpec reportDataSourceSpec,
044: ReportGeneratorConfiguration configuration)
045: throws InfoException {
046: super (reportSpec, reportDataSourceSpec);
047: setGeneratorConfiguration(configuration);
048: //dimensionsCount = reportSpec.getNotCalculatedDimensions().size();
049: File file = new File(reportDataSourceSpec.getExpression());
050: sheetName = reportDataSourceSpec.getSheetName();
051: dataEndingCell = reportDataSourceSpec.getDataEndingCell();
052: dataInitialCell = reportDataSourceSpec.getDataInitialCell();
053: try {
054: InputStream inputStream = new FileInputStream(file);
055: fileSystem = new org.apache.poi.poifs.filesystem.POIFSFileSystem(
056: inputStream);
057: } catch (Exception e) {
058: throw new InfoException(LanguageTraslator.traslate("473"),
059: e);
060: }
061: }
062:
063: public IDataSource getDataSource(Matrix matrix)
064: throws InfoException {
065: /*if (dataSource == null) {
066: dataSource = newDataSource();*/
067: fillFromFile(matrix);
068: /*}*/
069: return dataSource;
070: }
071:
072: private void fillFromFile(Matrix matrix) throws InfoException {
073: try {
074: HSSFWorkbook wb = new HSSFWorkbook(fileSystem);
075: HSSFSheet sheet;
076: if (sheetName != null && !sheetName.equalsIgnoreCase("")) {
077: sheet = wb.getSheet(sheetName);
078: } else {
079: sheet = wb.getSheetAt(0);
080: }
081: createInitialPositions(sheet);
082: //for(int i = sheet.getFirstRowNum() ; i <= sheet.getLastRowNum() ; i++) {
083: for (int i = initialPosition.getRow(); i <= endingPosition
084: .getRow(); i++) {
085: /*List dataSourceRow = new ArrayList();
086: fillDataSourceRowFrom(getItemVector(sheet.getRow(i)), dataSourceRow);*/
087: Object[] row = getItemVector(sheet.getRow(i));
088: try {
089: if ((getFilter() == null)
090: || ((getFilter() != null) && (getFilter()
091: .matches(row)))) {
092: //dataSource.addRow(dataSourceRow);
093: matrix.add(row);
094: }
095: } catch (InfoException e) {
096: throw new InfoException(LanguageTraslator
097: .traslate("386"), e);
098: }
099: }
100: } catch (Exception e) {
101: throw new InfoException(LanguageTraslator.traslate("316"),
102: e);
103: } catch (OutOfMemoryError e) {
104: throw new InfoException(LanguageTraslator.traslate("326"),
105: e);
106: }
107: }
108:
109: private void createInitialPositions(HSSFSheet sheet)
110: throws InfoException {
111: if (dataInitialCell == null
112: || dataInitialCell.equalsIgnoreCase("")) {
113: int initialRow = sheet.getFirstRowNum();
114: short initialColumn = sheet.getRow(initialRow)
115: .getFirstCellNum();
116: initialPosition = new ExcelSheetPosition(initialRow,
117: initialColumn);
118: } else {
119: initialPosition = new ExcelSheetPosition(
120: getDataInitialCell());
121: initialPosition.setRow(initialPosition.getRow() - 1);
122: }
123: if (dataEndingCell == null
124: || dataEndingCell.equalsIgnoreCase("")) {
125: int finalRow = sheet.getLastRowNum();
126: short finalColumn = (short) (sheet.getRow(finalRow)
127: .getLastCellNum() - 1);
128: endingPosition = new ExcelSheetPosition(finalRow,
129: finalColumn);
130: } else {
131: endingPosition = new ExcelSheetPosition(getDataEndingCell());
132: endingPosition.setRow(endingPosition.getRow() - 1);
133: }
134: }
135:
136: private String getDataEndingCell() {
137: return dataEndingCell;
138: }
139:
140: private String getDataInitialCell() {
141: return dataInitialCell;
142: }
143:
144: /* private void fillDataSourceRowFrom(Vector itemVector, List dataSourceRow) throws InfoException{
145: int i = 0;
146: //int size = getReportSpec().getDataSourceIndexes().size();
147: int dimensionSize = getReportSpec().getNotCalculatedDimensions().size();
148: for(Enumeration enumeration = itemVector.elements() ; enumeration.hasMoreElements() ; i++) {
149: Object object = enumeration.nextElement();
150: if(i < dimensionSize) {
151: String name = getReportSpec().getDataSourceIndexNameByIndex(i);
152: ReportDimensionSpec dimensionSpec = getReportSpec().getDimensionFromName(name);
153: //ReportDimensionSpec dimensionSpec = (ReportDimensionSpec)getReportSpec().getDimensionsByIndex().get(i);
154: if(object instanceof String) {
155: fillDataSourceItemFrom((String)object, dataSourceRow, dimensionSpec, i);
156: } else if(object instanceof Double){
157: fillDataSourceItemFrom((Double)object, dataSourceRow, dimensionSpec, i);
158: } else if(object instanceof Integer){
159: fillDataSourceItemFrom((Integer)object, dataSourceRow, dimensionSpec, i);
160: }
161:
162: } else {
163: dataSourceRow.add(object.toString());
164: }
165: }
166: }
167:
168: private void fillDataSourceItemFrom(Number value, List dataSourceRow, ReportDimensionSpec dimensionSpec, int index) throws InfoException{
169: switch(dimensionSpec.getDataType().getType()){
170: case ReportDataType.DATETIME_TYPE:
171: case ReportDataType.DATE_TYPE:
172: dataSourceRow.add(index, SharedDate.newFrom(new DateEx(value, getReportDataSourceSpec().getPattern(dimensionSpec.getDataType().getType()))));
173: //DecimalFormat numberFormat = new DecimalFormat("########");
174: //Format numberFormat = new DecimalFormat("#####################");
175: //dataSourceRow.add(index, SharedDate.newFrom(new DateEx(numberFormat.format(value), getReportDataSourceSpec().getPattern(dimensionSpec.getDataType().getType()))));
176: break;
177: case ReportDataType.STRING_TYPE:
178: dataSourceRow.add(index, SharedString.newFrom(value.toString()));
179: break;
180: case ReportDataType.FLOAT_TYPE:
181: dataSourceRow.add(index, SharedFloat.newFrom(new Float(value.floatValue())));
182: break;
183: case ReportDataType.INTEGER_TYPE:
184: dataSourceRow.add(index, SharedInteger.newFrom(new Integer(value.intValue())));
185: break;
186: case ReportDataType.BOOLEAN_TYPE:
187: throw new InfoException("374");
188: }
189: }
190:
191:
192:
193:
194: private void fillDataSourceItemFrom(String strValue, List dataSourceRow, ReportDimensionSpec dimensionSpec, int index) throws InfoException{
195: switch(dimensionSpec.getDataType().getType()){
196: case ReportDataType.DATETIME_TYPE:
197: dataSourceRow.add(index, SharedDate.newFrom(new DateEx(strValue, getReportDataSourceSpec().getDateTimePattern())));
198: break;
199: case ReportDataType.DATE_TYPE:
200: dataSourceRow.add(index, SharedDate.newFrom(new DateEx(strValue, getReportDataSourceSpec().getDatePattern())));
201: break;
202: case ReportDataType.STRING_TYPE:
203: dataSourceRow.add(index, SharedString.newFrom(strValue));
204: break;
205: case ReportDataType.FLOAT_TYPE:
206: dataSourceRow.add(index, SharedFloat.newFrom(new Float(strValue)));
207: break;
208: case ReportDataType.INTEGER_TYPE:
209: dataSourceRow.add(index, SharedInteger.newFrom(new Integer(strValue)));
210: break;
211: case ReportDataType.BOOLEAN_TYPE:
212: dataSourceRow.add(index, Boolean.valueOf(strValue));
213: break;
214: }
215: }*/
216:
217: private Object[] getItemVector(HSSFRow row) throws InfoException {
218: int dimCount = getReportSpec().getDimensionsByIndex().size();
219: int colNum = dimCount
220: + getReportSpec().getMetricsByIndex().size();
221: Object[] collection = new Object[colNum];
222: //for(int i = row.getFirstCellNum() ; i < row.getLastCellNum() ; i++) {
223: int j = initialPosition.getColumn();
224: for (int i = 0; i < (colNum); i++) {
225: if (i < dimCount) {
226: //Es dimension
227: ReportDimensionSpec dimension = getReportSpec()
228: .getDimensionFromIndex(i);
229: if (dimension.getCalculated()) {
230: collection[i] = dimension.getValue(collection,
231: getReportDataSourceSpec());
232: } else {
233: HSSFCell cell = row.getCell((short) j);
234: collection[i] = getValueForDimension(
235: getValueFromCell(cell), dimension,
236: collection, i);
237: j++;
238: }
239: } else {
240: //Es metrica
241: ReportMetricSpec metric = getReportSpec()
242: .getMetricFromIndex(i - dimCount);
243: if (metric.getCalculated()) {
244: collection[i] = metric.getValue(collection);
245: } else {
246: HSSFCell cell = row.getCell((short) j);
247: collection[i] = getValueForMetric(
248: getValueFromCell(cell), metric, collection,
249: i);
250: j++;
251: }
252: }
253: }
254: return collection;
255: }
256:
257: public int getFilterVarMode() {
258: return ReportFilterBuilder.VARMODE_DATAINDEX;
259: }
260:
261: private Object getValueFromCell(HSSFCell cell) {
262: if (cell != null) {
263: switch (cell.getCellType()) {
264: case STRING:
265: return (cell.getStringCellValue());
266: case NUMERIC:
267: Double aDouble = new Double(cell.getNumericCellValue());
268: if (aDouble.doubleValue() == aDouble.intValue()) {
269: return (new Integer(aDouble.intValue()));
270: } else {
271: return (aDouble);
272: }
273: default:
274: return ("");
275: }
276: } else {
277: return ("");
278: }
279: }
280:
281: }
|