1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package connectivity.tools; 28 29 import com.sun.star.container.ElementExistException; 30 import com.sun.star.container.NoSuchElementException; 31 import com.sun.star.container.XNameAccess; 32 import com.sun.star.container.XNameContainer; 33 import com.sun.star.lang.WrappedTargetException; 34 import com.sun.star.lang.XSingleServiceFactory; 35 import com.sun.star.lang.XMultiServiceFactory; 36 import com.sun.star.beans.XPropertySet; 37 import com.sun.star.sdb.XQueryDefinitionsSupplier; 38 import com.sun.star.sdbc.XDataSource; 39 import com.sun.star.uno.Exception; 40 import com.sun.star.uno.UnoRuntime; 41 import java.util.logging.Level; 42 import java.util.logging.Logger; 43 44 public class DataSource 45 { 46 // the service factory 47 48 private final XMultiServiceFactory m_orb; 49 private XDataSource m_dataSource; 50 51 public DataSource(final XMultiServiceFactory _orb, final String _registeredName) throws Exception 52 { 53 m_orb = _orb; 54 55 final XNameAccess dbContext = UnoRuntime.queryInterface( 56 XNameAccess.class, _orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) ); 57 58 m_dataSource = UnoRuntime.queryInterface( XDataSource.class, dbContext.getByName( _registeredName ) ); 59 } 60 61 public DataSource(final XMultiServiceFactory _orb,final XDataSource _dataSource) 62 { 63 m_orb = _orb; 64 m_dataSource = _dataSource; 65 } 66 67 final public XDataSource getXDataSource() 68 { 69 return m_dataSource; 70 } 71 72 /** 73 * retrieves the data source's settings 74 */ 75 public XPropertySet geSettings() 76 { 77 return UnoRuntime.queryInterface( XPropertySet.class, impl_getPropertyValue( "Settings" ) ); 78 } 79 80 /** creates a query with a given name and SQL command 81 */ 82 public void createQuery(final String _name, final String _sqlCommand) throws ElementExistException, WrappedTargetException, com.sun.star.lang.IllegalArgumentException 83 { 84 createQuery(_name, _sqlCommand, true); 85 } 86 87 /** creates a query with a given name, SQL command, and EscapeProcessing flag 88 */ 89 public void createQuery(final String _name, final String _sqlCommand, final boolean _escapeProcessing) throws ElementExistException, WrappedTargetException, com.sun.star.lang.IllegalArgumentException 90 { 91 final XSingleServiceFactory queryDefsFac = UnoRuntime.queryInterface( XSingleServiceFactory.class, getQueryDefinitions() ); 92 XPropertySet queryDef = null; 93 try 94 { 95 queryDef = UnoRuntime.queryInterface( XPropertySet.class, queryDefsFac.createInstance() ); 96 queryDef.setPropertyValue("Command", _sqlCommand); 97 queryDef.setPropertyValue("EscapeProcessing", Boolean.valueOf(_escapeProcessing)); 98 } 99 catch (com.sun.star.uno.Exception e) 100 { 101 e.printStackTrace(System.err); 102 } 103 104 final XNameContainer queryDefsContainer = UnoRuntime.queryInterface( XNameContainer.class, getQueryDefinitions() ); 105 queryDefsContainer.insertByName(_name, queryDef); 106 } 107 108 /** provides the query definition with the given name 109 */ 110 public QueryDefinition getQueryDefinition(final String _name) throws NoSuchElementException 111 { 112 final XNameAccess allDefs = getQueryDefinitions(); 113 try 114 { 115 return new QueryDefinition( UnoRuntime.queryInterface( XPropertySet.class, allDefs.getByName( _name) ) ); 116 } 117 catch (WrappedTargetException e) 118 { 119 } 120 throw new NoSuchElementException(); 121 } 122 123 /** provides the container of query definitions of the data source 124 */ 125 public XNameAccess getQueryDefinitions() 126 { 127 final XQueryDefinitionsSupplier suppQueries = UnoRuntime.queryInterface( 128 XQueryDefinitionsSupplier.class, m_dataSource); 129 return suppQueries.getQueryDefinitions(); 130 } 131 132 /** 133 * retrieves a property value from the data source 134 * @param i_propertyName 135 * the name of the property whose value is to be returned. 136 */ 137 private Object impl_getPropertyValue( final String i_propertyName ) 138 { 139 Object propertyValue = null; 140 try 141 { 142 final XPropertySet dataSourceProps = UnoRuntime.queryInterface( XPropertySet.class, m_dataSource ); 143 propertyValue = dataSourceProps.getPropertyValue( i_propertyName ); 144 } 145 catch (Exception ex) 146 { 147 Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex); 148 } 149 return propertyValue; 150 } 151 152 /** returns the name of the data source 153 * 154 * If a data source is registered at the database context, the name is the registration 155 * name. Otherwise, its the URL which the respective database document is based on. 156 * 157 * Note that the above definition is from the UNO API, not from this wrapper here. 158 */ 159 public String getName() 160 { 161 return (String)impl_getPropertyValue( "Name" ); 162 } 163 }; 164