1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import java.io.*;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir import com.sun.star.comp.helper.RegistryServiceFactory;
38*cdf0e10cSrcweir import com.sun.star.comp.servicemanager.ServiceManager;
39*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
40*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
41*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
42*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
43*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
44*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
45*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
46*cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
47*cdf0e10cSrcweir import com.sun.star.sdbc.*;
48*cdf0e10cSrcweir import com.sun.star.sdbcx.Privilege;
49*cdf0e10cSrcweir import com.sun.star.sdb.CommandType;
50*cdf0e10cSrcweir import com.sun.star.sdb.XRowSetApproveBroadcaster;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir public class RowSet
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir     private static XComponentContext xContext = null;
55*cdf0e10cSrcweir     private static XMultiComponentFactory xMCF = null;
56*cdf0e10cSrcweir 	public static void main(String argv[]) throws java.lang.Exception
57*cdf0e10cSrcweir 	{
58*cdf0e10cSrcweir         try {
59*cdf0e10cSrcweir             // get the remote office component context
60*cdf0e10cSrcweir             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
61*cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
62*cdf0e10cSrcweir             xMCF = xContext.getServiceManager();
63*cdf0e10cSrcweir         }
64*cdf0e10cSrcweir         catch( Exception e) {
65*cdf0e10cSrcweir             System.err.println("ERROR: can't get a component context from a running office ...");
66*cdf0e10cSrcweir             e.printStackTrace(System.out);
67*cdf0e10cSrcweir             System.exit(1);
68*cdf0e10cSrcweir         }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir         try{
71*cdf0e10cSrcweir 			showRowSetEvents();
72*cdf0e10cSrcweir 			showRowSetRowCount();
73*cdf0e10cSrcweir 			showRowSetPrivileges();
74*cdf0e10cSrcweir 			useRowSet();
75*cdf0e10cSrcweir 		}
76*cdf0e10cSrcweir 		catch(com.sun.star.uno.Exception e)
77*cdf0e10cSrcweir 		{
78*cdf0e10cSrcweir 			System.err.println(e);
79*cdf0e10cSrcweir 			e.printStackTrace();
80*cdf0e10cSrcweir 		}
81*cdf0e10cSrcweir 		System.exit(0);
82*cdf0e10cSrcweir 	}
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 	public static void printDataSources() throws com.sun.star.uno.Exception
85*cdf0e10cSrcweir 	{
86*cdf0e10cSrcweir 		// create a DatabaseContext and print all DataSource names
87*cdf0e10cSrcweir 		XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(
88*cdf0e10cSrcweir             XNameAccess.class,
89*cdf0e10cSrcweir             xMCF.createInstanceWithContext("com.sun.star.sdb.DatabaseContext",
90*cdf0e10cSrcweir                                            xContext));
91*cdf0e10cSrcweir 		String aNames [] = xNameAccess.getElementNames();
92*cdf0e10cSrcweir 		for(int i=0;i<aNames.length;++i)
93*cdf0e10cSrcweir 			System.out.println(aNames[i]);
94*cdf0e10cSrcweir 	}
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 	public static void useRowSet() throws com.sun.star.uno.Exception
97*cdf0e10cSrcweir 	{
98*cdf0e10cSrcweir 		// first we create our RowSet object
99*cdf0e10cSrcweir 		XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface(
100*cdf0e10cSrcweir             XRowSet.class,
101*cdf0e10cSrcweir             xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext));
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 		System.out.println("RowSet created!");
104*cdf0e10cSrcweir 		// set the properties needed to connect to a database
105*cdf0e10cSrcweir 		XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
106*cdf0e10cSrcweir 		xProp.setPropertyValue("DataSourceName","Bibliography");
107*cdf0e10cSrcweir 		xProp.setPropertyValue("Command","biblio");
108*cdf0e10cSrcweir 		xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 		xRowRes.execute();
111*cdf0e10cSrcweir 		System.out.println("RowSet executed!");
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 		XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes);
115*cdf0e10cSrcweir 		xComp.dispose();
116*cdf0e10cSrcweir 		System.out.println("RowSet destroyed!");
117*cdf0e10cSrcweir 	}
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	public static void showRowSetPrivileges() throws com.sun.star.uno.Exception
120*cdf0e10cSrcweir 	{
121*cdf0e10cSrcweir 		// first we create our RowSet object
122*cdf0e10cSrcweir 		XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface(
123*cdf0e10cSrcweir             XRowSet.class,
124*cdf0e10cSrcweir             xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext));
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 		System.out.println("RowSet created!");
127*cdf0e10cSrcweir 		// set the properties needed to connect to a database
128*cdf0e10cSrcweir 		XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
129*cdf0e10cSrcweir 		xProp.setPropertyValue("DataSourceName","Bibliography");
130*cdf0e10cSrcweir 		xProp.setPropertyValue("Command","biblio");
131*cdf0e10cSrcweir 		xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 		xRowRes.execute();
134*cdf0e10cSrcweir 		System.out.println("RowSet executed!");
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 		Integer aPriv = (Integer)xProp.getPropertyValue("Privileges");
137*cdf0e10cSrcweir 		int nPriv  = aPriv.intValue();
138*cdf0e10cSrcweir 		if( (nPriv & Privilege.SELECT) == Privilege.SELECT)
139*cdf0e10cSrcweir 			System.out.println("SELECT");
140*cdf0e10cSrcweir 		if( (nPriv & Privilege.INSERT) == Privilege.INSERT)
141*cdf0e10cSrcweir 			System.out.println("INSERT");
142*cdf0e10cSrcweir 		if( (nPriv & Privilege.UPDATE) == Privilege.UPDATE)
143*cdf0e10cSrcweir 			System.out.println("UPDATE");
144*cdf0e10cSrcweir 		if( (nPriv & Privilege.DELETE) == Privilege.DELETE)
145*cdf0e10cSrcweir 			System.out.println("DELETE");
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 		// now destroy the RowSet
148*cdf0e10cSrcweir 		XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes);
149*cdf0e10cSrcweir 		xComp.dispose();
150*cdf0e10cSrcweir 		System.out.println("RowSet destroyed!");
151*cdf0e10cSrcweir 	}
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 	public static void showRowSetRowCount() throws com.sun.star.uno.Exception
154*cdf0e10cSrcweir 	{
155*cdf0e10cSrcweir 		// first we create our RowSet object
156*cdf0e10cSrcweir 		XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface(
157*cdf0e10cSrcweir             XRowSet.class,
158*cdf0e10cSrcweir             xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext));
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 		System.out.println("RowSet created!");
161*cdf0e10cSrcweir 		// set the properties needed to connect to a database
162*cdf0e10cSrcweir 		XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
163*cdf0e10cSrcweir 		xProp.setPropertyValue("DataSourceName","Bibliography");
164*cdf0e10cSrcweir 		xProp.setPropertyValue("Command","biblio");
165*cdf0e10cSrcweir 		xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 		xRowRes.execute();
168*cdf0e10cSrcweir 		System.out.println("RowSet executed!");
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 		// now look if the RowCount is already final
171*cdf0e10cSrcweir 		System.out.println("The RowCount is final: " + xProp.getPropertyValue("IsRowCountFinal"));
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 		XResultSet xRes = (XResultSet)UnoRuntime.queryInterface(XResultSet.class,xRowRes);
174*cdf0e10cSrcweir 		xRes.last();
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 		System.out.println("The RowCount is final: " + xProp.getPropertyValue("IsRowCountFinal"));
177*cdf0e10cSrcweir 		System.out.println("There are " + xProp.getPropertyValue("RowCount") + " rows!");
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 		// now destroy the RowSet
180*cdf0e10cSrcweir 		XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes);
181*cdf0e10cSrcweir 		xComp.dispose();
182*cdf0e10cSrcweir 		System.out.println("RowSet destroyed!");
183*cdf0e10cSrcweir 	}
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 	public static void showRowSetEvents() throws com.sun.star.uno.Exception
186*cdf0e10cSrcweir 	{
187*cdf0e10cSrcweir 		// first we create our RowSet object
188*cdf0e10cSrcweir 		XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface(
189*cdf0e10cSrcweir             XRowSet.class,
190*cdf0e10cSrcweir             xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext));
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 		System.out.println("RowSet created!");
193*cdf0e10cSrcweir 		// add our Listener
194*cdf0e10cSrcweir 		System.out.println("Append our Listener!");
195*cdf0e10cSrcweir 		RowSetEventListener pRow = new RowSetEventListener();
196*cdf0e10cSrcweir 		XRowSetApproveBroadcaster xApBroad = (XRowSetApproveBroadcaster)UnoRuntime.queryInterface(XRowSetApproveBroadcaster.class,xRowRes);
197*cdf0e10cSrcweir 		xApBroad.addRowSetApproveListener(pRow);
198*cdf0e10cSrcweir 		xRowRes.addRowSetListener(pRow);
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 		// set the properties needed to connect to a database
201*cdf0e10cSrcweir 		XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
202*cdf0e10cSrcweir 		xProp.setPropertyValue("DataSourceName","Bibliography");
203*cdf0e10cSrcweir 		xProp.setPropertyValue("Command","biblio");
204*cdf0e10cSrcweir 		xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 		xRowRes.execute();
207*cdf0e10cSrcweir 		System.out.println("RowSet executed!");
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 		// do some movements to check if we got all notifications
210*cdf0e10cSrcweir 		XResultSet xRes = (XResultSet)UnoRuntime.queryInterface(XResultSet.class,xRowRes);
211*cdf0e10cSrcweir 		System.out.println("beforeFirst");
212*cdf0e10cSrcweir 		xRes.beforeFirst();
213*cdf0e10cSrcweir 		// this should lead to no notifications because
214*cdf0e10cSrcweir 		// we should stand before the first row at the beginning
215*cdf0e10cSrcweir 		System.out.println("We stand before the first row: " + xRes.isBeforeFirst());
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 		System.out.println("next");
218*cdf0e10cSrcweir 		xRes.next();
219*cdf0e10cSrcweir 		System.out.println("next");
220*cdf0e10cSrcweir 		xRes.next();
221*cdf0e10cSrcweir 		System.out.println("last");
222*cdf0e10cSrcweir 		xRes.last();
223*cdf0e10cSrcweir 		System.out.println("next");
224*cdf0e10cSrcweir 		xRes.next();
225*cdf0e10cSrcweir 		System.out.println("We stand after the last row: " + xRes.isAfterLast());
226*cdf0e10cSrcweir 		System.out.println("first");
227*cdf0e10cSrcweir 		xRes.first();
228*cdf0e10cSrcweir 		System.out.println("previous");
229*cdf0e10cSrcweir 		xRes.previous();
230*cdf0e10cSrcweir 		System.out.println("We stand before the first row: " + xRes.isBeforeFirst());
231*cdf0e10cSrcweir 		System.out.println("afterLast");
232*cdf0e10cSrcweir 		xRes.afterLast();
233*cdf0e10cSrcweir 		System.out.println("We stand after the last row: " + xRes.isAfterLast());
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 		// now destroy the RowSet
236*cdf0e10cSrcweir 		XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes);
237*cdf0e10cSrcweir 		xComp.dispose();
238*cdf0e10cSrcweir 		System.out.println("RowSet destroyed!");
239*cdf0e10cSrcweir 	}
240*cdf0e10cSrcweir }
241*cdf0e10cSrcweir 
242