connect 2014 jmp101: java for xpages development
DESCRIPTION
Paul T. Calhoun, panagenda IBM Connect 2014, OrlandoTRANSCRIPT
© 2014 IBM Corporation
JMP101: Java for XPages Development
Paul T. Calhoun, Panagenga
Your Presenter – This Is Me!
Paul T. CalhounSenior Software Engineer - [email protected] Owner – NetNotes Solutions Unlimited, Inc. - [email protected]
A highly rated architect, engineer, speaker and presenter who provides customer-focused knowledge transfer and consulting to organizations worldwide.
I have architected Domino, Web, Java, and XML Web Services solutions for his customers using Domino and WebSphere/WebSphere Portal as the primary platforms.
I am the co-author of the IBM Redbook “XML Powered by Domino,” have developed self paced and classroom training for XPages, XML and Web Services as well as Java. I have written dozens of articles for worldwide technical publications.
IBM Champion – 2013 and 2014
Certified – Administrator and Developer
Grandfather of two and ½.– Ask to see my pictures !!!
Agenda
The Java Language Fundamentals
Domino Designer Settings
Java Beans and Managed Beans
Calling Java Code from an XPage
Accessing Domino Objects
Using Third Party Libraries
Wrap Up
4
The Java Language Fundamentals
How do you eat an Elephant ?
One Bite at a time !!!
Learning Java™ is a lot like eating an Elephant !!– It’s a big job !!– And there’s no clear place where to start !!
Resources
Start with J2SE (Java 2 STANDARD Edition)– This covers core Java functionality
• Syntax• Data Types• Constructs• Core Classes
- java.lang- java.io- java.net- etc.
– Allow 3-6 Months
http://www.oracle.com/technetwork/java/javase/overview/index.html
Resources
Learn XML (not part of Sun Certification)– XML Syntax– DTD/XML Schema– XSL
• XSLT• XSL:FO
Finally jump on the J2E bandwagon (In this order!)– Servlets– JSPs– JSFs
– Allow another 3-6 months
Then the rest of the J2E specification– Allow another 3-6 months
Roadblocks
Road Blocks on your Journey to Learning Java– “Linear” thinking instead of thinking in “Objects”
• If you have done any LotusScript Class development you are on your way !– Starting to learn Java with J2E applications (Servlets, JSPs)– Trying to start with the Java Enterprise Technologies– Try to learn Java in conjunction with a HUGE Mission critical project– Not applying what you learn EVERYDAY !!
• This is the MOST critical roadblock !!!
Very Wise Saying
If the only tool you have is a hammer,
you tend to see every problem as a nail
Abraham Maslow
Your XPages Development Toolbox
Formula Language
LotusScript
Java JavaScript
XML
XSLT
dojo
jQuery
CSS
Is the Java Language still relevant ?
The Tiobe index as of December 2013
http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
Java Version in Notes / Domino
12
Java Standard Edition (J2SE)
Java 2 Java Development Kit (JDK) Version 6.0 Used in Notes Domino 9.0 and 8.5
Java 2 Java Development Kit (JDK) Version 1.5.0 Used in Notes Domino 8
Java 2 Software Development Kit (SDK) Version 1.4.2 Used in Notes Domino 7
Java 2 Software Development Kit (SDK) Version 1.4.1 (EOL)
Java 2 Software Development Kit (SDK) Version 1.4.0 (EOL)
Java 2 Software Development Kit (SDK) Version 1.3.1 Used in Notes Domino 6
Java 2 Software Development Kit (SDK) Version 1.3.0 (EOL)
Java 2 Software Development Kit (SDK) Version 1.2.2
Java 1 JDK Development Kit (JDK) Version 1.1.8 Used in Notes Domino 5
13
Domino Designer Settings
There are several things that can be configured that will make your Java in XPages development experience easier
The following are recommendations for setting properties in the Domino Designer BEFORE you start developing
14
Configuring Domino Designer for XPage Development
Configuring Domino Designer for XPage DevelopmentConfigure Memory
Change your memory allocation– Edit the jvm.properties file located in the client installation directory under
• framework/rcp/deploy
Configuring Domino Designer for XPage DevelopmentConfigure Memory
Edit with any text editor– Xmx – Total amount of RAM for Designer AND Client
• Set to at least 512m• Don’t set equal to the amount of system RAM
– Xms – Starting Heap size• Set to at least 128m• Don’t set equal to Xmx value
– Xmca – Memory block size• Set to at least 512k• Thanks a “k” NOT A “m”
Always set in multiple of “4”
Will not take effect until client is restarted if it is running when edited
Configuring Domino Designer for XPage DevelopmentShow Heap Status
Monitor Memory Used
In Designer Preferences– Select General– Check “Show heap status”
Even though this is a checkbox, it does not “remember” the setting.
– It has to be checked each time you start designer.
Heap status is displayed in the lower left hand corner of the designer client.
– Monitor the amount of memory being used– Click the trash can icon to trigger garbage
collection
17
Configuring Domino Designer for XPage DevelopmentConfigure XML Editor settings
Set XML Editor formatting for viewing XPage source
In Designer preferences– Select XML | XML Files | Editor– Change Line width– Check “Split multiple attributes each on a new line”– Check “Clear all blank lines”
Any new XPages source will adhere to these settings
Existing XPages can be “reformatted” to adhere to thesesettings by using the keyboard shortcut
– <shift><ctrl><f>
18
Configuring Domino Designer for XPage DevelopmentFormatting Differences
Before and after XPage Source Formatting
19
Configuring Domino Designer for XPage DevelopmentChange Java Editor Margin
In the Designer Preferences dialog, choose Java | Code Style | Formatter– Click the “New” button– The included profiles can not be edited
20
Configuring Domino Designer for XPage DevelopmentChange Java Editor Margin
Select the “Line Wrapping” tab – Change the “Maximum line width:” property to at least 120
21
Configuring Domino Designer for XPage DevelopmentChange Java Editor Margin
Ensure your profile is the active profile and save preferences
22
Code Snippets
When writing Java code in any Java Editor developers can copy code “snippets” to the snippet plugin.
Choose Window | Show Eclipse Views | Other…
23
Code Snippets
Type “Snippet” into the search field
Select “Snippets” and choose OK
24
Code Snippets
Snippet View is added to Current Perspective
25
Code Snippets
Highlight code in the editor
Right click and choose Add to Snippets…
A prompt to add to an existing category or create a new one will pop up
26
Code Snippets
Provide a Name for the Snippet that will appear in the snippet view under the category
27
Using Code Snippets
To use a code snippet– Place the cursor in the Java editor where the code should be inserted.– Double click on the Snippet name in the Snippet view to insert it
28
Code Snippet Categories can be Imported/Exported to XML Right click the Snippet View and choose “Customize”
In the editor select a snippet category and choose “Export”
The exported XML can then be imported by other developers
29
DemoJava Settings in Domino Designer
30
31
Java Beans and Managed Beans
Java Beans versus Managed Beans
This is possibly one of the most confusing issues when coding with Java
Lets start with Java Beans
A Java Bean is a Plain ‘ol Java Object (POJO), or Java class that conforms to a specific programming convention
– It must have a public no-argument constructor, otherwise the default no argument constructor is used
– The class properties (fields) must be defined as private and accessible using standard public method calls that start with get, set, or is (used for boolean properties instead of get). These are commonly referred to as "getter" and "setter" methods.
– The class must implement java.io.Serializable to be technically qualified as a JavaBean.• This is not “necessary” but a best practice
It is up to the developer to follow these conventions when creating a JavaBean.
32
Java Beans versus Managed Beans
So what is a Managed Bean?
It is important to understand that it makes no difference if a bean is going to be just a Java Bean or a Managed Bean, it is coded the exact same way
– Nothing in the Java source code defines it as a "Managed Bean” – This is all done in the framework, in our case, XPages
Java Beans are NOT Managed Beans; – But they can be, they just need to be "managed” – A Managed Bean follows all of the same conventions as a regular JavaBean, but is
"Managed" by virtue of being registered with the framework it is incorporated in (like the XPages runtime framework)
– A Managed Bean has scope in the framework and its methods can be called directly without the need to first instantiate an object variable for the Managed Bean
• This is the PRIMARY factor that separates Java Beans from Managed Beans
33
A Java Bean Example
Lets start with a simple Java Bean Example
34
A Java Bean Example
Public “getter” and “setters”
35
Calling the Methods from an XPage
Button Code – The SSJS Code– In order to “use” the Java Bean Code it has to be instantiated (lines 1&2)– The public getter and setter methods have to be called
36
importPackage(com.nnsu.beans);
var jce:MyBean = new MyBean();
jce.setFName("Paul");
jce.setLName("Calhoun");
jce.setAge(50);
jce.setHobbies(new java.util.ArrayList(java.util.Arrays.asList(["Riding Motorcycles","Playing with
Grandkids"])));
var retOutput = jce.getFName()+ " "+ jce.getLName()+"</br>";
retOutput += "He is: " + jce.getAge()+"</br>";
for (var i=0;i<jce.getHobbies().size();i++)
{
retOutput += jce.getHobbies().get(i) +"</br>";
}
getComponent("computedField1").setValue(retOutput);
DemoA Java Bean
37
Making that same code a “Managed Bean”
Java code becomes a “Managed Bean” when it is registered with the framework it is running in (In our case XPages)
Managed Beans are “Registered” in the faces-config.xml file in the application they will be used in
38
Faces Confg
The faces-config.xml file is an xml file where managed beans are defined using the following syntax
So to “Register” the previous bean as a “Managed Bean” the sytnax would be
39
<managed-bean>
<description>description</description>
<managed-bean-name>beanName</managed-bean-name>
<managed-bean-class>beanClass</managed-bean-class>
<managed-bean-scope>beanScope</managed-bean-scope>
</managed-bean>
<managed-bean>
<description>My Managed Bean</description>
<managed-bean-name>myBean</managed-bean-name>
<managed-bean-class>com.nnsu.beans.MyBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
Registration components
The description is just that, a short description of what the bean does
The name will be the programmatic name the bean is instantiated with
The class is the full class path to the code in the application
The scope is the “life” of the Managed Bean which can be– application - The bean is available for as long as the application is in memory and is
shared among all users of the application.– session - The bean is available for the current user session and is unique for each user.– view - The bean is available for the life of the current "view" or XPage.– request - The bean is available only while the content of the individual XPage is being
submitted.– none - The bean is instantiated each time it is referenced. This is helpful if the bean is
referenced within another bean.
40
Calling the Methods from an XPage
Button Code – The SSJS Code– In order to “use” the Managed Bean Code simply call the methods using the defined
bean name the code was registered with in the faces-config.xml– Notice that the Bean is never instantiated. This is because the framework (XPages)
instantiates it when it is called the first time.
41
myBean.setFName("Paul");
myBean.setLName("Calhoun");
myBean.setAge(50);
myBean.setHobbies(new java.util.ArrayList(java.util.Arrays.asList(["Riding Motorcycles","Playing
with Grandkids"])));
var retOutput = myBean.getFName()+ " "+ myBean.getLName()+"</br>";
retOutput += "He is: " + myBean.getAge()+"</br>";
for (var i=0;i<myBean.getHobbies().size();i++)
{
retOutput += myBean.getHobbies().get(i) +"</br>";
}
getComponent("computedField1").setValue(retOutput);
Benefits of Managed Bean
Managed Beans have scope
This means– The setter methods can be executed in one event and– The getter methods executed in another event– The methods of the managed bean can be used across multiple pages in the same
application
So should Java Beans ALWAYS be Managed Beans??– As a rule, NO !!!– Only use Managed Beans when you need the values in the bean to “persist” in your
application– If persistence is not required then just use POJO’s
42
DemoManaged Beans
43
44
Calling Java Code from an XPage
Executing Java Code from an XPage
The only Java code that can be “called” from an XPage is– Java Code Elements– Java Source Code in a source folders in the Virtual File System (VFS)
• Primarily prior to 9.0– Code in .jar files located in the JARs folder– Code in .jar files located in the WebContent/WEB-INF/lib folder– Compiled .class and .jar files located in the jvm/lib/ext folder in the file system
45
Executing Java Code from an XPageJava Code Elements
Create Java source code that will be compiled into individual class files when the project is built
Java Code elements are automatically added to the classpath
46
Executing Java Code from an XPageJava Source Code in a source folders in the Virtual File System (VFS)
Java Source code located in a Java Source folder in the VFS can be seen in the “Package Explorer” view of the XPages perspective
Java Code in a “source” folder is automatically added to the classpath
47
Executing Java Code from an XPageCode in .jar files located in the JARs folder
Locally developed or third party Java Archive Files (.jar) can be imported into the Jar design element
Classed in JAR elements are automatically added to the classpath
48
Executing Java Code from an XPageCompiled .class and .jar files located in the jvm/lib/ext folder in the file system
.class and .jar files located in this folder are available to all applications running locally and on the server
GOTCHA: Java source files (.java) files in this folder will not compile and execute
Java Code located in this folder is automatically added to the classpath
49
Referencing Java Code and Calling Methods from SSJS
Java code is instantiated and called from an SSJS event– Import the Java package (unless the default package was used)(which is a VERY bad
idea)– Instantiate an instance of a class from the imported package– Call a method from the instantiated class– “Do something” with the results or return a status message
50
DemoCalling Java code from SSJS
51
52
Accessing Domino Objects
Session
The Notes Session object is the top level object in the Domino class hierarchy
It’s the starting point for calling all other classes (database, view, etc) in the hierarchy
There are two ways to get the session object in Java code executed from an XPage– Pass it in as a parameter from the SSJS– Get it from the JSF “context”, the underlying architecture
53
Session
Pass the global “session” variable in as a parameter
– The SSJS Code
– The Java Code
54
importPackage(com.nnsu.domino);
var jce:DomSession1 = new DomSession1();
var retOutput = jce.getOutput(session);
getComponent("computedField1").setValue(retOutput);
import lotus.domino.Session;
public class DomSession1 {
private StringBuffer output = new StringBuffer();
// global session object passed as parameter in the method call
public String getOutput(Session session) {
try {
output.append("The Notes version is: " + session.getNotesVersion() + nl);
Session
Get it from the JSF “context”, the underlying architecture
55
import javax.faces.context.FacesContext;
import lotus.domino.Session;
public class DomSession2 {
private StringBuffer output = new StringBuffer();
private String nl = "<br />";
// global session object passed as parameter in the method call
public String getOutput() {
FacesContext context = FacesContext.getCurrentInstance();
Session session = (Session) context.getApplication().getVariableResolver().resolveVariable(context, "session");
…}
Session
Once there is a “session” object the rest of the Domino class hierarchy can be traversed
Database
View
ViewNavigator
Document
Item
DocumentCollection
Etc…
56
Recycling Domino Objects
Recycling is something that is unique to working with the Domino Object API
If done correctly then it’s not ever a concern
If NOT done, then the appearance of memory leaks can become an issue
Rule:– If ANY Domino object is iterated over (for or while loops)
• Item, Document, ViewEntry, Database, etc.– Then those items should be recycled as soon as the operation is complete
57
Recycling Domino Objects
In a for or while loop create two objects
Set the value of the first one
Process it
Set the value of the “next” to the second object name
Recycle first object
Set First object equal to the second object
Second Object no longerhas any memory reference
58
DemoDomino Objects
59
60
Using Third Party Libraries
Development Setup
There are two choices when configuring an application to use third party Java libraries– Put all the JARS in the NSF– Put the JARS on the Host File System
Put all the JARS in the NSF
Pros– Makes the application more portable.– Can be “deployed” to the test server and production by replication
Cons– JARS are only accessible by code in the containing NSF– If many NSF’s use this solution then maintenance can become difficult
• Going from version “x” to version “y” has to be done in every application
Put the JARS on Host File System
Pros– Easier to maintain code/upgrade code for all applications that use it
Cons– Harder to deploy (insert snarky Evil Admin comment here)
Development Setup
I prefer the deploy the JARS to the Host File system– (Insert self referencing Evil Admin Comment here)– If the code that CALLS the classes from the JARS is
• A Java Agent, Servlet, Java Class or Java Code Element (available in 8.5.3 and above)
- Deploy the JARS to the <installDir>/jvm/lib/ext folder• SSJS Code from an XPage
- Deploy the JARS to the <installDir>/xsp folder– The JARS will have to be deployed to the Notes Designer Client AND the
development/production servers !!– If the Client or Server is running then they will have to be restarted in order for your code
to recognize that they are there• This is the step you will forget. Just say’in
Development Setup
The JARS can also be added to the NSF container when developing XPages.– This is the option I’m using so you have a self contained demonstration/example system
you can play with locally or on a TEST Server.
Development Setup
In 8.5.3 and below– In the Application that will contain the XPage Code
• From the XPages perspective switch to the Package Explorer View• Expand the WebContent/WEB-INF folder
- Create a folder named “lib”• Import (you can also drag and drop)
the JARS to the lib folder
Development Setup
In 9.0– Import the Jars to the new “JAR” design element– This will automatically add it to the class/build path
• No other configuration is required
Third Party Libraries
Third party libraries can come from a variety of sources– Open Source– Purchased from vendors
Typically provide a “solution” to a particular coding problem not available in the host system API
– “I need to parse a String object at every capitalized letter”– “I need to export all my data to a spreadsheet”– “I need to create a PDF from this data”
One of the “Go To” solutions for tested, reliable Java Libraries is the Apache Software Foundation
68
What is APACHE ?
The Apache Software Foundation (www.apache.org) – Is an Open Source (Yes that means FREE) consortium of companies and developers – Donates time and resources to developing tools (primarily Java based) that simplify
many tasks that developers are faced with everyday– It’s a lot like OpenNTF, but for Java developers
What is APACHE ?
IBM is a major contributor/supporter
If you develop using Java code (and you should be) than you owe it to yourself to spend some time reviewing the projects on this site
Some I use all the time– Xerces– Xalan– XML Graphics (FOP)– POI– Commons
We will explore the following
There are too many projects to cover so we will explore three of the most popular and useful to Domino Developers
– Commons– POI
71
Apache Commons
http://commons.apache.org
Focused on all aspects of reusable Java components
Commons Proper
Commons Proper– Goal of creating and maintaining reusable java Components
Individual downloadable components– Not one GIANT utility library– Just get what you need
Commons Proper
Each of the following components can be downloaded individuallyComponent
Attributes Runtime API to metadata attributes such as doclet tags.
BCEL Byte Code Engineering Library - analyze, create, and manipulate Java class files
BeanUtils Easy-to-use wrappers around the Java reflection and introspection APIs.
Betwixt Services for mapping JavaBeans to XML documents, and vice versa.
BSF Bean Scripting Framework - interface to scripting languages, including JSR-223
Chain Chain of Responsibility pattern implemention.
CLI Command Line arguments parser.
Codec General encoding/decoding algorithms (for example phonetic, base64, URL).
Collections Extends or augments the Java Collections Framework.
Compress Defines an API for working with tar, zip and bzip2 files.
Configuration Reading of configuration/preferences files in various formats.
CSV Component for reading and writing comma separated value files.
Daemon Alternative invocation mechanism for unix-daemon-like java code.
DBCP Database connection pooling services.
DbUtils JDBC helper library.
Digester XML-to-Java-object mapping utility.
Commons Proper
Component
Discovery Tools for locating resources by mapping service/reference names to resource names.
EL Interpreter for the Expression Language defined by the JSP 2.0 specification.
Email Library for sending e-mail from Java.
Exec API for dealing with external process execution and environment management in Java.
FileUpload File upload capability for your servlets and web applications.
Functor A functor is a function that can be manipulated as an object, or an object representing a single, generic function.
Imaging (previously called Sanselan)
A pure-Java image library.
IO Collection of I/O utilities.
JCI Java Compiler InterfaceJCS Java Caching System
Jelly XML based scripting and processing engine.
Jexl Expression language which extends the Expression Language of the JSTL.
JXPath Utilities for manipulating Java Beans using the XPath syntax.
Lang Provides extra functionality for classes in java.lang.
Commons Proper
Component
Launcher Cross platform Java application launcher.
Logging Wrapper around a variety of logging API implementations.
Math Lightweight, self-contained mathematics and statistics components.
Modeler Mechanisms to create Model MBeans compatible with JMX specification.
Net Collection of network utilities and protocol implementations.
OGNL An Object-Graph Navigation Language
Pool Generic object pooling component.
Primitives Smaller, faster and easier to work with types supporting Java primitive types.
Proxy Library for creating dynamic proxies.
SCXML An implementation of the State Chart XML specification aimed at creating and maintaining a Java SCXML engine. It is capable of executing a state machine defined using a SCXML document, and abstracts out the environment interfaces.
Transaction Implementations for multi level locks, transactional collections and transactional file access.
Validator Framework to define validators and validation rules in an xml file.
VFS Virtual File System component for treating files, FTP, SMB, ZIP and such like as a single logical file system.
Commons Lang
Provides a host of helper utilities for the java.lang API– String manipulation methods– Basic numerical methods– Object reflection– Additionally it contains basic enhancements to java.util.Date
Commons Lang - Versions
Latest Version is 3.3.1– For Java 1.5 and higher– For Notes/Domino 8.x and 9.x
Version 2.6 is still available– For Java 1.2 and higher– For Notes Domino 7.x and below
http://commons.apache.org/proper/commons-lang/download_lang.cgi
Commons Lang API
Packages
org.apache.commons.lang3Provides highly reusable static utility methods, chiefly concerned with adding value to the java.lang classes.
org.apache.commons.lang3.builderAssists in creating consistent equals(Object), toString(), hashCode(), and compareTo(Object) methods.
org.apache.commons.lang3.concurrent Provides support classes for multi-threaded programming.
org.apache.commons.lang3.event Provides some useful event-based utilities.
org.apache.commons.lang3.exception Provides functionality for Exceptions.
org.apache.commons.lang3.math Extends java.math for business mathematical classes.
org.apache.commons.lang3.mutable Provides typed mutable wrappers to primitive values and Object.
org.apache.commons.lang3.reflect Accumulates common high-level uses of the java.lang.reflect APIs.
org.apache.commons.lang3.text Provides classes for handling and manipulating text, partly as an extension to java.text.
org.apache.commons.lang3.text.translate An API for creating text translation routines from a set of smaller building blocks.
org.apache.commons.lang3.time Provides classes and methods to work with dates and durations.
org.apache.commons.lang3.tuple Tuple classes, starting with a Pair class in version 3.0.
org.apache.commons.lang3.StringUtils
IsEmpty/IsBlank - checks if a String contains text
Trim/Strip - removes leading and trailing whitespace
Equals - compares two strings null-safe
startsWith - check if a String starts with a prefix null-safe
endsWith - check if a String ends with a suffix null-safe
IndexOf/LastIndexOf/Contains - null-safe index-of checks
IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings
org.apache.commons.lang3.StringUtils
ContainsOnly/ContainsNone/ContainsAny - does String contains only/none/any of these characters
Substring/Left/Right/Mid - null-safe substring extractions
SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
Split/Join - splits a String into an array of substrings and vice versa
Remove/Delete - removes part of a String
Replace/Overlay - Searches a String and replaces one String with another
org.apache.commons.lang3.StringUtils
Chomp/Chop - removes the last part of a String
LeftPad/RightPad/Center/Repeat - pads a String
UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String
CountMatches - counts the number of occurrences of one String in another
org.apache.commons.lang3.StringUtils
IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String
DefaultString - protects against a null input String
Reverse/ReverseDelimited - reverses a String
Abbreviate - abbreviates a string using ellipsis
Difference - compares Strings and reports on their differences
LevenshteinDistance - the number of changes needed to change one String into another
DemoCommons StringUtils
84
Apache POI
POI is a Java API for Microsoft Documents– Not just spreadsheets, but what it’s used most often for– Which is misleading because it contains libraries that specifically allow API access to file
formats based upon • OOXML – Open Office XML Standards • OLE2 – Microsoft’s Compound Document Format
– You do NOT have to have MS Office installed in order to use POI !!• This is a major benefit / enhancement to COM options that require MS Office to be
installed on Domino Servers
Apache POI
POI can “Read” and “Write” to MS and Open source versions of– Spreadsheets– Word Processing documents– Presentation (Powerpoint)– MS Publisher
Useless Trivia– POI is an acronym for “Poor Obfuscation Implementation” in reference to reverse
engineering the original MS Office document formats
Use Cases
Although POI can be used to create many types of MS Office formats we will concentrate on spread sheets
There are three primary use cases for creating spreadsheets from Domino Data– Export all the documents in a view– Export selected documents from a view– Export documents that match a query (Ad Hoc Reporting)
Export All Documents From a View
This is the primary use case– Create an XPage that displays the View
• This isn’t really required, but is nice for context purposes
Create a clickable component that will instantiate the Java code and pass parameters too it
The Button CodeSSJS code on onClick event of component
//Import the Java code package
importPackage(com.nnsu.util);
//Create an Instance of the Java Class
var jcode:POIAllDocs = new POIAllDocs();
//Set the varibles for passing to the Java Class method
var vName = "Main";
var fieldList = ["Company","FirstName","LastName","EmailAddress"];
// The Faces Context global object provides access to the servlet environment via the external content
var extCont = facesContext.getExternalContext();
// The servlet's response object provides control to the response object
var pageResponse = extCont.getResponse();
//Get the output stream to stream binary data
var pageOutput = pageResponse.getOutputStream();
//Set the file name to pass to the response header
var fileName = "CustomerView.xls";
// Set the content type and headers
pageResponse.setContentType("application/x-ms-excel");
pageResponse.setHeader("Cache-Control", "no-cache");
pageResponse.setHeader("Content-Disposition","inline; filename=" + fileName);
//Pass the variables to the Java Method to create the spreadsheet
jcode.createSpreadSheet(vName,fieldList,pageOutput);
//Flush the buffer and close the stream
pageOutput.flush();
pageOutput.close();
// Terminate the request processing lifecycle.
facesContext.responseComplete();
The Java Code
The Java code has one method “createSpreadSheet” that accepts the parameters from the SSJS code
package com.nnsu.util;
public class POIAllDocs {
public static void createSpreadSheet( String viewName, String[] fieldList, OutputStream fileout) {
try {
// Get Database and View to be processed
FacesContext context = FacesContext.getCurrentInstance();
Session s = (Session) context.getApplication().getVariableResolver().resolveVariable(context, "session");
Database db = s.getCurrentDatabase();
View nc = db.getView(viewName);
//Variables to create the workbook and sheet
String sheetName = "Customer View Data";
// Create variables for the notes document and temporary document
Document doc, ndoc;
The Java Code
Use the POI API to create the objects necessary to create a spreadsheet
// Create a new Workbook object from the POI library
HSSFWorkbook wb = new HSSFWorkbook();
// Create a sheet in the workbook
HSSFSheet sheet1 = wb.createSheet(sheetName);
// Create styles for dates and header columns
HSSFCellStyle dateStyle = wb.createCellStyle();
dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
HSSFCellStyle headerStyle = wb.createCellStyle();
HSSFFont headerFont = wb.createFont();
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerStyle.setFont(headerFont);
// Create the Column Header Rows from the Field array passed into the class
HSSFRow row = sheet1.createRow(0);
for (int i = 0; i <= fieldList.length - 1; i++) {
HSSFCell hCell = row.createCell(i);
hCell.setCellValue(fieldList[i]);
hCell.setCellStyle(headerStyle);
}
// Get the first document in the view and process each document in the view
doc = nc.getFirstDocument();
for (int d = 1; d <= nc.getEntryCount(); d++) {
row = sheet1.createRow(d);
for (int f = 0; f <= fieldList.length - 1; f++) {
Item itemval = doc.getFirstItem(fieldList[f]);
if (itemval.getType() == 768) {
row.createCell((Integer) (f)).setCellValue(
itemval.getValueDouble());
} else if (itemval.getType() == 1024) {
Date jdate = itemval.getDateTimeValue().toJavaDate();
HSSFCell dcell = row.createCell(f);
dcell.setCellValue(jdate);
dcell.setCellStyle(dateStyle);
} else {
row.createCell((java.lang.Integer) (f)).setCellValue(
itemval.getText());
}
}
ndoc = nc.getNextDocument(doc);
doc.recycle();
doc = ndoc;
}
// Autoset the width of the spreadsheet columns based upon the values
for (int c = 0; c <= fieldList.length - 1; c++) {
sheet1.autoSizeColumn(c);
}
//Write the contents of the spreadsheet to the output stream
wb.write(fileout);
} catch (EvaluationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotesException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The Java Code
Process every document in the view and create a “row” in the spreadsheet output
// Get the first document in the view and process each document in the view
doc = nc.getFirstDocument();
for (int d = 1; d <= nc.getEntryCount(); d++) {
row = sheet1.createRow(d);
for (int f = 0; f <= fieldList.length - 1; f++) {
Item itemval = doc.getFirstItem(fieldList[f]);
if (itemval.getType() == 768) {
row.createCell((Integer) (f)).setCellValue(
itemval.getValueDouble());
} else if (itemval.getType() == 1024) {
Date jdate = itemval.getDateTimeValue().toJavaDate();
HSSFCell dcell = row.createCell(f);
dcell.setCellValue(jdate);
dcell.setCellStyle(dateStyle);
} else {
row.createCell((java.lang.Integer) (f)).setCellValue(
itemval.getText());
}
}
ndoc = nc.getNextDocument(doc);
doc.recycle();
doc = ndoc;
}
The Java Code
Set the width of each column to the width of the longest entry in that column
“Write” the output of the spreadsheet to the output stream passed in as a parameter
// Autoset the width of the spreadsheet columns based upon the values
for (int c = 0; c <= fieldList.length - 1; c++) {
sheet1.autoSizeColumn(c);
}
//Write the contents of the spreadsheet to the output stream
wb.write(fileout);
} catch (EvaluationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotesException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
DemoExport All Documents in a View to a Spreadsheet
94
Exporting Selected Document from a View
The base code and setup are the same with the following exceptions– A View container must be included with the first column set to “select” the documents– The SSJS code must be edited to get the list of selected documents from the View and
pass them to the processing Java Code
95
//Get the list of selected documents to pass to the java code.
var viewPanel1:com.ibm.xsp.component.xp.XspViewPanel = getComponent("viewPanel1");
var docIds = viewPanel1.getSelectedIds();
…
//Pass the variables to the Java Method to create the spreadsheet
jcode.createSpreadSheet(vName,fieldList,pageOutput,docIds);
Exporting Selected Document from a View
The Java code receives the selected documents as an array of string id’s
In the code that creates the spread sheet rows each document id retrieved by it’s id from the array to be processed
96
public static void createSpreadSheet(String viewName, String[] fieldList, OutputStream fileout,String[] nc) {
try {
…
// Get the first document in the id array and process each document in the array
for (int d = 1; d <= nc.length ; d++) {
doc = db.getDocumentByID(nc[d-1]);
…
DemoExport Selected Documents in a View to a Spreadsheet
97
Exporting Query Result Documents
A Notes Document collection can be returned programmatically
The query generally includes the name of the form the document was created with and then the value from one of the fields
The same base code is used with fields added to capture the additional information needed to create the spreadsheet
98
SSJS to Capture the parameters
The SSJS code captures the parameters from the XPage components
99
//Import the Java code package
importPackage(com.nnsu.util);
//Create an Instance of the Java Class
var jcode:POIQueryDocs = new POIQueryDocs();
//Set the variables from the XPage input
var formName = getComponent("comboBox1").getValue();
var selFields = getComponent("inputText1").getValue();
var selLabels = getComponent("inputText2").getValue();
var fieldList = selFields.split(",");
var colLabels = selLabels.split(",");
var fileName = getComponent("wbName").getValue();
var sheetName = getComponent("sheetName").getValue();
var qString = getComponent("queryString").getValue();
SSJS to Capture the parameters
The parameters are then passed to the java code to be processed
100
…
//Pass the variables to the Java Method to create the spreadsheet
jcode.createSpreadSheet(fieldList,colLabels,pageOutput,formName,sheetName,qString);
//Flush the buffer and close the stream
pageOutput.flush();
pageOutput.close();
// Terminate the request processing lifecycle.
facesContext.responseComplete();
Java code to process the parameters
The Java code uses the passed in parameters to create a Notes Document Collection from a full text query
– This works even if the application is not full text indexed, it’s just not as efficient– GOTCHA: The administrator can set a switch on the application to NOT ALLOW full text
queries if the application is not full text indexed
101
public class POIQueryDocs {
public static void createSpreadSheet(String[] fieldList,String[] colList, OutputStream fileout,String formName,
String sheetName,String qString) {
try {
// Get Database and View to be processed
FacesContext context = FacesContext.getCurrentInstance();
Session s = (Session) context.getApplication().getVariableResolver().resolveVariable(context, "session");
Database db = s.getCurrentDatabase();
//Create query string from values passed in from the XPage
String queryString = "FIELD form CONTAINS " + formName + " AND " + qString;
DocumentCollection nc = db.FTSearch(queryString);
DemoExport Query Results Documents to a Spreadsheet
102
Bonus Material
Using APACHE FOP to Create PDF’s
103
So what is FOP?
FOP is a sub-project of the XML Graphics project at APACHE
FOP stands for Formatting Objects Processor– This is a print formatter driven by XSL FO (Formatted Objects)
So what is FOP?
An API that – Reads an FO Tree– Renders the resulting pages to a specified output
• PDF (Primary)• PS• PCL• AFP• XML• Print• AWT• PNG
Um.. How about that again in English !!
It’s a java based toolset that lets YOU (the developer) create solutions that allow your End Users (the Bain of all existence) to create PDF documents from Notes content for FREE !!!
Yes FREE!!!– Ok, Free is relative. – You are going to have to invest some time, but I’m going to give you a working
framework that you can implement out of the box.– For Free!!!
The Process
The great thing about FOP is it is a highly duplicable design pattern
XML Source XSLT StyleSheet
FO Source
FO Rendering
EngineRendered
Output
The Process
It all starts with an XML Document (can be from disk or in memory)
XML Source XSLT StyleSheet
FO Source
FO Rendering
EngineRendered
Output
The XML
The XML can be– A Static Document– The output from ?ReadViewEntries
• Appended to the end of a Domino View URL– The output from generateXML
• Method of the Notes Document class– The results of running an XAgent– The results of running an Agent– The results of running a Web Service
The XML
The source of the XML is not as important as the FORMAT and CONSISTANCY of the XML– The XML must be well formed and optionally valid– Make sure that if you do not control the source that you have an SLA with the source
provider that includes them providing the XML Schema AND changes to the XML Schema in enough advance that you have time to test it.
The XML
Using the ?ReadViewEntries option is HIGHLY duplicable– Once you have the stylesheet (and I’m providing that to you) that transforms the source
XML to the FO XML then ANY view source can be passed to the code to produce a PDF of the view
The XML
The other options require an XSLT stylesheet that is specific to transforming the specified XML to FO XML.
– This is not as flexible, but once the base stylesheet is created it can be stored in a notes document that is editable by a non-developer
• This option allows changes to colors, fonts etc without developer intervention and re-compiling/re-deploying the code
The Stylesheet
The stylesheet that is used is an XSLT document (written in XML) that uses the FOP tags from the tag library
XML Source XSLT StyleSheet
FO Source
FO
Rendering
Engine
Rendered
Output
The XSL FO Stylesheet
This is by far the most challenging part of this solution
Stylesheet creators must be able to create and edit XSLT stylesheets (Doh!)
The Tags used in the style sheet are not documented at the FOP site– The good news is they ARE documented at the w3 schools site
• http://www.w3schools.com/xslfo/default.asp– The other good news is they are documented in the form of a tutorial !!– You can follow the tutorial to examine how to create a base XSL FO style sheet
• You can also copy/paste this example code directly to your XSL FO style sheet to get started !!
Using Eclipse or an XSLT editor is the best choice
Download the version that has the Web Tools Plugin (WTP)
I usually download the one that supports J2EE development
The benefit of this option is there is an XSLT editor included not just an XML editor
Also you can train “power-users” to use eclipse to build, edit, maintain XSLT stylesheets for the purpose of maintaining their own output without the need for them to have designer
– This might sound difficult, but it is significantly easier to train both developers and power users on XSLT than it is Java !!
The XSLT
The Stylesheet is made up of a combination of XSLT and XSL:FO tags
Structure of the FO Tags
The XSL:FO tags are all about the layout of the “printed” page
XSL:FO tags always start with “root”– Followed by a “layout master”
• Followed by a “page master”• The a series of page sequences
that contain- Flows- Blocks
The Layout and Page Master
These tags define the output page– Height– Width– Margins– etc
The Page Sequence tag
References the page master set tag to get its output constraints
The Page Content
The Page content is the output using a series of “flows” and “blocks”
A “flow” contains a series of “blocks”
A “block” is roughly equivalent to a paragraph on the page
The Page Content
Blocks can contain other constraining tags like the “table” tag
Where are the stylesheets Stored
After the stylesheet is created there are two options– Save the stylesheet as a design resource (A stylesheet)– Save the stylesheet in a document that is accessible from the notes client
• This allows editing/maintainence of the stylesheets without the need of a designer client
The option you choose will depend upon level of expertise of the folks you have back at your house
Storing Stylesheets in Designer
The XSLT Sheets can bestored in DDE in the StyleSheets folder contained in the resources folder
Alternately Store in documents
You can create a – Form– View
to store Notes documents that contain the XSLT stylesheets
Your code will “lookup” the stylesheet when applying it to the XML source
The Code
Now that you have the XML Source and the XSLT stylesheet you are ready to write some code !!! (Yea !!)
Can be coded as– Java Agents– Java Code elements– Coded in SSJS (like an XAgent)
Creating the FO Source
The code will take the XSLT stylesheet, apply it to the XML source and produce the XSL:FO that is used by the rendering engine to produce the PDF
XML Source XSLT StyleSheet
FO Source
FO Rendering
EngineRendered
Output
The Code
The “SSJS” button code that calls the Java Code
The Java Code that creates the PDF
The Java Code takes the output stream as a parameter, reads the XML and XSLT and uses APACHE FOP to generate the PDF
The Complete Process
That finishes the design pattern
XML Source XSLT StyleSheet
FO Source
FO Rendering
EngineRendered
Output
DemoCreate PDF from Domino Data
130
Resources
IBM XPages Forum – Moderated by the community– http://www-10.lotus.com/ldd/xpagesforum.nsf/
TLCC – Self paced Courses on XPages and Java– http://www.tlcc.com
NetNotes Solutions Unlimited - My Web Site
http://www.nnsu.com (Sample code available here)
Apache Software Foundation– http://www.apache.org
OpenNTF– http://www.openntf.org
XPages Tips– http://xpagetips.blogspot.com
131
Access Connect Online to complete your session surveys using any:– Web or mobile browser – Connect Online kiosk onsite
132
Your Turn !!!Questions, Questions, Questions !!!!
133
134
Acknowledgements and Disclaimers
© Copyright IBM Corporation 2014. All rights reserved.
U.S. Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
Please update paragraph below for the particular product or family brand trademarks you mention such as WebSphere, DB2, Maximo, Clearcase, Lotus, etc.
IBM, the IBM logo, ibm.com, [IBM Brand, if trademarked], and [IBM Product, if trademarked] are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both. If these and other IBM trademarked terms are marked on their first occurrence in this information with a trademark symbol (® or ™), these symbols indicate U.S. registered or common law trademarks owned by IBM at the time this information was published. Such trademarks may also be registered or common law trademarks in other countries. A current list of IBM trademarks is available on the Web at “Copyright and trademark information” at www.ibm.com/legal/copytrade.shtml
If you have mentioned trademarks that are not from IBM, please update and add the following lines:
Java
Other company, product, or service names may be trademarks or service marks of others.
Availability. References in this presentation to IBM products, programs, or services do not imply that they will be available in all countries in which IBM operates.
The workshops, sessions and materials have been prepared by IBM or the session speakers and reflect their own views. They are provided for informational purposes only, and are neither intended to, nor shall have the effect of being, legal or other guidance or advice to any participant. While efforts were made to verify the completeness and accuracy of the information contained in this presentation, it is provided AS-IS without warranty of any kind, express or implied. IBM shall not be responsible for any damages arising out of the use of, or otherwise related to, this presentation or any other materials. Nothing contained in this presentation is intended to, nor shall have the effect of, creating any warranties or representations from IBM or its suppliers or licensors, or altering the terms and conditions of the applicable license agreement governing the use of IBM software.
All customer examples described are presented as illustrations of how those customers have used IBM products and the results they may have achieved. Actual environmental costs and performance characteristics may vary by customer. Nothing contained in these materials is intended to, nor shall have the effect of, stating or implying that any activities undertaken by you will result in any specific sales, revenue growth or other results.