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