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