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 com.sun.star.uno.Type CurType; 95 Object oAny; 96 String sValue = xResultSetRow.getString(ColIndex); //??? 97 CurType = new com.sun.star.uno.Type(String.class); 98 oAny = AnyConverter.toObject(CurType, sValue); 99 return oAny; 100 } 101 catch (Exception exception) 102 { 103 exception.printStackTrace(System.out); 104 return null; 105 } 106 } 107 getColumnDoubleValue(int ColIndex, boolean bisDate)108 private Object getColumnDoubleValue(int ColIndex, boolean bisDate) 109 { 110 try 111 { 112 Double DblValue; 113 if (bisDate) 114 { 115 DblValue = new Double(xResultSetRow.getDouble(ColIndex) + (double) super.getNullDateCorrection()); 116 } 117 else 118 { 119 DblValue = new Double(xResultSetRow.getDouble(ColIndex)); 120 } 121 if (!xResultSetRow.wasNull()) 122 { 123 return DblValue; 124 } 125 } 126 catch (Exception exception) 127 { 128 exception.printStackTrace(System.out); 129 } 130 return Any.VOID; 131 } 132 getColumnValue(int ColIndex, int iType)133 public Object getColumnValue(int ColIndex, int iType) 134 { 135 boolean bResult; 136 Object oAny = Any.VOID; 137 switch (iType) 138 { 139 case DataType.BIT: // == -7; 140 case DataType.BOOLEAN: 141 oAny = getColumnDoubleValue(ColIndex, false); 142 break; 143 144 case DataType.TINYINT: // == -6; 145 oAny = getColumnDoubleValue(ColIndex, false); 146 break; 147 148 case DataType.BIGINT: // == -5; 149 oAny = getColumnDoubleValue(ColIndex, false); 150 break; 151 152 case DataType.CHAR: // == 1; 153 oAny = getColumnStringValue(ColIndex); 154 break; 155 156 case DataType.VARCHAR: // == 12; 157 oAny = getColumnStringValue(ColIndex); 158 // double sResult = xResultSetRow.getClob(ColIndex); //??? 159 break; 160 161 case DataType.LONGVARCHAR: // == -1; 162 oAny = getColumnStringValue(ColIndex); 163 // double sResult = xResultSetRow.getCharacterStream(ColIndex); 164 break; 165 166 case DataType.NUMERIC: // == 2; 167 oAny = getColumnDoubleValue(ColIndex, false); 168 break; 169 170 case DataType.INTEGER: // == 4; 171 oAny = getColumnDoubleValue(ColIndex, false); 172 break; 173 174 case DataType.SMALLINT: // == 5; 175 oAny = getColumnDoubleValue(ColIndex, false); 176 break; 177 178 case DataType.DECIMAL: // == 3; [mit Nachkommastellen] 179 oAny = getColumnDoubleValue(ColIndex, false); 180 break; 181 182 case DataType.FLOAT: // == 6; 183 oAny = getColumnDoubleValue(ColIndex, false); 184 break; 185 186 case DataType.REAL: // == 7; 187 oAny = getColumnDoubleValue(ColIndex, false); 188 break; 189 190 case DataType.DOUBLE: // == 8; 191 oAny = getColumnDoubleValue(ColIndex, false); 192 break; 193 194 case DataType.DATE: // == 91; 195 oAny = getColumnDoubleValue(ColIndex, true); 196 break; 197 198 case DataType.TIME: // == 92; 199 oAny = getColumnDoubleValue(ColIndex, false); 200 break; 201 202 case DataType.TIMESTAMP: // == 93; 203 oAny = getColumnDoubleValue(ColIndex, true); 204 break; 205 } 206 207 // Is the index OK; increment? 208 // String sValue = xResultSetRow.getString(ColIndex); 209 return oAny; 210 } 211 executeCommand(int _nCommandType)212 public boolean executeCommand(int _nCommandType) throws InvalidQueryException 213 { 214 try 215 { 216 Helper.setUnoPropertyValue(xRowSet, "DataSourceName", DataSourceName); 217 Helper.setUnoPropertyValue(xRowSet, PropertyNames.ACTIVE_CONNECTION, DBConnection); 218 Helper.setUnoPropertyValue(xRowSet, PropertyNames.COMMAND, Command); 219 Helper.setUnoPropertyValue(xRowSet, PropertyNames.COMMAND_TYPE, new Integer(_nCommandType)); // CommandType 220 xExecute.executeWithCompletion(xInteraction); 221 com.sun.star.sdb.XResultSetAccess xResultAccess = UnoRuntime.queryInterface(com.sun.star.sdb.XResultSetAccess.class, xRowSet); 222 ResultSet = xResultAccess.createResultSet(); 223 xResultSetRow = UnoRuntime.queryInterface(com.sun.star.sdbc.XRow.class, ResultSet); 224 XColumnsSupplier xDBCols = UnoRuntime.queryInterface(XColumnsSupplier.class, ResultSet); 225 xColumns = xDBCols.getColumns(); 226 setCommandType(_nCommandType); 227 return true; 228 } 229 catch (Exception exception) 230 { 231 exception.printStackTrace(System.out); 232 throw new InvalidQueryException(xMSF, Command); 233 } 234 } 235 getFields(String[] _sFieldNames, boolean binitializeDBColumns)236 public boolean getFields(String[] _sFieldNames, boolean binitializeDBColumns) 237 { 238 try 239 { 240 if (binitializeDBColumns) 241 { 242 initializeFieldColumns(_sFieldNames, xColumns); 243 } 244 String[] AllQueryFieldNames = xColumns.getElementNames(); 245 String[] sFieldNames = getFieldNames(); 246 for (int i = 0; i < FieldColumns.length; i++) 247 { 248 String sFieldName = FieldColumns[i].getFieldName(); 249 int nColIndex = JavaTools.FieldInList(AllQueryFieldNames, sFieldName) + 1; 250 FieldColumns[i].ColIndex = nColIndex; 251 if (nColIndex == -1) 252 { 253 throw new InvalidQueryException(xMSF, Command); 254 } 255 } 256 GroupFieldColumns = getFieldColumnList(GroupFieldNames); 257 RecordFieldColumns = getFieldColumnList(getRecordFieldNames()); 258 return true; 259 } 260 catch (InvalidQueryException queryexception) 261 { 262 queryexception.printStackTrace(System.out); 263 return false; 264 } 265 } 266 getFieldColumnList(String[] _FieldNames)267 private FieldColumn[] getFieldColumnList(String[] _FieldNames) 268 { 269 FieldColumn[] LocFieldColumns = new FieldColumn[_FieldNames.length]; 270 for (int i = 0; i < _FieldNames.length; i++) 271 { 272 LocFieldColumns[i] = super.getFieldColumnByFieldName(_FieldNames[i]); 273 } 274 return LocFieldColumns; 275 } 276 getGroupColumnValue(int ColIndex)277 public Object getGroupColumnValue(int ColIndex) 278 { 279 FieldColumn CurDBFieldColumn = this.GroupFieldColumns[ColIndex]; 280 return getColumnValue(CurDBFieldColumn.ColIndex, CurDBFieldColumn.getFieldType()); 281 } 282 getcurrentRecordData(java.util.Vector DataVector)283 public boolean getcurrentRecordData(java.util.Vector DataVector) 284 { 285 Object[] RecordValueArray = new Object[RecordFieldColumns.length]; 286 for (int i = 0; i < RecordFieldColumns.length; i++) 287 { 288 FieldColumn CurDBFieldColumn = this.RecordFieldColumns[i]; 289 RecordValueArray[i] = getColumnValue(CurDBFieldColumn.ColIndex, CurDBFieldColumn.getFieldType()); //FinalColIndex 290 } 291 DataVector.addElement(RecordValueArray); 292 return true; 293 } 294 dispose()295 public void dispose() 296 { 297 if (xRowSetComponent != null) 298 { 299 xRowSetComponent.dispose(); 300 } 301 super.dispose(); 302 } 303 } 304