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