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