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