1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * The Contents of this file are made available subject to the terms of 4*cdf0e10cSrcweir * the BSD license. 5*cdf0e10cSrcweir * 6*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 7*cdf0e10cSrcweir * All rights reserved. 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * Redistribution and use in source and binary forms, with or without 10*cdf0e10cSrcweir * modification, are permitted provided that the following conditions 11*cdf0e10cSrcweir * are met: 12*cdf0e10cSrcweir * 1. Redistributions of source code must retain the above copyright 13*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer. 14*cdf0e10cSrcweir * 2. Redistributions in binary form must reproduce the above copyright 15*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer in the 16*cdf0e10cSrcweir * documentation and/or other materials provided with the distribution. 17*cdf0e10cSrcweir * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18*cdf0e10cSrcweir * contributors may be used to endorse or promote products derived 19*cdf0e10cSrcweir * from this software without specific prior written permission. 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*cdf0e10cSrcweir * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*cdf0e10cSrcweir * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24*cdf0e10cSrcweir * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25*cdf0e10cSrcweir * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26*cdf0e10cSrcweir * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27*cdf0e10cSrcweir * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28*cdf0e10cSrcweir * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29*cdf0e10cSrcweir * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30*cdf0e10cSrcweir * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31*cdf0e10cSrcweir * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32*cdf0e10cSrcweir * 33*cdf0e10cSrcweir *************************************************************************/ 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir // Lotus Notes Domino API 36*cdf0e10cSrcweir import lotus.domino.NotesThread; 37*cdf0e10cSrcweir import lotus.domino.Session; 38*cdf0e10cSrcweir import lotus.domino.Database; 39*cdf0e10cSrcweir import lotus.domino.DocumentCollection; 40*cdf0e10cSrcweir import lotus.domino.Document; 41*cdf0e10cSrcweir import lotus.domino.NotesFactory; 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir // OpenOffice.org API 44*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 45*cdf0e10cSrcweir import com.sun.star.lang.XComponent; 46*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 47*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 48*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 49*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader; 50*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 51*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 52*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument; 53*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets; 54*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet; 55*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess; 56*cdf0e10cSrcweir import com.sun.star.table.XCell; 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir /** This class creates an OpenOffice.org Calc spreadsheet document and fills it 59*cdf0e10cSrcweir * with existing values of documents from a Lotus Notes database. 60*cdf0e10cSrcweir */ 61*cdf0e10cSrcweir public class NotesAccess implements Runnable { 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir /** Host server of the Domino Directory. 64*cdf0e10cSrcweir */ 65*cdf0e10cSrcweir static String stringHost = ""; 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir /** User in the host's Domino Directory. 68*cdf0e10cSrcweir */ 69*cdf0e10cSrcweir static String stringUser = ""; 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir /** Password for the user in the host's Domino Directory. 72*cdf0e10cSrcweir */ 73*cdf0e10cSrcweir static String stringPassword = ""; 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir /** Database with documents to get data from. 76*cdf0e10cSrcweir */ 77*cdf0e10cSrcweir static String stringDatabase = ""; 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir /** Reading the arguments and constructing the thread. 80*cdf0e10cSrcweir * @param argv Holding values for the host, user, and the password of the user. 81*cdf0e10cSrcweir */ 82*cdf0e10cSrcweir public static void main( String args[] ) { 83*cdf0e10cSrcweir Thread thread; 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir if ( args.length < 4 ) { 86*cdf0e10cSrcweir System.out.println( 87*cdf0e10cSrcweir "usage: java -jar NotesAccess.jar \"<Domino Host>\" \"<User>\" " + 88*cdf0e10cSrcweir "\"<Password>\" \"<Database>\"" ); 89*cdf0e10cSrcweir System.out.println( "\ne.g.:" ); 90*cdf0e10cSrcweir System.out.println( 91*cdf0e10cSrcweir "java -jar NotesAccess.jar \"\" \"\" \"\" \"Stocks.nsf\"" ); 92*cdf0e10cSrcweir System.exit( 1 ); 93*cdf0e10cSrcweir } 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir if ( !args[ 0 ].trim().equals( "" ) ) { 96*cdf0e10cSrcweir stringHost = args[ 0 ].trim(); 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir if ( !args[ 1 ].trim().equals( "" ) ) { 99*cdf0e10cSrcweir stringUser = args[ 1 ].trim(); 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir stringPassword = args[ 2 ].trim(); 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir try { 104*cdf0e10cSrcweir java.io.File sourceFile = new java.io.File(args[ 3 ].trim()); 105*cdf0e10cSrcweir stringDatabase = sourceFile.getCanonicalPath(); 106*cdf0e10cSrcweir } catch (java.io.IOException e) { 107*cdf0e10cSrcweir System.out.println("Error: Please check the name or path to your database file."); 108*cdf0e10cSrcweir e.printStackTrace(); 109*cdf0e10cSrcweir System.exit( 1 ); 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir if ( stringHost.equals( "" ) ) { 113*cdf0e10cSrcweir // Initializing. 114*cdf0e10cSrcweir NotesAccess notesaccess = new NotesAccess(); 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir // Allowing only local calls to the Domino classes. 117*cdf0e10cSrcweir thread = new NotesThread( ( Runnable ) notesaccess ); 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir else { 120*cdf0e10cSrcweir // Extracting the host, user, and password. 121*cdf0e10cSrcweir NotesAccess notesaccess = new NotesAccess(); 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir // Allowing remote calls to the Domino classes. 124*cdf0e10cSrcweir thread = new Thread( ( Runnable ) notesaccess ); 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir // Starting the thread. 128*cdf0e10cSrcweir thread.start(); 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir /** This is the default constructor without arguments. 132*cdf0e10cSrcweir */ 133*cdf0e10cSrcweir public NotesAccess() { 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir /** Reading all documents from the given database and writing the data to 137*cdf0e10cSrcweir * an OpenOffice.org Calc spreadsheet document. 138*cdf0e10cSrcweir */ 139*cdf0e10cSrcweir public void run() { 140*cdf0e10cSrcweir try { 141*cdf0e10cSrcweir // get the remote office component context 142*cdf0e10cSrcweir XComponentContext xContext = 143*cdf0e10cSrcweir com.sun.star.comp.helper.Bootstrap.bootstrap(); 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir XMultiComponentFactory xMCF = xContext.getServiceManager(); 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir /* A desktop environment contains tasks with one or more 150*cdf0e10cSrcweir frames in which components can be loaded. Desktop is the 151*cdf0e10cSrcweir environment for components which can instanciate within 152*cdf0e10cSrcweir frames. */ 153*cdf0e10cSrcweir XComponentLoader xLoader = ( XComponentLoader ) 154*cdf0e10cSrcweir UnoRuntime.queryInterface(XComponentLoader.class, 155*cdf0e10cSrcweir xMCF.createInstanceWithContext( 156*cdf0e10cSrcweir "com.sun.star.frame.Desktop", xContext)); 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir // Load a Writer document, which will be automaticly displayed 159*cdf0e10cSrcweir XComponent xComponent = xLoader.loadComponentFromURL( 160*cdf0e10cSrcweir "private:factory/scalc", "_blank", 0, 161*cdf0e10cSrcweir new PropertyValue[0] ); 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir // Querying for the interface XSpreadsheetDocument 164*cdf0e10cSrcweir XSpreadsheetDocument xSpreadsheetDoc = 165*cdf0e10cSrcweir (XSpreadsheetDocument) UnoRuntime.queryInterface( 166*cdf0e10cSrcweir XSpreadsheetDocument.class, xComponent); 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir // Getting all sheets from the spreadsheet document. 169*cdf0e10cSrcweir XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets() ; 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir // Querying for the interface XIndexAccess. 172*cdf0e10cSrcweir XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface( 173*cdf0e10cSrcweir XIndexAccess.class, xSpreadsheets); 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir // Getting the first spreadsheet. 176*cdf0e10cSrcweir XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface( 177*cdf0e10cSrcweir XSpreadsheet.class, xIndexAccess.getByIndex(0)); 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir Session session; 180*cdf0e10cSrcweir if ( !stringHost.equals( "" ) ) { 181*cdf0e10cSrcweir // Creating a Notes session for remote calls to the Domino classes. 182*cdf0e10cSrcweir session = NotesFactory.createSession(stringHost, stringUser, 183*cdf0e10cSrcweir stringPassword); 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir else { 186*cdf0e10cSrcweir // Creating a Notes session for only local calls to the 187*cdf0e10cSrcweir // Domino classes. 188*cdf0e10cSrcweir session = NotesFactory.createSession(); 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir // Getting the specified Notes database. 192*cdf0e10cSrcweir Database database = session.getDatabase( "", stringDatabase ); 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir // Getting a collection of all documents from the database. 195*cdf0e10cSrcweir DocumentCollection documentCollection = database.getAllDocuments(); 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir // Getting the first document from the database 198*cdf0e10cSrcweir Document document = documentCollection.getFirstDocument(); 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir // Start to write to cells at this row. 201*cdf0e10cSrcweir int intRowToStart = 0; 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir // The current row. 204*cdf0e10cSrcweir int intRow = intRowToStart; 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir // The current column. 207*cdf0e10cSrcweir int intColumn = 0; 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir // Process all documents 210*cdf0e10cSrcweir while ( document != null ) { 211*cdf0e10cSrcweir // Getting the name of the stock. 212*cdf0e10cSrcweir String stringName = document.getItemValueString("Name"); 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir // Inserting the name to a specified cell. 215*cdf0e10cSrcweir insertIntoCell(intColumn, intRow, stringName, xSpreadsheet, ""); 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir // Getting the number of stocks. 218*cdf0e10cSrcweir double intNumber = document.getItemValueInteger( "Number" ); 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir // Inserting the number of stocks to a specified cell. 221*cdf0e10cSrcweir insertIntoCell( intColumn + 1, intRow, String.valueOf(intNumber), 222*cdf0e10cSrcweir xSpreadsheet, "V" ); 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir // Getting current share price. 225*cdf0e10cSrcweir double doubleSharePrice = document.getItemValueDouble("SharePrice"); 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir // Inserting the current share price to a specified cell. 228*cdf0e10cSrcweir insertIntoCell(intColumn + 2, intRow, 229*cdf0e10cSrcweir String.valueOf(doubleSharePrice), 230*cdf0e10cSrcweir xSpreadsheet, "V"); 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir // Inserting the total value. 233*cdf0e10cSrcweir insertIntoCell(intColumn + 3, intRow, "=B" 234*cdf0e10cSrcweir + String.valueOf( intRow + 1 ) 235*cdf0e10cSrcweir + "*C" + String.valueOf(intRow + 1), 236*cdf0e10cSrcweir xSpreadsheet, ""); 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir // Increasing the current row. 239*cdf0e10cSrcweir intRow++; 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir // Getting the next document from the collection. 242*cdf0e10cSrcweir document = documentCollection.getNextDocument(); 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir // Summing all specific amounts. 246*cdf0e10cSrcweir insertIntoCell(intColumn + 3, intRow, "=sum(D" 247*cdf0e10cSrcweir + String.valueOf( intRowToStart + 1 ) + ":D" 248*cdf0e10cSrcweir + String.valueOf( intRow ), 249*cdf0e10cSrcweir xSpreadsheet, ""); 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir xContext = null; 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir // Leaving the program. 254*cdf0e10cSrcweir System.exit(0); 255*cdf0e10cSrcweir } 256*cdf0e10cSrcweir catch (Exception e) { 257*cdf0e10cSrcweir e.printStackTrace(); 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir } 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir /** Inserting a value or formula to a cell defined by the row and column. 262*cdf0e10cSrcweir * @param intCellX Row. 263*cdf0e10cSrcweir * @param intCellY Column. 264*cdf0e10cSrcweir * @param stringValue This value will be written to the cell. 265*cdf0e10cSrcweir * @param xSpreadsheet Write the value to the cells of this spreadsheet. 266*cdf0e10cSrcweir * @param stringFlag If this string contains "V", the value will be written, 267*cdf0e10cSrcweir * otherwise the formula. 268*cdf0e10cSrcweir */ 269*cdf0e10cSrcweir public static void insertIntoCell(int intCellX, int intCellY, 270*cdf0e10cSrcweir String stringValue, 271*cdf0e10cSrcweir XSpreadsheet xSpreadsheet, 272*cdf0e10cSrcweir String stringFlag) 273*cdf0e10cSrcweir { 274*cdf0e10cSrcweir XCell xCell = null; 275*cdf0e10cSrcweir 276*cdf0e10cSrcweir try { 277*cdf0e10cSrcweir xCell = xSpreadsheet.getCellByPosition( intCellX, intCellY ); 278*cdf0e10cSrcweir } catch ( com.sun.star.lang.IndexOutOfBoundsException exception ) { 279*cdf0e10cSrcweir System.out.println( "Could not get Cell" ); 280*cdf0e10cSrcweir } 281*cdf0e10cSrcweir if ( stringFlag.equals( "V" )) { 282*cdf0e10cSrcweir xCell.setValue((new Float(stringValue)).floatValue()); 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir else { 285*cdf0e10cSrcweir xCell.setFormula(stringValue); 286*cdf0e10cSrcweir } 287*cdf0e10cSrcweir } 288*cdf0e10cSrcweir } 289