Showing posts with label IBM i. Show all posts
Showing posts with label IBM i. Show all posts

Tuesday, April 18, 2017

Dynamic HTML with CGI Programming

Hello Friends,

Last couple of articles, we have been working with some static HTML. Now let’s go a step ahead and create a dynamic HTML with CGI. We are also going to see how to deal with database (PF) as well.

Typically we are going to do below stuffs,
  1. Create a RPGLE program which can get input from browser
  2. Check if we have any data in our database for the input
  3. Respond to browser with the data retrieved from PF
Basic Idea:

I am going to have a database called ORDERS. This will be having order information. We shall get order number from the browser input and will return the order information.




















Basically I am going to return the values in the form of HTML Table. So we need to construct table header (<TH>) and table data (<TD>) based on the number of rows.

In summary http://youras400ip/cgi/ordlist/123 should return details of order 123 in table format.

Here ORDLIST is RPGLE , 123 is order number

How to parse URI:

In order to get the order number from the URL we have to parse it. Using IBM procedure GETENV we can retrieve environment variable. Below command will help to retrieve the full URI. 

 Uri = %Str( GetEnv('REQUEST_URI') );   In our case URI = “/cgi/ordlist/123”

Once you get this it is easy to find order number from this. 

 ID1 = '/ordlist/' ;
 Pos = %Scan(ID1: Uri) + %Len(ID1) ;
 InpOrd = %int(%Subst(Uri:Pos));    

Once you get the order number then it is generic programming logic to loop through the order number in ORDERS file and for each record build the Table Detail HTML tag and write it into browser using QtmhWrStout.

Setll  InpOrd  Orders;                                
ReadE  InpOrd  Orders;                                
DoW Not %Eof(Orders);
   Data = '<tr>'                                   +              
            '<td>'+ %Trim(%Char(OrdNo))   +'<td>'  +  
            '<td>'+ %Trim(OrdItm)         +'<td>'  +        
            '<td>'+ %Trim(%Char(OrdQty))  +'<td>'  + 
            '<td>'+ %Trim(%Char(OrdDate)) +'<td>'  +
          '<tr>'                                   ;              
QtmhWrStout(Data: %len(%trimr(Data)): Err);           
ReadE  InpOrd Orders;                                 
EndDo;                                                

How to set library list:

We may have our PF in some other library or we may have different PF sitting in different library. In this case we have to instruct system to load require library list for running the program. This can be done by adding QIBM_CGI_LIBRARY_LIST command in httpd.conf

Multiple libraries can be given by separating with ";"

SetEnv QIBM_CGI_LIBRARY_LIST "LIB1;LIB2;LIB3"








This is it. Just restart your Apache server and point to your browser for accessing the program.

Full version of program is available here .

http://youras400ip/cgi/ordlist/123












http://youras400ip/cgi/ordlist/555












http://youras400ip/cgi/ordlist/1234













So how do you feel..!!! Interesting right... Again it is just simple prototype. We can do even more using CGI. I will try to cover another topic in my next post. Until then...

Have Fun...!!! Happy Coding...!!!

Sunday, March 19, 2017

Time Zone component in AS400


Hello Friends,

Have you come across a scenario on which your AS400 system (IBM i) may need to communicate with other system (those are in different time zone) and you needed to interface any date (or) timestamp value which eventually require time zone component added to it?

Actually I did come across this situation and I was searching for a clean method for time zone conversion in AS400. This should have the capability of converting time zone from one another considering Day Light Saving (DST). Interesting right?

Luckily AS400 is providing a “Convert Date and Time Format” (QWCCVTDT) API to do the same.


So how does it work?


DSPSYSVAL  SYSVAL(QTIMZON) → Use this command to see your system time zone



We can see here time zone is QP0100CET4. What does it mean? If you check with WRKOBJ command we can see it is a time zone component. To understand more on it use below command.

WRKTIMZON →   This will list all time zone component you have in your system.



Wow!! We have time zone description including day light saving time shift as well. That s great.


How to use this in our QWCCVTDT API?


Basically this API has 3 main parameter Input_TimeStamp, From Time Zone & To Time Zone.

From & To time zone parameters are time zone components (like QP0100CET4). I have written a small utility program which will convert time zone & also return time zone abbreviation value for the converted time zone. Click here to view the code.

Note: AS400 itself is not coming with all list of Time Zone component installed. But we have a provision to create our own by CRTTIMZON command. We can give our own offset values (like UTC+5) and define our DST timing as well.

Hope you will find this helpful if you are searching a solution for time zone conversion in AS400.

Have Fun!!! Happy Coding…!!!