Building the Open Access handler was easier than we thought
This month, we want to introduce you to another simple RPG Open Access (OA) handler—this one provides the capability to convert a printer file report program to output on the Web. In previous articles and blogs related to OA, we noted that writing a handler to convert 5250 applications to the Web was probably not something you would want to do in-house. Better to purchase one from an experienced vendor and take advantage of their experience. However, converting a printer file to output to a browser is a somewhat simpler task—although not without its challenges.
We first approached the topic of “webulating” report programs in 2001 in a three-part series that started with “RPG for the Web.” In the third part, we introduced the notion of simplifying the Web-programming process by using the CGIDEV2 toolkit, a topic we’ve revisited for many reasons over the years. CGIDEV2 has a major advantage by using a templating system that lets the HTML be separated from the program logic in much the same way as DDS-defined printer files let us separate the layout of a report. This way the layout of the report can be changed without impacting the logic of the program—unless of course you’re adding new record formats and/or fields. Since CGIDEV2 is a free download, we decided to once again use that as our foundation.
That original approach utilized an RPG SPECIAL file, which we’ve noted has several limitations—the major one being that to have a generic “file,” you must sacrifice formatting options. Alternatively, you can write a custom “file” for each report, or write a very sophisticated “file” that uses APIs to interrogate the printer file to determine the buffer layout. Even then, you may still have problems working out which record format is which. Neither of these options is desirable if what you’re trying to achieve is the fast and simple webulation of a print program.
Open Access to the Rescue!
When we first heard about OA, one of the first things we thought about was revisiting the whole printer webulation idea—but it seemed a little complex for our initial experiments. Turns out we were wrong—it’s actually very simple as you’ll see. The real difficulty was in determining the architectural details. Once we had figured those out, the rest was easy.
So how could we make the process generic? First of all, we require a separate template for each printer file. We decided that if we always used the same location in the IFS to store those templates, and always used a common suffix for the file names, then we could use the file name supplied to the handler by OA to form the name of the template. Similarly, we decided if we used the names of the record formats and fields from the printer file as the section and substitution variable names, we could easily populate the values and determine what sections to output from the data supplied in the main OA structure and Name/Value information that’s passed on each request.
Here, you can see an extract from the original printer file DDS and the corresponding HTML skeleton. Notice the correlation of the names used.
Extract of Printer file DDS
Extract
A R DETAIL SPACEA(1)
A CATCODE R 3
A PRODCODE R 10
A DESCR 30 21
A SELLPRICE R 57EDTCDE(K)
A DISCPRICE R 72EDTCDE(K)
A QTYONHAND R 86EDTCDE(K)
A R CATTOTALS SPACEB(1)
A SPACEA(1)
A 1'Values for category: '
A XCCATNAME 30A 22
Extract of corresponding portion of HTML template
<!-- Detail -->
<tr>
<td width=75 height=39 align="center">/%CatCode%/</td>
<td width=75 align="center">/%ProdCode%/</td>
<td width=300 align="left">/%Descr%/</td>
<td width=100 align="right">/%SellPrice%/</td>
<td width=100 align="right">/%DiscPrice%/</td>
<td width=100 align="right">/%QtyOnHand%/</td>
</tr>
<!-- CatTotals -->
<tr>
<td colspan=2 height=39>Values for category:</td>
<td width=300 align="left">/%XCCatName%/</td>
Additionally, we decided we’d use a standardized name (Footer) for a final HTML section that would be used to wrap up the HTML.
Now that the naming conventions are in place, and the template built, let's see how the OA handler was coded.
Coding the OA Handler
Before you read any further, we should point out that the following presumes that you’re already familiar with the basics of writing an OA handler. If that’s not the case, we suggest you read our earlier article, “Getting a Handle on RPG’s Open Access,” before continuing or the following won’t make much sense.
This first code extract is from the open routine. It begins by building the name of the HTML skeleton by combining the directory name (pre-set in the variable skeletonName) with the file name from the OA info structure and our common suffix (A). Note that the file name supplied is in the form of a fixed-length character field and so we trim it when forming the name.
The resulting name is used to load the HTML skeleton (B). Note the use of the second and third parameters to set the starting and ending section delimiters. We do this so they’re in the form of HTML comments, which makes it easier to edit the template in a conventional HTML editor.
Once the template is loaded, we can set the Date variable (C)—more on this later. Notice that no HTML output takes place during the open operation. You could, if you wished, always output a section with a predetermined name to output the standard page headers, for example. In our case, we’ve incorporated these in the section associated with the report's page headings.
D skeletonName s 256a Varying Inz('/Partner400/')
/free
// Build template name using File name then retrieve template (A)
skeletonName += ( %TrimR(info.externalFile.name) + '_Template.html' );
(B) GetHTMLIFS( skeletonName: '<!-- ': ' -->');
(C) UpdHTMLVar( 'Date': %Char(%Date()): '0');
Much to our surprise, the code to handle the write operations is almost as simple. As is normal with OA handler write operations, we use a FOR loop to process each field in turn, and use the VALUE pointer in the Names/Value structure to gain access to the field data (D). Once we can access the data, we use the field's name as the substitution variable name and the supplied length to access the requisite substring (E). All that remains is to use the record format name to determine which HTML section we need to write (F). If it got much simpler than this, the code would write itself!
D value s 32470a Based(pvalue) D i s 5i 0 /free // Process all fields in record For i = 1 to nvInput.num; (D) pvalue = nvInput.field(i).value; // set up to access data (E) UpdHTMLVar( nvInput.field(i).externalName: %subst( value: 1:nvInput.field(i).valueLenBytes )); EndFor; // Now write out current record format (F) WrtSection( info.recordName );
All that remains is to quickly look at the close routine, although this is so simple you could probably work it out for yourself. All we have to do is write out the predetermined footer section and the *Fini special section that sends the HTML to the browser (G). That's it—that's the sum total of the code required to “close” the file.
(G) WrtSection( 'Footer *Fini' );
Additional Thoughts
Although the fundamentals of this approach turned out to be far simpler than we expected, a few things cropped up that we thought we should mention:
- Special functions in the DDS, such as the DATE() one used in the headings of our report, don’t appear as “fields” to the OA handler. They are normally processed by the system's printer file handling routines. We decided to deal with this by using a predetermined name (the highly original “Date” in our case) and to set the value in the open routine. Time and similar values could be handled the same way, but other details such as the user name might be a little more challenging.
- We decided to ignore all spacing and handle it within the HTML skeleton. The only problem with this approach is determining how to handle records that do no spacing (i.e., their data is added to the current line). We suspect that this may be more of a theoretical problem than a real one in most cases. However, in some situations, it may be necessary to modify the original print program to write out a “dummy” section that serves no purpose other than to trigger the output of the data from a number of previous write operations.
- While the data supplied is in human-readable form, it only has the simple editing of the %CHAR BIF applied. In other words, any edit code specified in the printer file definition is ignored. As a result, our Web version of the report doesn’t include comma separators for the thousands and wouldn’t include floating currency signs had we specified them. The latter can easily be accommodated within the HTML template, but the thousands separator would take a bit more work. Luckily, because the number of digits is provided in the Name/Value information, we could handle this fairly easily. The biggest problem would be to determine which fields need such additional editing and which don’t. For example, a seven-digit part number should probably not be output as 9,999,999.
- It’s probably simpler to output the page headings once and subsequently ignore them, but this of course requires that the handler be able to recognize what constitutes a page heading. It can’t do this from the skip/space information as this isn’t handled by RPG but rather by the system's printer file routines. To properly handle this might require either using a standardized name or adding a parameter to the handler specification so the names could be passed from the report program to the handler.
- More sophisticated printer files that incorporate things such as bar codes are obviously going to present a far bigger challenge. We haven't really begun to think about that yet. This is probably not the right solution for those situations.
As you can see, writing a simple generic handler that can quickly and easily allow you to repurpose a report program for the Web isn’t that difficult. It’d be much harder to write one that could literally accommodate anything and everything that could ever conceivably be included in a report program, but most of the time we don't need that. We can probably deal with it by having a few versions customized to the vagaries of specific sets of printer file definitions.
The code used in this example (including the original RPG report program) and the DDS for the files used will be available on our website. If you have any comments on this example, please direct them to our blog entry.