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 connectivity.tools; 24 25 import com.sun.star.container.ElementExistException; 26 import com.sun.star.container.NoSuchElementException; 27 import com.sun.star.container.XNameAccess; 28 import com.sun.star.container.XNameContainer; 29 import com.sun.star.lang.WrappedTargetException; 30 import com.sun.star.lang.XSingleServiceFactory; 31 import com.sun.star.lang.XMultiServiceFactory; 32 import com.sun.star.beans.XPropertySet; 33 import com.sun.star.sdb.XQueryDefinitionsSupplier; 34 import com.sun.star.sdbc.XDataSource; 35 import com.sun.star.uno.Exception; 36 import com.sun.star.uno.UnoRuntime; 37 import java.util.logging.Level; 38 import java.util.logging.Logger; 39 40 public class DataSource 41 { 42 // the service factory 43 44 private final XMultiServiceFactory m_orb; 45 private XDataSource m_dataSource; 46 DataSource(final XMultiServiceFactory _orb, final String _registeredName)47 public DataSource(final XMultiServiceFactory _orb, final String _registeredName) throws Exception 48 { 49 m_orb = _orb; 50 51 final XNameAccess dbContext = UnoRuntime.queryInterface( 52 XNameAccess.class, _orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) ); 53 54 m_dataSource = UnoRuntime.queryInterface( XDataSource.class, dbContext.getByName( _registeredName ) ); 55 } 56 DataSource(final XMultiServiceFactory _orb,final XDataSource _dataSource)57 public DataSource(final XMultiServiceFactory _orb,final XDataSource _dataSource) 58 { 59 m_orb = _orb; 60 m_dataSource = _dataSource; 61 } 62 getXDataSource()63 final public XDataSource getXDataSource() 64 { 65 return m_dataSource; 66 } 67 68 /** 69 * retrieves the data source's settings 70 */ geSettings()71 public XPropertySet geSettings() 72 { 73 return UnoRuntime.queryInterface( XPropertySet.class, impl_getPropertyValue( "Settings" ) ); 74 } 75 76 /** creates a query with a given name and SQL command 77 */ createQuery(final String _name, final String _sqlCommand)78 public void createQuery(final String _name, final String _sqlCommand) throws ElementExistException, WrappedTargetException, com.sun.star.lang.IllegalArgumentException 79 { 80 createQuery(_name, _sqlCommand, true); 81 } 82 83 /** creates a query with a given name, SQL command, and EscapeProcessing flag 84 */ createQuery(final String _name, final String _sqlCommand, final boolean _escapeProcessing)85 public void createQuery(final String _name, final String _sqlCommand, final boolean _escapeProcessing) throws ElementExistException, WrappedTargetException, com.sun.star.lang.IllegalArgumentException 86 { 87 final XSingleServiceFactory queryDefsFac = UnoRuntime.queryInterface( XSingleServiceFactory.class, getQueryDefinitions() ); 88 XPropertySet queryDef = null; 89 try 90 { 91 queryDef = UnoRuntime.queryInterface( XPropertySet.class, queryDefsFac.createInstance() ); 92 queryDef.setPropertyValue("Command", _sqlCommand); 93 queryDef.setPropertyValue("EscapeProcessing", Boolean.valueOf(_escapeProcessing)); 94 } 95 catch (com.sun.star.uno.Exception e) 96 { 97 e.printStackTrace(System.err); 98 } 99 100 final XNameContainer queryDefsContainer = UnoRuntime.queryInterface( XNameContainer.class, getQueryDefinitions() ); 101 queryDefsContainer.insertByName(_name, queryDef); 102 } 103 104 /** provides the query definition with the given name 105 */ getQueryDefinition(final String _name)106 public QueryDefinition getQueryDefinition(final String _name) throws NoSuchElementException 107 { 108 final XNameAccess allDefs = getQueryDefinitions(); 109 try 110 { 111 return new QueryDefinition( UnoRuntime.queryInterface( XPropertySet.class, allDefs.getByName( _name) ) ); 112 } 113 catch (WrappedTargetException e) 114 { 115 } 116 throw new NoSuchElementException(); 117 } 118 119 /** provides the container of query definitions of the data source 120 */ getQueryDefinitions()121 public XNameAccess getQueryDefinitions() 122 { 123 final XQueryDefinitionsSupplier suppQueries = UnoRuntime.queryInterface( 124 XQueryDefinitionsSupplier.class, m_dataSource); 125 return suppQueries.getQueryDefinitions(); 126 } 127 128 /** 129 * retrieves a property value from the data source 130 * @param i_propertyName 131 * the name of the property whose value is to be returned. 132 */ impl_getPropertyValue( final String i_propertyName )133 private Object impl_getPropertyValue( final String i_propertyName ) 134 { 135 Object propertyValue = null; 136 try 137 { 138 final XPropertySet dataSourceProps = UnoRuntime.queryInterface( XPropertySet.class, m_dataSource ); 139 propertyValue = dataSourceProps.getPropertyValue( i_propertyName ); 140 } 141 catch (Exception ex) 142 { 143 Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex); 144 } 145 return propertyValue; 146 } 147 148 /** returns the name of the data source 149 * 150 * If a data source is registered at the database context, the name is the registration 151 * name. Otherwise, its the URL which the respective database document is based on. 152 * 153 * Note that the above definition is from the UNO API, not from this wrapper here. 154 */ getName()155 public String getName() 156 { 157 return (String)impl_getPropertyValue( "Name" ); 158 } 159 } 160