diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
commit | 267c6f2ac71f92999e969232431ba04678e7437e (patch) | |
tree | 358c9467650e1d0a1d7227a21dac2e3d08b622b2 /odk/examples/java/NotesAccess | |
parent | Initial commit. (diff) | |
download | libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.tar.xz libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.zip |
Adding upstream version 4:24.2.0.upstream/4%24.2.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'odk/examples/java/NotesAccess')
-rw-r--r-- | odk/examples/java/NotesAccess/Makefile | 112 | ||||
-rw-r--r-- | odk/examples/java/NotesAccess/NotesAccess.java | 282 | ||||
-rw-r--r-- | odk/examples/java/NotesAccess/Stocks.nsf | bin | 0 -> 262144 bytes |
3 files changed, 394 insertions, 0 deletions
diff --git a/odk/examples/java/NotesAccess/Makefile b/odk/examples/java/NotesAccess/Makefile new file mode 100644 index 0000000000..47d024db8d --- /dev/null +++ b/odk/examples/java/NotesAccess/Makefile @@ -0,0 +1,112 @@ +#************************************************************************* +# +# The Contents of this file are made available subject to the terms of +# the BSD license. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. Neither the name of Sun Microsystems, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR +# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE +# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +#************************************************************************** + +# Builds the Java NotesAccess example of the SDK. + +PRJ=../../.. +SETTINGS=$(PRJ)/settings + +include $(SETTINGS)/settings.mk +include $(SETTINGS)/std.mk + +# Define non-platform/compiler specific settings +EXAMPLE_NAME=NotesAccessExample +OUT_APP_CLASS = $(OUT_CLASS)/$(EXAMPLE_NAME) + +APP_NAME=NotesAccess +APP_JAR=$(OUT_APP_CLASS)/$(APP_NAME).jar + +JAVAFILES = \ + NotesAccess.java + +CLASSFILES = $(patsubst %.java,$(OUT_APP_CLASS)/%.class,$(JAVAFILES)) + +SDK_CLASSPATH = $(subst $(EMPTYSTRING) $(PATH_SEPARATOR),$(PATH_SEPARATOR),$(CLASSPATH)\ + $(PATH_SEPARATOR)$(OUT_APP_CLASS)) + + +# Targets +.PHONY: ALL +ALL : \ + JavaNotesAccessExample + +include $(SETTINGS)/stdtarget.mk + +NotesAPIs : + @echo -------------------------------------------------------------------------------- + @echo This example uses external APIs from Lotus Notes/Domino. You can download a + @echo trial version from "$(QM)http://www.lotus.com/downloads$(QM)". After installation + @echo please do the following: + @echo 1. Add the install path to the PATH or LD_LIBRARY_PATH of this shell. + @echo e.g "$(QM)PATH=C:\Program Files\lotus\notes;...$(QM)" + @echo 2. Add the "$(QM)Notes.jar$(QM)" to your CLASSPATH + @echo e.g. "$(QM)CLASSPATH=C:\Program Files\lotus\notes\Notes.jar;$(QM)" + @echo -------------------------------------------------------------------------------- + + +$(CLASSFILES) : $(JAVAFILES) + -$(MKDIR) $(subst /,$(PS),$(@D)) + $(SDK_JAVAC) $(JAVAC_FLAGS) -classpath "$(SDK_CLASSPATH)" -d $(OUT_APP_CLASS) $(JAVAFILES) + +$(OUT_APP_CLASS)/%.mf : + -$(MKDIR) $(subst /,$(PS),$(@D)) + @echo Class-Path: Notes.jar> $@ + @echo Main-Class: com.sun.star.lib.loader.Loader>> $@ + $(ECHOLINE)>> $@ + @echo Name: com/sun/star/lib/loader/Loader.class>> $@ + @echo Application-Class: $*>> $@ + +$(APP_JAR) : $(OUT_APP_CLASS)/$(APP_NAME).mf $(OUT_APP_CLASS)/$(APP_NAME).class + -$(DEL) $(subst \\,\,$(subst /,$(PS),$@)) + -$(MKDIR) $(subst /,$(PS),$(@D)) + +cd $(subst /,$(PS),$(OUT_APP_CLASS)) && $(SDK_JAR) cvfm $(@F) $(APP_NAME).mf $(APP_NAME).class + +$(SDK_JAR) uvf $@ $(SDK_JAVA_UNO_BOOTSTRAP_FILES) + +JavaNotesAccessExample : NotesAPIs $(APP_JAR) + @echo -------------------------------------------------------------------------------- + @echo NOTE: Before you run this example make sure that the external "$(QM)Notes.jar$(QM)" can + @echo be found besides "$(QM)$(APP_JAR)"$(QM) + @echo and that the PATH variable includes the Notes installation directory. + @echo - + @echo Please use the following command to execute the example! + @echo - + @echo $(MAKE) NotesAccess.run + @echo -------------------------------------------------------------------------------- + +%.run: $(APP_JAR) + $(SDK_JAVA) -Dcom.sun.star.lib.loader.unopath="$(OFFICE_PROGRAM_PATH)" -jar $< "$(QM)$(QM)" "$(QM)$(QM)" "$(QM)$(QM)" Stocks.nsf + +.PHONY: clean +clean : + -$(DELRECURSIVE) $(subst \\,\,$(subst /,$(PS),$(OUT_APP_CLASS))) diff --git a/odk/examples/java/NotesAccess/NotesAccess.java b/odk/examples/java/NotesAccess/NotesAccess.java new file mode 100644 index 0000000000..8945a9e998 --- /dev/null +++ b/odk/examples/java/NotesAccess/NotesAccess.java @@ -0,0 +1,282 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + +// Lotus Notes Domino API +import lotus.domino.NotesThread; +import lotus.domino.Session; +import lotus.domino.Database; +import lotus.domino.DocumentCollection; +import lotus.domino.Document; +import lotus.domino.NotesFactory; + +import com.sun.star.lang.XComponent; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.uno.XComponentContext; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XComponentLoader; +import com.sun.star.beans.PropertyValue; +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.sheet.XSpreadsheets; +import com.sun.star.sheet.XSpreadsheet; +import com.sun.star.container.XIndexAccess; +import com.sun.star.table.XCell; + +/** This class creates an OpenOffice.org Calc spreadsheet document and fills it + * with existing values of documents from a Lotus Notes database. + */ +public class NotesAccess implements Runnable { + + /** Host server of the Domino Directory. + */ + static String stringHost = ""; + + /** User in the host's Domino Directory. + */ + static String stringUser = ""; + + /** Password for the user in the host's Domino Directory. + */ + static String stringPassword = ""; + + /** Database with documents to get data from. + */ + static String stringDatabase = ""; + + /** Reading the arguments and constructing the thread. + * @param args Holding values for the host, user, and the password of the user. + */ + public static void main( String args[] ) { + Thread thread; + + if ( args.length < 4 ) { + System.out.println( + "usage: java -jar NotesAccess.jar \"<Domino Host>\" \"<User>\" " + + "\"<Password>\" \"<Database>\"" ); + System.out.println( "\ne.g.:" ); + System.out.println( + "java -jar NotesAccess.jar \"\" \"\" \"\" \"Stocks.nsf\"" ); + System.exit( 1 ); + } + + if ( !args[ 0 ].trim().equals( "" ) ) { + stringHost = args[ 0 ].trim(); + } + if ( !args[ 1 ].trim().equals( "" ) ) { + stringUser = args[ 1 ].trim(); + } + stringPassword = args[ 2 ].trim(); + + try { + java.io.File sourceFile = new java.io.File(args[ 3 ].trim()); + stringDatabase = sourceFile.getCanonicalPath(); + } catch (java.io.IOException e) { + System.out.println("Error: Please check the name or path to your database file."); + e.printStackTrace(); + System.exit( 1 ); + } + + if ( stringHost.equals( "" ) ) { + // Initializing. + NotesAccess notesaccess = new NotesAccess(); + + // Allowing only local calls to the Domino classes. + thread = new NotesThread( notesaccess ); + } + else { + // Extracting the host, user, and password. + NotesAccess notesaccess = new NotesAccess(); + + // Allowing remote calls to the Domino classes. + thread = new Thread( notesaccess ); + } + + // Starting the thread. + thread.start(); + } + + /** Reading all documents from the given database and writing the data to + * an OpenOffice.org Calc spreadsheet document. + */ + public void run() { + try { + // get the remote office component context + XComponentContext xContext = + com.sun.star.comp.helper.Bootstrap.bootstrap(); + + System.out.println("Connected to a running office ..."); + + XMultiComponentFactory xMCF = xContext.getServiceManager(); + + /* A desktop environment contains tasks with one or more + frames in which components can be loaded. Desktop is the + environment for components which can instantiate within + frames. */ + XComponentLoader xLoader = UnoRuntime.queryInterface(XComponentLoader.class, + xMCF.createInstanceWithContext( + "com.sun.star.frame.Desktop", xContext)); + + // Load a Writer document, which will be automatically displayed + XComponent xComponent = xLoader.loadComponentFromURL( + "private:factory/scalc", "_blank", 0, + new PropertyValue[0] ); + + // Querying for the interface XSpreadsheetDocument + XSpreadsheetDocument xSpreadsheetDoc = + UnoRuntime.queryInterface( + XSpreadsheetDocument.class, xComponent); + + // Getting all sheets from the spreadsheet document. + XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets() ; + + // Querying for the interface XIndexAccess. + XIndexAccess xIndexAccess = UnoRuntime.queryInterface( + XIndexAccess.class, xSpreadsheets); + + // Getting the first spreadsheet. + XSpreadsheet xSpreadsheet = UnoRuntime.queryInterface( + XSpreadsheet.class, xIndexAccess.getByIndex(0)); + + Session session; + if ( !stringHost.equals( "" ) ) { + // Creating a Notes session for remote calls to the Domino classes. + session = NotesFactory.createSession(stringHost, stringUser, + stringPassword); + } + else { + // Creating a Notes session for only local calls to the + // Domino classes. + session = NotesFactory.createSession(); + } + + // Getting the specified Notes database. + Database database = session.getDatabase( "", stringDatabase ); + + // Getting a collection of all documents from the database. + DocumentCollection documentCollection = database.getAllDocuments(); + + // Getting the first document from the database + Document document = documentCollection.getFirstDocument(); + + // Start to write to cells at this row. + int intRowToStart = 0; + + // The current row. + int intRow = intRowToStart; + + // The current column. + int intColumn = 0; + + // Process all documents + while ( document != null ) { + // Getting the name of the stock. + String stringName = document.getItemValueString("Name"); + + // Inserting the name to a specified cell. + insertIntoCell(intColumn, intRow, stringName, xSpreadsheet, ""); + + // Getting the number of stocks. + double intNumber = document.getItemValueInteger( "Number" ); + + // Inserting the number of stocks to a specified cell. + insertIntoCell( intColumn + 1, intRow, String.valueOf(intNumber), + xSpreadsheet, "V" ); + + // Getting current share price. + double doubleSharePrice = document.getItemValueDouble("SharePrice"); + + // Inserting the current share price to a specified cell. + insertIntoCell(intColumn + 2, intRow, + String.valueOf(doubleSharePrice), + xSpreadsheet, "V"); + + // Inserting the total value. + insertIntoCell(intColumn + 3, intRow, "=B" + + ( intRow + 1 ) + + "*C" + (intRow + 1), + xSpreadsheet, ""); + + // Increasing the current row. + intRow++; + + // Getting the next document from the collection. + document = documentCollection.getNextDocument(); + } + + // Summing all specific amounts. + insertIntoCell(intColumn + 3, intRow, "=sum(D" + + ( intRowToStart + 1 ) + ":D" + + intRow, + xSpreadsheet, ""); + + xContext = null; + + // Leaving the program. + System.exit(0); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + /** Inserting a value or formula to a cell defined by the row and column. + * @param intCellX Row. + * @param intCellY Column. + * @param stringValue This value will be written to the cell. + * @param xSpreadsheet Write the value to the cells of this spreadsheet. + * @param stringFlag If this string contains "V", the value will be written, + * otherwise the formula. + */ + public static void insertIntoCell(int intCellX, int intCellY, + String stringValue, + XSpreadsheet xSpreadsheet, + String stringFlag) + { + XCell xCell = null; + + try { + xCell = xSpreadsheet.getCellByPosition( intCellX, intCellY ); + } catch ( com.sun.star.lang.IndexOutOfBoundsException exception ) { + System.out.println( "Could not get Cell" ); + } + if ( stringFlag.equals( "V" )) { + xCell.setValue((Float.valueOf(stringValue)).floatValue()); + } + else { + xCell.setFormula(stringValue); + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/java/NotesAccess/Stocks.nsf b/odk/examples/java/NotesAccess/Stocks.nsf Binary files differnew file mode 100644 index 0000000000..10ed138ed3 --- /dev/null +++ b/odk/examples/java/NotesAccess/Stocks.nsf |