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 // Lotus Notes Domino API 36 import lotus.domino.NotesThread; 37 import lotus.domino.Session; 38 import lotus.domino.Database; 39 import lotus.domino.DocumentCollection; 40 import lotus.domino.Document; 41 import lotus.domino.NotesFactory; 42 43 // OpenOffice.org API 44 import com.sun.star.bridge.XUnoUrlResolver; 45 import com.sun.star.lang.XComponent; 46 import com.sun.star.lang.XMultiComponentFactory; 47 import com.sun.star.uno.XComponentContext; 48 import com.sun.star.uno.UnoRuntime; 49 import com.sun.star.frame.XComponentLoader; 50 import com.sun.star.beans.PropertyValue; 51 import com.sun.star.beans.XPropertySet; 52 import com.sun.star.sheet.XSpreadsheetDocument; 53 import com.sun.star.sheet.XSpreadsheets; 54 import com.sun.star.sheet.XSpreadsheet; 55 import com.sun.star.container.XIndexAccess; 56 import com.sun.star.table.XCell; 57 58 /** This class creates an OpenOffice.org Calc spreadsheet document and fills it 59 * with existing values of documents from a Lotus Notes database. 60 */ 61 public class NotesAccess implements Runnable { 62 63 /** Host server of the Domino Directory. 64 */ 65 static String stringHost = ""; 66 67 /** User in the host's Domino Directory. 68 */ 69 static String stringUser = ""; 70 71 /** Password for the user in the host's Domino Directory. 72 */ 73 static String stringPassword = ""; 74 75 /** Database with documents to get data from. 76 */ 77 static String stringDatabase = ""; 78 79 /** Reading the arguments and constructing the thread. 80 * @param argv Holding values for the host, user, and the password of the user. 81 */ 82 public static void main( String args[] ) { 83 Thread thread; 84 85 if ( args.length < 4 ) { 86 System.out.println( 87 "usage: java -jar NotesAccess.jar \"<Domino Host>\" \"<User>\" " + 88 "\"<Password>\" \"<Database>\"" ); 89 System.out.println( "\ne.g.:" ); 90 System.out.println( 91 "java -jar NotesAccess.jar \"\" \"\" \"\" \"Stocks.nsf\"" ); 92 System.exit( 1 ); 93 } 94 95 if ( !args[ 0 ].trim().equals( "" ) ) { 96 stringHost = args[ 0 ].trim(); 97 } 98 if ( !args[ 1 ].trim().equals( "" ) ) { 99 stringUser = args[ 1 ].trim(); 100 } 101 stringPassword = args[ 2 ].trim(); 102 103 try { 104 java.io.File sourceFile = new java.io.File(args[ 3 ].trim()); 105 stringDatabase = sourceFile.getCanonicalPath(); 106 } catch (java.io.IOException e) { 107 System.out.println("Error: Please check the name or path to your database file."); 108 e.printStackTrace(); 109 System.exit( 1 ); 110 } 111 112 if ( stringHost.equals( "" ) ) { 113 // Initializing. 114 NotesAccess notesaccess = new NotesAccess(); 115 116 // Allowing only local calls to the Domino classes. 117 thread = new NotesThread( ( Runnable ) notesaccess ); 118 } 119 else { 120 // Extracting the host, user, and password. 121 NotesAccess notesaccess = new NotesAccess(); 122 123 // Allowing remote calls to the Domino classes. 124 thread = new Thread( ( Runnable ) notesaccess ); 125 } 126 127 // Starting the thread. 128 thread.start(); 129 } 130 131 /** This is the default constructor without arguments. 132 */ 133 public NotesAccess() { 134 } 135 136 /** Reading all documents from the given database and writing the data to 137 * an OpenOffice.org Calc spreadsheet document. 138 */ 139 public void run() { 140 try { 141 // get the remote office component context 142 XComponentContext xContext = 143 com.sun.star.comp.helper.Bootstrap.bootstrap(); 144 145 System.out.println("Connected to a running office ..."); 146 147 XMultiComponentFactory xMCF = xContext.getServiceManager(); 148 149 /* A desktop environment contains tasks with one or more 150 frames in which components can be loaded. Desktop is the 151 environment for components which can instanciate within 152 frames. */ 153 XComponentLoader xLoader = ( XComponentLoader ) 154 UnoRuntime.queryInterface(XComponentLoader.class, 155 xMCF.createInstanceWithContext( 156 "com.sun.star.frame.Desktop", xContext)); 157 158 // Load a Writer document, which will be automaticly displayed 159 XComponent xComponent = xLoader.loadComponentFromURL( 160 "private:factory/scalc", "_blank", 0, 161 new PropertyValue[0] ); 162 163 // Querying for the interface XSpreadsheetDocument 164 XSpreadsheetDocument xSpreadsheetDoc = 165 (XSpreadsheetDocument) UnoRuntime.queryInterface( 166 XSpreadsheetDocument.class, xComponent); 167 168 // Getting all sheets from the spreadsheet document. 169 XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets() ; 170 171 // Querying for the interface XIndexAccess. 172 XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface( 173 XIndexAccess.class, xSpreadsheets); 174 175 // Getting the first spreadsheet. 176 XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface( 177 XSpreadsheet.class, xIndexAccess.getByIndex(0)); 178 179 Session session; 180 if ( !stringHost.equals( "" ) ) { 181 // Creating a Notes session for remote calls to the Domino classes. 182 session = NotesFactory.createSession(stringHost, stringUser, 183 stringPassword); 184 } 185 else { 186 // Creating a Notes session for only local calls to the 187 // Domino classes. 188 session = NotesFactory.createSession(); 189 } 190 191 // Getting the specified Notes database. 192 Database database = session.getDatabase( "", stringDatabase ); 193 194 // Getting a collection of all documents from the database. 195 DocumentCollection documentCollection = database.getAllDocuments(); 196 197 // Getting the first document from the database 198 Document document = documentCollection.getFirstDocument(); 199 200 // Start to write to cells at this row. 201 int intRowToStart = 0; 202 203 // The current row. 204 int intRow = intRowToStart; 205 206 // The current column. 207 int intColumn = 0; 208 209 // Process all documents 210 while ( document != null ) { 211 // Getting the name of the stock. 212 String stringName = document.getItemValueString("Name"); 213 214 // Inserting the name to a specified cell. 215 insertIntoCell(intColumn, intRow, stringName, xSpreadsheet, ""); 216 217 // Getting the number of stocks. 218 double intNumber = document.getItemValueInteger( "Number" ); 219 220 // Inserting the number of stocks to a specified cell. 221 insertIntoCell( intColumn + 1, intRow, String.valueOf(intNumber), 222 xSpreadsheet, "V" ); 223 224 // Getting current share price. 225 double doubleSharePrice = document.getItemValueDouble("SharePrice"); 226 227 // Inserting the current share price to a specified cell. 228 insertIntoCell(intColumn + 2, intRow, 229 String.valueOf(doubleSharePrice), 230 xSpreadsheet, "V"); 231 232 // Inserting the total value. 233 insertIntoCell(intColumn + 3, intRow, "=B" 234 + String.valueOf( intRow + 1 ) 235 + "*C" + String.valueOf(intRow + 1), 236 xSpreadsheet, ""); 237 238 // Increasing the current row. 239 intRow++; 240 241 // Getting the next document from the collection. 242 document = documentCollection.getNextDocument(); 243 } 244 245 // Summing all specific amounts. 246 insertIntoCell(intColumn + 3, intRow, "=sum(D" 247 + String.valueOf( intRowToStart + 1 ) + ":D" 248 + String.valueOf( intRow ), 249 xSpreadsheet, ""); 250 251 xContext = null; 252 253 // Leaving the program. 254 System.exit(0); 255 } 256 catch (Exception e) { 257 e.printStackTrace(); 258 } 259 } 260 261 /** Inserting a value or formula to a cell defined by the row and column. 262 * @param intCellX Row. 263 * @param intCellY Column. 264 * @param stringValue This value will be written to the cell. 265 * @param xSpreadsheet Write the value to the cells of this spreadsheet. 266 * @param stringFlag If this string contains "V", the value will be written, 267 * otherwise the formula. 268 */ 269 public static void insertIntoCell(int intCellX, int intCellY, 270 String stringValue, 271 XSpreadsheet xSpreadsheet, 272 String stringFlag) 273 { 274 XCell xCell = null; 275 276 try { 277 xCell = xSpreadsheet.getCellByPosition( intCellX, intCellY ); 278 } catch ( com.sun.star.lang.IndexOutOfBoundsException exception ) { 279 System.out.println( "Could not get Cell" ); 280 } 281 if ( stringFlag.equals( "V" )) { 282 xCell.setValue((new Float(stringValue)).floatValue()); 283 } 284 else { 285 xCell.setFormula(stringValue); 286 } 287 } 288 } 289