Wednesday 22 December 2010

STRUTS 2 Simple Master-Detail JAVA program

This is a simple STRUTS 2 example done with NetBeans 6.9.1. It is a simple Master-Detail example. The master list data is kept in a list and displayed in a table. When a row is chosen with the view link a detail JSP screen is opened and the selected item is displayed. The list that is used by the master table is loaded with the setList action at the very beginning of the application.

This is a migration of the same program from Struts 1 to Struts 2. It looks simple but this technique may be used to populate DDLBs in real applications. A reservation about this is: if the amount of the material is too much it causes substantial delays in the startup of the program.

Some credit goes to Sedat Ergin and RoseIndia whose works I changed and took out bits and pieces.

Kind regards.
Ali Riza SARAL

Note: I left intentionally some debugging lines to give some hints.






web.xml
-------
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Struts Blank</display-name>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>


index.html
-----------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=ars/setList.action">
</head>

<body>
<p>Loading ...</p>
</body>
</html>







struts.xml
----------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<include file="ars.xml"/>

<!-- Add packages here -->

</struts>



ars.xml
-------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="ars" namespace="/ars" extends="struts-default">

<action name="setList" class="ars.SetListAction">
<result name="success">/listProcess.jsp</result>
</action>
<action name="viewDetail" class="ars.ViewDetailAction">
<result name="success">/viewDetailProcess.jsp</result>
</action>
</package>
</struts>



SetListAction.java
------------------
package ars;
import com.opensymphony.xwork2.ActionContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

public final class SetListAction extends ActionSupport implements
ServletRequestAware,ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse response;
private List list;

public SetListAction() {
request = getServletRequest();
}

public String execute() throws Exception {
System.out.println("ARSSSss SetListAction.execute()");
System.out.println("ARSSSss ContextPath="+request.getContextPath());

HttpSession session = request.getSession();
System.out.println("ARSSSss ServletContext="+session.getServletContext());
list = new ArrayList();
list.add("Fruits");
list.add("Apple");
list.add("Mango");
list.add("Orange");
list.add("Pine Apple");

session.setAttribute("baseList",list);
System.out.println("ARSSSsss baseList="+session.getAttribute("baseList").toString());
// ServletActionContext.getRequest().getSession().setAttribute("baseList", list);
//Map session1 = (Map) ActionContext.getContext().get("session");
//session1.put("baseList", list);


return SUCCESS ;
}
public List getList(){
return list;
}
public void setServletRequest(HttpServletRequest request){
this.request = request;
}

public HttpServletRequest getServletRequest(){
return request;
}

public void setServletResponse(HttpServletResponse response){
this.response = response;
}

public HttpServletResponse getServletResponse(){
return response;
}
}



listProcess.jsp
---------------
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Iterator</title>
</head>
<body>
<table border="2px" bgcolor="#CCCC99" cellspacing="1">
<s:iterator value="#session.baseList" id="iteratorItem">
<tr>
<td>Item Value:</td>
<td>
<s:property value="iteratorItem"/>
</td>
<td>
<s:url var="url" action="/viewDetail">
<s:param name="iteratorItem" value="iteratorItem"/>
</s:url>
<s:a href="%{url}">View</s:a>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>









ViewDetailAction.java
---------------------
package ars;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

public final class ViewDetailAction extends ActionSupport implements
ServletRequestAware, ServletResponseAware {

private HttpServletRequest request;
private HttpServletResponse response;

public ViewDetailAction() {
request = getServletRequest();
}

public String execute() throws Exception {

System.out.println("ARSSSsss ViewDetailAction java");

HttpSession session = request.getSession();
String detailInfo = request.getParameter("iteratorItem");
System.out.println("ARSSSsss detailInfo=" + detailInfo);
System.out.println("ARSSSsss request " + request.getParameterNames());
session.setAttribute("baseDetail", detailInfo);
return SUCCESS;
}

public void setServletRequest(HttpServletRequest request) {
this.request = request;
}

public HttpServletRequest getServletRequest() {
return request;
}

public void setServletResponse(HttpServletResponse response) {
this.response = response;
}

public HttpServletResponse getServletResponse() {
return response;
}
}



viewDetailProcess.jsp
---------------------
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Detail</title>
</head>
<body>
<s:text name="#session.baseDetail" />
</body>
</html>



Wednesday 8 December 2010

CONVERTING PROGRAMS FROM MAINFRAME TO UNIX (3)

IMPLEMENTATION OF STARTING A TRANSACTION

Index.html
Sets frame to login.jsp


Login.jsp
Sets Library.jsp which carries the global substructure…
Checks whether there is a previous session
Invalidates previous session
Gets logon parameters such as user pwd working library etc.
Form action posts library.jsp

Library.jsp
Check whether there is a previous screen lock and go to a frame which warns backbutton has been pressed
Get parameters from the previous screen via String user = request.getParameter(“user”)
Remove session parameters related to window processing
Get properties file related to security and do security checking
Instantiate library object using l = (Library) Class.forName(“library”+ “.application”).newInstance();
Set library attributes from the logon data
Get session attribute values
Set session attributes such as user pwd
Check STACK and if it is full remove first and pageContext.forward to action.jsp and command (module name) as param

Action.jsp
Check the command parameter if it is wrong pageContext.forward.
Get Library from the session via Library lib=(Library)request.getSession().getAttribute("library");
Check if lib exists
Check via Object[] lockx=(Object[])request.getSession().getAttribute("lock"); if it exists display backbutton.jsp
Handle various commands such as beginning with logon globals etc
Extract command parms
At the end:
Object[] lock=new Object[2];
request.getSession().setAttribute("lock",lock);
lib.setPageContext(pageContext);
synchronized(lock) {
lib.invokeProgram(command,lock);
lock.wait();
}

The library method invokeProgram calls the mechanism which first finds the related class name of that program name in that module, it also checks whether a class for this program name exists. It records the last fetched program for future references. It runs the main method of that program.
{

Program p=getProgram(name);

p.main();
}

public Class getProgramClass(String _name,String _package) throws Exception {

Class cl=(Class) Class.forName(qualifiedName);
batchDebug("Class loaded successfully "+cl);
return cl;
}
return (Class) Class.forName(qualifiedName);
}


Friday 3 December 2010

CONVERTING PROGRAMS FROM MAINFRAME TO UNIX (2)

BASIC SUBSTRUCTURE

There has to be three or four programs that carry the basic infrastructure.

1- A library.java program which contains all the general structures and functions.

2- A program.java program which contains all the structures related with java programs or beans.

3- A global memory file or dir which contains all the DB acces related structures such as Hybernate definitions etc.

library.java has to be instantiated from the beginning of each JSP and it should be possible to be referred to with a simple function from each java program.
A reference to the global memory has to be created at the beginning of each java program.

It is important to design the system early so that the contents of these files are clear.

Library.java
Set the identity of the current lib and program
Read the properties file
Set DB connection parameters
Set all the system variables based on properties values
Set encoding, datetime, user
Program management (invoke, thread processing, stack management)
Input handling(thread processing, STRUTS functions)
Screen and window (normal and modal) processing
STRUTS processing for returning input values from the screens
Dynamic help
DB functions similar to Hybernate
Debug utilities
Mainframe like workfile utilities
Error message processing
Printer management
Original languages program calling/fetching utilities
Exception processing
Connection Pool management
Connections to External routines
Properties utilities
Lib lock processing

Program.java
Input control related to JSP management
Fields related management STRUTS mechanisms
Locking mechanisms
Screen stack and BACK functions
Program termination
WINDOW (position etc) management
General stack management
Edit masks management
Functions converted from the original language but does not exist in JAVA
SORT
Printing substructure
Utility functions for programming
Help substructure
Utilities of the original system

Global memory file is an extension of the Library.java. It should inherit Library.java and add Hybernate definitions etc.

Thursday 2 December 2010

CONVERTING PROGRAMS FROM MAINFRAME TO UNIX

This will be series of simple notes on the conversion of legacy mainframe software systems to JAVA/UNIX systems. The target audience is IT management in the beginning but later on there will be detailed information to the point of Software Requirements Doc-SRD with absolute references to the Detailed Design Doc-DDD.

I have spent the last two years of my life working on a conversion project for Royal Saudi Naval Airforce using an American software company’s conversion product. While I will pay due attention not to infringe the rights of these it is also my right to use this experience to sustain my family’s well-being.

Program conversion of a legacy mainframe system is composed of many phases and parts:
1- The conversion of the programming language code – for example COBOL codes have to be converted to JAVA.
a. This requires a GRAMMAR definition for the legacy programming language.
b. A parser – ex. JavaCC
c. A converter which will convert the parser output to the target language.
d. Support infrastructure that will remedy the structural differences between the source and the target language - ex. PIC data type does not exist in JAVA.

2- The conversion of the data
a. The data in the database have to be exported/imported and converted if necessary
b. The database system itself has to be changed or a UNIX version of the same system must be installed.
c. The access to the DB table fields has to be automated via a custom system or Hybernate.

3- Conversion of legacy programs does not mean only the two items above, it also means the change of the system that underlies the online and batch sections of the software.
a. Transaction processing mechanism of the Online system – ex. CICS
b. Screen MAP processing mechanism
c. The access to the values of screen fields have to be automated via a custom system or STRUTS.
d. BATCH processing mechanism.
e. A system library which includes all the programs that support the FRAMEWORK.

4- Actual conversion
a. Conversion of MAPs to JSPs.
b. Conversion of Online-CICS programs to JAVA-JSP programs.
c. Conversion of BATCH JCL JOBs to UNIX shell scripts.
d. Conversion of BATCH programs to JAVA.