Open-source applications are programs whose source is freely available to be redistributed and enhanced as the user sees fit. The origins of the open-source software movement can be traced back to a man named Richard Stallman, who was at one time a programmer at the MIT AI lab. In the early '80s, Stallman fathered the GNU's Not Unix (GNU) project, whose goal was to create an entire operating system and a set of development tools that would be freely distributable. At the same time, he authored his vision for what is known today as open-source software in a work entitled the "GNU Manifesto."
Although GNU isn't UNIX, it's intended to be a UNIX-compatible operating system. Building on the GNU project in the early '90s, a programmer named Linus Torvald developed the kernel for a new operating system based on the GNU software license. That operating system (Linux) is now the third most popular operating system in the world. Today, the open-source movement thrives in every aspect of the software world. In fact, probably most software applications you can imagine have an open-source cousin. In this article, we'll examine how to make the most of open-source applications on your System i.
The concept of open-source software is nothing new on the System i. In fact, the System i HTTP Web Server is based on the popular open-source Apache Web server. And let's not forget that the Linux operating system has been available for the System i for years. Having said that, System i–specific open-source projects have, unfortunately, been few and far between. The good news, however, is that many open-source applications can be utilized on the System i, even though they're not System i–specific. One example of this is Apache Tomcat. This extension to the Apache Web server adds Java servlet and JavaServer Pages (JSP) capabilities. A Redbook is available with instructions for installing Tomcat on your System i.
Open-Source Tools for Building Office Documents
Another example of an open-source technology that you can use on the iSeries is the Jakarta POI project. This project is responsible for developing a set of open-source Java APIs for reading and writing Microsoft Office documents. This set of APIs is broken down into a set of subcomponents, as shown in the table below.
Jakarta POI Java APIs | |
Component | Description |
POIFS | APIs for reading and writing OLE 2 Compound Document Formats in Java |
HSSF | APIs for reading and writing Microsoft Excel 97(-XP) spreadsheets in Java |
HWPF | APIs for reading and writing Microsoft Word 97(-XP) documents using Java |
HPFS | APIs that allow access to Microsoft Office document file properties (title, author, etc.) |
The POIFS API allows you to access documents using the OLE 2 Compound Document Format, the common "archived file" format used by Microsoft Office documents. Generally, the other APIs described here use the POIFS APIs under the covers. These APIs give us access to the file system itself through the org.apache.poi.poifs.filesystem.POIFSFileSystem class, the file system directory structure through the org.apache.poi.poifs.filesystem.DirectoryEntry instance, and individual documents through the org.apache.poi.poifs.filesystem.DocumentInputStream instance.
Microsoft Excel spreadsheet documents can be manipulated using the HSSF APIs, which are discussed in detail in the MC Press TechTip "Excel on the Fly" by Giuseppe Costagliola.
The HWPF project is intended to give a Java programmer access to Microsoft Word documents. This project is in a very early development phase and in fact has been stalled for quite some time. Word documents themselves are accessed through the org.apache.poi.hwpf.HWPFDocument class. Currently, this API supports basic document reading and editing capabilities, including the ability to read and update/append text within a Word document. Text can be extracted from a Word document using the org.apache.poi.hwpf.extractor.WordExtractor class. The getRange() method can be used to retrieve a specific portion of a document. A retrieve range can be used with the insertBefore() and insertAfter() methods to add text before or after that range.
The HPFS API gives us access to additional details related to a Microsoft Office document. These properties include the same type of details that can be found within the document properties dialog within Word or Excel, for example title and/or author. This API is accessed via the org.apache.poi.hpsf.SummaryInformation class. Individual properties can be accessed using the SummaryInformation class methods getTitle(), getAuthor(), etc.
Each of these APIs can be incorporated into Java applications running on your System i to create Microsoft Office–compatible documents while utilizing data in your System i database. Additional information on the Jakarta POI project can be found on the project Web site.
Open-Source Report Creation
Most System i programmers are used to using RPG to create report programs. Today, many of us use translation tools to convert the spooled file output generated by these RPG programs into PDF format so that the reports can be easily displayed with Adobe Acrobat Reader.
An example of a utility that does this is the SCS2iText utility illustrated in the TechTip "Spooled Files and PDF, Part Three" by Giuseppe Costagliola. The sample utility is based on the open-source iText Java class library. This class library gives you the ability to dynamically generate PDF documents from within a Java application. With that idea in mind, why not eliminate the middle man and generate your report programs in pure Java and at the same time send the output directly to a PDF file? This is all possible using iText. The iText class library is simple enough to use even for a novice Java programmer. Let's take a look at exactly how to use the iText class library within a Java reporting program. By combining JDBC database connectivity with iText capability to output a PDF, you can easily generate a PDF report on the fly. The Java source shown below illustrates this.
// PDFFieldList.java
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.sql.*;
class PDFFieldList
{
public static void main (String [] args) throws Exception
{
// Input Parameters
String fileName = args[0]; // File Name
String libName = args[1]; // Library Name
String outPDF = args[2]; // Output PDF Path/filename
Document doc = new Document (PageSize.LETTER.rotate ());
PdfWriter.getInstance (doc, new FileOutputStream (outPDF));
doc.open ();
// Create a 5-column table.
PdfPTable table = new PdfPTable (5);
// Create database connection
try {
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
Connection conn =
DriverManager.getConnection("jdbc:db2:*local");
// Create and add a title across both columns.
String heading = "Field Listing for File " + fileName + " in
library " + libName + ".";
PdfPCell cell = new PdfPCell (new Paragraph (heading));
cell.setColspan (5);
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
table.addCell (cell);
// Add header cells for these columns.
table.addCell ("Field");
table.addCell ("Description");
table.addCell ("Type");
table.addCell ("Length");
table.addCell ("Dec");
// Add each row.
try {
String sqlStr = "SELECT SYS_CNAME, LABEL, COLTYPE, LENGTH,
SCALE " +
" FROM QSYS2.SYSCOLUMNS WHERE SYS_TNAME ='" + fileName
+ "' AND SYS_DNAME ='" + libName + "' ";
PreparedStatement s = conn.prepareStatement(sqlStr);
ResultSet rs = s.executeQuery();
while (rs.next()) {
table.addCell (rs.getString(1));
table.addCell (rs.getString(2));
table.addCell (rs.getString(3));
table.addCell (rs.getString(4));
table.addCell (rs.getString(5));
}
}
catch (SQLException e) {
System.out.println("SQLException exception: ");
System.out.println("Message:....." +
e.getMessage());
System.out.println("SQLState:...." +
e.getSQLState());
System.out.println("Vendor Code:." +
e.getErrorCode());
}
// Add the table to the document.
doc.add (table);
doc.close ();
} catch (Exception e) {
System.out.println("Database exception: " +
e.getMessage());
}
}
}
This sample program lists field information for a defined System i file. The first argument on the call identifies the file name, while the second identifies the library containing the defined file, and the third supplies the location and filename for the output PDF file.
This example imports the com.lowagie.text.* class along with the com.lowagie.text.pdf.* class. You create a new instance of the Document object named doc. This object represents a PDF document with a letter-size page in landscape orientation. The data for the report is read using a standard JDBC connection using a prepared SQL statement based on the SYSCOLUMNS file. The data from this file is output to a PDF table, where each cell in the table contains a data element from the SYSCOLUMNS file. Once all of the table records have been output, the table is added to the PDF document and the document is closed.
Prior to compiling this example, you'll need to download the iText jar from the project Web Site. Once you've downloaded the jar file, create a new folder in the root of the IFS named "iText" and place the iText jar in that folder. Also move the source for the PDFFieldList.java example into that folder as well. Next, enter a Qshell session using the QSH command. From the QSH command line, enter the following command to ensure that your classpath is set correctly prior to compiling the new class:
Note that the version of the iText jar file should match the version downloaded.
Now you're ready to compile your Java program using these commands:
javac PDFFieldList.java
Notice that you first relocate to the iText folder prior to compiling. Once you've successfully compiled this program, you can call the newly created class using the following command:
This statement executes the PDFFieldList class to generate a report containing a list of fields for the SYSTABLES file. The list is output to a PDF named SYSTABLES.PDF in the iText folder. The resulting PDF file shows a single table that contains all of the report data. Figure 2 illustrates this output PDF.
Figure 2: This is the output from the PDF report. (Click image to enlarge.)
The true beauty of using iText is that you can completely customize your report output, including fonts, text placement, and even images. This simple example illustrates the basic technique you need to use to create PDF reports using iText and Java.
Considering the Open-Source Alternative
In this article, I've covered just a few examples of how to integrate open-source software with your System i. I hope I've helped to convince you to consider checking out the open-source options for your next programming task. You may find that you've opened yourself to a whole new world of possibilities.
Mike Faust is Applications Support Manager for Invivo Corp in Orlando, Florida. Mike is also the author of the books The iSeries and AS/400 Programmer's Guide to Cool Things and Active Server Pages Primer and SQL Built-in Functions and Stored Procedures. You can contact Mike at
LATEST COMMENTS
MC Press Online