001: package com.calipso.reportgenerator.reportmanager;
002:
003: import com.calipso.reportgenerator.reportcalculator.IDataSource;
004: import com.calipso.reportgenerator.reportcalculator.Matrix;
005: import com.calipso.reportgenerator.common.InfoException;
006: import com.sun.star.sheet.*;
007: import com.sun.star.uno.Exception;
008: import com.sun.star.uno.UnoRuntime;
009: import com.sun.star.frame.XComponentLoader;
010: import com.sun.star.beans.PropertyValue;
011: import com.sun.star.lang.XComponent;
012: import com.sun.star.lang.IndexOutOfBoundsException;
013: import com.sun.star.lang.WrappedTargetException;
014: import com.sun.star.container.XIndexAccess;
015: import com.sun.star.table.*;
016: import com.sun.star.text.XText;
017: import com.calipso.reportgenerator.common.*;
018: import java.util.List;
019: import java.util.ArrayList;
020:
021: /**
022: * Resuelve un data source desde la clanilla de cálculo de Open Office
023: */
024:
025: public class OOCalcReportDataSource extends ReportDataSource {
026:
027: private OOConnectionManager connectionManager;
028: private IDataSource dataSource;
029: private int rowStart, columnStart, rowEnd, columnEnd;
030:
031: public OOCalcReportDataSource(ReportSpec reportSpec,
032: ReportDataSourceSpec dataSourceSpec,
033: ReportGeneratorConfiguration managerConfiguration) {
034: super (reportSpec, dataSourceSpec);
035: super .setGeneratorConfiguration(managerConfiguration);
036: connectionManager = new OOConnectionManager(
037: "uno:socket,host=223.255.255.147,port=8100;urp;StarOffice.ServiceManager");
038: }
039:
040: private void loadFromOOCalc(Matrix matrix) throws InfoException {
041: try {
042: XSpreadsheetDocument xSpreadsheetDocument = loadFile();
043: XSpreadsheet xSheet = getWorkingSheet(xSpreadsheetDocument);
044: setEndPoint(xSheet);
045: fillDataSource(xSheet, matrix);
046: } catch (Exception e) {
047: throw new InfoException(LanguageTraslator.traslate("275"),
048: e);
049: }
050: }
051:
052: private void fillDataSource(XSpreadsheet xSheet, Matrix matrix)
053: throws IndexOutOfBoundsException, InfoException {
054: XCell xCell;
055: try {
056: for (int i = rowStart; i < rowEnd; i++) {
057: Object[] row = getRow(xSheet, i);
058: /*for( int j = columnStart ; j < columnEnd ; j++) {
059: xCell = xSheet.getCellByPosition(j, i);
060: XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
061: row.add(xCellText.getString());
062: }*/
063: matrix.add(row);
064: }
065: } catch (OutOfMemoryError e) {
066: throw new InfoException(LanguageTraslator.traslate("326"),
067: e);
068: }
069: }
070:
071: private Object[] getRow(XSpreadsheet xSheet, int row)
072: throws IndexOutOfBoundsException, InfoException {
073: int dimCount = getReportSpec().getDimensionsByIndex().size();
074: int colNum = dimCount
075: + getReportSpec().getMetricsByIndex().size();
076: Object[] collection = new Object[colNum];
077: //for(int i = row.getFirstCellNum() ; i < row.getLastCellNum() ; i++) {
078: int j = columnStart;
079: for (int i = 0; i < (colNum); i++) {
080: if (i < dimCount) {
081: //Es dimension
082: ReportDimensionSpec dimension = getReportSpec()
083: .getDimensionFromIndex(i);
084: if (dimension.getCalculated()) {
085: collection[i] = dimension.getValue(collection,
086: getReportDataSourceSpec());
087: } else {
088: XCell cell = xSheet.getCellByPosition(row, j);
089: XText xCellText = (XText) UnoRuntime
090: .queryInterface(XText.class, cell);
091: Object value = xCellText.getString();
092: collection[i] = getValueForDimension(value,
093: dimension, collection, i);
094: j++;
095: }
096: } else {
097: //Es metrica
098: ReportMetricSpec metric = getReportSpec()
099: .getMetricFromIndex(i - dimCount);
100: if (metric.getCalculated()) {
101: collection[i] = metric.getValue(collection);
102: } else {
103: XCell cell = xSheet.getCellByPosition(row, j);
104: XText xCellText = (XText) UnoRuntime
105: .queryInterface(XText.class, cell);
106: Object value = xCellText.getString();
107: collection[i] = getValueForMetric(value, metric,
108: collection, i);
109: j++;
110: }
111: }
112: }
113: return collection;
114:
115: }
116:
117: private XSpreadsheet getWorkingSheet(
118: XSpreadsheetDocument xSpreadsheetDocument)
119: throws IndexOutOfBoundsException, WrappedTargetException {
120: XSpreadsheets xSheets = xSpreadsheetDocument.getSheets();
121: XIndexAccess xSIndexAccess = (XIndexAccess) UnoRuntime
122: .queryInterface(XIndexAccess.class, xSheets);
123: return (XSpreadsheet) xSIndexAccess.getByIndex(0);
124: }
125:
126: private void setEndPoint(XSpreadsheet xSheet)
127: throws IndexOutOfBoundsException {
128: rowEnd = getEndCoordinate(xSheet, rowStart, columnStart, true);
129: columnEnd = getEndCoordinate(xSheet, columnStart, rowStart,
130: false);
131: }
132:
133: private int getEndCoordinate(XSpreadsheet xSheet, int num1,
134: int num2, boolean row) throws IndexOutOfBoundsException {
135: int point = 0;
136: for (int i = num1;; i++) {
137: for (int j = num2;; j++) {
138: XCell xCell;
139: XText xCellText;
140: if (row) {
141: xCell = xSheet.getCellByPosition(i, j);
142: xCellText = (XText) UnoRuntime.queryInterface(
143: XText.class, xCell);
144: } else {
145: xCell = xSheet.getCellByPosition(j, i);
146: xCellText = (XText) UnoRuntime.queryInterface(
147: XText.class, xCell);
148: }
149: if (xCellText.getString().equals("")) {
150: point = j;
151: break;
152: }
153: }
154: break;
155: }
156: return point;
157: }
158:
159: private XSpreadsheetDocument loadFile() throws InfoException {
160: try {
161: Object desktop = connectionManager.getConnection()
162: .getServiceManager().createInstanceWithContext(
163: "com.sun.star.frame.Desktop",
164: connectionManager.getConnection());
165: XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime
166: .queryInterface(XComponentLoader.class, desktop);
167: PropertyValue[] loadProps = new PropertyValue[1];
168: loadProps[0] = new PropertyValue();
169: loadProps[0].Name = "Hidden";
170: loadProps[0].Value = new Boolean(true);
171: XComponent xComponent = xComponentLoader
172: .loadComponentFromURL(
173: "file:///sourcefiles/datasources/VENTAS_CLIENTES.sxc",
174: "_blank", 0, loadProps);
175: XSpreadsheetDocument spreadsheetDocument = (XSpreadsheetDocument) UnoRuntime
176: .queryInterface(XSpreadsheetDocument.class,
177: xComponent);
178: return spreadsheetDocument;
179: } catch (Exception e) {
180: throw new InfoException(LanguageTraslator.traslate("274"),
181: e);
182: }
183: }
184:
185: public IDataSource getDataSource(Matrix matrix)
186: throws InfoException {
187: //if (dataSource == null) {
188: //dataSource = newDataSource();
189: loadFromOOCalc(matrix);
190: //}
191: return dataSource;
192: }
193:
194: public int getFilterVarMode() {
195: return ReportFilterBuilder.VARMODE_DATAINDEX;
196: }
197: }
|