Home > AS/400 Tips > iSeries programmer tips > Coming to terms with call back
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES PROGRAMMER TIPS

Coming to terms with call back


Paul Tuohy
02.08.2006
Rating: -4.00- (out of 5)


iSeries news and advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



[TABLE]

I want to give you a practical example of using a call back along with a couple of procedures that might be of use when displaying lists of information on a Web page using CGIDEV2.

But let's get started with the concept of call back. Call back is where a program issues a call to a procedure that, in turn, issues a call back to a procedure in the calling program. Clear as mud, right? Figure 1 should make it a little easier to understand. Your program issues a call to a standard procedure in a service program. But there is a little bit of logic that must be performed by this standard procedure that may be different on every call, depending on the caller. Instead of coding a lot of complex if/else logic, the standard procedure will call a procedure back in the calling program. Of course, the "call back" procedure does not have to be back in the calling program just as long as it is accessible as a bound call (i.e. it can be in another module or service program).

[TABLE]

Figure 1: Call back

Usually, the call back technique is used at a fairly low level of programming, such as using the C functions qsort and bsearch, both of which are extremely well documented in the Redbook "Who Knew You Could Do That with RPG IV?". But you do not need to go to a very low level to make use of the technique. Let's look at an example.

An Example

Figure 2 shows a web page that displays a list of products. Above the list are a couple of options where the user can select whether the list should be displayed by row or by column and how many columns should be displayed. They simply select the two radio buttons and click the change button. All of the logic for handling and processing the selection is written in a couple of subprocedures: all you have to do is write a procedure that places the information for a product and follow a couple of rules with your HTML.

Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
iSeries programmer tips
Groovy programming on IBM i
EGL Rich UI on IBM i: Do you Dojo?
Running PHP open source applications: NOBODY needs authority
Programming for the Web on the IBM i, what is possible
Using geocoding on AS/400 to enhance your Web presence
The best technologies and tools for System i programmers in 2009
Seven IBM i project lessons learned in 2008
Documenting nested program structures on the AS/400
What is an integrated database?
An automated CL method of moving a query from AS/400 to Excel

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


[TABLE]

Figure 2: List manipulation on a Web page

Figure 3 shows the code for the called program. The main points to note are:

  • The program uses the CGIDEV2 procedure "gethtmlIFSMult" to load multiple documents for the construction of the Web page. This is very much the same as constructing a 5250 screen from many record formats but here you have the benefit of being able to use the same format for multiple Web pages.

  • One of the standard formats that are loaded is StandardFormatSelect.html. This contains the form used for the selection by row or column and the number of columns. This is standard for all Web pages and you do not need to amend it in any way for an individual program.

  • The program loads all information to be displayed into a data structure array – LoadArray. The field NumRows indicates the number of rows that are loaded in the array.

  • Calls are made to the two standard procedures FormatSelect and FormatList. FormatSelect constructs and displays the selection options and FormatList constructs and displays the list -- more about these in a moment.

  • Coded at the very end of the program is a subprocedure named SetColumnData. This is the "call back" procedure. The procedure is passed an integer which indicates which row should be placed in the document. The procedure used the updHTMLvar procedure to update values for the required product code and description.

    [TABLE]

    The HTML

    There are a few rules that you must follow when defining the HTML (shown in Figure 4) for the list to be displayed. Remember, this list is going to be written by a standard routine.

    The HTML must contain seven sections that are written by the FormatList procedure:

  • StartFormatTable contains the table definition tag. The width of the table is set by the variable TableWidth (this value is calculated by FormatList).

  • StartHeaderRow contains the tag to start the definition of a table row.

  • HeaderColumn contains the tags and text for the header information for one entry in the list: a product code and description in this case. This section is repeated for the number of selected columns.

  • EndHeaderRow contains the tags for the end of the header row.

  • StartDataRow contains the tags for the start of a data row (i.e. this is the start of one row on the page).

  • DataColumn contains the tags and variables for one entry in the list. These are the variables that will be updated by the called back procedure (SetColumnData in Figure 3). This section is repeated for the number of selected columns.

  • EndDataRow contains the tags to mark the end of a data row on the page.

    HeaderColumn and DataColumn are the two sections that require all the work. The rest of the sections will, for the most part, remain the same unless you need to customize any of the table or row attributes.

    [TABLE]

    The procedures

    Figure 5 shows the prototypes for the two standard procedures and the call back procedure.

    Figure 5: Prototypes for the procedures

    These are the main points to consider:

  • FormatSelect is the procedure that processes the standard selection format. The first parameter indicates the name of the program that is called when the Change button is selected i.e. the name of the program you are writing. Back in Figure 3 the program name is picked up from the program status data structure. The optional second parameter allows you to specify the maximum number of columns that may be selected -- it defaults to six.

  • FormatList is the procedure that constructs and writes the list. The first parameter indicates the number of entries in the list. The second parameter is a procedure pointer to the call back procedure. Back in Figure 3 the %PAddr built in function is used to pass the address of the SetColumnData subprocedure. The optional third parameter allows you to define the width of a list entry in pixels -- it defaults to 250.

  • SetColumnData is the call back procedure. The name of the procedure is irrelevant since the procedure is called based on a procedure pointer -- but the prototype must define a ten digit integer as the sole parameter. This integer indicates the item in the list that should be added to the document.

    And the rest

    And then there is the code for the standard procedures and HTML documents. But one of the great things about ILE is that you do not care about the code as long as it works – just as you know that all those CGIDEV2 procedures work. So all you have to do is download the code and install it .

    And finally

    Now you have an idea of how call backs can be used and you have added another tool to your programming tool belt.

    And you have the basis of a little utility that allows your users to customize lists the way they want.

    -----------------------------------
    About the author: Paul Tuohy is CEO of ComCon, an iSeries consulting company. He is the author of Re-Engineering RPG Legacy Applications and is one of the quoted industry experts in the IBM Redbook "Who Knew You Could Do That With RPG IV?"

    Rate this Tip
    To rate tips, you must be a member of Search400.com.
    Register now to start rating these tips. Log in if you are already a member.


    Submit a Tip




    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



  • iSeries Security - Security Tools, Physical Security and System Security
    HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts