1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package mod._dbaccess;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir //import com.sun.star.awt.XControl;
26cdf0e10cSrcweir //import com.sun.star.awt.XControlModel;
27cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException;
28cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException;
29cdf0e10cSrcweir import com.sun.star.sdbc.SQLException;
30cdf0e10cSrcweir import com.sun.star.sdbc.XConnection;
31cdf0e10cSrcweir import com.sun.star.uno.Exception;
32cdf0e10cSrcweir import java.io.PrintWriter;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import lib.TestCase;
35cdf0e10cSrcweir import lib.TestEnvironment;
36cdf0e10cSrcweir import lib.TestParameters;
37cdf0e10cSrcweir import util.DesktopTools;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
40cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
41cdf0e10cSrcweir import com.sun.star.frame.XController;
42cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
43cdf0e10cSrcweir import com.sun.star.frame.XDispatch;
44cdf0e10cSrcweir import com.sun.star.frame.XDispatchProvider;
45cdf0e10cSrcweir import com.sun.star.frame.XFrame;
46cdf0e10cSrcweir import com.sun.star.frame.XModel;
47cdf0e10cSrcweir import com.sun.star.lang.XInitialization;
48cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
49cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
50cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
51cdf0e10cSrcweir import com.sun.star.uno.XInterface;
52cdf0e10cSrcweir import com.sun.star.util.URL;
53cdf0e10cSrcweir import lib.StatusException;
54cdf0e10cSrcweir import util.SOfficeFactory;
55cdf0e10cSrcweir import com.sun.star.sdb.XDocumentDataSource;
56cdf0e10cSrcweir import com.sun.star.sdbc.XDataSource;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir public class OQueryDesign extends TestCase {
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     private static XDesktop xDesk;
61cdf0e10cSrcweir     private static XFrame xFrame;
62cdf0e10cSrcweir     private final String sDataSourceName = "Bibliography";
63cdf0e10cSrcweir     private static XConnection xConn;
64cdf0e10cSrcweir     private static XTextDocument xTextDoc;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     /**
68cdf0e10cSrcweir      * Creates the Desktop service (<code>com.sun.star.frame.Desktop</code>).
69cdf0e10cSrcweir      */
initialize(TestParameters Param, PrintWriter log)70cdf0e10cSrcweir     protected void initialize(TestParameters Param, PrintWriter log) {
71cdf0e10cSrcweir         xDesk = (XDesktop) UnoRuntime.queryInterface(
72cdf0e10cSrcweir                     XDesktop.class, DesktopTools.createDesktop((XMultiServiceFactory)Param.getMSF()) );
73cdf0e10cSrcweir     }
74cdf0e10cSrcweir 
createTestEnvironment(TestParameters Param, PrintWriter log)75cdf0e10cSrcweir     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         log.println( "creating a test environment" );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         XInterface oObj = null;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         XDispatchProvider aProv = (XDispatchProvider)
82cdf0e10cSrcweir                 UnoRuntime.queryInterface(XDispatchProvider.class,xDesk);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         XDispatch getting = null;
85cdf0e10cSrcweir         XMultiServiceFactory xMSF = (XMultiServiceFactory) Param.getMSF();
86cdf0e10cSrcweir 
87cdf0e10cSrcweir       		XNameAccess xNameAccess = null;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir             // we use the first datasource
90cdf0e10cSrcweir     		XDataSource xDS = null;
91cdf0e10cSrcweir         try {
92cdf0e10cSrcweir             xNameAccess = (XNameAccess) UnoRuntime.queryInterface(
93cdf0e10cSrcweir                         XNameAccess.class,
94cdf0e10cSrcweir                         xMSF.createInstance("com.sun.star.sdb.DatabaseContext"));
95cdf0e10cSrcweir         } catch (Exception ex) {
96cdf0e10cSrcweir             ex.printStackTrace( log );
97cdf0e10cSrcweir             throw new StatusException( "Could not get Databasecontext", ex );
98cdf0e10cSrcweir         }
99cdf0e10cSrcweir         try {
100cdf0e10cSrcweir             xDS = (XDataSource) UnoRuntime.queryInterface(
101cdf0e10cSrcweir                     XDataSource.class, xNameAccess.getByName( "Bibliography" ));
102cdf0e10cSrcweir         } catch (NoSuchElementException ex) {
103cdf0e10cSrcweir             ex.printStackTrace( log );
104cdf0e10cSrcweir             throw new StatusException( "Could not get XDataSource", ex );
105cdf0e10cSrcweir         } catch (WrappedTargetException ex) {
106cdf0e10cSrcweir             ex.printStackTrace( log );
107cdf0e10cSrcweir             throw new StatusException( "Could not get XDataSource", ex );
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir         try {
110cdf0e10cSrcweir             xNameAccess = (XNameAccess) UnoRuntime.queryInterface(
111cdf0e10cSrcweir                         XNameAccess.class,
112cdf0e10cSrcweir                         xMSF.createInstance("com.sun.star.sdb.DatabaseContext"));
113cdf0e10cSrcweir         } catch (Exception ex) {
114cdf0e10cSrcweir             ex.printStackTrace( log );
115cdf0e10cSrcweir             throw new StatusException( "Could not get DatabaseConext", ex );
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir             log.println("check XMultiServiceFactory");
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         try {
121cdf0e10cSrcweir             xConn = xDS.getConnection(new String(), new String());
122cdf0e10cSrcweir         } catch (SQLException ex) {
123cdf0e10cSrcweir             ex.printStackTrace( log );
124cdf0e10cSrcweir             throw new StatusException( "Could not get XConnection", ex );
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         log.println( "opening QueryDesign" );
128cdf0e10cSrcweir         URL the_url = new URL();
129cdf0e10cSrcweir         the_url.Complete = ".component:DB/QueryDesign";
130cdf0e10cSrcweir         getting = aProv.queryDispatch(the_url,"Query",12);
131cdf0e10cSrcweir         PropertyValue[] Args = new PropertyValue[2];
132cdf0e10cSrcweir         PropertyValue param1 = new PropertyValue();
133cdf0e10cSrcweir         param1.Name = "DataSourceName";
134cdf0e10cSrcweir         param1.Value = "Bibliography";
135cdf0e10cSrcweir         Args[0] = param1;
136cdf0e10cSrcweir         PropertyValue param2 = new PropertyValue();
137cdf0e10cSrcweir         param2.Name = "QueryDesignView";
138cdf0e10cSrcweir         param2.Value = new Boolean(false);
139cdf0e10cSrcweir         Args[1] = param2;
140cdf0e10cSrcweir         param1.Name = "ActiveConnection";
141cdf0e10cSrcweir         param1.Value = xConn;
142cdf0e10cSrcweir         Args[1] = param2;
143cdf0e10cSrcweir         getting.dispatch(the_url,Args);
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         shortWait();
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         Object oDBC = null;
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         try {
150cdf0e10cSrcweir             oDBC = xMSF.createInstance( "com.sun.star.sdb.DatabaseContext" );
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir         catch( com.sun.star.uno.Exception e ) {
153cdf0e10cSrcweir             throw new StatusException("Could not instantiate DatabaseContext", e) ;
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         Object oDataSource = null;
157cdf0e10cSrcweir         try{
158cdf0e10cSrcweir             XNameAccess xNA = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oDBC);
159cdf0e10cSrcweir             oDataSource = xNA.getByName(sDataSourceName);
160cdf0e10cSrcweir         } catch ( com.sun.star.container.NoSuchElementException e){
161cdf0e10cSrcweir             throw new StatusException("could not get '" + sDataSourceName + "'" , e) ;
162cdf0e10cSrcweir         } catch ( com.sun.star.lang.WrappedTargetException e){
163cdf0e10cSrcweir             throw new StatusException("could not get '" + sDataSourceName + "'" , e) ;
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir         XDocumentDataSource xDDS = (XDocumentDataSource) UnoRuntime.queryInterface(XDocumentDataSource.class, oDataSource);
166cdf0e10cSrcweir //        XModel xMod = (XModel) UnoRuntime.queryInterface(XModel.class, xDDS.getDatabaseDocument ());
167cdf0e10cSrcweir 
168cdf0e10cSrcweir //        Frame = xMod.getCurrentController().getFrame();
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         xFrame = DesktopTools.getCurrentFrame(xMSF);
171cdf0e10cSrcweir 
172cdf0e10cSrcweir          // get an instance of Frame
173cdf0e10cSrcweir         Object oFrame = null;
174cdf0e10cSrcweir         SOfficeFactory SOF = null;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir         SOF = SOfficeFactory.getFactory( xMSF );
177cdf0e10cSrcweir         try {
178cdf0e10cSrcweir             log.println( "creating a textdocument" );
179cdf0e10cSrcweir             xTextDoc = SOF.createTextDoc( null );
180cdf0e10cSrcweir         } catch ( com.sun.star.uno.Exception e ) {
181cdf0e10cSrcweir             e.printStackTrace( log );
182cdf0e10cSrcweir             throw new StatusException( "Could not create document", e );
183cdf0e10cSrcweir         }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir         XModel xDocMod = (XModel) UnoRuntime.queryInterface(XModel.class, xTextDoc);
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         XFrame xTextFrame  = xDocMod.getCurrentController().getFrame();
188cdf0e10cSrcweir 
189cdf0e10cSrcweir         Object[] params = new Object[3];
190cdf0e10cSrcweir         param1 = new PropertyValue();
191cdf0e10cSrcweir         param1.Name = "DataSourceName";
192cdf0e10cSrcweir         param1.Value = "Bibliography";
193cdf0e10cSrcweir         params[0] = param1;
194cdf0e10cSrcweir         param2 = new PropertyValue();
195cdf0e10cSrcweir         param2.Name = "Frame";
196cdf0e10cSrcweir         param2.Value = xTextFrame;
197cdf0e10cSrcweir         params[1] = param2;
198cdf0e10cSrcweir         PropertyValue param3 = new PropertyValue();
199cdf0e10cSrcweir         param3.Name = "QueryDesignView";
200cdf0e10cSrcweir         param3.Value = new Boolean(true);
201cdf0e10cSrcweir         params[2] = param3;
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 
204cdf0e10cSrcweir         oObj = xFrame.getController();
205cdf0e10cSrcweir 
206cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment(oObj);
207cdf0e10cSrcweir 
208cdf0e10cSrcweir         //Adding ObjRelations for XInitialization
209cdf0e10cSrcweir         tEnv.addObjRelation("XInitialization.args", params);
210cdf0e10cSrcweir 
211cdf0e10cSrcweir         //Object[] ExceptionParams = new Object[3];
212cdf0e10cSrcweir         //ExceptionParams = params;
213cdf0e10cSrcweir         //((PropertyValue) ExceptionParams[1]).Value = Frame;
214cdf0e10cSrcweir         Object[] ExceptionParams = new Object[3];
215cdf0e10cSrcweir         PropertyValue ExceptionParam1 = new PropertyValue();
216cdf0e10cSrcweir         ExceptionParam1.Name = "DataSourceName";
217cdf0e10cSrcweir         ExceptionParam1.Value = "Bibliography2";
218cdf0e10cSrcweir         ExceptionParams[0] = ExceptionParam1;
219cdf0e10cSrcweir         PropertyValue ExceptionParam2 = new PropertyValue();
220cdf0e10cSrcweir         ExceptionParam2.Name = "Frame";
221cdf0e10cSrcweir         ExceptionParam2.Value = null;
222cdf0e10cSrcweir         ExceptionParams[1] = ExceptionParam2;
223cdf0e10cSrcweir         PropertyValue ExceptionParam3 = new PropertyValue();
224cdf0e10cSrcweir         ExceptionParam3.Name = "QueryDesignView";
225cdf0e10cSrcweir         ExceptionParam3.Value = new Integer(17);//new Boolean(true);
226cdf0e10cSrcweir         ExceptionParams[2] = ExceptionParam3;
227cdf0e10cSrcweir 
228cdf0e10cSrcweir         tEnv.addObjRelation("XInitialization.ExceptionArgs", ExceptionParams);
229cdf0e10cSrcweir 
230cdf0e10cSrcweir         tEnv.addObjRelation("Frame", xFrame);
231cdf0e10cSrcweir 
232cdf0e10cSrcweir         tEnv.addObjRelation("XInitialization.xIni", getUnititializedObj(Param));
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         log.println("ImplementationName: "+util.utils.getImplName(oObj));
235cdf0e10cSrcweir 
236cdf0e10cSrcweir         return tEnv;
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     } // finish method getTestEnvironment
239cdf0e10cSrcweir 
getUnititializedObj(TestParameters Param)240cdf0e10cSrcweir     private XInitialization getUnititializedObj(TestParameters Param){
241cdf0e10cSrcweir         // creating an object wihch ist not initialized
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         // get a model of a DataSource
244cdf0e10cSrcweir         Object oDBC = null;
245cdf0e10cSrcweir         XMultiServiceFactory xMSF;
246cdf0e10cSrcweir 
247cdf0e10cSrcweir         try {
248cdf0e10cSrcweir             xMSF = (XMultiServiceFactory)Param.getMSF();
249cdf0e10cSrcweir             oDBC = xMSF.createInstance( "com.sun.star.sdb.DatabaseContext" );
250cdf0e10cSrcweir         }
251cdf0e10cSrcweir         catch( com.sun.star.uno.Exception e ) {
252cdf0e10cSrcweir             throw new StatusException("Could not instantiate DatabaseContext", e) ;
253cdf0e10cSrcweir         }
254cdf0e10cSrcweir 
255cdf0e10cSrcweir         Object oDataSource = null;
256cdf0e10cSrcweir         try{
257cdf0e10cSrcweir             XNameAccess xNA = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oDBC);
258cdf0e10cSrcweir             oDataSource = xNA.getByName(sDataSourceName);
259cdf0e10cSrcweir         } catch ( com.sun.star.container.NoSuchElementException e){
260cdf0e10cSrcweir             throw new StatusException("could not get '" + sDataSourceName + "'" , e) ;
261cdf0e10cSrcweir         } catch ( com.sun.star.lang.WrappedTargetException e){
262cdf0e10cSrcweir             throw new StatusException("could not get '" + sDataSourceName + "'" , e) ;
263cdf0e10cSrcweir         }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir         XDocumentDataSource xDDS = (XDocumentDataSource) UnoRuntime.queryInterface(XDocumentDataSource.class, oDataSource);
266cdf0e10cSrcweir         XModel xMod = (XModel) UnoRuntime.queryInterface(XModel.class, xDDS.getDatabaseDocument ());
267cdf0e10cSrcweir 
268cdf0e10cSrcweir         // get an intaces of QueryDesign
269cdf0e10cSrcweir         Object oQueryDesign = null;
270cdf0e10cSrcweir         try{
271cdf0e10cSrcweir             oQueryDesign = xMSF.createInstance("com.sun.star.sdb.QueryDesign");
272cdf0e10cSrcweir         }catch( com.sun.star.uno.Exception e ) {
273cdf0e10cSrcweir             throw new StatusException("Could not instantiate QueryDesign", e) ;
274cdf0e10cSrcweir         }
275cdf0e10cSrcweir 
276cdf0e10cSrcweir         XController xCont = (XController) UnoRuntime.queryInterface(XController.class, oQueryDesign);
277cdf0e10cSrcweir 
278cdf0e10cSrcweir         // marry them all
279cdf0e10cSrcweir         xCont.attachModel(xMod);
280cdf0e10cSrcweir         xMod.connectController(xCont);
281cdf0e10cSrcweir         try{
282cdf0e10cSrcweir             xMod.setCurrentController(xCont);
283cdf0e10cSrcweir         } catch (com.sun.star.container.NoSuchElementException e){
284cdf0e10cSrcweir             throw new StatusException("Could not set controller", e) ;
285cdf0e10cSrcweir         }
286cdf0e10cSrcweir 
287cdf0e10cSrcweir         //xCont.attachFrame(xFrame);
288cdf0e10cSrcweir 
289cdf0e10cSrcweir         return (XInitialization) UnoRuntime.queryInterface(XInitialization.class, oQueryDesign);
290cdf0e10cSrcweir 
291cdf0e10cSrcweir     }
292cdf0e10cSrcweir 
293cdf0e10cSrcweir     @Override
cleanup(TestParameters tParam, PrintWriter log)294cdf0e10cSrcweir     protected void cleanup(TestParameters tParam, PrintWriter log) {
295cdf0e10cSrcweir         try {
296cdf0e10cSrcweir             xConn.close() ;
297cdf0e10cSrcweir             DesktopTools.closeDoc(xFrame);
298cdf0e10cSrcweir             DesktopTools.closeDoc(xTextDoc);
299cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
300cdf0e10cSrcweir             log.println("Can't close the connection") ;
301cdf0e10cSrcweir             e.printStackTrace(log) ;
302cdf0e10cSrcweir         } catch (com.sun.star.lang.DisposedException e) {
303cdf0e10cSrcweir             log.println("Connection was already closed. It's OK.") ;
304cdf0e10cSrcweir         }
305cdf0e10cSrcweir 
306cdf0e10cSrcweir     }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir     /**
309cdf0e10cSrcweir     * Sleeps for 0.2 sec. to allow StarOffice to react on <code>
310cdf0e10cSrcweir     * reset</code> call.
311cdf0e10cSrcweir     */
shortWait()312cdf0e10cSrcweir     private void shortWait() {
313cdf0e10cSrcweir         try {
314cdf0e10cSrcweir             Thread.sleep(5000) ;
315cdf0e10cSrcweir         } catch (InterruptedException e) {
316cdf0e10cSrcweir             System.out.println("While waiting :" + e) ;
317cdf0e10cSrcweir         }
318cdf0e10cSrcweir     }
319cdf0e10cSrcweir 
320cdf0e10cSrcweir }    // finish class oDatasourceBrowser
321cdf0e10cSrcweir 
322