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