Saturday, December 17, 2011

Form Hanlder Example

In this example we want to give the user HTML form based access to a Pen component, and we could do that directly, but the user would be fairly free to set invalid data for the properties of the Pen. To prevent this we need some kind of validation processing and that's where the FormHandler comes in. It 'sits' between the form and the Pen component (or database in real life). The form fields are associated with the properties of the PenFormHandler component instead of with the Pen properties directly. Additionally the SUBMIT button of the JHTML form is associated with the submit property of the PenFormHandler.

When the form is submitted, Dynamo will copy the form field values into the PenFormHandler properties and finally the bean association on the Submit button causes the PenFormHandler ::handleSubmit method to get invoked to validate the user input. If the handleSubmit method finds no errors it updates the precious data in the Pen object, otherwise it adds errors to the form exceptions list (from GenericFormHandler). When the page is redisplayed the ErrorMessageForEach droplet iterates over the PenFormHandlers formException list and displays any error messages that it finds

package imagescript.atg.demo;
/**
 *
 * @copyright Copyright (c) 2002
 * @author Stuart Jones
 * @version 1.1
 */

public class Pen
{
  public Pen()  {  }

  String mColor;
  String mTipType;

  public  String getColor(){ return mColor; }
  public  void setColor(String str){ mColor = str; }

  public  String getTipType(){return mTipType; }
  public  void setTipType(String str){ mTipType = str; }

  public void sayHello()
  {
     System.out.println("Hello");
  }
}
 
 
The PenFormHandler extends GenericFormHandler, this gives it some very
useful logging and error handling capabilities. 
 

package imagescript.atg.demo;
/**
 *
 * @copyright Copyright (c) 2002
 * @author Stuart Jones
 * @version 1.1
 */
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

import atg.droplet.GenericFormHandler;
import atg.droplet.DropletFormException;

import atg.servlet.DynamoHttpServletRequest;
import atg.servlet.DynamoHttpServletResponse;

public class PenFormHandler extends GenericFormHandler
{

  public PenFormHandler()
  {
  }

  String mColor;
  String mTipType;
  Pen mPen;
  boolean mUseErrorCodes = false;
  String mSubmitSuccessURL;
  String mSubmitErrorURL;

  public Pen getPen(){ return mPen; }
  public void setPen(Pen p){ mPen = p; }

  public  String getColor(){ 
                  return ((mColor == null) 
                     && (mPen != null))? mPen.getColor() : mColor; }
  public  String getTipType(){
                  return ((mTipType == null) 
                     && (mPen != null))? mPen.getTipType() : mTipType; }

  public  void setColor(String str){ mColor = str; }
  public  void setTipType(String str){ mTipType = str; }

  public boolean isUseErrorCodes()  {    return mUseErrorCodes;  }
  public void setUseErrorCodes(boolean b) { mUseErrorCodes = b;  }

  public void setSubmitSuccessURL(String pSubmitSuccessURL){    
                       mSubmitSuccessURL=pSubmitSuccessURL;}
  public String getSubmitSuccessURL(){  return mSubmitSuccessURL;}

  public void setSubmitErrorURL(String pSubmitErrorURL){   
                        mSubmitErrorURL=pSubmitErrorURL;}
  public String getSubmitErrorURL(){   return mSubmitErrorURL;  }


  boolean isNothing(String s)
  {
    boolean bResult = ((s == null) || (s.length()==0));
    return bResult;
  }
  void doFormError(String property,
                   String errorCode,
                   String message)
  {
      if(isUseErrorCodes())
      {
        DropletFormException ex 
                 = new DropletFormException(message,property,errorCode);

        logDebug("Adding exception errorCode '" + ex.getErrorCode()
                 + "' for property '" + ex.getPropertyName() + "'"
                 + "' for property path '" + ex.getPropertyPath() + "'"
                 + "(" + ex.getMessage() + ")");
        addFormException(ex);
      }
      else
      {
        logDebug("Adding exception message '" 
                      + message + "' for property '" + property + "'");
        addFormException(new DropletFormException(message,property));
      }
  }

  public void handleFormException(DropletFormException ex, 
                      DynamoHttpServletRequest dynamohttpservletrequest, 
                      DynamoHttpServletResponse dynamohttpservletresponse)
  {
      logDebug("handleFormException : '" + ex.getErrorCode()
               + "' for property '" + ex.getPropertyName() + "'"
               + "' for property path '" + ex.getPropertyPath() + "'"
               + "(" + ex.getMessage() + ")");
      addFormException(ex);
  }

  public boolean handleSubmit(DynamoHttpServletRequest req,
                              DynamoHttpServletResponse res)
      throws ServletException, IOException
  {
    boolean bResult = true;

    logDebug("handleSubmit");

    if(mPen == null)
    {
      ServletException e 
              = new ServletException("No Pen Specified for PenFormHandler");
      logError(e);
      throw e;
    }

    if(isNothing(mColor))
    {
      // Use the generic error
      doFormError(getAbsoluteName() + 
                    ".color","missingRequiredValue","You must specify a color");
    }
    if(isNothing(mTipType))
    {
      // Use a custom error
      doFormError(getAbsoluteName() + 
                     ".tipType","missingTipType","You must specify a tip type");
    }

    // This is an abstract example but it illustrates that
    // some things can only be checked in the submit handler
    if((mTipType!=null)
        && (mTipType.indexOf("fine") >= 0)
        && (mColor!=null)
        && (mColor.indexOf("green") >= 0)
        )
    {
      doFormError(getAbsoluteName() + ".tipType", 
                         "noThinGreen", 
                         "Green Pens don't come with a fine tip");
    }


     if (!getFormError())
     {
        logDebug("setting values :" + mColor + ", " + mTipType);
        mPen.setColor(mColor);
        mPen.setTipType(mTipType);

       // If a successURL was specified then redirect to it
       //
       if (mSubmitSuccessURL != null)
       {
          logDebug("Redirecting to: " + mSubmitSuccessURL);
          res.sendLocalRedirect(mSubmitSuccessURL, req);
          bResult = false;
       }

     }
     // If a errorURL was specified then redirect to it
     else if (mSubmitErrorURL != null)
     {
        logDebug("Redirecting to: " + mSubmitErrorURL);
        res.sendLocalRedirect(mSubmitErrorURL, req);
        bResult = false;
     }

     return bResult;
  }


}

No comments:

Popular Posts