1c3ab0d6aSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3c3ab0d6aSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4c3ab0d6aSAndrew Rist * or more contributor license agreements. See the NOTICE file 5c3ab0d6aSAndrew Rist * distributed with this work for additional information 6c3ab0d6aSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7c3ab0d6aSAndrew Rist * to you under the Apache License, Version 2.0 (the 8c3ab0d6aSAndrew Rist * "License"); you may not use this file except in compliance 9c3ab0d6aSAndrew Rist * with the License. You may obtain a copy of the License at 10c3ab0d6aSAndrew Rist * 11c3ab0d6aSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12c3ab0d6aSAndrew Rist * 13c3ab0d6aSAndrew Rist * Unless required by applicable law or agreed to in writing, 14c3ab0d6aSAndrew Rist * software distributed under the License is distributed on an 15c3ab0d6aSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16c3ab0d6aSAndrew Rist * KIND, either express or implied. See the License for the 17c3ab0d6aSAndrew Rist * specific language governing permissions and limitations 18c3ab0d6aSAndrew Rist * under the License. 19c3ab0d6aSAndrew Rist * 20c3ab0d6aSAndrew Rist *************************************************************/ 21c3ab0d6aSAndrew Rist 22c3ab0d6aSAndrew Rist 23cdf0e10cSrcweir package connectivity.tools; 24cdf0e10cSrcweir 25cdf0e10cSrcweir import com.sun.star.container.ElementExistException; 26cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException; 27cdf0e10cSrcweir import com.sun.star.container.XNameAccess; 28cdf0e10cSrcweir import com.sun.star.container.XNameContainer; 29cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException; 30cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 31cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 32cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 33cdf0e10cSrcweir import com.sun.star.sdb.XQueryDefinitionsSupplier; 34cdf0e10cSrcweir import com.sun.star.sdbc.XDataSource; 35cdf0e10cSrcweir import com.sun.star.uno.Exception; 36cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 37cdf0e10cSrcweir import java.util.logging.Level; 38cdf0e10cSrcweir import java.util.logging.Logger; 39cdf0e10cSrcweir 40cdf0e10cSrcweir public class DataSource 41cdf0e10cSrcweir { 42cdf0e10cSrcweir // the service factory 43cdf0e10cSrcweir 44cdf0e10cSrcweir private final XMultiServiceFactory m_orb; 45cdf0e10cSrcweir private XDataSource m_dataSource; 46cdf0e10cSrcweir DataSource(final XMultiServiceFactory _orb, final String _registeredName)47cdf0e10cSrcweir public DataSource(final XMultiServiceFactory _orb, final String _registeredName) throws Exception 48cdf0e10cSrcweir { 49cdf0e10cSrcweir m_orb = _orb; 50cdf0e10cSrcweir 51cdf0e10cSrcweir final XNameAccess dbContext = UnoRuntime.queryInterface( 52cdf0e10cSrcweir XNameAccess.class, _orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) ); 53cdf0e10cSrcweir 54cdf0e10cSrcweir m_dataSource = UnoRuntime.queryInterface( XDataSource.class, dbContext.getByName( _registeredName ) ); 55cdf0e10cSrcweir } 56cdf0e10cSrcweir DataSource(final XMultiServiceFactory _orb,final XDataSource _dataSource)57cdf0e10cSrcweir public DataSource(final XMultiServiceFactory _orb,final XDataSource _dataSource) 58cdf0e10cSrcweir { 59cdf0e10cSrcweir m_orb = _orb; 60cdf0e10cSrcweir m_dataSource = _dataSource; 61cdf0e10cSrcweir } 62cdf0e10cSrcweir getXDataSource()63cdf0e10cSrcweir final public XDataSource getXDataSource() 64cdf0e10cSrcweir { 65cdf0e10cSrcweir return m_dataSource; 66cdf0e10cSrcweir } 67cdf0e10cSrcweir 68cdf0e10cSrcweir /** 69cdf0e10cSrcweir * retrieves the data source's settings 70cdf0e10cSrcweir */ geSettings()71cdf0e10cSrcweir public XPropertySet geSettings() 72cdf0e10cSrcweir { 73cdf0e10cSrcweir return UnoRuntime.queryInterface( XPropertySet.class, impl_getPropertyValue( "Settings" ) ); 74cdf0e10cSrcweir } 75cdf0e10cSrcweir 76cdf0e10cSrcweir /** creates a query with a given name and SQL command 77cdf0e10cSrcweir */ createQuery(final String _name, final String _sqlCommand)78cdf0e10cSrcweir public void createQuery(final String _name, final String _sqlCommand) throws ElementExistException, WrappedTargetException, com.sun.star.lang.IllegalArgumentException 79cdf0e10cSrcweir { 80cdf0e10cSrcweir createQuery(_name, _sqlCommand, true); 81cdf0e10cSrcweir } 82cdf0e10cSrcweir 83cdf0e10cSrcweir /** creates a query with a given name, SQL command, and EscapeProcessing flag 84cdf0e10cSrcweir */ createQuery(final String _name, final String _sqlCommand, final boolean _escapeProcessing)85cdf0e10cSrcweir public void createQuery(final String _name, final String _sqlCommand, final boolean _escapeProcessing) throws ElementExistException, WrappedTargetException, com.sun.star.lang.IllegalArgumentException 86cdf0e10cSrcweir { 87cdf0e10cSrcweir final XSingleServiceFactory queryDefsFac = UnoRuntime.queryInterface( XSingleServiceFactory.class, getQueryDefinitions() ); 88cdf0e10cSrcweir XPropertySet queryDef = null; 89cdf0e10cSrcweir try 90cdf0e10cSrcweir { 91cdf0e10cSrcweir queryDef = UnoRuntime.queryInterface( XPropertySet.class, queryDefsFac.createInstance() ); 92cdf0e10cSrcweir queryDef.setPropertyValue("Command", _sqlCommand); 93cdf0e10cSrcweir queryDef.setPropertyValue("EscapeProcessing", Boolean.valueOf(_escapeProcessing)); 94cdf0e10cSrcweir } 95cdf0e10cSrcweir catch (com.sun.star.uno.Exception e) 96cdf0e10cSrcweir { 97cdf0e10cSrcweir e.printStackTrace(System.err); 98cdf0e10cSrcweir } 99cdf0e10cSrcweir 100cdf0e10cSrcweir final XNameContainer queryDefsContainer = UnoRuntime.queryInterface( XNameContainer.class, getQueryDefinitions() ); 101cdf0e10cSrcweir queryDefsContainer.insertByName(_name, queryDef); 102cdf0e10cSrcweir } 103cdf0e10cSrcweir 104cdf0e10cSrcweir /** provides the query definition with the given name 105cdf0e10cSrcweir */ getQueryDefinition(final String _name)106cdf0e10cSrcweir public QueryDefinition getQueryDefinition(final String _name) throws NoSuchElementException 107cdf0e10cSrcweir { 108cdf0e10cSrcweir final XNameAccess allDefs = getQueryDefinitions(); 109cdf0e10cSrcweir try 110cdf0e10cSrcweir { 111cdf0e10cSrcweir return new QueryDefinition( UnoRuntime.queryInterface( XPropertySet.class, allDefs.getByName( _name) ) ); 112cdf0e10cSrcweir } 113cdf0e10cSrcweir catch (WrappedTargetException e) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir } 116cdf0e10cSrcweir throw new NoSuchElementException(); 117cdf0e10cSrcweir } 118cdf0e10cSrcweir 119cdf0e10cSrcweir /** provides the container of query definitions of the data source 120cdf0e10cSrcweir */ getQueryDefinitions()121cdf0e10cSrcweir public XNameAccess getQueryDefinitions() 122cdf0e10cSrcweir { 123cdf0e10cSrcweir final XQueryDefinitionsSupplier suppQueries = UnoRuntime.queryInterface( 124cdf0e10cSrcweir XQueryDefinitionsSupplier.class, m_dataSource); 125cdf0e10cSrcweir return suppQueries.getQueryDefinitions(); 126cdf0e10cSrcweir } 127cdf0e10cSrcweir 128cdf0e10cSrcweir /** 129cdf0e10cSrcweir * retrieves a property value from the data source 130cdf0e10cSrcweir * @param i_propertyName 131cdf0e10cSrcweir * the name of the property whose value is to be returned. 132cdf0e10cSrcweir */ impl_getPropertyValue( final String i_propertyName )133cdf0e10cSrcweir private Object impl_getPropertyValue( final String i_propertyName ) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir Object propertyValue = null; 136cdf0e10cSrcweir try 137cdf0e10cSrcweir { 138cdf0e10cSrcweir final XPropertySet dataSourceProps = UnoRuntime.queryInterface( XPropertySet.class, m_dataSource ); 139cdf0e10cSrcweir propertyValue = dataSourceProps.getPropertyValue( i_propertyName ); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir catch (Exception ex) 142cdf0e10cSrcweir { 143cdf0e10cSrcweir Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex); 144cdf0e10cSrcweir } 145cdf0e10cSrcweir return propertyValue; 146cdf0e10cSrcweir } 147cdf0e10cSrcweir 148cdf0e10cSrcweir /** returns the name of the data source 149cdf0e10cSrcweir * 150cdf0e10cSrcweir * If a data source is registered at the database context, the name is the registration 151cdf0e10cSrcweir * name. Otherwise, its the URL which the respective database document is based on. 152cdf0e10cSrcweir * 153cdf0e10cSrcweir * Note that the above definition is from the UNO API, not from this wrapper here. 154cdf0e10cSrcweir */ getName()155cdf0e10cSrcweir public String getName() 156cdf0e10cSrcweir { 157cdf0e10cSrcweir return (String)impl_getPropertyValue( "Name" ); 158cdf0e10cSrcweir } 159*170fb961SPedro Giffuni } 160