1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 package com.sun.star.wizards.db; 24 25 import com.sun.star.lang.XMultiServiceFactory; 26 import com.sun.star.container.XNameAccess; 27 import com.sun.star.lang.XComponent; 28 import com.sun.star.sdbc.DataType; 29 import com.sun.star.sdbcx.XColumnsSupplier; 30 import com.sun.star.uno.Any; 31 import com.sun.star.uno.AnyConverter; 32 import com.sun.star.uno.Exception; 33 import com.sun.star.uno.UnoRuntime; 34 import com.sun.star.uno.XInterface; 35 import com.sun.star.wizards.common.Helper; 36 import com.sun.star.sdb.XCompletedExecution; 37 import com.sun.star.lang.Locale; 38 import com.sun.star.wizards.common.InvalidQueryException; 39 import com.sun.star.wizards.common.JavaTools; 40 import com.sun.star.wizards.common.NumberFormatter; 41 import com.sun.star.sdbc.XResultSet; 42 import com.sun.star.task.XInteractionHandler; 43 import com.sun.star.wizards.common.PropertyNames; 44 45 public class RecordParser extends QueryMetaData 46 { 47 48 XNameAccess xColumns; 49 com.sun.star.sdbc.XRow xRow; 50 com.sun.star.sdbc.XRow xResultSetRow; 51 public XResultSet ResultSet; 52 XInterface xRowSet; 53 XCompletedExecution xExecute; 54 XColumnsSupplier xRowSetColumnsSupplier; 55 XComponent xRowSetComponent; 56 XInteractionHandler xInteraction; 57 public FieldColumn[] GroupFieldColumns; 58 public FieldColumn[] RecordFieldColumns; 59 RecordParser(XMultiServiceFactory _xMSF, Locale _aLocale, NumberFormatter _oNumberFormatter)60 public RecordParser(XMultiServiceFactory _xMSF, Locale _aLocale, NumberFormatter _oNumberFormatter) 61 { 62 super(_xMSF, _aLocale, _oNumberFormatter); 63 getInterfaces(); 64 } 65 66 /** Creates a new instance of RecordParser */ RecordParser(XMultiServiceFactory _xMSF)67 public RecordParser(XMultiServiceFactory _xMSF) 68 { 69 super(_xMSF); 70 getInterfaces(); 71 } 72 getInterfaces()73 private void getInterfaces() 74 { 75 try 76 { 77 xRowSet = (XInterface) xMSF.createInstance("com.sun.star.sdb.RowSet"); 78 xRowSetColumnsSupplier = UnoRuntime.queryInterface(XColumnsSupplier.class, xRowSet); 79 xRowSetComponent = UnoRuntime.queryInterface(XComponent.class, xRowSet); 80 xExecute = UnoRuntime.queryInterface(XCompletedExecution.class, xRowSet); 81 XInterface oInteraction = (XInterface) xMSF.createInstance("com.sun.star.task.InteractionHandler"); 82 xInteraction = UnoRuntime.queryInterface(XInteractionHandler.class, oInteraction); 83 } 84 catch (Exception exception) 85 { 86 exception.printStackTrace(System.out); 87 } 88 } 89 getColumnStringValue(int ColIndex)90 private Object getColumnStringValue(int ColIndex) 91 { 92 try 93 { 94 String sValue = xResultSetRow.getString(ColIndex); //??? 95 return AnyConverter.toObject(String.class, sValue); 96 } 97 catch (Exception exception) 98 { 99 exception.printStackTrace(System.out); 100 return null; 101 } 102 } 103 getColumnDoubleValue(int ColIndex, boolean bisDate)104 private Object getColumnDoubleValue(int ColIndex, boolean bisDate) 105 { 106 try 107 { 108 Double DblValue; 109 if (bisDate) 110 { 111 DblValue = new Double(xResultSetRow.getDouble(ColIndex) + (double) super.getNullDateCorrection()); 112 } 113 else 114 { 115 DblValue = new Double(xResultSetRow.getDouble(ColIndex)); 116 } 117 if (!xResultSetRow.wasNull()) 118 { 119 return DblValue; 120 } 121 } 122 catch (Exception exception) 123 { 124 exception.printStackTrace(System.out); 125 } 126 return Any.VOID; 127 } 128 getColumnValue(int ColIndex, int iType)129 public Object getColumnValue(int ColIndex, int iType) 130 { 131 boolean bResult; 132 Object oAny = Any.VOID; 133 switch (iType) 134 { 135 case DataType.BIT: // == -7; 136 case DataType.BOOLEAN: 137 oAny = getColumnDoubleValue(ColIndex, false); 138 break; 139 140 case DataType.TINYINT: // == -6; 141 oAny = getColumnDoubleValue(ColIndex, false); 142 break; 143 144 case DataType.BIGINT: // == -5; 145 oAny = getColumnDoubleValue(ColIndex, false); 146 break; 147 148 case DataType.CHAR: // == 1; 149 oAny = getColumnStringValue(ColIndex); 150 break; 151 152 case DataType.VARCHAR: // == 12; 153 oAny = getColumnStringValue(ColIndex); 154 // double sResult = xResultSetRow.getClob(ColIndex); //??? 155 break; 156 157 case DataType.LONGVARCHAR: // == -1; 158 oAny = getColumnStringValue(ColIndex); 159 // double sResult = xResultSetRow.getCharacterStream(ColIndex); 160 break; 161 162 case DataType.NUMERIC: // == 2; 163 oAny = getColumnDoubleValue(ColIndex, false); 164 break; 165 166 case DataType.INTEGER: // == 4; 167 oAny = getColumnDoubleValue(ColIndex, false); 168 break; 169 170 case DataType.SMALLINT: // == 5; 171 oAny = getColumnDoubleValue(ColIndex, false); 172 break; 173 174 case DataType.DECIMAL: // == 3; [mit Nachkommastellen] 175 oAny = getColumnDoubleValue(ColIndex, false); 176 break; 177 178 case DataType.FLOAT: // == 6; 179 oAny = getColumnDoubleValue(ColIndex, false); 180 break; 181 182 case DataType.REAL: // == 7; 183 oAny = getColumnDoubleValue(ColIndex, false); 184 break; 185 186 case DataType.DOUBLE: // == 8; 187 oAny = getColumnDoubleValue(ColIndex, false); 188 break; 189 190 case DataType.DATE: // == 91; 191 oAny = getColumnDoubleValue(ColIndex, true); 192 break; 193 194 case DataType.TIME: // == 92; 195 oAny = getColumnDoubleValue(ColIndex, false); 196 break; 197 198 case DataType.TIMESTAMP: // == 93; 199 oAny = getColumnDoubleValue(ColIndex, true); 200 break; 201 } 202 203 // Is the index OK; increment? 204 // String sValue = xResultSetRow.getString(ColIndex); 205 return oAny; 206 } 207 executeCommand(int _nCommandType)208 public boolean executeCommand(int _nCommandType) throws InvalidQueryException 209 { 210 try 211 { 212 Helper.setUnoPropertyValue(xRowSet, "DataSourceName", DataSourceName); 213 Helper.setUnoPropertyValue(xRowSet, PropertyNames.ACTIVE_CONNECTION, DBConnection); 214 Helper.setUnoPropertyValue(xRowSet, PropertyNames.COMMAND, Command); 215 Helper.setUnoPropertyValue(xRowSet, PropertyNames.COMMAND_TYPE, new Integer(_nCommandType)); // CommandType 216 xExecute.executeWithCompletion(xInteraction); 217 com.sun.star.sdb.XResultSetAccess xResultAccess = UnoRuntime.queryInterface(com.sun.star.sdb.XResultSetAccess.class, xRowSet); 218 ResultSet = xResultAccess.createResultSet(); 219 xResultSetRow = UnoRuntime.queryInterface(com.sun.star.sdbc.XRow.class, ResultSet); 220 XColumnsSupplier xDBCols = UnoRuntime.queryInterface(XColumnsSupplier.class, ResultSet); 221 xColumns = xDBCols.getColumns(); 222 setCommandType(_nCommandType); 223 return true; 224 } 225 catch (Exception exception) 226 { 227 exception.printStackTrace(System.out); 228 throw new InvalidQueryException(xMSF, Command); 229 } 230 } 231 getFields(String[] _sFieldNames, boolean binitializeDBColumns)232 public boolean getFields(String[] _sFieldNames, boolean binitializeDBColumns) 233 { 234 try 235 { 236 if (binitializeDBColumns) 237 { 238 initializeFieldColumns(_sFieldNames, xColumns); 239 } 240 String[] AllQueryFieldNames = xColumns.getElementNames(); 241 String[] sFieldNames = getFieldNames(); 242 for (int i = 0; i < FieldColumns.length; i++) 243 { 244 String sFieldName = FieldColumns[i].getFieldName(); 245 int nColIndex = JavaTools.FieldInList(AllQueryFieldNames, sFieldName) + 1; 246 FieldColumns[i].ColIndex = nColIndex; 247 if (nColIndex == -1) 248 { 249 throw new InvalidQueryException(xMSF, Command); 250 } 251 } 252 GroupFieldColumns = getFieldColumnList(GroupFieldNames); 253 RecordFieldColumns = getFieldColumnList(getRecordFieldNames()); 254 return true; 255 } 256 catch (InvalidQueryException queryexception) 257 { 258 queryexception.printStackTrace(System.out); 259 return false; 260 } 261 } 262 getFieldColumnList(String[] _FieldNames)263 private FieldColumn[] getFieldColumnList(String[] _FieldNames) 264 { 265 FieldColumn[] LocFieldColumns = new FieldColumn[_FieldNames.length]; 266 for (int i = 0; i < _FieldNames.length; i++) 267 { 268 LocFieldColumns[i] = super.getFieldColumnByFieldName(_FieldNames[i]); 269 } 270 return LocFieldColumns; 271 } 272 getGroupColumnValue(int ColIndex)273 public Object getGroupColumnValue(int ColIndex) 274 { 275 FieldColumn CurDBFieldColumn = this.GroupFieldColumns[ColIndex]; 276 return getColumnValue(CurDBFieldColumn.ColIndex, CurDBFieldColumn.getFieldType()); 277 } 278 getcurrentRecordData(java.util.Vector DataVector)279 public boolean getcurrentRecordData(java.util.Vector DataVector) 280 { 281 Object[] RecordValueArray = new Object[RecordFieldColumns.length]; 282 for (int i = 0; i < RecordFieldColumns.length; i++) 283 { 284 FieldColumn CurDBFieldColumn = this.RecordFieldColumns[i]; 285 RecordValueArray[i] = getColumnValue(CurDBFieldColumn.ColIndex, CurDBFieldColumn.getFieldType()); //FinalColIndex 286 } 287 DataVector.addElement(RecordValueArray); 288 return true; 289 } 290 dispose()291 public void dispose() 292 { 293 if (xRowSetComponent != null) 294 { 295 xRowSetComponent.dispose(); 296 } 297 super.dispose(); 298 } 299 } 300