1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OQueryDesign.java,v $
10 * $Revision: 1.11.8.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package mod
._dbaccess
;
32 //import com.sun.star.awt.XControl;
33 //import com.sun.star.awt.XControlModel;
34 import com
.sun
.star
.container
.NoSuchElementException
;
35 import com
.sun
.star
.lang
.WrappedTargetException
;
36 import com
.sun
.star
.sdbc
.SQLException
;
37 import com
.sun
.star
.sdbc
.XConnection
;
38 import com
.sun
.star
.uno
.Exception
;
39 import java
.io
.PrintWriter
;
42 import lib
.TestEnvironment
;
43 import lib
.TestParameters
;
44 import util
.DesktopTools
;
46 import com
.sun
.star
.beans
.PropertyValue
;
47 import com
.sun
.star
.container
.XNameAccess
;
48 import com
.sun
.star
.frame
.XController
;
49 import com
.sun
.star
.frame
.XDesktop
;
50 import com
.sun
.star
.frame
.XDispatch
;
51 import com
.sun
.star
.frame
.XDispatchProvider
;
52 import com
.sun
.star
.frame
.XFrame
;
53 import com
.sun
.star
.frame
.XModel
;
54 import com
.sun
.star
.lang
.XInitialization
;
55 import com
.sun
.star
.lang
.XMultiServiceFactory
;
56 import com
.sun
.star
.text
.XTextDocument
;
57 import com
.sun
.star
.uno
.UnoRuntime
;
58 import com
.sun
.star
.uno
.XInterface
;
59 import com
.sun
.star
.util
.URL
;
60 import lib
.StatusException
;
61 import util
.SOfficeFactory
;
62 import com
.sun
.star
.sdb
.XDocumentDataSource
;
63 import com
.sun
.star
.sdbc
.XDataSource
;
65 public class OQueryDesign
extends TestCase
{
67 private static XDesktop xDesk
;
68 private static XFrame xFrame
;
69 private final String sDataSourceName
= "Bibliography";
70 private static XConnection xConn
;
71 private static XTextDocument xTextDoc
;
75 * Creates the Desktop service (<code>com.sun.star.frame.Desktop</code>).
77 protected void initialize(TestParameters Param
, PrintWriter log
) {
78 xDesk
= (XDesktop
) UnoRuntime
.queryInterface(
79 XDesktop
.class, DesktopTools
.createDesktop((XMultiServiceFactory
)Param
.getMSF()) );
82 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
84 log
.println( "creating a test environment" );
86 XInterface oObj
= null;
88 XDispatchProvider aProv
= (XDispatchProvider
)
89 UnoRuntime
.queryInterface(XDispatchProvider
.class,xDesk
);
91 XDispatch getting
= null;
92 XMultiServiceFactory xMSF
= (XMultiServiceFactory
) Param
.getMSF();
94 XNameAccess xNameAccess
= null;
96 // we use the first datasource
97 XDataSource xDS
= null;
99 xNameAccess
= (XNameAccess
) UnoRuntime
.queryInterface(
101 xMSF
.createInstance("com.sun.star.sdb.DatabaseContext"));
102 } catch (Exception ex
) {
103 ex
.printStackTrace( log
);
104 throw new StatusException( "Could not get Databasecontext", ex
);
107 xDS
= (XDataSource
) UnoRuntime
.queryInterface(
108 XDataSource
.class, xNameAccess
.getByName( "Bibliography" ));
109 } catch (NoSuchElementException ex
) {
110 ex
.printStackTrace( log
);
111 throw new StatusException( "Could not get XDataSource", ex
);
112 } catch (WrappedTargetException ex
) {
113 ex
.printStackTrace( log
);
114 throw new StatusException( "Could not get XDataSource", ex
);
117 xNameAccess
= (XNameAccess
) UnoRuntime
.queryInterface(
119 xMSF
.createInstance("com.sun.star.sdb.DatabaseContext"));
120 } catch (Exception ex
) {
121 ex
.printStackTrace( log
);
122 throw new StatusException( "Could not get DatabaseConext", ex
);
125 log
.println("check XMultiServiceFactory");
128 xConn
= xDS
.getConnection(new String(), new String());
129 } catch (SQLException ex
) {
130 ex
.printStackTrace( log
);
131 throw new StatusException( "Could not get XConnection", ex
);
134 log
.println( "opening QueryDesign" );
135 URL the_url
= new URL();
136 the_url
.Complete
= ".component:DB/QueryDesign";
137 getting
= aProv
.queryDispatch(the_url
,"Query",12);
138 PropertyValue
[] Args
= new PropertyValue
[2];
139 PropertyValue param1
= new PropertyValue();
140 param1
.Name
= "DataSourceName";
141 param1
.Value
= "Bibliography";
143 PropertyValue param2
= new PropertyValue();
144 param2
.Name
= "QueryDesignView";
145 param2
.Value
= new Boolean(false);
147 param1
.Name
= "ActiveConnection";
148 param1
.Value
= xConn
;
150 getting
.dispatch(the_url
,Args
);
157 oDBC
= xMSF
.createInstance( "com.sun.star.sdb.DatabaseContext" );
159 catch( com
.sun
.star
.uno
.Exception e
) {
160 throw new StatusException("Could not instantiate DatabaseContext", e
) ;
163 Object oDataSource
= null;
165 XNameAccess xNA
= (XNameAccess
) UnoRuntime
.queryInterface(XNameAccess
.class, oDBC
);
166 oDataSource
= xNA
.getByName(sDataSourceName
);
167 } catch ( com
.sun
.star
.container
.NoSuchElementException e
){
168 throw new StatusException("could not get '" + sDataSourceName
+ "'" , e
) ;
169 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
){
170 throw new StatusException("could not get '" + sDataSourceName
+ "'" , e
) ;
172 XDocumentDataSource xDDS
= (XDocumentDataSource
) UnoRuntime
.queryInterface(XDocumentDataSource
.class, oDataSource
);
173 // XModel xMod = (XModel) UnoRuntime.queryInterface(XModel.class, xDDS.getDatabaseDocument ());
175 // Frame = xMod.getCurrentController().getFrame();
177 xFrame
= DesktopTools
.getCurrentFrame(xMSF
);
179 // get an instance of Frame
180 Object oFrame
= null;
181 SOfficeFactory SOF
= null;
183 SOF
= SOfficeFactory
.getFactory( xMSF
);
185 log
.println( "creating a textdocument" );
186 xTextDoc
= SOF
.createTextDoc( null );
187 } catch ( com
.sun
.star
.uno
.Exception e
) {
188 e
.printStackTrace( log
);
189 throw new StatusException( "Could not create document", e
);
192 XModel xDocMod
= (XModel
) UnoRuntime
.queryInterface(XModel
.class, xTextDoc
);
194 XFrame xTextFrame
= xDocMod
.getCurrentController().getFrame();
196 Object
[] params
= new Object
[3];
197 param1
= new PropertyValue();
198 param1
.Name
= "DataSourceName";
199 param1
.Value
= "Bibliography";
201 param2
= new PropertyValue();
202 param2
.Name
= "Frame";
203 param2
.Value
= xTextFrame
;
205 PropertyValue param3
= new PropertyValue();
206 param3
.Name
= "QueryDesignView";
207 param3
.Value
= new Boolean(true);
211 oObj
= xFrame
.getController();
213 TestEnvironment tEnv
= new TestEnvironment(oObj
);
215 //Adding ObjRelations for XInitialization
216 tEnv
.addObjRelation("XInitialization.args", params
);
218 //Object[] ExceptionParams = new Object[3];
219 //ExceptionParams = params;
220 //((PropertyValue) ExceptionParams[1]).Value = Frame;
221 Object
[] ExceptionParams
= new Object
[3];
222 PropertyValue ExceptionParam1
= new PropertyValue();
223 ExceptionParam1
.Name
= "DataSourceName";
224 ExceptionParam1
.Value
= "Bibliography2";
225 ExceptionParams
[0] = ExceptionParam1
;
226 PropertyValue ExceptionParam2
= new PropertyValue();
227 ExceptionParam2
.Name
= "Frame";
228 ExceptionParam2
.Value
= null;
229 ExceptionParams
[1] = ExceptionParam2
;
230 PropertyValue ExceptionParam3
= new PropertyValue();
231 ExceptionParam3
.Name
= "QueryDesignView";
232 ExceptionParam3
.Value
= new Integer(17);//new Boolean(true);
233 ExceptionParams
[2] = ExceptionParam3
;
235 tEnv
.addObjRelation("XInitialization.ExceptionArgs", ExceptionParams
);
237 tEnv
.addObjRelation("Frame", xFrame
);
239 tEnv
.addObjRelation("XInitialization.xIni", getUnititializedObj(Param
));
241 log
.println("ImplementationName: "+util
.utils
.getImplName(oObj
));
245 } // finish method getTestEnvironment
247 private XInitialization
getUnititializedObj(TestParameters Param
){
248 // creating an object wihch ist not initialized
250 // get a model of a DataSource
252 XMultiServiceFactory xMSF
;
255 xMSF
= (XMultiServiceFactory
)Param
.getMSF();
256 oDBC
= xMSF
.createInstance( "com.sun.star.sdb.DatabaseContext" );
258 catch( com
.sun
.star
.uno
.Exception e
) {
259 throw new StatusException("Could not instantiate DatabaseContext", e
) ;
262 Object oDataSource
= null;
264 XNameAccess xNA
= (XNameAccess
) UnoRuntime
.queryInterface(XNameAccess
.class, oDBC
);
265 oDataSource
= xNA
.getByName(sDataSourceName
);
266 } catch ( com
.sun
.star
.container
.NoSuchElementException e
){
267 throw new StatusException("could not get '" + sDataSourceName
+ "'" , e
) ;
268 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
){
269 throw new StatusException("could not get '" + sDataSourceName
+ "'" , e
) ;
272 XDocumentDataSource xDDS
= (XDocumentDataSource
) UnoRuntime
.queryInterface(XDocumentDataSource
.class, oDataSource
);
273 XModel xMod
= (XModel
) UnoRuntime
.queryInterface(XModel
.class, xDDS
.getDatabaseDocument ());
275 // get an intaces of QueryDesign
276 Object oQueryDesign
= null;
278 oQueryDesign
= xMSF
.createInstance("com.sun.star.sdb.QueryDesign");
279 }catch( com
.sun
.star
.uno
.Exception e
) {
280 throw new StatusException("Could not instantiate QueryDesign", e
) ;
283 XController xCont
= (XController
) UnoRuntime
.queryInterface(XController
.class, oQueryDesign
);
286 xCont
.attachModel(xMod
);
287 xMod
.connectController(xCont
);
289 xMod
.setCurrentController(xCont
);
290 } catch (com
.sun
.star
.container
.NoSuchElementException e
){
291 throw new StatusException("Could not set controller", e
) ;
294 //xCont.attachFrame(xFrame);
296 return (XInitialization
) UnoRuntime
.queryInterface(XInitialization
.class, oQueryDesign
);
301 protected void cleanup(TestParameters tParam
, PrintWriter log
) {
304 DesktopTools
.closeDoc(xFrame
);
305 DesktopTools
.closeDoc(xTextDoc
);
306 } catch (com
.sun
.star
.uno
.Exception e
) {
307 log
.println("Can't close the connection") ;
308 e
.printStackTrace(log
) ;
309 } catch (com
.sun
.star
.lang
.DisposedException e
) {
310 log
.println("Connection was already closed. It's OK.") ;
316 * Sleeps for 0.2 sec. to allow StarOffice to react on <code>
319 private void shortWait() {
322 } catch (InterruptedException e
) {
323 System
.out
.println("While waiting :" + e
) ;
327 } // finish class oDatasourceBrowser