Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Dispatcher eases workflow implementation

Improve your application's workflow with the Dispatcher design pattern and XSL

  • Print
  • Feedback

Page 5 of 7

package workflow.example;
import java.util.*;
import workflow.*;
/**
* The Application class is a Dispatchable object.
* It holds all the values that make up an Application.
**/
public class Application implements Dispatchable {
    public String first = "";
    public String last = "";
    public Vector felonies = new Vector();
    public Vector experiences = new Vector();
    public String position  = "";
    public Date appSubmitDate = null;
    /**
    * Persist the application to XML
    **/
    public String toXML() {
        StringBuffer buff = new StringBuffer("<application>");
        buff.append("<first>"+first+"</first>");
        buff.append("<last>"+last+"</last>");
        buff.append("<position>"+position+"</position>");
        for ( int i = 0; i < experiences.size(); i ++)
          buff.append(((Dispatchable)(experiences.elementAt(i))).toXML());
        for ( int i = 0; i < felonies.size(); i ++)
          buff.append(((Dispatchable)(felonies.elementAt(i))).toXML());
        buff.append("</application>");
        return buff.toString();
    }
}


The Controller

The Application's controller is not part of the Dispatcher framework. Rather, it's a simple object that builds the Application object so that our Dispatcher can process it:

package workflow.example;
/* Generated by Together */
import javax.servlet.*;
import javax.servlet.http.*;
import workflow.servlet.*;
import java.util.Date;
public class ApplicationController implements Controller {
    /**
    *  This method will process the request and add all the data to
      * Application object. It also manipulates the session with
      * the appropriate information.
    **/
    public void process(HttpServletRequest request,
       HttpServletResponse response)
    {
        // Get Application.
        // Add data to Application.
        HttpSession session = request.getSession(true);
        Application app = (Application)session.getAttribute("dispatchable");
        String current = (String)request.getParameter("current");
        // Each of these sections of the iff statement represents
        // the different screens in the workflow.
        if ( current.equals("demographics") )
        {
          app.first=request.getParameter("firstname");
          app.last=request.getParameter("lastname");
          app.position=request.getParameter("position");
        }
        else if ( current.equals("felonies") )
        {
          Felony felony = new Felony();
          felony.description = request.getParameter("description");
          System.out.println("#"+felony.description+"#");
          if ( felony.description != null && !felony.description.equals("") )
          {
            System.out.println(request.getParameter("yearCommitted"));
            felony.yearCommitted = Integer.parseInt
                (request.getParameter("yearCommitted"));
            app.felonies.add(felony);
          }
        }
        else if ( current.equals("workhistory") )
        {
          Experience experience = new Experience();
          experience.title = request.getParameter("title");
          if ( experience.title != null && !experience.title.equals(""))
          {
            System.out.println(request.getParameter("startYear"));
            experience.startYear = Integer.parseInt
                (request.getParameter("startYear"));
            experience.endYear = Integer.parseInt
                (request.getParameter("endYear"));
            experience.employer = request.getParameter("employer");
            app.experiences.add(experience);
          }
        }
        else if ( current.equals("legal") )
        {
            app.appSubmitDate = new Date();//the current date and time
        }
        else if ( current.equals("start") )
        {
        }
        else if ( current.equals("summary") )
        {
        }
    }
}


application.jsp, seen next, controls the headers and footers to keep everything consistent. For each page in the workflow, the system creates a JSP.

  • Print
  • Feedback

Resources