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 ; 19 import java.util.ArrayList ; 20 21 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 e) { 44 throw new InfoException(LanguageTraslator.traslate("275"),e); 45 } 46 } 47 48 private void fillDataSource(XSpreadsheet xSheet, Matrix matrix) throws IndexOutOfBoundsException , InfoException { 49 XCell xCell; 50 try { 51 for(int i = rowStart ; i < rowEnd ; i++) { 52 Object [] row = getRow(xSheet, i); 53 58 matrix.add(row); 59 } 60 } catch(OutOfMemoryError e) { 61 throw new InfoException(LanguageTraslator.traslate("326"), e); 62 } 63 } 64 65 private Object [] getRow(XSpreadsheet xSheet, int row) throws IndexOutOfBoundsException , InfoException { 66 int dimCount = getReportSpec().getDimensionsByIndex().size(); 67 int colNum = dimCount + getReportSpec().getMetricsByIndex().size(); 68 Object [] collection = new Object [colNum]; 69 int j = columnStart; 71 for (int i = 0; i < (colNum); i++) { 72 if(i < dimCount){ 73 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 value = xCellText.getString(); 81 collection[i] = getValueForDimension(value, dimension, collection, i); 82 j++; 83 } 84 }else{ 85 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 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 , 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 { 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 { 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 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 (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 e){ 152 throw new InfoException(LanguageTraslator.traslate("274"),e); 153 } 154 } 155 156 public IDataSource getDataSource(Matrix matrix) throws InfoException { 157 loadFromOOCalc(matrix); 160 return dataSource; 162 } 163 164 public int getFilterVarMode() { 165 return ReportFilterBuilder.VARMODE_DATAINDEX; 166 } 167 } 168 | Popular Tags |