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.comp.helper.RegistryServiceFactory; 27 import com.sun.star.comp.servicemanager.ServiceManager; 28 import com.sun.star.lang.XMultiComponentFactory; 29 import com.sun.star.lang.XServiceInfo; 30 import com.sun.star.lang.XComponent; 31 import com.sun.star.bridge.XUnoUrlResolver; 32 import com.sun.star.uno.UnoRuntime; 33 import com.sun.star.uno.XComponentContext; 34 import com.sun.star.beans.XPropertySet; 35 import com.sun.star.container.XNameAccess; 36 import com.sun.star.sdbc.*; 37 import com.sun.star.sdbcx.Privilege; 38 import com.sun.star.sdb.CommandType; 39 import com.sun.star.sdb.XRowSetApproveBroadcaster; 40 41 public class RowSet 42 { 43 private static XComponentContext xContext = null; 44 private static XMultiComponentFactory xMCF = null; main(String argv[])45 public static void main(String argv[]) throws java.lang.Exception 46 { 47 try { 48 // get the remote office component context 49 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 50 System.out.println("Connected to a running office ..."); 51 xMCF = xContext.getServiceManager(); 52 } 53 catch( Exception e) { 54 System.err.println("ERROR: can't get a component context from a running office ..."); 55 e.printStackTrace(System.out); 56 System.exit(1); 57 } 58 59 try{ 60 showRowSetEvents(); 61 showRowSetRowCount(); 62 showRowSetPrivileges(); 63 useRowSet(); 64 } 65 catch(com.sun.star.uno.Exception e) 66 { 67 System.err.println(e); 68 e.printStackTrace(); 69 } 70 System.exit(0); 71 } 72 printDataSources()73 public static void printDataSources() throws com.sun.star.uno.Exception 74 { 75 // create a DatabaseContext and print all DataSource names 76 XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( 77 XNameAccess.class, 78 xMCF.createInstanceWithContext("com.sun.star.sdb.DatabaseContext", 79 xContext)); 80 String aNames [] = xNameAccess.getElementNames(); 81 for(int i=0;i<aNames.length;++i) 82 System.out.println(aNames[i]); 83 } 84 useRowSet()85 public static void useRowSet() throws com.sun.star.uno.Exception 86 { 87 // first we create our RowSet object 88 XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface( 89 XRowSet.class, 90 xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext)); 91 92 System.out.println("RowSet created!"); 93 // set the properties needed to connect to a database 94 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes); 95 xProp.setPropertyValue("DataSourceName","Bibliography"); 96 xProp.setPropertyValue("Command","biblio"); 97 xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE)); 98 99 xRowRes.execute(); 100 System.out.println("RowSet executed!"); 101 102 103 XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes); 104 xComp.dispose(); 105 System.out.println("RowSet destroyed!"); 106 } 107 showRowSetPrivileges()108 public static void showRowSetPrivileges() throws com.sun.star.uno.Exception 109 { 110 // first we create our RowSet object 111 XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface( 112 XRowSet.class, 113 xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext)); 114 115 System.out.println("RowSet created!"); 116 // set the properties needed to connect to a database 117 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes); 118 xProp.setPropertyValue("DataSourceName","Bibliography"); 119 xProp.setPropertyValue("Command","biblio"); 120 xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE)); 121 122 xRowRes.execute(); 123 System.out.println("RowSet executed!"); 124 125 Integer aPriv = (Integer)xProp.getPropertyValue("Privileges"); 126 int nPriv = aPriv.intValue(); 127 if( (nPriv & Privilege.SELECT) == Privilege.SELECT) 128 System.out.println("SELECT"); 129 if( (nPriv & Privilege.INSERT) == Privilege.INSERT) 130 System.out.println("INSERT"); 131 if( (nPriv & Privilege.UPDATE) == Privilege.UPDATE) 132 System.out.println("UPDATE"); 133 if( (nPriv & Privilege.DELETE) == Privilege.DELETE) 134 System.out.println("DELETE"); 135 136 // now destroy the RowSet 137 XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes); 138 xComp.dispose(); 139 System.out.println("RowSet destroyed!"); 140 } 141 showRowSetRowCount()142 public static void showRowSetRowCount() throws com.sun.star.uno.Exception 143 { 144 // first we create our RowSet object 145 XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface( 146 XRowSet.class, 147 xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext)); 148 149 System.out.println("RowSet created!"); 150 // set the properties needed to connect to a database 151 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes); 152 xProp.setPropertyValue("DataSourceName","Bibliography"); 153 xProp.setPropertyValue("Command","biblio"); 154 xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE)); 155 156 xRowRes.execute(); 157 System.out.println("RowSet executed!"); 158 159 // now look if the RowCount is already final 160 System.out.println("The RowCount is final: " + xProp.getPropertyValue("IsRowCountFinal")); 161 162 XResultSet xRes = (XResultSet)UnoRuntime.queryInterface(XResultSet.class,xRowRes); 163 xRes.last(); 164 165 System.out.println("The RowCount is final: " + xProp.getPropertyValue("IsRowCountFinal")); 166 System.out.println("There are " + xProp.getPropertyValue("RowCount") + " rows!"); 167 168 // now destroy the RowSet 169 XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes); 170 xComp.dispose(); 171 System.out.println("RowSet destroyed!"); 172 } 173 showRowSetEvents()174 public static void showRowSetEvents() throws com.sun.star.uno.Exception 175 { 176 // first we create our RowSet object 177 XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface( 178 XRowSet.class, 179 xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext)); 180 181 System.out.println("RowSet created!"); 182 // add our Listener 183 System.out.println("Append our Listener!"); 184 RowSetEventListener pRow = new RowSetEventListener(); 185 XRowSetApproveBroadcaster xApBroad = (XRowSetApproveBroadcaster)UnoRuntime.queryInterface(XRowSetApproveBroadcaster.class,xRowRes); 186 xApBroad.addRowSetApproveListener(pRow); 187 xRowRes.addRowSetListener(pRow); 188 189 // set the properties needed to connect to a database 190 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes); 191 xProp.setPropertyValue("DataSourceName","Bibliography"); 192 xProp.setPropertyValue("Command","biblio"); 193 xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE)); 194 195 xRowRes.execute(); 196 System.out.println("RowSet executed!"); 197 198 // do some movements to check if we got all notifications 199 XResultSet xRes = (XResultSet)UnoRuntime.queryInterface(XResultSet.class,xRowRes); 200 System.out.println("beforeFirst"); 201 xRes.beforeFirst(); 202 // this should lead to no notifications because 203 // we should stand before the first row at the beginning 204 System.out.println("We stand before the first row: " + xRes.isBeforeFirst()); 205 206 System.out.println("next"); 207 xRes.next(); 208 System.out.println("next"); 209 xRes.next(); 210 System.out.println("last"); 211 xRes.last(); 212 System.out.println("next"); 213 xRes.next(); 214 System.out.println("We stand after the last row: " + xRes.isAfterLast()); 215 System.out.println("first"); 216 xRes.first(); 217 System.out.println("previous"); 218 xRes.previous(); 219 System.out.println("We stand before the first row: " + xRes.isBeforeFirst()); 220 System.out.println("afterLast"); 221 xRes.afterLast(); 222 System.out.println("We stand after the last row: " + xRes.isAfterLast()); 223 224 // now destroy the RowSet 225 XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes); 226 xComp.dispose(); 227 System.out.println("RowSet destroyed!"); 228 } 229 } 230 231