1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 // Lotus Notes Domino API
37 import lotus
.domino
.NotesThread
;
38 import lotus
.domino
.Session
;
39 import lotus
.domino
.Database
;
40 import lotus
.domino
.DocumentCollection
;
41 import lotus
.domino
.Document
;
42 import lotus
.domino
.NotesFactory
;
44 import com
.sun
.star
.lang
.XComponent
;
45 import com
.sun
.star
.lang
.XMultiComponentFactory
;
46 import com
.sun
.star
.uno
.XComponentContext
;
47 import com
.sun
.star
.uno
.UnoRuntime
;
48 import com
.sun
.star
.frame
.XComponentLoader
;
49 import com
.sun
.star
.beans
.PropertyValue
;
50 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
51 import com
.sun
.star
.sheet
.XSpreadsheets
;
52 import com
.sun
.star
.sheet
.XSpreadsheet
;
53 import com
.sun
.star
.container
.XIndexAccess
;
54 import com
.sun
.star
.table
.XCell
;
56 /** This class creates an OpenOffice.org Calc spreadsheet document and fills it
57 * with existing values of documents from a Lotus Notes database.
59 public class NotesAccess
implements Runnable
{
61 /** Host server of the Domino Directory.
63 static String stringHost
= "";
65 /** User in the host's Domino Directory.
67 static String stringUser
= "";
69 /** Password for the user in the host's Domino Directory.
71 static String stringPassword
= "";
73 /** Database with documents to get data from.
75 static String stringDatabase
= "";
77 /** Reading the arguments and constructing the thread.
78 * @param args Holding values for the host, user, and the password of the user.
80 public static void main( String args
[] ) {
83 if ( args
.length
< 4 ) {
85 "usage: java -jar NotesAccess.jar \"<Domino Host>\" \"<User>\" " +
86 "\"<Password>\" \"<Database>\"" );
87 System
.out
.println( "\ne.g.:" );
89 "java -jar NotesAccess.jar \"\" \"\" \"\" \"Stocks.nsf\"" );
93 if ( !args
[ 0 ].trim().equals( "" ) ) {
94 stringHost
= args
[ 0 ].trim();
96 if ( !args
[ 1 ].trim().equals( "" ) ) {
97 stringUser
= args
[ 1 ].trim();
99 stringPassword
= args
[ 2 ].trim();
102 java
.io
.File sourceFile
= new java
.io
.File(args
[ 3 ].trim());
103 stringDatabase
= sourceFile
.getCanonicalPath();
104 } catch (java
.io
.IOException e
) {
105 System
.out
.println("Error: Please check the name or path to your database file.");
110 if ( stringHost
.equals( "" ) ) {
112 NotesAccess notesaccess
= new NotesAccess();
114 // Allowing only local calls to the Domino classes.
115 thread
= new NotesThread( notesaccess
);
118 // Extracting the host, user, and password.
119 NotesAccess notesaccess
= new NotesAccess();
121 // Allowing remote calls to the Domino classes.
122 thread
= new Thread( notesaccess
);
125 // Starting the thread.
129 /** Reading all documents from the given database and writing the data to
130 * an OpenOffice.org Calc spreadsheet document.
134 // get the remote office component context
135 XComponentContext xContext
=
136 com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
138 System
.out
.println("Connected to a running office ...");
140 XMultiComponentFactory xMCF
= xContext
.getServiceManager();
142 /* A desktop environment contains tasks with one or more
143 frames in which components can be loaded. Desktop is the
144 environment for components which can instantiate within
146 XComponentLoader xLoader
= UnoRuntime
.queryInterface(XComponentLoader
.class,
147 xMCF
.createInstanceWithContext(
148 "com.sun.star.frame.Desktop", xContext
));
150 // Load a Writer document, which will be automatically displayed
151 XComponent xComponent
= xLoader
.loadComponentFromURL(
152 "private:factory/scalc", "_blank", 0,
153 new PropertyValue
[0] );
155 // Querying for the interface XSpreadsheetDocument
156 XSpreadsheetDocument xSpreadsheetDoc
=
157 UnoRuntime
.queryInterface(
158 XSpreadsheetDocument
.class, xComponent
);
160 // Getting all sheets from the spreadsheet document.
161 XSpreadsheets xSpreadsheets
= xSpreadsheetDoc
.getSheets() ;
163 // Querying for the interface XIndexAccess.
164 XIndexAccess xIndexAccess
= UnoRuntime
.queryInterface(
165 XIndexAccess
.class, xSpreadsheets
);
167 // Getting the first spreadsheet.
168 XSpreadsheet xSpreadsheet
= UnoRuntime
.queryInterface(
169 XSpreadsheet
.class, xIndexAccess
.getByIndex(0));
172 if ( !stringHost
.equals( "" ) ) {
173 // Creating a Notes session for remote calls to the Domino classes.
174 session
= NotesFactory
.createSession(stringHost
, stringUser
,
178 // Creating a Notes session for only local calls to the
180 session
= NotesFactory
.createSession();
183 // Getting the specified Notes database.
184 Database database
= session
.getDatabase( "", stringDatabase
);
186 // Getting a collection of all documents from the database.
187 DocumentCollection documentCollection
= database
.getAllDocuments();
189 // Getting the first document from the database
190 Document document
= documentCollection
.getFirstDocument();
192 // Start to write to cells at this row.
193 int intRowToStart
= 0;
196 int intRow
= intRowToStart
;
198 // The current column.
201 // Process all documents
202 while ( document
!= null ) {
203 // Getting the name of the stock.
204 String stringName
= document
.getItemValueString("Name");
206 // Inserting the name to a specified cell.
207 insertIntoCell(intColumn
, intRow
, stringName
, xSpreadsheet
, "");
209 // Getting the number of stocks.
210 double intNumber
= document
.getItemValueInteger( "Number" );
212 // Inserting the number of stocks to a specified cell.
213 insertIntoCell( intColumn
+ 1, intRow
, String
.valueOf(intNumber
),
216 // Getting current share price.
217 double doubleSharePrice
= document
.getItemValueDouble("SharePrice");
219 // Inserting the current share price to a specified cell.
220 insertIntoCell(intColumn
+ 2, intRow
,
221 String
.valueOf(doubleSharePrice
),
224 // Inserting the total value.
225 insertIntoCell(intColumn
+ 3, intRow
, "=B"
227 + "*C" + (intRow
+ 1),
230 // Increasing the current row.
233 // Getting the next document from the collection.
234 document
= documentCollection
.getNextDocument();
237 // Summing all specific amounts.
238 insertIntoCell(intColumn
+ 3, intRow
, "=sum(D"
239 + ( intRowToStart
+ 1 ) + ":D"
245 // Leaving the program.
248 catch (Exception e
) {
253 /** Inserting a value or formula to a cell defined by the row and column.
254 * @param intCellX Row.
255 * @param intCellY Column.
256 * @param stringValue This value will be written to the cell.
257 * @param xSpreadsheet Write the value to the cells of this spreadsheet.
258 * @param stringFlag If this string contains "V", the value will be written,
259 * otherwise the formula.
261 public static void insertIntoCell(int intCellX
, int intCellY
,
263 XSpreadsheet xSpreadsheet
,
269 xCell
= xSpreadsheet
.getCellByPosition( intCellX
, intCellY
);
270 } catch ( com
.sun
.star
.lang
.IndexOutOfBoundsException exception
) {
271 System
.out
.println( "Could not get Cell" );
273 if ( stringFlag
.equals( "V" )) {
274 xCell
.setValue((new Float(stringValue
)).floatValue());
277 xCell
.setFormula(stringValue
);
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */