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