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 package com.sun.star.wizards.db; 23 24 import com.sun.star.awt.XWindow; 25 import com.sun.star.lang.XInitialization; 26 import com.sun.star.ui.dialogs.XExecutableDialog; 27 28 import com.sun.star.lang.IllegalArgumentException; 29 import com.sun.star.lang.WrappedTargetException; 30 import com.sun.star.lang.XMultiServiceFactory; 31 import com.sun.star.awt.VclWindowPeerAttribute; 32 import com.sun.star.awt.XWindowPeer; 33 import com.sun.star.beans.PropertyValue; 34 import com.sun.star.beans.UnknownPropertyException; 35 import com.sun.star.beans.XPropertySet; 36 import com.sun.star.container.XHierarchicalNameAccess; 37 import com.sun.star.container.XHierarchicalNameContainer; 38 import com.sun.star.container.XNameAccess; 39 import com.sun.star.container.XNameContainer; 40 import com.sun.star.frame.XModel; 41 import com.sun.star.frame.XStorable; 42 import com.sun.star.lang.XComponent; 43 import com.sun.star.sdbc.DataType; 44 import com.sun.star.sdb.XOfficeDatabaseDocument; 45 import com.sun.star.sdb.XDocumentDataSource; 46 import com.sun.star.sdb.tools.XConnectionTools; 47 import com.sun.star.sdbcx.XColumnsSupplier; 48 49 import com.sun.star.ucb.XSimpleFileAccess; 50 import com.sun.star.uno.UnoRuntime; 51 import com.sun.star.uno.XInterface; 52 import com.sun.star.uno.AnyConverter; 53 import com.sun.star.util.XCloseable; 54 import com.sun.star.util.XNumberFormatsSupplier; 55 56 import com.sun.star.task.XInteractionHandler; 57 import com.sun.star.sdb.XFormDocumentsSupplier; 58 import com.sun.star.sdb.XQueryDefinitionsSupplier; 59 import com.sun.star.sdb.XReportDocumentsSupplier; 60 import com.sun.star.sdbc.SQLException; 61 import com.sun.star.sdbc.XDatabaseMetaData; 62 import com.sun.star.sdbc.XDataSource; 63 import com.sun.star.sdbc.XResultSet; 64 import com.sun.star.sdbc.XRow; 65 import com.sun.star.sdb.XCompletedConnection; 66 import com.sun.star.lang.Locale; 67 import com.sun.star.lang.XSingleServiceFactory; 68 import com.sun.star.sdb.XQueriesSupplier; 69 import com.sun.star.sdbc.XConnection; 70 import com.sun.star.sdbcx.XTablesSupplier; 71 import com.sun.star.wizards.common.Configuration; 72 import com.sun.star.wizards.common.Desktop; 73 import com.sun.star.wizards.common.FileAccess; 74 import com.sun.star.wizards.common.JavaTools; 75 import com.sun.star.wizards.common.NamedValueCollection; 76 import com.sun.star.wizards.common.NumberFormatter; 77 import com.sun.star.wizards.common.Properties; 78 import com.sun.star.wizards.common.Resource; 79 import com.sun.star.wizards.common.SystemDialog; 80 import com.sun.star.wizards.common.PropertyNames; 81 import java.util.Vector; 82 import java.util.logging.Level; 83 import java.util.logging.Logger; 84 85 public class DBMetaData 86 { 87 private XNameAccess xQueryNames; 88 public XDatabaseMetaData xDBMetaData; 89 private XDataSource m_dataSource; 90 private XPropertySet m_dataSourceSettings; 91 private XOfficeDatabaseDocument xModel; 92 private XPropertySet xDataSourcePropertySet; 93 public String[] DataSourceNames; 94 public String[] CommandNames; 95 public java.util.Vector CommandObjects = new Vector(1); 96 public Locale aLocale; 97 public int[] CommandTypes; 98 public String DataSourceName; 99 public com.sun.star.sdbc.XConnection DBConnection; 100 private com.sun.star.sdb.tools.XConnectionTools m_connectionTools; 101 public com.sun.star.lang.XMultiServiceFactory xMSF; 102 public XComponent xConnectionComponent; 103 104 private XNameAccess xNameAccess; 105 private XInterface xDatabaseContext; 106 private XWindowPeer xWindowPeer; 107 private String[] TableNames = new String[] {}; 108 private String[] QueryNames = new String[] {}; 109 110 protected int[][] WidthList; 111 protected static final int[] NumericTypes = { 112 DataType.TINYINT, // == -6; 113 DataType.BIGINT, // == -5 114 DataType.NUMERIC, // == - 2 115 DataType.DECIMAL, // == 3; 116 DataType.INTEGER, // == 4; 117 DataType.SMALLINT, // == 5; 118 DataType.FLOAT, // == 6; 119 DataType.REAL, // == 7; 120 DataType.DOUBLE, // == 8; 121 }; 122 protected static final int[] BinaryTypes = { //new int[12]; 123 DataType.BINARY, 124 DataType.VARBINARY, 125 DataType.LONGVARBINARY, 126 DataType.BLOB, 127 DataType.SQLNULL, 128 DataType.OBJECT, 129 DataType.DISTINCT, 130 DataType.STRUCT, 131 DataType.ARRAY, 132 DataType.CLOB, 133 DataType.REF 134 /* DataType.OTHER, */ 135 }; 136 137 private int iMaxColumnsInSelect; 138 private int iMaxColumnsInGroupBy; 139 private int iMaxColumnNameLength = -1; 140 private int iMaxTableNameLength = -1; 141 private boolean bPasswordIsRequired; 142 private final static int NOLIMIT = 9999999; 143 protected final static int RID_DB_COMMON = 1000; 144 private final static int INVALID = 9999999; 145 public TypeInspector oTypeInspector; 146 private NumberFormatter oNumberFormatter = null; 147 private long lDateCorrection = INVALID; 148 private boolean bdisposeConnection = false; 149 getDataSourcePropertySet()150 public XPropertySet getDataSourcePropertySet() 151 { 152 return xDataSourcePropertySet; 153 } 154 DBMetaData(XMultiServiceFactory xMSF)155 public DBMetaData(XMultiServiceFactory xMSF) 156 { 157 getInterfaces(xMSF); 158 InitializeWidthList(); 159 } 160 DBMetaData(XMultiServiceFactory xMSF, Locale _aLocale, NumberFormatter _oNumberFormatter)161 public DBMetaData(XMultiServiceFactory xMSF, Locale _aLocale, NumberFormatter _oNumberFormatter) 162 { 163 oNumberFormatter = _oNumberFormatter; 164 aLocale = _aLocale; 165 getInterfaces(xMSF); 166 InitializeWidthList(); 167 } 168 getNumberFormatter()169 public NumberFormatter getNumberFormatter() 170 { 171 if (oNumberFormatter == null) 172 { 173 try 174 { 175 XNumberFormatsSupplier xNumberFormatsSupplier = AnyConverter.toObject(XNumberFormatsSupplier.class, xDataSourcePropertySet.getPropertyValue("NumberFormatsSupplier")); 176 //TODO get the locale from the datasource 177 aLocale = Configuration.getOfficeLocale(xMSF); 178 oNumberFormatter = new NumberFormatter(xMSF, xNumberFormatsSupplier, aLocale); 179 lDateCorrection = oNumberFormatter.getNullDateCorrection(); 180 } 181 catch (Exception e) 182 { 183 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 184 } 185 } 186 return oNumberFormatter; 187 } 188 getNullDateCorrection()189 public long getNullDateCorrection() 190 { 191 if (lDateCorrection == INVALID) 192 { 193 if (oNumberFormatter == null) 194 { 195 oNumberFormatter = getNumberFormatter(); 196 } 197 lDateCorrection = oNumberFormatter.getNullDateCorrection(); 198 } 199 return lDateCorrection; 200 } 201 getInterfaces(XMultiServiceFactory xMSF)202 private void getInterfaces(XMultiServiceFactory xMSF) 203 { 204 try 205 { 206 this.xMSF = xMSF; 207 xDatabaseContext = (XInterface) xMSF.createInstance("com.sun.star.sdb.DatabaseContext"); 208 xNameAccess = UnoRuntime.queryInterface( XNameAccess.class, xDatabaseContext ); 209 DataSourceNames = xNameAccess.getElementNames(); 210 } 211 catch (Exception e) 212 { 213 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 214 } 215 } 216 setCommandTypes()217 public void setCommandTypes() 218 { 219 int TableCount; 220 int QueryCount; 221 int CommandCount; 222 int i; 223 int a; 224 TableCount = JavaTools.getArraylength(TableNames); 225 QueryCount = JavaTools.getArraylength(QueryNames); 226 CommandCount = TableCount + QueryCount; 227 CommandTypes = new int[CommandCount]; 228 if (TableCount > 0) 229 { 230 for (i = 0; i < TableCount; i++) 231 { 232 CommandTypes[i] = com.sun.star.sdb.CommandType.TABLE; 233 } 234 a = i; 235 for (i = 0; i < QueryCount; i++) 236 { 237 CommandTypes[a] = com.sun.star.sdb.CommandType.QUERY; 238 a += 1; 239 } 240 } 241 } 242 hasTableByName(String _stablename)243 public boolean hasTableByName(String _stablename) 244 { 245 return getTableNamesAsNameAccess().hasByName(_stablename); 246 } 247 248 @SuppressWarnings("unchecked") setTableByName(String _tableName)249 public void setTableByName(String _tableName) 250 { 251 CommandObject oTableObject = new CommandObject(_tableName, com.sun.star.sdb.CommandType.TABLE); 252 this.CommandObjects.addElement(oTableObject); 253 } 254 getTableByName(String _tablename)255 public CommandObject getTableByName(String _tablename) 256 { 257 return getCommandByName(_tablename, com.sun.star.sdb.CommandType.TABLE); 258 } 259 getQueryByName(String _queryname)260 public CommandObject getQueryByName(String _queryname) 261 { 262 return getCommandByName(_queryname, com.sun.star.sdb.CommandType.QUERY); 263 } 264 getCommandByName(String _commandname, int _commandtype)265 public CommandObject getCommandByName(String _commandname, int _commandtype) 266 { 267 CommandObject oCommand = null; 268 for (int i = 0; i < CommandObjects.size(); i++) 269 { 270 oCommand = (CommandObject) CommandObjects.elementAt(i); 271 if ((oCommand.Name.equals(_commandname)) && (oCommand.CommandType == _commandtype)) 272 { 273 return oCommand; 274 } 275 } 276 if (oCommand == null) 277 { 278 oCommand = new CommandObject(_commandname, _commandtype); 279 CommandObjects.addElement(oCommand); 280 } 281 return oCommand; 282 } 283 setQueryByName(String _QueryName)284 public void setQueryByName(String _QueryName) 285 { 286 CommandObject oQueryObject = new CommandObject(_QueryName, com.sun.star.sdb.CommandType.QUERY); 287 this.CommandObjects.addElement(oQueryObject); 288 } 289 290 public class CommandObject 291 { 292 293 private XNameAccess xColumns; 294 private XPropertySet xPropertySet; 295 private String Name; 296 private int CommandType; 297 CommandObject(String _CommandName, int _CommandType)298 public CommandObject(String _CommandName, int _CommandType) 299 { 300 try 301 { 302 Object oCommand; 303 this.Name = _CommandName; 304 this.CommandType = _CommandType; 305 // if (getTableNamesAsNameAccess() == null) 306 // { 307 // initCommandNames(); 308 // } 309 if (CommandType == com.sun.star.sdb.CommandType.TABLE) 310 { 311 oCommand = getTableNamesAsNameAccess().getByName(Name); 312 } 313 else 314 { 315 oCommand = getQueryNamesAsNameAccess().getByName(Name); 316 } 317 XColumnsSupplier xCommandCols = UnoRuntime.queryInterface( XColumnsSupplier.class, oCommand ); 318 xPropertySet = UnoRuntime.queryInterface( XPropertySet.class, oCommand ); 319 // TODO: Performance leak getColumns() take very long. 320 xColumns = UnoRuntime.queryInterface( XNameAccess.class, xCommandCols.getColumns() ); 321 } 322 catch (Exception e) 323 { 324 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 325 } 326 } getColumns()327 public XNameAccess getColumns() 328 { 329 return xColumns; 330 } getName()331 public String getName() 332 { 333 return Name; 334 } getPropertySet()335 public XPropertySet getPropertySet() 336 { 337 return xPropertySet; 338 } 339 } 340 hasEscapeProcessing(XPropertySet _xQueryPropertySet)341 public boolean hasEscapeProcessing(XPropertySet _xQueryPropertySet) 342 { 343 boolean bHasEscapeProcessing = false; 344 try 345 { 346 if (_xQueryPropertySet.getPropertySetInfo().hasPropertyByName("EscapeProcessing")) 347 { 348 bHasEscapeProcessing = AnyConverter.toBoolean(_xQueryPropertySet.getPropertyValue("EscapeProcessing")); 349 } 350 } 351 catch (Exception e) 352 { 353 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 354 } 355 return bHasEscapeProcessing; 356 } 357 getQueryNamesAsNameAccess()358 public XNameAccess getQueryNamesAsNameAccess() 359 { 360 XQueriesSupplier xDBQueries = UnoRuntime.queryInterface( XQueriesSupplier.class, DBConnection ); 361 xQueryNames = xDBQueries.getQueries(); 362 return xQueryNames; 363 } 364 getTableNamesAsNameAccess()365 public XNameAccess getTableNamesAsNameAccess() 366 { 367 XTablesSupplier xDBTables = UnoRuntime.queryInterface( XTablesSupplier.class, DBConnection ); 368 return xDBTables.getTables(); 369 } 370 getQueryNames()371 public String[] getQueryNames() 372 { 373 if (QueryNames != null) 374 { 375 if (QueryNames.length > 0) 376 { 377 return QueryNames; 378 } 379 } 380 QueryNames = getQueryNamesAsNameAccess().getElementNames(); 381 return QueryNames; 382 } 383 getTableNames()384 public String[] getTableNames() 385 { 386 if (TableNames != null) 387 { 388 if (TableNames.length > 0) 389 { 390 return TableNames; 391 } 392 } 393 TableNames = getTableNamesAsNameAccess().getElementNames(); 394 return TableNames; 395 } 396 InitializeWidthList()397 private void InitializeWidthList() 398 { 399 WidthList = new int[17][2]; 400 WidthList[0][0] = DataType.BIT; // == -7; 401 WidthList[1][0] = DataType.BOOLEAN; // = 16 402 WidthList[2][0] = DataType.TINYINT; // == -6; 403 WidthList[3][0] = DataType.BIGINT; // == -5; 404 WidthList[4][0] = DataType.LONGVARCHAR; // == -1; 405 WidthList[5][0] = DataType.CHAR; // == 1; 406 WidthList[6][0] = DataType.NUMERIC; // == 2; 407 WidthList[7][0] = DataType.DECIMAL; // == 3; [mit Nachkommastellen] 408 WidthList[8][0] = DataType.INTEGER; // == 4; 409 WidthList[9][0] = DataType.SMALLINT; // == 5; 410 WidthList[10][0] = DataType.FLOAT; // == 6; 411 WidthList[11][0] = DataType.REAL; // == 7; 412 WidthList[12][0] = DataType.DOUBLE; // == 8; 413 WidthList[13][0] = DataType.VARCHAR; // == 12; 414 WidthList[14][0] = DataType.DATE; // == 91; 415 WidthList[15][0] = DataType.TIME; // == 92; 416 WidthList[16][0] = DataType.TIMESTAMP; // == 93; 417 // NumericTypes are all types where aggregate functions can be performed on. 418 // Similarly to a major competitor date/time/timestamp fields are not included 419 420 421 } 422 isBinaryDataType(int _itype)423 public boolean isBinaryDataType(int _itype) 424 { 425 if (NumericTypes == null) 426 { 427 InitializeWidthList(); 428 } 429 return (JavaTools.FieldInIntTable(BinaryTypes, _itype) > -1); 430 } 431 getMaxTablesInSelect()432 public int getMaxTablesInSelect() 433 { 434 try 435 { 436 int itablecount = xDBMetaData.getMaxTablesInSelect(); 437 if (itablecount == 0) 438 { 439 return DBMetaData.NOLIMIT; 440 } 441 else 442 { 443 return itablecount; 444 } 445 } 446 catch (SQLException e) 447 { 448 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 449 return - 1; 450 } 451 } 452 getMaxColumnsInSelect()453 public int getMaxColumnsInSelect() 454 { 455 return iMaxColumnsInSelect; 456 } 457 getMaxColumnsInGroupBy()458 public int getMaxColumnsInGroupBy() 459 { 460 return iMaxColumnsInGroupBy; 461 } 462 setMaxColumnsInSelect()463 private void setMaxColumnsInSelect() throws SQLException 464 { 465 iMaxColumnsInSelect = xDBMetaData.getMaxColumnsInSelect(); 466 if (iMaxColumnsInSelect == 0) 467 { 468 iMaxColumnsInSelect = DBMetaData.NOLIMIT; 469 } 470 } 471 setMaxColumnsInGroupBy()472 private void setMaxColumnsInGroupBy() throws SQLException 473 { 474 iMaxColumnsInGroupBy = xDBMetaData.getMaxColumnsInGroupBy(); 475 if (iMaxColumnsInGroupBy == 0) 476 { 477 iMaxColumnsInGroupBy = DBMetaData.NOLIMIT; 478 } 479 } 480 getMaxColumnsInTable()481 public int getMaxColumnsInTable() throws SQLException 482 { 483 int iMaxColumnsInTable = xDBMetaData.getMaxColumnsInTable(); 484 if (iMaxColumnsInTable == 0) 485 { 486 iMaxColumnsInTable = DBMetaData.NOLIMIT; 487 } 488 return iMaxColumnsInTable; 489 } 490 getDataSourceObjects()491 private void getDataSourceObjects() throws Exception 492 { 493 try 494 { 495 xDBMetaData = DBConnection.getMetaData(); 496 getDataSourceInterfaces(); 497 setMaxColumnsInGroupBy(); 498 setMaxColumnsInSelect(); 499 } 500 catch (SQLException e) 501 { 502 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 503 } 504 } 505 ensureDataSourceSettings()506 private void ensureDataSourceSettings() throws UnknownPropertyException, WrappedTargetException 507 { 508 if ( m_dataSourceSettings != null ) 509 return; 510 511 XPropertySet dataSourceProperties = UnoRuntime.queryInterface( XPropertySet.class, getDataSource() ); 512 m_dataSourceSettings = UnoRuntime.queryInterface( XPropertySet.class, dataSourceProperties.getPropertyValue( "Settings" ) ); 513 } 514 isSQL92CheckEnabled()515 public boolean isSQL92CheckEnabled() 516 { 517 boolean isSQL92CheckEnabled = false; 518 try 519 { 520 ensureDataSourceSettings(); 521 isSQL92CheckEnabled = AnyConverter.toBoolean( m_dataSourceSettings.getPropertyValue( "EnableSQL92Check" ) ); 522 } 523 catch (Exception e) 524 { 525 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 526 } 527 return isSQL92CheckEnabled; 528 } 529 verifyName(String _sname, int _maxlen)530 public String verifyName(String _sname, int _maxlen) 531 { 532 if (_sname.length() > _maxlen) 533 { 534 return _sname.substring(0, _maxlen); 535 } 536 if (this.isSQL92CheckEnabled()) 537 { 538 return Desktop.removeSpecialCharacters(xMSF, Configuration.getOfficeLocale(xMSF), _sname); 539 } 540 return _sname; 541 } 542 getDataSource()543 public XDataSource getDataSource() 544 { 545 if (m_dataSource == null) 546 { 547 try 548 { 549 Object oDataSource = xNameAccess.getByName(DataSourceName); 550 m_dataSource = UnoRuntime.queryInterface( XDataSource.class, oDataSource ); 551 } 552 catch (com.sun.star.container.NoSuchElementException e) 553 { 554 } 555 catch (com.sun.star.lang.WrappedTargetException e) 556 { 557 } 558 } 559 return m_dataSource; 560 } 561 setDataSourceByName(String _DataSourceName)562 private void setDataSourceByName(String _DataSourceName) 563 { 564 try 565 { 566 this.DataSourceName = _DataSourceName; 567 getDataSourceInterfaces(); 568 XDocumentDataSource xDocu = UnoRuntime.queryInterface( XDocumentDataSource.class, getDataSource() ); 569 if (xDocu != null) 570 { 571 xModel = xDocu.getDatabaseDocument(); 572 } 573 } 574 catch (Exception e) 575 { 576 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 577 } 578 } 579 getDataSourceInterfaces()580 public void getDataSourceInterfaces() throws Exception 581 { 582 xDataSourcePropertySet = UnoRuntime.queryInterface( XPropertySet.class, getDataSource() ); 583 bPasswordIsRequired = ((Boolean) xDataSourcePropertySet.getPropertyValue("IsPasswordRequired")).booleanValue(); 584 } 585 getConnection(PropertyValue[] curproperties)586 public boolean getConnection(PropertyValue[] curproperties) 587 { 588 try 589 { 590 XConnection xConnection = null; 591 if (Properties.hasPropertyValue(curproperties, PropertyNames.ACTIVE_CONNECTION)) 592 { 593 xConnection = UnoRuntime.queryInterface( XConnection.class, Properties.getPropertyValue( curproperties, PropertyNames.ACTIVE_CONNECTION ) ); 594 if (xConnection != null) 595 { 596 com.sun.star.container.XChild child = UnoRuntime.queryInterface( com.sun.star.container.XChild.class, xConnection ); 597 598 m_dataSource = UnoRuntime.queryInterface( XDataSource.class, child.getParent() ); 599 XDocumentDataSource xDocu = UnoRuntime.queryInterface( XDocumentDataSource.class, m_dataSource ); 600 if (xDocu != null) 601 { 602 xModel = xDocu.getDatabaseDocument(); 603 } 604 XPropertySet xPSet = UnoRuntime.queryInterface( XPropertySet.class, m_dataSource ); 605 if (xPSet != null) 606 { 607 DataSourceName = AnyConverter.toString(xPSet.getPropertyValue(PropertyNames.PROPERTY_NAME)); 608 } 609 return getConnection(xConnection); 610 } 611 else 612 { 613 bdisposeConnection = true; 614 } 615 } 616 else 617 { 618 bdisposeConnection = true; 619 } 620 if (Properties.hasPropertyValue(curproperties, "DataSourceName")) 621 { 622 String sDataSourceName = AnyConverter.toString(Properties.getPropertyValue(curproperties, "DataSourceName")); 623 return getConnection(sDataSourceName); 624 } 625 else if (Properties.hasPropertyValue(curproperties, "DataSource")) 626 { 627 m_dataSource = UnoRuntime.queryInterface( XDataSource.class, Properties.getPropertyValue( curproperties, "DataSource" ) ); 628 XDocumentDataSource xDocu = UnoRuntime.queryInterface( XDocumentDataSource.class, this.m_dataSource ); 629 if (xDocu != null) 630 { 631 xModel = xDocu.getDatabaseDocument(); 632 } 633 return getConnection(m_dataSource); 634 } 635 if (Properties.hasPropertyValue(curproperties, "DatabaseLocation")) 636 { 637 String sDataSourceName = AnyConverter.toString(Properties.getPropertyValue(curproperties, "DatabaseLocation")); 638 return getConnection(sDataSourceName); 639 } 640 } 641 catch (IllegalArgumentException e) 642 { 643 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 644 } 645 catch (UnknownPropertyException e) 646 { 647 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 648 } 649 catch (WrappedTargetException e) 650 { 651 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 652 } 653 654 return false; 655 } 656 getConnection(String _DataSourceName)657 private boolean getConnection(String _DataSourceName) 658 { 659 setDataSourceByName(_DataSourceName); 660 return getConnection( getDataSource() ); 661 } 662 getConnection(com.sun.star.sdbc.XConnection _DBConnection)663 private boolean getConnection(com.sun.star.sdbc.XConnection _DBConnection) 664 { 665 try 666 { 667 this.DBConnection = _DBConnection; 668 this.m_connectionTools = UnoRuntime.queryInterface( XConnectionTools.class, this.DBConnection ); 669 getDataSourceObjects(); 670 return true; 671 } 672 catch (Exception e) 673 { 674 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 675 return false; 676 } 677 } 678 getConnection(XDataSource _dataSource)679 private boolean getConnection(XDataSource _dataSource) 680 { 681 Resource oResource = new Resource(xMSF, "Database", "dbw"); 682 try 683 { 684 int iMsg = 0; 685 boolean bgetConnection = false; 686 if (DBConnection != null) 687 { 688 xConnectionComponent.dispose(); 689 } 690 getDataSourceInterfaces(); 691 if (!bPasswordIsRequired) 692 { 693 DBConnection = _dataSource.getConnection(PropertyNames.EMPTY_STRING, PropertyNames.EMPTY_STRING); 694 bgetConnection = true; 695 } 696 else 697 { 698 XInteractionHandler xInteractionHandler = UnoRuntime.queryInterface( XInteractionHandler.class, xMSF.createInstance("com.sun.star.task.InteractionHandler") ); 699 boolean bExitLoop = true; 700 do 701 { 702 XCompletedConnection xCompleted2 = UnoRuntime.queryInterface( XCompletedConnection.class, _dataSource ); 703 try 704 { 705 DBConnection = xCompleted2.connectWithCompletion( xInteractionHandler ); 706 bgetConnection = DBConnection != null; 707 if (!bgetConnection) 708 { 709 bExitLoop = true; 710 } 711 } 712 catch (Exception exception) 713 { 714 // Note: WindowAttributes from toolkit/source/awt/vclxtoolkit.cxx 715 String sMsgNoConnection = oResource.getResText(RID_DB_COMMON + 14); 716 iMsg = showMessageBox("QueryBox", VclWindowPeerAttribute.RETRY_CANCEL, sMsgNoConnection); 717 bExitLoop = iMsg == 0; 718 bgetConnection = false; 719 } 720 } 721 while (!bExitLoop); 722 } 723 if (!bgetConnection) 724 { 725 String sMsgConnectionImpossible = oResource.getResText(RID_DB_COMMON + 35); 726 showMessageBox("ErrorBox", VclWindowPeerAttribute.OK, sMsgConnectionImpossible); 727 } 728 else 729 { 730 xConnectionComponent = UnoRuntime.queryInterface( XComponent.class, DBConnection ); 731 m_connectionTools = UnoRuntime.queryInterface( XConnectionTools.class, DBConnection ); 732 getDataSourceObjects(); 733 } 734 return bgetConnection; 735 } 736 catch (Exception e) 737 { 738 String sMsgConnectionImpossible = oResource.getResText(RID_DB_COMMON + 35); 739 showMessageBox("ErrorBox", VclWindowPeerAttribute.OK, sMsgConnectionImpossible); 740 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 741 return false; 742 } 743 } 744 getMaxColumnNameLength()745 public int getMaxColumnNameLength() 746 { 747 try 748 { 749 if (iMaxColumnNameLength <= 0) 750 { 751 iMaxColumnNameLength = xDBMetaData.getMaxColumnNameLength(); 752 } 753 return iMaxColumnNameLength; 754 } 755 catch (SQLException e) 756 { 757 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 758 return 0; 759 } 760 } 761 getMaxTableNameLength()762 public int getMaxTableNameLength() 763 { 764 try 765 { 766 if (iMaxTableNameLength <= 0) 767 { 768 iMaxTableNameLength = xDBMetaData.getMaxTableNameLength(); 769 } 770 return iMaxTableNameLength; 771 } 772 catch (SQLException e) 773 { 774 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 775 return 0; 776 } 777 } 778 supportsPrimaryKeys()779 public boolean supportsPrimaryKeys() 780 { 781 boolean supportsPrimaryKeys = false; 782 try 783 { 784 ensureDataSourceSettings(); 785 Object primaryKeySupport = m_dataSourceSettings.getPropertyValue( "PrimaryKeySupport" ); 786 if ( AnyConverter.isVoid( primaryKeySupport ) ) 787 supportsPrimaryKeys = supportsCoreSQLGrammar(); 788 else 789 supportsPrimaryKeys = AnyConverter.toBoolean( primaryKeySupport ); 790 } 791 catch ( Exception ex ) 792 { 793 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, ex ); 794 } 795 return supportsPrimaryKeys; 796 } 797 supportsCoreSQLGrammar()798 public boolean supportsCoreSQLGrammar() 799 { 800 try 801 { 802 return xDBMetaData.supportsCoreSQLGrammar(); 803 } 804 catch (SQLException e) 805 { 806 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 807 return false; 808 } 809 } 810 supportsAutoIncrementation()811 public boolean supportsAutoIncrementation() 812 { 813 return false; 814 } 815 supportsQueriesInFrom()816 public boolean supportsQueriesInFrom() 817 { 818 return m_connectionTools.getDataSourceMetaData().supportsQueriesInFrom(); 819 } 820 suggestName( final int i_objectType, final String i_baseName )821 public String suggestName( final int i_objectType, final String i_baseName ) throws IllegalArgumentException 822 { 823 return m_connectionTools.getObjectNames().suggestName( i_objectType, i_baseName ); 824 } 825 826 /** 827 * inserts a Query to a datasource; There is no validation if the queryname is already existing in the datasource 828 * @param oQuery 829 * @param QueryName 830 */ createQuery(SQLQueryComposer _oSQLQueryComposer, String _QueryName)831 public boolean createQuery(SQLQueryComposer _oSQLQueryComposer, String _QueryName) 832 { 833 try 834 { 835 XQueryDefinitionsSupplier xQueryDefinitionsSuppl = UnoRuntime.queryInterface( XQueryDefinitionsSupplier.class, m_dataSource ); 836 XNameAccess xQueryDefs = xQueryDefinitionsSuppl.getQueryDefinitions(); 837 XSingleServiceFactory xSSFQueryDefs = UnoRuntime.queryInterface( XSingleServiceFactory.class, xQueryDefs ); 838 Object oQuery = xSSFQueryDefs.createInstance(); //"com.sun.star.sdb.QueryDefinition" 839 XPropertySet xPSet = UnoRuntime.queryInterface( XPropertySet.class, oQuery ); 840 841 String s = _oSQLQueryComposer.m_xQueryAnalyzer.getQuery(); 842 xPSet.setPropertyValue(PropertyNames.COMMAND, s); 843 844 XNameContainer xNameCont = UnoRuntime.queryInterface( XNameContainer.class, xQueryDefs ); 845 m_connectionTools.getObjectNames().checkNameForCreate(com.sun.star.sdb.CommandType.QUERY, _QueryName); 846 xNameCont.insertByName(_QueryName, oQuery); 847 return true; 848 } 849 catch (WrappedTargetException exception) 850 { 851 SQLException sqlError = null; 852 try 853 { 854 sqlError = (SQLException) exception.TargetException; 855 } 856 catch (ClassCastException castError) 857 { 858 } 859 860 if (sqlError != null) 861 { 862 callSQLErrorMessageDialog(sqlError, null); 863 return false; 864 } 865 exception.printStackTrace(System.out); 866 } 867 catch (SQLException e) 868 { 869 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 870 } 871 catch (Exception e) 872 { 873 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 874 } 875 return false; 876 } 877 dispose()878 public void dispose() 879 { 880 if ((DBConnection != null) && (this.bdisposeConnection)) 881 { 882 xConnectionComponent.dispose(); 883 } 884 } 885 getReportDocuments()886 public XHierarchicalNameAccess getReportDocuments() 887 { 888 XReportDocumentsSupplier xReportDocumentSuppl = UnoRuntime.queryInterface( XReportDocumentsSupplier.class, this.xModel ); 889 xReportDocumentSuppl.getReportDocuments(); 890 return UnoRuntime.queryInterface( XHierarchicalNameAccess.class, xReportDocumentSuppl.getReportDocuments() ); 891 } 892 getFormDocuments()893 public XHierarchicalNameAccess getFormDocuments() 894 { 895 XFormDocumentsSupplier xFormDocumentSuppl = UnoRuntime.queryInterface( XFormDocumentsSupplier.class, xModel ); 896 return UnoRuntime.queryInterface( XHierarchicalNameAccess.class, xFormDocumentSuppl.getFormDocuments() ); 897 } 898 hasFormDocumentByName(String _sFormName)899 public boolean hasFormDocumentByName(String _sFormName) 900 { 901 XFormDocumentsSupplier xFormDocumentSuppl = UnoRuntime.queryInterface( XFormDocumentsSupplier.class, xModel ); 902 XNameAccess xFormNameAccess = UnoRuntime.queryInterface( XNameAccess.class, xFormDocumentSuppl.getFormDocuments() ); 903 return xFormNameAccess.hasByName(_sFormName); 904 } 905 addFormDocument(XComponent _xComponent)906 public void addFormDocument(XComponent _xComponent) 907 { 908 XHierarchicalNameAccess _xFormDocNameAccess = getFormDocuments(); 909 addDatabaseDocument(_xComponent, _xFormDocNameAccess, false); 910 } 911 addReportDocument(XComponent _xComponent, boolean _bcreatedynamicreport)912 public void addReportDocument(XComponent _xComponent, boolean _bcreatedynamicreport) 913 { 914 XHierarchicalNameAccess xReportDocNameAccess = getReportDocuments(); 915 addDatabaseDocument(_xComponent, xReportDocNameAccess, _bcreatedynamicreport); 916 } 917 918 /** 919 * adds the passed document as a report or a form to the database. Afterwards the document is deleted. 920 * the document may not be open 921 * @param _xComponent 922 * @param _xDocNameAccess 923 * @param _bcreateTemplate describes the type of the document: "form" or "report" 924 */ addDatabaseDocument(XComponent _xComponent, XHierarchicalNameAccess _xDocNameAccess, boolean i_createTemplate)925 public void addDatabaseDocument(XComponent _xComponent, XHierarchicalNameAccess _xDocNameAccess, boolean i_createTemplate) 926 { 927 try 928 { 929 XModel xDocumentModel = UnoRuntime.queryInterface( XModel.class, _xComponent ); 930 String documentURL = xDocumentModel.getURL(); 931 String basename = FileAccess.getBasename(documentURL, "/"); 932 XCloseable xCloseable = UnoRuntime.queryInterface( XCloseable.class, _xComponent ); 933 xCloseable.close(false); 934 935 NamedValueCollection creationArgs = new NamedValueCollection(); 936 creationArgs.put( PropertyNames.PROPERTY_NAME, basename ); 937 creationArgs.put( PropertyNames.URL, documentURL ); 938 creationArgs.put( "AsTemplate", i_createTemplate ); 939 XMultiServiceFactory xDocMSF = UnoRuntime.queryInterface( XMultiServiceFactory.class, _xDocNameAccess ); 940 Object oDBDocument = xDocMSF.createInstanceWithArguments( "com.sun.star.sdb.DocumentDefinition", creationArgs.getPropertyValues() ); 941 XHierarchicalNameContainer xHier = UnoRuntime.queryInterface( XHierarchicalNameContainer.class, _xDocNameAccess ); 942 String sdocname = Desktop.getUniqueName(_xDocNameAccess, basename); 943 xHier.insertByHierarchicalName(sdocname, oDBDocument); 944 XInterface xInterface = (XInterface) xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess"); 945 XSimpleFileAccess xSimpleFileAccess = UnoRuntime.queryInterface( XSimpleFileAccess.class, xInterface ); 946 xSimpleFileAccess.kill(documentURL); 947 } 948 catch (Exception e) 949 { 950 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 951 } 952 } 953 createTypeInspector()954 public void createTypeInspector() throws SQLException 955 { 956 oTypeInspector = new TypeInspector(xDBMetaData.getTypeInfo()); 957 } 958 getDBDataTypeInspector()959 public TypeInspector getDBDataTypeInspector() 960 { 961 return oTypeInspector; 962 } 963 StringsFromResultSet(XResultSet _xResultSet, int _icol)964 private String[] StringsFromResultSet(XResultSet _xResultSet, int _icol) 965 { 966 String[] sColValues = null; 967 try 968 { 969 XRow xRow = UnoRuntime.queryInterface( XRow.class, _xResultSet ); 970 Vector aColVector = new Vector(); 971 while (_xResultSet.next()) 972 { 973 aColVector.addElement(xRow.getString(_icol)); 974 } 975 sColValues = new String[aColVector.size()]; 976 aColVector.toArray(sColValues); 977 } 978 catch (SQLException e) 979 { 980 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 981 } 982 return sColValues; 983 } 984 getCatalogNames()985 public String[] getCatalogNames() 986 { 987 try 988 { 989 XResultSet xResultSet = xDBMetaData.getCatalogs(); 990 return StringsFromResultSet(xResultSet, 1); 991 } 992 catch (SQLException e) 993 { 994 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 995 return null; 996 } 997 } 998 getSchemaNames()999 public String[] getSchemaNames() 1000 { 1001 try 1002 { 1003 XResultSet xResultSet = xDBMetaData.getSchemas(); 1004 return StringsFromResultSet(xResultSet, 1); 1005 } 1006 catch (SQLException e) 1007 { 1008 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 1009 return null; 1010 } 1011 } 1012 storeDatabaseDocumentToTempPath(XComponent _xcomponent, String _storename)1013 public boolean storeDatabaseDocumentToTempPath(XComponent _xcomponent, String _storename) 1014 { 1015 try 1016 { 1017 XInterface xInterface = (XInterface) xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess"); 1018 XSimpleFileAccess xSimpleFileAccess = UnoRuntime.queryInterface( XSimpleFileAccess.class, xInterface ); 1019 String storepath = FileAccess.getOfficePath(xMSF, "Temp", xSimpleFileAccess) + "/" + _storename; 1020 XStorable xStoreable = UnoRuntime.queryInterface( XStorable.class, _xcomponent ); 1021 PropertyValue[] oStoreProperties = new PropertyValue[1]; 1022 oStoreProperties[0] = Properties.createProperty("FilterName", "writer8"); 1023 storepath += ".odt"; 1024 xStoreable.storeAsURL(storepath, oStoreProperties); 1025 return true; 1026 } 1027 catch (Exception e) 1028 { 1029 Logger.getLogger( DBMetaData.class.getName() ).log( Level.SEVERE, null, e ); 1030 return false; 1031 } 1032 } 1033 showMessageBox(String windowServiceName, int windowAttribute, String MessageText)1034 public int showMessageBox(String windowServiceName, int windowAttribute, String MessageText) 1035 { 1036 if (getWindowPeer() != null) 1037 { 1038 return SystemDialog.showMessageBox(xMSF, xWindowPeer, windowServiceName, windowAttribute, MessageText); 1039 } 1040 else 1041 { 1042 return SystemDialog.showMessageBox(xMSF, windowServiceName, windowAttribute, MessageText); 1043 } 1044 } 1045 1046 /** 1047 * @return Returns the xWindowPeer. 1048 */ getWindowPeer()1049 public XWindowPeer getWindowPeer() 1050 { 1051 return xWindowPeer; 1052 } 1053 1054 /** 1055 * @param windowPeer The xWindowPeer to set. 1056 * Should be called as soon as a Windowpeer of a wizard dialog is available 1057 * The windowpeer is needed to call a Messagebox 1058 */ setWindowPeer(XWindowPeer windowPeer)1059 public void setWindowPeer(XWindowPeer windowPeer) 1060 { 1061 xWindowPeer = windowPeer; 1062 } 1063 callSQLErrorMessageDialog(SQLException oSQLException, XWindow _xWindow)1064 public void callSQLErrorMessageDialog(SQLException oSQLException, XWindow _xWindow) 1065 { 1066 try 1067 { 1068 Object oDialog = xMSF.createInstance("com.sun.star.sdb.ErrorMessageDialog"); 1069 XInitialization xInitialization = UnoRuntime.queryInterface( XInitialization.class, oDialog ); 1070 PropertyValue[] aPropertyValue = new PropertyValue[2]; 1071 aPropertyValue[0] = Properties.createProperty("SQLException", oSQLException); 1072 aPropertyValue[1] = Properties.createProperty("ParentWindow", _xWindow); 1073 xInitialization.initialize(aPropertyValue); 1074 XExecutableDialog xExecutableDialog = UnoRuntime.queryInterface( XExecutableDialog.class, oDialog ); 1075 xExecutableDialog.execute(); 1076 } 1077 catch (com.sun.star.uno.Exception ex) 1078 { 1079 Logger.getLogger( getClass().getName() ).log( Level.SEVERE, "error calling the error dialog", ex ); 1080 } 1081 } 1082 finish()1083 public void finish() 1084 { 1085 xQueryNames = null; 1086 xNameAccess = null; 1087 xDatabaseContext = null; 1088 xDBMetaData = null; 1089 m_dataSource = null; 1090 xModel = null; 1091 xDataSourcePropertySet = null; 1092 xWindowPeer = null; 1093 DBConnection = null; 1094 m_connectionTools = null; 1095 xMSF = null; 1096 xConnectionComponent = null; 1097 CommandObjects = null; 1098 } 1099 } 1100