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