KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.calipso.reportgenerator.reportmanager;
2
3 import com.calipso.reportgenerator.reportcalculator.IDataSource;
4 import com.calipso.reportgenerator.reportcalculator.Matrix;
5 import com.calipso.reportgenerator.common.InfoException;
6 import com.sun.star.sheet.*;
7 import com.sun.star.uno.Exception;
8 import com.sun.star.uno.UnoRuntime;
9 import com.sun.star.frame.XComponentLoader;
10 import com.sun.star.beans.PropertyValue;
11 import com.sun.star.lang.XComponent;
12 import com.sun.star.lang.IndexOutOfBoundsException;
13 import com.sun.star.lang.WrappedTargetException;
14 import com.sun.star.container.XIndexAccess;
15 import com.sun.star.table.*;
16 import com.sun.star.text.XText;
17 import com.calipso.reportgenerator.common.*;
18 import java.util.List JavaDoc;
19 import java.util.ArrayList JavaDoc;
20
21 /**
22  * Resuelve un data source desde la clanilla de cálculo de Open Office
23  */

24
25 public class OOCalcReportDataSource extends ReportDataSource {
26
27   private OOConnectionManager connectionManager;
28   private IDataSource dataSource;
29   private int rowStart, columnStart, rowEnd, columnEnd;
30
31   public OOCalcReportDataSource(ReportSpec reportSpec, ReportDataSourceSpec dataSourceSpec, ReportGeneratorConfiguration managerConfiguration) {
32     super(reportSpec, dataSourceSpec);
33     super.setGeneratorConfiguration(managerConfiguration);
34     connectionManager = new OOConnectionManager("uno:socket,host=223.255.255.147,port=8100;urp;StarOffice.ServiceManager");
35   }
36
37   private void loadFromOOCalc(Matrix matrix) throws InfoException {
38     try {
39       XSpreadsheetDocument xSpreadsheetDocument = loadFile();
40       XSpreadsheet xSheet = getWorkingSheet(xSpreadsheetDocument);
41       setEndPoint(xSheet);
42       fillDataSource(xSheet, matrix);
43     } catch (Exception JavaDoc e) {
44       throw new InfoException(LanguageTraslator.traslate("275"),e);
45     }
46   }
47
48   private void fillDataSource(XSpreadsheet xSheet, Matrix matrix) throws IndexOutOfBoundsException JavaDoc, InfoException {
49     XCell xCell;
50     try {
51       for(int i = rowStart ; i < rowEnd ; i++) {
52         Object JavaDoc[] row = getRow(xSheet, i);
53         /*for( int j = columnStart ; j < columnEnd ; j++) {
54           xCell = xSheet.getCellByPosition(j, i);
55           XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
56           row.add(xCellText.getString());
57         }*/

58         matrix.add(row);
59       }
60     } catch(OutOfMemoryError JavaDoc e) {
61       throw new InfoException(LanguageTraslator.traslate("326"), e);
62     }
63   }
64
65   private Object JavaDoc[] getRow(XSpreadsheet xSheet, int row) throws IndexOutOfBoundsException JavaDoc, InfoException {
66     int dimCount = getReportSpec().getDimensionsByIndex().size();
67     int colNum = dimCount + getReportSpec().getMetricsByIndex().size();
68     Object JavaDoc[] collection = new Object JavaDoc[colNum];
69     //for(int i = row.getFirstCellNum() ; i < row.getLastCellNum() ; i++) {
70
int j = columnStart;
71     for (int i = 0; i < (colNum); i++) {
72       if(i < dimCount){
73         //Es dimension
74
ReportDimensionSpec dimension = getReportSpec().getDimensionFromIndex(i);
75         if(dimension.getCalculated()){
76           collection[i] = dimension.getValue(collection, getReportDataSourceSpec());
77         }else{
78           XCell cell = xSheet.getCellByPosition(row, j);
79           XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, cell);
80           Object JavaDoc value = xCellText.getString();
81           collection[i] = getValueForDimension(value, dimension, collection, i);
82           j++;
83         }
84       }else{
85         //Es metrica
86
ReportMetricSpec metric = getReportSpec().getMetricFromIndex(i - dimCount);
87         if(metric.getCalculated()){
88           collection[i] = metric.getValue(collection);
89         }else{
90           XCell cell = xSheet.getCellByPosition(row, j);
91           XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, cell);
92           Object JavaDoc value = xCellText.getString();
93           collection[i] = getValueForMetric(value, metric, collection, i);
94           j++;
95         }
96       }
97     }
98     return collection;
99
100   }
101
102   private XSpreadsheet getWorkingSheet(XSpreadsheetDocument xSpreadsheetDocument) throws IndexOutOfBoundsException JavaDoc, WrappedTargetException {
103     XSpreadsheets xSheets = xSpreadsheetDocument.getSheets();
104     XIndexAccess xSIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xSheets);
105     return (XSpreadsheet) xSIndexAccess.getByIndex(0);
106   }
107
108   private void setEndPoint(XSpreadsheet xSheet) throws IndexOutOfBoundsException JavaDoc {
109     rowEnd = getEndCoordinate(xSheet, rowStart, columnStart, true);
110     columnEnd = getEndCoordinate(xSheet, columnStart, rowStart, false);
111   }
112
113   private int getEndCoordinate(XSpreadsheet xSheet, int num1, int num2, boolean row) throws IndexOutOfBoundsException JavaDoc {
114     int point = 0;
115     for(int i = num1 ; ; i++) {
116       for(int j = num2 ; ; j++) {
117         XCell xCell;
118         XText xCellText;
119         if(row) {
120           xCell = xSheet.getCellByPosition(i, j);
121           xCellText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
122         } else {
123           xCell = xSheet.getCellByPosition(j, i);
124           xCellText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
125         }
126         if(xCellText.getString().equals("")) {
127           point = j;
128           break;
129         }
130       }
131       break;
132     }
133     return point;
134   }
135
136
137   private XSpreadsheetDocument loadFile() throws InfoException {
138     try {
139       Object JavaDoc desktop = connectionManager.getConnection().getServiceManager().createInstanceWithContext(
140         "com.sun.star.frame.Desktop", connectionManager.getConnection());
141       XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(
142         XComponentLoader.class, desktop);
143       PropertyValue [] loadProps = new PropertyValue[1];
144       loadProps[0] = new PropertyValue();
145       loadProps[0].Name = "Hidden";
146       loadProps[0].Value = new Boolean JavaDoc(true);
147       XComponent xComponent = xComponentLoader.loadComponentFromURL("file:///sourcefiles/datasources/VENTAS_CLIENTES.sxc","_blank", 0, loadProps);
148       XSpreadsheetDocument spreadsheetDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(
149         XSpreadsheetDocument.class, xComponent);
150       return spreadsheetDocument;
151     }catch(Exception JavaDoc e){
152       throw new InfoException(LanguageTraslator.traslate("274"),e);
153     }
154   }
155
156   public IDataSource getDataSource(Matrix matrix) throws InfoException {
157     //if (dataSource == null) {
158
//dataSource = newDataSource();
159
loadFromOOCalc(matrix);
160       //}
161
return dataSource;
162   }
163
164   public int getFilterVarMode() {
165     return ReportFilterBuilder.VARMODE_DATAINDEX;
166   }
167 }
168
Popular Tags