1*34dd1e25SAndrew Rist /************************************************************** 2*34dd1e25SAndrew Rist * 3*34dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*34dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*34dd1e25SAndrew Rist * distributed with this work for additional information 6*34dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*34dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*34dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 9*34dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 10*34dd1e25SAndrew Rist * 11*34dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*34dd1e25SAndrew Rist * 13*34dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*34dd1e25SAndrew Rist * software distributed under the License is distributed on an 15*34dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*34dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 17*34dd1e25SAndrew Rist * specific language governing permissions and limitations 18*34dd1e25SAndrew Rist * under the License. 19*34dd1e25SAndrew Rist * 20*34dd1e25SAndrew Rist *************************************************************/ 21*34dd1e25SAndrew Rist 22*34dd1e25SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir import java.io.*; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.comp.helper.RegistryServiceFactory; 27cdf0e10cSrcweir import com.sun.star.comp.servicemanager.ServiceManager; 28cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 29cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 30cdf0e10cSrcweir import com.sun.star.lang.XComponent; 31cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 32cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 33cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 34cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 35cdf0e10cSrcweir import com.sun.star.container.XNameAccess; 36cdf0e10cSrcweir import com.sun.star.sdbc.*; 37cdf0e10cSrcweir import com.sun.star.sdbcx.Privilege; 38cdf0e10cSrcweir import com.sun.star.sdb.CommandType; 39cdf0e10cSrcweir import com.sun.star.sdb.XRowSetApproveBroadcaster; 40cdf0e10cSrcweir 41cdf0e10cSrcweir public class RowSet 42cdf0e10cSrcweir { 43cdf0e10cSrcweir private static XComponentContext xContext = null; 44cdf0e10cSrcweir private static XMultiComponentFactory xMCF = null; main(String argv[])45cdf0e10cSrcweir public static void main(String argv[]) throws java.lang.Exception 46cdf0e10cSrcweir { 47cdf0e10cSrcweir try { 48cdf0e10cSrcweir // get the remote office component context 49cdf0e10cSrcweir xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 50cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 51cdf0e10cSrcweir xMCF = xContext.getServiceManager(); 52cdf0e10cSrcweir } 53cdf0e10cSrcweir catch( Exception e) { 54cdf0e10cSrcweir System.err.println("ERROR: can't get a component context from a running office ..."); 55cdf0e10cSrcweir e.printStackTrace(System.out); 56cdf0e10cSrcweir System.exit(1); 57cdf0e10cSrcweir } 58cdf0e10cSrcweir 59cdf0e10cSrcweir try{ 60cdf0e10cSrcweir showRowSetEvents(); 61cdf0e10cSrcweir showRowSetRowCount(); 62cdf0e10cSrcweir showRowSetPrivileges(); 63cdf0e10cSrcweir useRowSet(); 64cdf0e10cSrcweir } 65cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) 66cdf0e10cSrcweir { 67cdf0e10cSrcweir System.err.println(e); 68cdf0e10cSrcweir e.printStackTrace(); 69cdf0e10cSrcweir } 70cdf0e10cSrcweir System.exit(0); 71cdf0e10cSrcweir } 72cdf0e10cSrcweir printDataSources()73cdf0e10cSrcweir public static void printDataSources() throws com.sun.star.uno.Exception 74cdf0e10cSrcweir { 75cdf0e10cSrcweir // create a DatabaseContext and print all DataSource names 76cdf0e10cSrcweir XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( 77cdf0e10cSrcweir XNameAccess.class, 78cdf0e10cSrcweir xMCF.createInstanceWithContext("com.sun.star.sdb.DatabaseContext", 79cdf0e10cSrcweir xContext)); 80cdf0e10cSrcweir String aNames [] = xNameAccess.getElementNames(); 81cdf0e10cSrcweir for(int i=0;i<aNames.length;++i) 82cdf0e10cSrcweir System.out.println(aNames[i]); 83cdf0e10cSrcweir } 84cdf0e10cSrcweir useRowSet()85cdf0e10cSrcweir public static void useRowSet() throws com.sun.star.uno.Exception 86cdf0e10cSrcweir { 87cdf0e10cSrcweir // first we create our RowSet object 88cdf0e10cSrcweir XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface( 89cdf0e10cSrcweir XRowSet.class, 90cdf0e10cSrcweir xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext)); 91cdf0e10cSrcweir 92cdf0e10cSrcweir System.out.println("RowSet created!"); 93cdf0e10cSrcweir // set the properties needed to connect to a database 94cdf0e10cSrcweir XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes); 95cdf0e10cSrcweir xProp.setPropertyValue("DataSourceName","Bibliography"); 96cdf0e10cSrcweir xProp.setPropertyValue("Command","biblio"); 97cdf0e10cSrcweir xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE)); 98cdf0e10cSrcweir 99cdf0e10cSrcweir xRowRes.execute(); 100cdf0e10cSrcweir System.out.println("RowSet executed!"); 101cdf0e10cSrcweir 102cdf0e10cSrcweir 103cdf0e10cSrcweir XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes); 104cdf0e10cSrcweir xComp.dispose(); 105cdf0e10cSrcweir System.out.println("RowSet destroyed!"); 106cdf0e10cSrcweir } 107cdf0e10cSrcweir showRowSetPrivileges()108cdf0e10cSrcweir public static void showRowSetPrivileges() throws com.sun.star.uno.Exception 109cdf0e10cSrcweir { 110cdf0e10cSrcweir // first we create our RowSet object 111cdf0e10cSrcweir XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface( 112cdf0e10cSrcweir XRowSet.class, 113cdf0e10cSrcweir xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext)); 114cdf0e10cSrcweir 115cdf0e10cSrcweir System.out.println("RowSet created!"); 116cdf0e10cSrcweir // set the properties needed to connect to a database 117cdf0e10cSrcweir XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes); 118cdf0e10cSrcweir xProp.setPropertyValue("DataSourceName","Bibliography"); 119cdf0e10cSrcweir xProp.setPropertyValue("Command","biblio"); 120cdf0e10cSrcweir xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE)); 121cdf0e10cSrcweir 122cdf0e10cSrcweir xRowRes.execute(); 123cdf0e10cSrcweir System.out.println("RowSet executed!"); 124cdf0e10cSrcweir 125cdf0e10cSrcweir Integer aPriv = (Integer)xProp.getPropertyValue("Privileges"); 126cdf0e10cSrcweir int nPriv = aPriv.intValue(); 127cdf0e10cSrcweir if( (nPriv & Privilege.SELECT) == Privilege.SELECT) 128cdf0e10cSrcweir System.out.println("SELECT"); 129cdf0e10cSrcweir if( (nPriv & Privilege.INSERT) == Privilege.INSERT) 130cdf0e10cSrcweir System.out.println("INSERT"); 131cdf0e10cSrcweir if( (nPriv & Privilege.UPDATE) == Privilege.UPDATE) 132cdf0e10cSrcweir System.out.println("UPDATE"); 133cdf0e10cSrcweir if( (nPriv & Privilege.DELETE) == Privilege.DELETE) 134cdf0e10cSrcweir System.out.println("DELETE"); 135cdf0e10cSrcweir 136cdf0e10cSrcweir // now destroy the RowSet 137cdf0e10cSrcweir XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes); 138cdf0e10cSrcweir xComp.dispose(); 139cdf0e10cSrcweir System.out.println("RowSet destroyed!"); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir showRowSetRowCount()142cdf0e10cSrcweir public static void showRowSetRowCount() throws com.sun.star.uno.Exception 143cdf0e10cSrcweir { 144cdf0e10cSrcweir // first we create our RowSet object 145cdf0e10cSrcweir XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface( 146cdf0e10cSrcweir XRowSet.class, 147cdf0e10cSrcweir xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext)); 148cdf0e10cSrcweir 149cdf0e10cSrcweir System.out.println("RowSet created!"); 150cdf0e10cSrcweir // set the properties needed to connect to a database 151cdf0e10cSrcweir XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes); 152cdf0e10cSrcweir xProp.setPropertyValue("DataSourceName","Bibliography"); 153cdf0e10cSrcweir xProp.setPropertyValue("Command","biblio"); 154cdf0e10cSrcweir xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE)); 155cdf0e10cSrcweir 156cdf0e10cSrcweir xRowRes.execute(); 157cdf0e10cSrcweir System.out.println("RowSet executed!"); 158cdf0e10cSrcweir 159cdf0e10cSrcweir // now look if the RowCount is already final 160cdf0e10cSrcweir System.out.println("The RowCount is final: " + xProp.getPropertyValue("IsRowCountFinal")); 161cdf0e10cSrcweir 162cdf0e10cSrcweir XResultSet xRes = (XResultSet)UnoRuntime.queryInterface(XResultSet.class,xRowRes); 163cdf0e10cSrcweir xRes.last(); 164cdf0e10cSrcweir 165cdf0e10cSrcweir System.out.println("The RowCount is final: " + xProp.getPropertyValue("IsRowCountFinal")); 166cdf0e10cSrcweir System.out.println("There are " + xProp.getPropertyValue("RowCount") + " rows!"); 167cdf0e10cSrcweir 168cdf0e10cSrcweir // now destroy the RowSet 169cdf0e10cSrcweir XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes); 170cdf0e10cSrcweir xComp.dispose(); 171cdf0e10cSrcweir System.out.println("RowSet destroyed!"); 172cdf0e10cSrcweir } 173cdf0e10cSrcweir showRowSetEvents()174cdf0e10cSrcweir public static void showRowSetEvents() throws com.sun.star.uno.Exception 175cdf0e10cSrcweir { 176cdf0e10cSrcweir // first we create our RowSet object 177cdf0e10cSrcweir XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface( 178cdf0e10cSrcweir XRowSet.class, 179cdf0e10cSrcweir xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext)); 180cdf0e10cSrcweir 181cdf0e10cSrcweir System.out.println("RowSet created!"); 182cdf0e10cSrcweir // add our Listener 183cdf0e10cSrcweir System.out.println("Append our Listener!"); 184cdf0e10cSrcweir RowSetEventListener pRow = new RowSetEventListener(); 185cdf0e10cSrcweir XRowSetApproveBroadcaster xApBroad = (XRowSetApproveBroadcaster)UnoRuntime.queryInterface(XRowSetApproveBroadcaster.class,xRowRes); 186cdf0e10cSrcweir xApBroad.addRowSetApproveListener(pRow); 187cdf0e10cSrcweir xRowRes.addRowSetListener(pRow); 188cdf0e10cSrcweir 189cdf0e10cSrcweir // set the properties needed to connect to a database 190cdf0e10cSrcweir XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes); 191cdf0e10cSrcweir xProp.setPropertyValue("DataSourceName","Bibliography"); 192cdf0e10cSrcweir xProp.setPropertyValue("Command","biblio"); 193cdf0e10cSrcweir xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE)); 194cdf0e10cSrcweir 195cdf0e10cSrcweir xRowRes.execute(); 196cdf0e10cSrcweir System.out.println("RowSet executed!"); 197cdf0e10cSrcweir 198cdf0e10cSrcweir // do some movements to check if we got all notifications 199cdf0e10cSrcweir XResultSet xRes = (XResultSet)UnoRuntime.queryInterface(XResultSet.class,xRowRes); 200cdf0e10cSrcweir System.out.println("beforeFirst"); 201cdf0e10cSrcweir xRes.beforeFirst(); 202cdf0e10cSrcweir // this should lead to no notifications because 203cdf0e10cSrcweir // we should stand before the first row at the beginning 204cdf0e10cSrcweir System.out.println("We stand before the first row: " + xRes.isBeforeFirst()); 205cdf0e10cSrcweir 206cdf0e10cSrcweir System.out.println("next"); 207cdf0e10cSrcweir xRes.next(); 208cdf0e10cSrcweir System.out.println("next"); 209cdf0e10cSrcweir xRes.next(); 210cdf0e10cSrcweir System.out.println("last"); 211cdf0e10cSrcweir xRes.last(); 212cdf0e10cSrcweir System.out.println("next"); 213cdf0e10cSrcweir xRes.next(); 214cdf0e10cSrcweir System.out.println("We stand after the last row: " + xRes.isAfterLast()); 215cdf0e10cSrcweir System.out.println("first"); 216cdf0e10cSrcweir xRes.first(); 217cdf0e10cSrcweir System.out.println("previous"); 218cdf0e10cSrcweir xRes.previous(); 219cdf0e10cSrcweir System.out.println("We stand before the first row: " + xRes.isBeforeFirst()); 220cdf0e10cSrcweir System.out.println("afterLast"); 221cdf0e10cSrcweir xRes.afterLast(); 222cdf0e10cSrcweir System.out.println("We stand after the last row: " + xRes.isAfterLast()); 223cdf0e10cSrcweir 224cdf0e10cSrcweir // now destroy the RowSet 225cdf0e10cSrcweir XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes); 226cdf0e10cSrcweir xComp.dispose(); 227cdf0e10cSrcweir System.out.println("RowSet destroyed!"); 228cdf0e10cSrcweir } 229cdf0e10cSrcweir } 230cdf0e10cSrcweir 231