xref: /AOO41X/main/odk/examples/DevelopersGuide/Database/sdbcx.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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.lang.XComponent;
38*cdf0e10cSrcweir import com.sun.star.uno.*;
39*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
40*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
41*cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
42*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
43*cdf0e10cSrcweir import com.sun.star.sdbc.*;
44*cdf0e10cSrcweir import com.sun.star.sdbcx.*;
45*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir public class sdbcx
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir 	private XMultiServiceFactory xORB;
50*cdf0e10cSrcweir 	private static XConnection con;
51*cdf0e10cSrcweir 	private XTablesSupplier xTabSup;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir         public static XMultiServiceFactory rSmgr;
54*cdf0e10cSrcweir 	public static void main(String argv[]) throws java.lang.Exception
55*cdf0e10cSrcweir 	{
56*cdf0e10cSrcweir 		try{
57*cdf0e10cSrcweir 			rSmgr = connect("socket,host=localhost,port=8100");
58*cdf0e10cSrcweir                         sdbcx test = new sdbcx(rSmgr);
59*cdf0e10cSrcweir                         test.createConnection();
60*cdf0e10cSrcweir                         test.displayTableProperties();
61*cdf0e10cSrcweir                         // now we dispose the connection to close it
62*cdf0e10cSrcweir                         XComponent xComponent = (XComponent)UnoRuntime.queryInterface(XComponent.class,con);
63*cdf0e10cSrcweir                         if(xComponent != null)
64*cdf0e10cSrcweir                         {
65*cdf0e10cSrcweir                                 xComponent.dispose();
66*cdf0e10cSrcweir                                 System.out.println("Connection disposed!");
67*cdf0e10cSrcweir                         }
68*cdf0e10cSrcweir                 }
69*cdf0e10cSrcweir 		catch(com.sun.star.uno.Exception e)
70*cdf0e10cSrcweir 		{
71*cdf0e10cSrcweir 			System.out.println(e);
72*cdf0e10cSrcweir 			e.printStackTrace();
73*cdf0e10cSrcweir 		}
74*cdf0e10cSrcweir 		System.exit(0);
75*cdf0e10cSrcweir         }
76*cdf0e10cSrcweir         public static XMultiServiceFactory connect( String connectStr )
77*cdf0e10cSrcweir 		throws com.sun.star.uno.Exception,
78*cdf0e10cSrcweir 		com.sun.star.uno.RuntimeException, java.lang.Exception
79*cdf0e10cSrcweir 	{
80*cdf0e10cSrcweir 		// initial serviceManager
81*cdf0e10cSrcweir 		XMultiServiceFactory xLocalServiceManager =
82*cdf0e10cSrcweir 			com.sun.star.comp.helper.Bootstrap.createSimpleServiceManager();
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 		// create a connector, so that it can contact the office
85*cdf0e10cSrcweir 		Object  xUrlResolver  = xLocalServiceManager.createInstance( "com.sun.star.bridge.UnoUrlResolver" );
86*cdf0e10cSrcweir 		XUnoUrlResolver urlResolver = (XUnoUrlResolver)UnoRuntime.queryInterface(
87*cdf0e10cSrcweir             XUnoUrlResolver.class, xUrlResolver );
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 		Object rInitialObject = urlResolver.resolve( "uno:" + connectStr + ";urp;StarOffice.NamingService" );
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 		XNamingService rName = (XNamingService)UnoRuntime.queryInterface(
92*cdf0e10cSrcweir             XNamingService.class, rInitialObject );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 		XMultiServiceFactory xMSF = null;
95*cdf0e10cSrcweir 		if( rName != null ) {
96*cdf0e10cSrcweir 			System.err.println( "got the remote naming service !" );
97*cdf0e10cSrcweir 			Object rXsmgr = rName.getRegisteredObject("StarOffice.ServiceManager" );
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 			xMSF = (XMultiServiceFactory)
100*cdf0e10cSrcweir 				UnoRuntime.queryInterface( XMultiServiceFactory.class, rXsmgr );
101*cdf0e10cSrcweir 		}
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 		return ( xMSF );
104*cdf0e10cSrcweir 	}
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 	public sdbcx(XMultiServiceFactory rSmgr )
108*cdf0e10cSrcweir 	{
109*cdf0e10cSrcweir 		xORB = rSmgr;
110*cdf0e10cSrcweir 	}
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 	public void createConnection() throws com.sun.star.uno.Exception
113*cdf0e10cSrcweir 	{
114*cdf0e10cSrcweir 		// create the Driver with the implementation name
115*cdf0e10cSrcweir 		Object aDriver = xORB.createInstance("com.sun.star.comp.sdbcx.adabas.ODriver");
116*cdf0e10cSrcweir 		// query for the interface
117*cdf0e10cSrcweir 		com.sun.star.sdbc.XDriver xDriver;
118*cdf0e10cSrcweir 		xDriver = (XDriver)UnoRuntime.queryInterface(XDriver.class,aDriver);
119*cdf0e10cSrcweir 		if(xDriver != null)
120*cdf0e10cSrcweir 		{
121*cdf0e10cSrcweir 			// first create the needed url
122*cdf0e10cSrcweir 			String adabasURL = "sdbc:adabas::MYDB0";
123*cdf0e10cSrcweir 			// second create the necessary properties
124*cdf0e10cSrcweir 			com.sun.star.beans.PropertyValue [] adabasProps = new com.sun.star.beans.PropertyValue[]
125*cdf0e10cSrcweir 			{
126*cdf0e10cSrcweir 				new com.sun.star.beans.PropertyValue("user",0,"test1",com.sun.star.beans.PropertyState.DIRECT_VALUE),
127*cdf0e10cSrcweir 				new com.sun.star.beans.PropertyValue("password",0,"test1",com.sun.star.beans.PropertyState.DIRECT_VALUE)
128*cdf0e10cSrcweir 			};
129*cdf0e10cSrcweir 			//
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 			// now create a connection to adabas
132*cdf0e10cSrcweir 			con = xDriver.connect(adabasURL,adabasProps);
133*cdf0e10cSrcweir 			if(con != null)
134*cdf0e10cSrcweir 			{
135*cdf0e10cSrcweir 				System.out.println("Connection could be created!");
136*cdf0e10cSrcweir 				// we the XDatabaseDefinitionSupplier interface from the driver to get the XTablesSupplier
137*cdf0e10cSrcweir 				XDataDefinitionSupplier xDDSup = (XDataDefinitionSupplier)UnoRuntime.queryInterface(
138*cdf0e10cSrcweir 						XDataDefinitionSupplier.class,xDriver);
139*cdf0e10cSrcweir 				if(xDDSup != null)
140*cdf0e10cSrcweir 				{
141*cdf0e10cSrcweir 					xTabSup = xDDSup.getDataDefinitionByConnection(con);
142*cdf0e10cSrcweir 					if(xTabSup != null)
143*cdf0e10cSrcweir 					{
144*cdf0e10cSrcweir 						XNameAccess xTables = xTabSup.getTables();
145*cdf0e10cSrcweir 						// now print all table names
146*cdf0e10cSrcweir 						System.out.println("Tables available:");
147*cdf0e10cSrcweir 						String [] aTableNames = xTables.getElementNames();
148*cdf0e10cSrcweir 						for ( int i =0; i<= aTableNames.length-1; i++)
149*cdf0e10cSrcweir 							System.out.println(aTableNames[i]);
150*cdf0e10cSrcweir 					}
151*cdf0e10cSrcweir 				}
152*cdf0e10cSrcweir 				else
153*cdf0e10cSrcweir 					System.out.println("The driver is not a SDBCX capable!");
154*cdf0e10cSrcweir 			}
155*cdf0e10cSrcweir 			else
156*cdf0e10cSrcweir 				System.out.println("Connection could not be created!");
157*cdf0e10cSrcweir 		}
158*cdf0e10cSrcweir 	}
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	public void displayTableProperties() throws com.sun.star.uno.Exception
161*cdf0e10cSrcweir 	{
162*cdf0e10cSrcweir 		XNameAccess xTables = xTabSup.getTables();
163*cdf0e10cSrcweir 		String [] aTableNames = xTables.getElementNames();
164*cdf0e10cSrcweir 		if(0 != aTableNames.length)
165*cdf0e10cSrcweir 		{
166*cdf0e10cSrcweir 			Object table = xTables.getByName(aTableNames[0]);
167*cdf0e10cSrcweir 			XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,table);
168*cdf0e10cSrcweir 			System.out.println("Name:          " + xProp.getPropertyValue("Name"));
169*cdf0e10cSrcweir 			System.out.println("CatalogName:   " + xProp.getPropertyValue("CatalogName"));
170*cdf0e10cSrcweir 			System.out.println("SchemaName:    " + xProp.getPropertyValue("SchemaName"));
171*cdf0e10cSrcweir 			System.out.println("Description:   " + xProp.getPropertyValue("Description"));
172*cdf0e10cSrcweir 			// the following property is optional so we first must check if it exists
173*cdf0e10cSrcweir 			if(xProp.getPropertySetInfo().hasPropertyByName("Type"))
174*cdf0e10cSrcweir 				System.out.println("Type:          " + xProp.getPropertyValue("Type"));
175*cdf0e10cSrcweir 		}
176*cdf0e10cSrcweir 	}
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 	//###########################################################
179*cdf0e10cSrcweir 	// 15. example
180*cdf0e10cSrcweir 	// print all columns of a XColumnsSupplier
181*cdf0e10cSrcweir 	//###########################################################
182*cdf0e10cSrcweir 	public static void printColumns(XColumnsSupplier xColumnsSup) throws com.sun.star.uno.Exception,SQLException
183*cdf0e10cSrcweir 	{
184*cdf0e10cSrcweir 		System.out.println("Example printColumns");
185*cdf0e10cSrcweir 		// the table must be at least support a XColumnsSupplier interface
186*cdf0e10cSrcweir 		System.out.println("--- Columns ---");
187*cdf0e10cSrcweir 		XNameAccess xColumns = xColumnsSup.getColumns();
188*cdf0e10cSrcweir 		String [] aColumnNames = xColumns.getElementNames();
189*cdf0e10cSrcweir 		for ( int i =0; i<= aColumnNames.length-1; i++)
190*cdf0e10cSrcweir 			System.out.println("    " + aColumnNames[i]);
191*cdf0e10cSrcweir 	}
192*cdf0e10cSrcweir 	//###########################################################
193*cdf0e10cSrcweir 	// 16. example
194*cdf0e10cSrcweir 	// print all keys inclusive the columns of a key
195*cdf0e10cSrcweir 	//###########################################################
196*cdf0e10cSrcweir 	public static void printKeys(XColumnsSupplier xColumnsSup) throws com.sun.star.uno.Exception,SQLException
197*cdf0e10cSrcweir 	{
198*cdf0e10cSrcweir 		System.out.println("Example printKeys");
199*cdf0e10cSrcweir 		XKeysSupplier xKeysSup = (XKeysSupplier)UnoRuntime.queryInterface(XKeysSupplier.class,xColumnsSup);
200*cdf0e10cSrcweir 		if(xKeysSup != null)
201*cdf0e10cSrcweir 		{
202*cdf0e10cSrcweir 			System.out.println("--- Keys ---");
203*cdf0e10cSrcweir 			XIndexAccess xKeys = xKeysSup.getKeys();
204*cdf0e10cSrcweir 			for ( int i =0; i < xKeys.getCount(); i++)
205*cdf0e10cSrcweir 			{
206*cdf0e10cSrcweir 				Object key = xKeys.getByIndex(i);
207*cdf0e10cSrcweir 				XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,key);
208*cdf0e10cSrcweir 				System.out.println("    " + xProp.getPropertyValue("Name"));
209*cdf0e10cSrcweir 				XColumnsSupplier xKeyColumnsSup = ( XColumnsSupplier ) UnoRuntime.queryInterface(XColumnsSupplier.class,xProp);
210*cdf0e10cSrcweir 				printColumns(xKeyColumnsSup);
211*cdf0e10cSrcweir 			}
212*cdf0e10cSrcweir 		}
213*cdf0e10cSrcweir 	}
214*cdf0e10cSrcweir 	//###########################################################
215*cdf0e10cSrcweir 	// 17. example
216*cdf0e10cSrcweir 	// print all keys inclusive the columns of a key
217*cdf0e10cSrcweir 	//###########################################################
218*cdf0e10cSrcweir 	public static void printIndexes(XColumnsSupplier xColumnsSup) throws com.sun.star.uno.Exception,SQLException
219*cdf0e10cSrcweir 	{
220*cdf0e10cSrcweir 		System.out.println("Example printIndexes");
221*cdf0e10cSrcweir 		XIndexesSupplier xIndexesSup = (XIndexesSupplier)UnoRuntime.queryInterface(XIndexesSupplier.class,xColumnsSup);
222*cdf0e10cSrcweir 		if(xIndexesSup != null)
223*cdf0e10cSrcweir 		{
224*cdf0e10cSrcweir 			System.out.println("--- Indexes ---");
225*cdf0e10cSrcweir 			XNameAccess xIndexs = xIndexesSup.getIndexes();
226*cdf0e10cSrcweir 			String [] aIndexNames = xIndexs.getElementNames();
227*cdf0e10cSrcweir 			for ( int i =0; i<= aIndexNames.length-1; i++)
228*cdf0e10cSrcweir 			{
229*cdf0e10cSrcweir 				System.out.println("    " + aIndexNames[i]);
230*cdf0e10cSrcweir 				Object index = xIndexs.getByName(aIndexNames[i]);
231*cdf0e10cSrcweir 				XColumnsSupplier xIndexColumnsSup = (XColumnsSupplier)UnoRuntime.queryInterface(XColumnsSupplier.class,index);
232*cdf0e10cSrcweir 				printColumns(xIndexColumnsSup);
233*cdf0e10cSrcweir 			}
234*cdf0e10cSrcweir 		}
235*cdf0e10cSrcweir 	}
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 	//###########################################################
238*cdf0e10cSrcweir 	// 18. example
239*cdf0e10cSrcweir 	// column properties
240*cdf0e10cSrcweir 	//###########################################################
241*cdf0e10cSrcweir     public static void printColumnProperties(Object column) throws com.sun.star.uno.Exception,SQLException
242*cdf0e10cSrcweir 	{
243*cdf0e10cSrcweir 		System.out.println("Example printColumnProperties");
244*cdf0e10cSrcweir 		XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,column);
245*cdf0e10cSrcweir 		System.out.println("Name:            " + xProp.getPropertyValue("Name"));
246*cdf0e10cSrcweir 		System.out.println("Type:            " + xProp.getPropertyValue("Type"));
247*cdf0e10cSrcweir 		System.out.println("TypeName:        " + xProp.getPropertyValue("TypeName"));
248*cdf0e10cSrcweir 		System.out.println("Precision:       " + xProp.getPropertyValue("Precision"));
249*cdf0e10cSrcweir 		System.out.println("Scale:           " + xProp.getPropertyValue("Scale"));
250*cdf0e10cSrcweir 		System.out.println("IsNullable:      " + xProp.getPropertyValue("IsNullable"));
251*cdf0e10cSrcweir 		System.out.println("IsAutoIncrement: " + xProp.getPropertyValue("IsAutoIncrement"));
252*cdf0e10cSrcweir 		System.out.println("IsCurrency:      " + xProp.getPropertyValue("IsCurrency"));
253*cdf0e10cSrcweir 		// the following property is optional so we first must check if it exists
254*cdf0e10cSrcweir 		if(xProp.getPropertySetInfo().hasPropertyByName("IsRowVersion"))
255*cdf0e10cSrcweir 			System.out.println("IsRowVersion:    " + xProp.getPropertyValue("IsRowVersion"));
256*cdf0e10cSrcweir 		if(xProp.getPropertySetInfo().hasPropertyByName("Description"))
257*cdf0e10cSrcweir 			System.out.println("Description:     " + xProp.getPropertyValue("Description"));
258*cdf0e10cSrcweir 		if(xProp.getPropertySetInfo().hasPropertyByName("DefaultValue"))
259*cdf0e10cSrcweir 			System.out.println("DefaultValue:    " + xProp.getPropertyValue("DefaultValue"));
260*cdf0e10cSrcweir 	}
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 	//###########################################################
263*cdf0e10cSrcweir 	// 19. example
264*cdf0e10cSrcweir 	// index properties
265*cdf0e10cSrcweir 	//###########################################################
266*cdf0e10cSrcweir 	public static void printIndexProperties(Object index) throws com.sun.star.uno.Exception,SQLException
267*cdf0e10cSrcweir 	{
268*cdf0e10cSrcweir 		System.out.println("Example printIndexProperties");
269*cdf0e10cSrcweir 		XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,index);
270*cdf0e10cSrcweir 		System.out.println("Name:              " + xProp.getPropertyValue("Name"));
271*cdf0e10cSrcweir 		System.out.println("Catalog:           " + xProp.getPropertyValue("Catalog"));
272*cdf0e10cSrcweir 		System.out.println("IsUnique:          " + xProp.getPropertyValue("IsUnique"));
273*cdf0e10cSrcweir 		System.out.println("IsPrimaryKeyIndex: " + xProp.getPropertyValue("IsPrimaryKeyIndex"));
274*cdf0e10cSrcweir 		System.out.println("IsClustered:       " + xProp.getPropertyValue("IsClustered"));
275*cdf0e10cSrcweir 	}
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir 	//###########################################################
278*cdf0e10cSrcweir 	// 20. example
279*cdf0e10cSrcweir 	// key properties
280*cdf0e10cSrcweir 	//###########################################################
281*cdf0e10cSrcweir 	public static void printKeyProperties(Object key) throws com.sun.star.uno.Exception,SQLException
282*cdf0e10cSrcweir 	{
283*cdf0e10cSrcweir 		System.out.println("Example printKeyProperties");
284*cdf0e10cSrcweir 		XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,key);
285*cdf0e10cSrcweir 		System.out.println("Name:            " + xProp.getPropertyValue("Name"));
286*cdf0e10cSrcweir 		System.out.println("Type:            " + xProp.getPropertyValue("Type"));
287*cdf0e10cSrcweir 		System.out.println("ReferencedTable: " + xProp.getPropertyValue("ReferencedTable"));
288*cdf0e10cSrcweir 		System.out.println("UpdateRule:      " + xProp.getPropertyValue("UpdateRule"));
289*cdf0e10cSrcweir 		System.out.println("DeleteRule:      " + xProp.getPropertyValue("DeleteRule"));
290*cdf0e10cSrcweir 	}
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir 	//###########################################################
293*cdf0e10cSrcweir 	// 21. example
294*cdf0e10cSrcweir 	// print all groups and the users with their privileges who belong to this group
295*cdf0e10cSrcweir 	//###########################################################
296*cdf0e10cSrcweir 	public static void printGroups(XTablesSupplier xTabSup) throws com.sun.star.uno.Exception,SQLException
297*cdf0e10cSrcweir 	{
298*cdf0e10cSrcweir 		System.out.println("Example printGroups");
299*cdf0e10cSrcweir 		XGroupsSupplier xGroupsSup = (XGroupsSupplier)UnoRuntime.queryInterface(XGroupsSupplier.class,xTabSup);
300*cdf0e10cSrcweir 		if(xGroupsSup != null)
301*cdf0e10cSrcweir 		{
302*cdf0e10cSrcweir 			// the table must be at least support a XColumnsSupplier interface
303*cdf0e10cSrcweir 			System.out.println("--- Groups ---");
304*cdf0e10cSrcweir 			XNameAccess xGroups = xGroupsSup.getGroups();
305*cdf0e10cSrcweir 			String [] aGroupNames = xGroups.getElementNames();
306*cdf0e10cSrcweir 			for ( int i =0; i < aGroupNames.length; i++)
307*cdf0e10cSrcweir 			{
308*cdf0e10cSrcweir 				System.out.println("    " + aGroupNames[i]);
309*cdf0e10cSrcweir 				XUsersSupplier xUsersSup = (XUsersSupplier)UnoRuntime.queryInterface(XUsersSupplier.class,xGroups.getByName(aGroupNames[i]));
310*cdf0e10cSrcweir 				if(xUsersSup != null)
311*cdf0e10cSrcweir 				{
312*cdf0e10cSrcweir 					XAuthorizable xAuth = (XAuthorizable)UnoRuntime.queryInterface(XAuthorizable.class,xUsersSup);
313*cdf0e10cSrcweir 					// the table must be at least support a XColumnsSupplier interface
314*cdf0e10cSrcweir 					System.out.println("\t--- Users ---");
315*cdf0e10cSrcweir 					XNameAccess xUsers = xUsersSup.getUsers();
316*cdf0e10cSrcweir 					String [] aUserNames = xUsers.getElementNames();
317*cdf0e10cSrcweir 					for ( int j =0; j < aUserNames.length; j++)
318*cdf0e10cSrcweir 					{
319*cdf0e10cSrcweir 						System.out.println("\t    " + aUserNames[j] + " Privileges: " + xAuth.getPrivileges(aUserNames[j],PrivilegeObject.TABLE));
320*cdf0e10cSrcweir 					}
321*cdf0e10cSrcweir 				}
322*cdf0e10cSrcweir 			}
323*cdf0e10cSrcweir 		}
324*cdf0e10cSrcweir 	}
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir 	//###########################################################
327*cdf0e10cSrcweir 	// 22. example
328*cdf0e10cSrcweir 	// create the table salesmen
329*cdf0e10cSrcweir 	//###########################################################
330*cdf0e10cSrcweir 	public static void createTableSalesMen(XNameAccess xTables) throws com.sun.star.uno.Exception,SQLException
331*cdf0e10cSrcweir 	{
332*cdf0e10cSrcweir 		System.out.println("Example createTableSalesMen");
333*cdf0e10cSrcweir 		XDataDescriptorFactory xTabFac = (XDataDescriptorFactory)UnoRuntime.queryInterface(XDataDescriptorFactory.class,xTables);
334*cdf0e10cSrcweir 		if(xTabFac != null)
335*cdf0e10cSrcweir 		{
336*cdf0e10cSrcweir 			// create the new table
337*cdf0e10cSrcweir 			XPropertySet xTable = xTabFac.createDataDescriptor();
338*cdf0e10cSrcweir 			// set the name of the new table
339*cdf0e10cSrcweir 			xTable.setPropertyValue("Name","SALESMAN");
340*cdf0e10cSrcweir 			// append the columns
341*cdf0e10cSrcweir 			XColumnsSupplier xColumSup = (XColumnsSupplier)UnoRuntime.queryInterface(XColumnsSupplier.class,xTable);
342*cdf0e10cSrcweir 			XDataDescriptorFactory xColFac = (XDataDescriptorFactory)UnoRuntime.queryInterface(XDataDescriptorFactory.class,xColumSup.getColumns());
343*cdf0e10cSrcweir 			XAppend xAppend = (XAppend)UnoRuntime.queryInterface(XAppend.class,xColFac);
344*cdf0e10cSrcweir 			// we only need one descriptor
345*cdf0e10cSrcweir 			XPropertySet xCol = xColFac.createDataDescriptor();
346*cdf0e10cSrcweir 			// create first column and append
347*cdf0e10cSrcweir 			xCol.setPropertyValue("Name","SNR");
348*cdf0e10cSrcweir 			xCol.setPropertyValue("Type",new Integer(DataType.INTEGER));
349*cdf0e10cSrcweir 			xCol.setPropertyValue("IsNullable",new Integer(ColumnValue.NO_NULLS));
350*cdf0e10cSrcweir 			xAppend.appendByDescriptor(xCol);
351*cdf0e10cSrcweir 			// 2nd only set the properties which differs
352*cdf0e10cSrcweir 			xCol.setPropertyValue("Name","FIRSTNAME");
353*cdf0e10cSrcweir 			xCol.setPropertyValue("Type",new Integer(DataType.VARCHAR));
354*cdf0e10cSrcweir 			xCol.setPropertyValue("IsNullable",new Integer(ColumnValue.NULLABLE));
355*cdf0e10cSrcweir 			xCol.setPropertyValue("Precision",new Integer(50));
356*cdf0e10cSrcweir 			xAppend.appendByDescriptor(xCol);
357*cdf0e10cSrcweir 			// 3nd only set the properties which differs
358*cdf0e10cSrcweir 			xCol.setPropertyValue("Name","LASTNAME");
359*cdf0e10cSrcweir 			xCol.setPropertyValue("Precision",new Integer(100));
360*cdf0e10cSrcweir 			xAppend.appendByDescriptor(xCol);
361*cdf0e10cSrcweir 			// 4nd only set the properties which differs
362*cdf0e10cSrcweir 			xCol.setPropertyValue("Name","STREET");
363*cdf0e10cSrcweir 			xCol.setPropertyValue("Precision",new Integer(50));
364*cdf0e10cSrcweir 			xAppend.appendByDescriptor(xCol);
365*cdf0e10cSrcweir 			// 5nd only set the properties which differs
366*cdf0e10cSrcweir 			xCol.setPropertyValue("Name","STATE");
367*cdf0e10cSrcweir 			xAppend.appendByDescriptor(xCol);
368*cdf0e10cSrcweir 			// 6nd only set the properties which differs
369*cdf0e10cSrcweir 			xCol.setPropertyValue("Name","ZIP");
370*cdf0e10cSrcweir 			xCol.setPropertyValue("Type",new Integer(DataType.INTEGER));
371*cdf0e10cSrcweir 			xCol.setPropertyValue("Precision",new Integer(10)); // default value integer
372*cdf0e10cSrcweir 			xAppend.appendByDescriptor(xCol);
373*cdf0e10cSrcweir 			// 7nd only set the properties which differs
374*cdf0e10cSrcweir 			xCol.setPropertyValue("Name","BIRTHDATE");
375*cdf0e10cSrcweir 			xCol.setPropertyValue("Type",new Integer(DataType.DATE));
376*cdf0e10cSrcweir 			xCol.setPropertyValue("Precision",new Integer(10)); // default value integer
377*cdf0e10cSrcweir 			xAppend.appendByDescriptor(xCol);
378*cdf0e10cSrcweir 			// now we create the primary key
379*cdf0e10cSrcweir 			XKeysSupplier xKeySup = (XKeysSupplier)UnoRuntime.queryInterface(XKeysSupplier.class,xTable);
380*cdf0e10cSrcweir 			XDataDescriptorFactory xKeyFac = (XDataDescriptorFactory)UnoRuntime.queryInterface(XDataDescriptorFactory.class,xKeySup.getKeys());
381*cdf0e10cSrcweir 			XAppend xKeyAppend = (XAppend)UnoRuntime.queryInterface(XAppend.class,xKeyFac);
382*cdf0e10cSrcweir 			XPropertySet xKey = xKeyFac.createDataDescriptor();
383*cdf0e10cSrcweir 			xKey.setPropertyValue("Type",new Integer(KeyType.PRIMARY));
384*cdf0e10cSrcweir 			// now append the columns to key
385*cdf0e10cSrcweir 			XColumnsSupplier xKeyColumSup = (XColumnsSupplier)UnoRuntime.queryInterface(XColumnsSupplier.class,xKey);
386*cdf0e10cSrcweir 			XDataDescriptorFactory xKeyColFac = (XDataDescriptorFactory)UnoRuntime.queryInterface(XDataDescriptorFactory.class,xKeyColumSup.getColumns());
387*cdf0e10cSrcweir 			XAppend xKeyColAppend = (XAppend)UnoRuntime.queryInterface(XAppend.class,xKeyColFac);
388*cdf0e10cSrcweir 			// we only need one descriptor
389*cdf0e10cSrcweir 			XPropertySet xKeyCol = xKeyColFac.createDataDescriptor();
390*cdf0e10cSrcweir 			xKeyCol.setPropertyValue("Name","SNR");
391*cdf0e10cSrcweir 			// append the key column
392*cdf0e10cSrcweir 			xKeyColAppend.appendByDescriptor(xKeyCol);
393*cdf0e10cSrcweir 			// apend the key
394*cdf0e10cSrcweir 			xKeyAppend.appendByDescriptor(xKey);
395*cdf0e10cSrcweir 			// the last step is to append the new table to the tables collection
396*cdf0e10cSrcweir 			 XAppend xTableAppend = (XAppend)UnoRuntime.queryInterface(XAppend.class,xTabFac);
397*cdf0e10cSrcweir 			 xTableAppend.appendByDescriptor(xTable);
398*cdf0e10cSrcweir 		}
399*cdf0e10cSrcweir 	}
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir 	//###########################################################
402*cdf0e10cSrcweir 	// 23. example
403*cdf0e10cSrcweir 	// create a user
404*cdf0e10cSrcweir 	//###########################################################
405*cdf0e10cSrcweir 	public static void createUser(XNameAccess xUsers) throws com.sun.star.uno.Exception,SQLException
406*cdf0e10cSrcweir 	{
407*cdf0e10cSrcweir 		System.out.println("Example createUser");
408*cdf0e10cSrcweir 		XDataDescriptorFactory xUserFac = (XDataDescriptorFactory)UnoRuntime.queryInterface(XDataDescriptorFactory.class,xUsers);
409*cdf0e10cSrcweir 		if(xUserFac != null)
410*cdf0e10cSrcweir 		{
411*cdf0e10cSrcweir 			// create the new table
412*cdf0e10cSrcweir 			XPropertySet xUser = xUserFac.createDataDescriptor();
413*cdf0e10cSrcweir 			// set the name of the new table
414*cdf0e10cSrcweir 			xUser.setPropertyValue("Name","BOSS");
415*cdf0e10cSrcweir 			xUser.setPropertyValue("Password","BOSSWIFENAME");
416*cdf0e10cSrcweir 			XAppend xAppend = (XAppend)UnoRuntime.queryInterface(XAppend.class,xUserFac);
417*cdf0e10cSrcweir 			xAppend.appendByDescriptor(xUser);
418*cdf0e10cSrcweir 		}
419*cdf0e10cSrcweir 	}
420*cdf0e10cSrcweir }
421