fhg
Volume 7, Number 38 -- October 31, 2007

Wrapping Free Form Text

Published: October 31, 2007

by Michael Sansoterra


Note: The code accompanying this article is available for download here.

One common integration problem encountered by those building client/server or Web-based interfaces to a legacy green-screen application is mapping a free form text field into a fixed width field. This tip presents a code segment I wrote to solve this problem.

For example, say you have the following DDS defined legacy table called ORDCOMT that is designed to store the comments for a customer order:

Column

Data Type

Description

ORDID

7P,0

Order Id

CMTSEQ

3P,0

Comment Sequence

CMTTXT

25A

Comment Text

In the legacy application, a customer service representative enters the customer's comments line by line in a green screen where the entry fields are limited to 25 characters. Now you bolt a Web front end on your legacy order entry system, but there is a small problem--who in the Web world wants to enter order comments 25 characters at a time? No one. Typically, Web-based front ends allow the user to type their comment(s) in one large free form box.

Enter the "WrapText" RPG subprocedure (code is available at the beginning of article). The prototype for the subprocedure is as follows:

D	WrapText	PR	12288 
D	UnfText		8192	Varying Const Options(*VarSize)
D	LineLen		5 	0 Const
D	LineBreak		10	Varying Const Options(*NoPass)

"WrapText" will receive up to 8K of unformatted text and "wrap" it into multiple lines based on a specified line length. If the free form text allows embedded line break codes (such as carriage return and line feed), the new line indicator can be passed as an optional parameter. The utility will generate new lines automatically when it encounters a line break code.

The utility returns a fixed length text field (12K) containing pseudo-lines formatted at the specified line length. The calling program will need to process this text field either by overlaying an array of the appropriate element size on the returned data or by parsing the data manually.

For example, if variable "FmtText" holds the result of the "WrapText" procedure for a line length of 25 characters, you could extract line two from the variable as follows:

Line2=%Subst(FmtText:26:25);

Or in general, extract any line using variables:

(LineNo=line to extract, LineLen=length of line):
AnyLine=%Subst(FmtText:(LineNo-1)*LineLen+1:LineLen);

As an added goodie, I created an SQL table function wrapper, (subprocedure WrapText_TF), that allows "WrapText" to be used by SQL (see code at the beginning of this article for the required CREATE FUNCTION statement to use WRAPTEXT with SQL).

For example, the following query converts free text into a tabular format, with each wrapped line being assigned its own row:

Select * 
From Table(WrapText( 
'The quick brown fox jumps over the lazy dog',25,'')) A

The result set returned is as follows:

LINENO	LINETEXT 
1	The quick brown fox jumps 
2	over the lazy dog 

Wrapping at 15 characters per line instead of 25, the results are adjusted accordingly:

LINENO 	LINETEXT
1	The quick brown
2	fox jumps over 
3	the lazy dog 

For one more example, the following sample free form comment has an embedded carriage return and line feed (EBCDIC X'0D25') that causes an additional break to be inserted:

Select * 
From Table(WrapText( 
'The quick brown fox jumps over'||x'0D25'||'the lazy dog',25,
x'0D25')) A 

The above query returns the following (including a forced new line after the word "over"):

LINENO	LINETEXT
1	The quick brown fox jumps
2	over 
3	the lazy dog 

This table function can ease the conversion of free form text into the ORDCOMT DB2 table mentioned above as follows:

Insert Into OrdComt
Select :OrdId, LineNo*10 As Seq, LineText
From Table(WrapText(:Comment,25,X'0D25')) Comments

As a final thought, this utility has other uses. I've used it to display free form comments from a database table on the good old fixed width green screen. I've also used this utility to wrap computer generated SQL statements into a more readable format (just a warning, this utility is not smart enough to consider constants with embedded spaces as a single "word," so the wrapping may not occur correctly.) A little imagination can find many uses.

Michael Sansoterra is a programmer/analyst for i3 Business Solutions, an IT services firm based in Grand Rapids, Michigan. You can email him at the IT Jungle contact page.




                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
GUILD COMPANIES

New Book: The System i RPG & RPG IV
Tutorial and Lab Exercises

 

This affordable self-study package by Brian Kelly was originally as a lab guide. The book comes packaged with a 1-2-3-type tutorial, lab exercises, and PowerPoint presentations with notes on the slides that make for handy reference material immediately available on your workstation.

 

You won't want to put down this comprehensive guide to learning System i RPG/RPG IV.

 

Price: $59.95
Get Your Copy at the IT Jungle bookstore today!


Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

SafeData:  The iSeries HA Solution that’s Guaranteed
COMMON:  Join us at the annual 2008 conference, March 30 - April 3, in Nashville, Tennessee
NowWhatJobs.net:  NowWhatJobs.net is the resource for job transitions after age 40


 

IT Jungle Store Top Book Picks

The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket Developers' Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
iSeries Express Web Implementer's Guide: List Price, $59.00
Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
WebFacing Application Design and Development Guide: List Price, $55.00
Can the AS/400 Survive IBM?: List Price, $49.00
The All-Everything Machine: List Price, $29.95
Chip Wars: List Price, $29.95


 
The Four Hundred
IBM's Math on User-Priced System i Versus Vintage Machines

Midrange Shops Get Disaster Recovery Services from IBM

Ask TPM: Two System i Questions, and Two Responses

As I See It: Survey Says . . .

The Linux Beacon
Egenera Breaks Blade Software Free from Hardware

Citrix Closes XenSource Deal, Does Deal with Dell and Xen Desktops

Sun Puts Intel Quad-Core Chips into Ultra Workstations

Midrange Shops Get Disaster Recovery Services from IBM

Four Hundred Stuff
Zend Plans Treats, Tricks for System i Programmers

Mantis Bug Tracker Ported to i5/OS

mrc Goes Web 2.0 with m-Power

IBM Updates Disk and Tape, Buys Storage Software Developer

Big Iron
IBM Hit by Financial Services Slowdown in Q3

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
October 27, 2007: Volume 9, Number 43

October 20, 2007: Volume 9, Number 42

October 13, 2007: Volume 9, Number 41

October 6, 2007: Volume 9, Number 40

September 29, 2007: Volume 9, Number 39

September 22, 2007: Volume 9, Number 38

The Windows Observer
Office Communication Server 2007 Launched by Microsoft

Will OCS 2007 Live Up to the Hype?

Zend Puts Out New Release of Commercial-Grade PHP

Growing Businesses, Upgrades Drive IT Hiring in Q4

The Unix Guardian
IBM's Power-Based Servers Save the Day in Q3

Sun Puts Intel Quad-Core Chips into Ultra Workstations

Intel Is Back on Track in Q3, AMD Is Fighting to Get There

Mad Dog 21/21: Symphony for the Devil

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

THIS ISSUE SPONSORED BY:

Profound Logic Software
Guild Companies
ARCAD Software


Printer Friendly Version


TABLE OF CONTENTS
Use PHP to Bring i5/OS Resources to the Web

Wrapping Free Form Text

Admin Alert: Limiting System i User Sign-ons the Smart Way

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
Reallocate disk space from one lpar to another

how to retrieve a workstation ID

Finding *OUTFILE Template Files

i5/OS V5R4 Release Notes

MCH1202





 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement