XMLForm's HowTo Step 4
http://xml.apache.org/http://www.apache.org/http://www.w3.org/

Main

How-Tos
Index

XMLForm Wizard
Introduction
Step 1
Step 2
Step 3
Step 4
Step 5

Step 4: Writing the Action

HowtoWizardAction.java is based on the WizardAction.java which is part of the XMLForm feedback wizard demo distributed with Cocoon. You need to adjust the prepare, perform and reset methods. You also need to define the pages which comprise the navigation states.

XML Pages defined
        
// different form views 
// participating in the wizard
final String VIEW_START = "start";
final String VIEW_REGISTRATION = "registration";
final String VIEW_INTEREST = "interest";
final String VIEW_GARDENING = "organicGardening";
final String VIEW_COOKING = "cooking";
final String VIEW_SMALLHOLDING = "smallholdingManagement";
final String VIEW_CONFIRM = "confirm";
final String VIEW_END = "end";
        
        
Prepare Method

This method prepares the form and the pages to be returned before actual form population starts.

The first time the URL http://localhost:8080/cocoon/mount/xmlform/howto-wizard.html is requested, no command is passed. Thus, the "if statement" test is met, and the start page is returned.

After the start page has been viewed, and the user clicks on "Start", the command start is passed. Thus, the "else test" is met, and the registration page is returned. Any old forms are removed, and a form listener is added to the form.

If neither of these tests are met, then nothing is returned.

        

package org.apache.cocoon.samples.xmlform.howto;

// Java classes
import java.util.Map;

// Cocoon classes
import org.apache.cocoon.acting.*;

// Cocoon Form
import org.apache.cocoon.acting.AbstractXMLFormAction;
import org.apache.cocoon.components.xmlform.Form;
import org.apache.cocoon.components.xmlform.FormListener;


/**
 * This action demonstrates 
 * a relatively complex form handling scenario.
 *
 * @author Heidi Brannan, heidi@wkwyw.net
 */
public class HowtoWizardAction 
  extends AbstractXMLFormAction
  implements FormListener
  
{ 


  // different form views 
  // participating in the wizard
  final String VIEW_START = "start";
  final String VIEW_REGISTRATION = "registration";
  final String VIEW_INTEREST = "interest";
  final String VIEW_GARDENING = "organicGardening";
  final String VIEW_COOKING = "cooking";
  final String VIEW_SMALLHOLDING = "smallholdingManagement";
  final String VIEW_CONFIRM = "confirm";
  final String VIEW_END = "end";

  // action commands used in the wizard
  final String CMD_START = "start";
  final String CMD_NEXT = "next";
  final String CMD_PREV = "prev";
        
  
  /**
   * The first callback method which is called
   * when an action is invoked.
   *
   * It is called before population.
   *
   *
   * @return null if the Action is prepared to continue.
   * an objectModel map which will be immediately returned by the action.
   *
   * This method is a good place to handle buttons with Cancel
   * kind of semantics. For example 
   * <pre>if getCommand().equals("Cancel") return page("input");</pre>
   *
   */
  protected Map prepare() 
  {
    
    if ( getCommand() == null )
      {
        return page( VIEW_START );
      }
    else   if ( getCommand().equals( CMD_START ) )
    {
      // reset state by removing old form
      // if one exists
      Form.remove( getObjectModel(), getFormId() );
      getForm().addFormListener( this );

      return page( VIEW_REGISTRATION );
    }

    
    // get ready for action
    // if not ready return page("whereNext");
    return null;
  }

  
  /**
   * Invoked after form population
   *
   * Semanticly similar to Struts Action.perform()
   *
   * Take appropriate action based on the command
   *
   */
  public Map perform ()
  {

    // get the actual model which this Form encapsulates
    // and apply additional buziness logic to the model
    HowToBean  jBean = (HowToBean) getForm().getModel();
    //jBean.incrementCount();

    // set the page control flow parameter 
    // according to the validation result
    if ( getCommand().equals( CMD_NEXT ) && 
      getForm().getViolations () != null )
    {
      // errors, back to the same page
      return page( getFormView() );
    }
    else 
    {
      // validation passed
      // continue with control flow
       
      // clear validation left overs in case the user 
      // did not press the Next button
      getForm().clearViolations();
      
      // get the user submitted command (through a submit button)
      String command = getCommand();
      // get the form view which was submitted
      String formView = getFormView();

      // apply control flow rules
      if ( formView.equals ( VIEW_REGISTRATION ) )
      {
        if ( command.equals( CMD_NEXT ) )
        {
          return page(  VIEW_INTEREST );
        }        
      }
      else if ( formView.equals ( VIEW_INTEREST ) )
      {
        if ( command.equals( CMD_NEXT ) )
        {
           if ( jBean.getOrganicGardening() == true )
           {
             return page( VIEW_GARDENING );
           }
           else if ( jBean.getCooking() == true )
           {
             return page( VIEW_COOKING );
           }
           else if ( jBean.getSmallholdingManagement() == true )
           {
             return page( VIEW_SMALLHOLDING );
           }
           //else if ( getForm().get
          return page(  VIEW_CONFIRM );
        } 
        if ( command.equals( CMD_PREV ) )
        {
           return page( VIEW_REGISTRATION );
        }
      }
      else if ( formView.equals ( VIEW_GARDENING ) )
      {
        if ( command.equals ( CMD_NEXT ) )
        {
           if ( jBean.getCooking() == true )
           {
             return page( VIEW_COOKING );
           }
           else if ( jBean.getSmallholdingManagement() == true )
           {
             return page( VIEW_SMALLHOLDING );
           }          
          return page( VIEW_CONFIRM );
        }
        else if( command.equals( CMD_PREV ) )
        {
          return page( VIEW_INTEREST );
        }
      }
      else if ( formView.equals ( VIEW_COOKING ) )
      {
        if ( command.equals ( CMD_NEXT ) )
        {
           if ( jBean.getSmallholdingManagement() == true )
           {
             return page( VIEW_SMALLHOLDING );
           }          
          return page( VIEW_CONFIRM );
        }
        else if ( command.equals( CMD_PREV ) )
        {          
          if ( jBean.getOrganicGardening() == true )
          {
            return page( VIEW_GARDENING );
          }
          return page( VIEW_INTEREST );
        }
      }
      else if ( formView.equals ( VIEW_SMALLHOLDING ) )
      {
        if ( command.equals( CMD_NEXT ) )
        {
          return page( VIEW_CONFIRM );
        }
        else if ( command.equals( CMD_PREV ) )
        {
          if ( jBean.getCooking() == true ) 
          {
            return page( VIEW_COOKING );
          }
          else if ( jBean.getOrganicGardening() == true )
          {
            return page( VIEW_GARDENING );
          }
          return page( VIEW_INTEREST );
        }
      }
      else if ( formView.equals ( VIEW_CONFIRM ) )
      {
        if ( command.equals( CMD_NEXT ) )
        {
           return page( VIEW_END );
        }
        else if( command.equals( CMD_PREV ) )
        {
           if ( jBean.getOrganicGardening() == true )
           {
             return page( VIEW_GARDENING );
           }          
          return page( VIEW_INTEREST );
        }
      }
    }

    // should never reach this statement
    return page( VIEW_START );    
    
  }  

  

  
  
  /** 
   *
   * FormListener callback 
   * called in the beginning Form.populate()
   * before population starts.
   *
   * This is NOT the place to handle unchecked checkboxes,
   * if the form is stored in the session.
   * The XMLForm framework will automatically handle unchecked
   * check-boxes for session scope forms.
   *
   * Only request scoped forms need handle check boxes explicitly.
   *
   *
   */
  public void reset( Form form )
  {
    // nothing to do here
    
    // unchecked check boxes are handled automatically
    // since this is a session scoped form
   
    /*
      No need for any of the following:
     
      form.setValue( "/organicGardening", Boolean.FALSE );
      form.setValue( "/cooking", Boolean.FALSE );
      form.setValue( "/smallholdingManagement", Boolean.FALSE );

      form.setValue( "/flowers", Boolean.FALSE );
      form.setValue( "/vegetables", Boolean.FALSE );
      form.setValue( "/fruitTrees", Boolean.FALSE );

      form.setValue( "/traditionalReciepes", Boolean.FALSE );
      form.setValue( "/soups", Boolean.FALSE );
      form.setValue( "/veganCookery", Boolean.FALSE );

      form.setValue( "/pigKeeping", Boolean.FALSE );
      form.setValue( "/pygmyGoats", Boolean.FALSE );
      form.setValue( "/henKeeping", Boolean.FALSE );
    */
  }
  
  
  /** 
   * FormListener callback 
   * 
   * Invoked during Form.populate();
   *
   * It is invoked before a request parameter is mapped to
   * an attribute of the form model.
   *
   * It is appropriate to use this method for filtering 
   * custom request parameters which do not reference
   * the model.
   *
   * Another appropriate use of this method is for graceful filtering of invalid
   * values, in case that knowledge of the system state or 
   * other circumstainces make the standard validation 
   * insufficient. For example if a registering user choses a username which
   * is already taken - the check requires database transaction, which is 
   * beyond the scope of document validating schemas. 
   * Of course customized Validators can be implemented to do 
   * this kind of domain specific validation
   * instead of using this method.
   * 
   *
   * @return false if the request parameter should not be filtered.
   * true otherwise.
   */
  public boolean filterRequestParameter (Form form, String parameterName)
  {
    // Nothing to do in this case
    return false;
  }
 
   
  public  String getFile( String FileName ) {
    try
    {
      final String  FILE_PREFIX = "file:";
      String path = getSourceResolver().resolve(FileName).getSystemId();
      if(path.startsWith(FILE_PREFIX))
         path = path.substring(FILE_PREFIX.length());
      return path;
    }
    catch(Exception e)
    {
       getLogger().error("could not read mapping file",e);
      return null;
    }
  }
    
}

    
    

Finally Step 5: The Sitemap

Copyright © 1999-2002 The Apache Software Foundation. All Rights Reserved.