You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by ce...@apache.org on 2001/10/08 15:40:14 UTC

cvs commit: jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/dynPortal SetPortalPrefsAction.java RetrievePortalAction.java PortalPrefsForm.java

cedric      01/10/08 06:40:14

  Added:       contrib/tiles/src/tutorial/org/apache/struts/example/tiles/dynPortal
                        SetPortalPrefsAction.java RetrievePortalAction.java
                        PortalPrefsForm.java
  Log:
  start dynamic portal example
  
  Revision  Changes    Path
  1.1                  jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/dynPortal/SetPortalPrefsAction.java
  
  Index: SetPortalPrefsAction.java
  ===================================================================
  /*
   */
  
  
  package org.apache.struts.example.tiles.dynPortal;
  
  import org.apache.struts.tiles.*;
  
  import java.io.IOException;
  import java.util.Locale;
  import java.util.Vector;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.struts.action.Action;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  import org.apache.struts.action.ActionServlet;
  import org.apache.struts.util.MessageResources;
  
  import org.apache.struts.util.RequestUtils;
  
  
  /**
   * Implementation of <strong>Action</strong> that populates an instance of
   * <code>SubscriptionForm</code> from the currently specified subscription.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2001/10/08 13:40:14 $
   */
  
  public final class SetPortalPrefsAction extends Action {
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Process the specified HTTP request, and create the corresponding HTTP
       * response (or forward to another web component that will create it).
       * Return an <code>ActionForward</code> instance describing where and how
       * control should be forwarded, or <code>null</code> if the response has
       * already been completed.
       *
       * @param servlet The ActionServlet making this request
       * @param mapping The ActionMapping used to select this instance
       * @param actionForm The optional ActionForm bean for this request (if any)
       * @param request The HTTP request we are processing
       * @param response The HTTP response we are creating
       *
       * @exception IOException if an input/output error occurs
       * @exception ServletException if a servlet exception occurs
       */
      public ActionForward perform(
                                   ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response)
                            throws IOException, ServletException
    {
      System.out.println("Enter action SetPortalPrefsAction");
  	HttpSession session = request.getSession();
    PortalPrefsForm prefsForm = (PortalPrefsForm)form;
  
            // Try to retrieve tile context
      ComponentContext context = ComponentContext.getContext( request );
      if( context == null )
        {
        throw new ServletException( "This action must be called by a Tile, not directly" );
        }
  
        // Get user portal list from user context
      PortalSettings settings = RetrievePortalAction.getSettings( context, session );
  
    if( prefsForm.isSubmitted() )
      {  // read arrays
      System.out.println("form submitted");
  
        // Set settings cols according to user choice
      for( int i=0;i<prefsForm.getNumCol(); i++)
        {
        settings.resetListAt( i, prefsForm.getNewCol(i));
        } // end loop
  
  
      //settings.resetListAt( 0, prefsForm.getL0());
      //settings.resetListAt( 1, prefsForm.getL1());
      prefsForm.reset();
  	  return (mapping.findForward("portal"));
      }
  
        // Set lists values to be shown
      for( int i=0;i<settings.getNumCols(); i++ )
        {
        prefsForm.addCol(settings.getListAt(i) );
        prefsForm.addColLabels(settings.getListLabelAt(i) );
        } // end loop
  
      prefsForm.setChoices(settings.getChoices() );
      prefsForm.setChoiceLabels(settings.getChoiceLabels() );
  
      System.out.println("Exit action SetPortalPrefsAction");
  	  return (mapping.findForward("preferences"));
      }
  
  
  }
  
  
  
  1.1                  jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/dynPortal/RetrievePortalAction.java
  
  Index: RetrievePortalAction.java
  ===================================================================
  /*
   */
  
  
  package org.apache.struts.example.tiles.dynPortal;
  
  import org.apache.struts.tiles.*;
  
  import java.io.IOException;
  import java.util.Locale;
  //import java.util.Vector;
  import java.util.List;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.struts.action.Action;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  import org.apache.struts.action.ActionServlet;
  import org.apache.struts.util.MessageResources;
  
  import org.apache.struts.util.RequestUtils;
  
  
  /**
   * Implementation of <strong>Action</strong> that populates an instance of
   * <code>SubscriptionForm</code> from the currently specified subscription.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2001/10/08 13:40:14 $
   */
  
  public final class RetrievePortalAction extends Action {
  
        /** Name use to store settings in session context */
      public static String USER_PORTAL_SETTINGS = "DynamicPortal.USER_PORTAL_SETTINGS";
        /** Tile parameter name */
      public static String PARAM_NUMCOLS = "numCols";
        /** Tile parameter name */
      public static String PARAM_LIST = "list";
        /** Tile parameter name */
      public static String PARAM_LIST_LABELS = "listLabels";
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Process the specified HTTP request, and create the corresponding HTTP
       * response (or forward to another web component that will create it).
       * Return an <code>ActionForward</code> instance describing where and how
       * control should be forwarded, or <code>null</code> if the response has
       * already been completed.
       *
       * @param servlet The ActionServlet making this request
       * @param mapping The ActionMapping used to select this instance
       * @param actionForm The optional ActionForm bean for this request (if any)
       * @param request The HTTP request we are processing
       * @param response The HTTP response we are creating
       *
       * @exception IOException if an input/output error occurs
       * @exception ServletException if a servlet exception occurs
       */
      public ActionForward perform(
                                   ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response)
                            throws IOException, ServletException
      {
      System.out.println("Enter action RetrievePortalAction");
        // Get current session.
  	  HttpSession session = request.getSession();
  
            // Try to retrieve tile context
      ComponentContext context = ComponentContext.getContext( request );
      if( context == null )
        {
        throw new ServletException( "This action must be called by a Tile, not directly" );
        }
  
        // Get user portal list from user context
      PortalSettings settings = getSettings( context, session );
  
        // Set parameters for tiles
      context.putAttribute( "numCols", Integer.toString(settings.getNumCols()) );
      for( int i=0; i<settings.getNumCols(); i++ )
        context.putAttribute( "list"+i, settings.getListAt(i) );
  
      System.out.println("Exit action RetrievePortalAction");
  	  return (mapping.findForward("success"));
      }
  
      /**
       * Retrieve user setting from session.
       * If settings are not found, initialized them.
       */
    public static PortalSettings getSettings( ComponentContext context, HttpSession session )
    {
        // Get user portal list from user context
      PortalSettings settings = (PortalSettings)session.getAttribute( USER_PORTAL_SETTINGS );
  
      if( settings == null )
        { // List doesn't exist, create it and initialize it from Tiles parameters
        settings = new PortalSettings();
        settings.setNumCols( (String)context.getAttribute( PARAM_NUMCOLS ) );
        for( int i=0; i<settings.getNumCols(); i++ )
          {
          List col = (List)context.getAttribute( ((String)PARAM_LIST+i) );
          List labels = (List)context.getAttribute( ((String)PARAM_LIST_LABELS+i) );
          settings.addChoices( col, labels );
          settings.addList( col );
          } // end loop
  
          // Save user settings in session
        session.setAttribute( USER_PORTAL_SETTINGS, settings);
        } // end if
  
    return settings;
    }
  
  
  }
  
  
  
  
  1.1                  jakarta-struts/contrib/tiles/src/tutorial/org/apache/struts/example/tiles/dynPortal/PortalPrefsForm.java
  
  Index: PortalPrefsForm.java
  ===================================================================
  /*
   */
  
  
  package org.apache.struts.example.tiles.dynPortal;
  
  
  import java.util.List;
  import java.util.ArrayList;
  import org.apache.struts.action.ActionForm;
  
  
  /**
   */
  
  public final class PortalPrefsForm extends ActionForm  {
  
      /** Validate value */
    protected String validate;
      /** empty list used by reset */
    protected String[] empty = {};
      /** list of "remaining" choices */
    protected String[] remaining = empty;
      /** list of user columns */
    protected List columns = new ArrayList();
      /** list of user columns labels */
    protected List columnLabels = new ArrayList();
      /** list of columns selected by user */
    protected List newCols = new ArrayList();
  
      /** Choice list */
    protected List choice;
      /** Choice list labels */
    protected List choiceLabels;
      /** Is initialized ? */
    protected boolean initialized = false;
  
  
      /**
       * Set col
       */
    public void setCol( int index, List col )
      {
      columns.set( index, col);
      }
  
      /**
       * Add col
       */
    public void addCol( List col )
      {
      columns.add( col);
      }
  
      /**
       * Get col Labels
       */
    public List getColLabels(int index)
      {
      return (List)columnLabels.get(index);
      }
  
      /**
       * Set col Labels
       */
    public void setColLabels( int index, List col )
      {
      columnLabels.set( index, col);
      }
  
      /**
       * Add col Labels
       */
    public void addColLabels( List col )
      {
      columnLabels.add( col);
      }
  
      /**
       * Get col
       */
    public List getCol(int index)
      {
      return (List)columns.get(index);
      }
  
      /**
       * Set col Labels
       */
    public void setNewCol( int index, String list[] )
      {
        // ensure capacity
      while( index>=newCols.size())newCols.add(null);
      newCols.set( index, list);
      }
  
      /**
       * Get col
       */
    public String[] getNewCol(int index)
      {
      if(newCols==null || index>=newCols.size())
        return null;
      return (String[])newCols.get(index);
      }
  
      /**
       * Get number of columns
       */
    public int getNumCol()
      {
      return newCols.size();
      }
  
      /**
       * Set list1
       */
    public void setL1( String array[] )
      {
      setNewCol(1, array);
      }
      /**
       * Set list1
       */
    public String[] getL1()
      {
      return getNewCol(1);
      }
  
      /**
       * Set list1
       */
    public void setL0( String array[] )
      {
      setNewCol(0, array);
      }
      /**
       * Set list1
       */
    public String[] getL0()
      {
      return getNewCol(0);
      }
      /**
       * Set list1
       */
    public void setRemaining( String array[] )
      {
      remaining = array;
      }
      /**
       * Set list1
       */
    public String[] getRemaining()
      {
      return remaining;
      }
  
  
      /**
       * Set list1
       */
    public void setChoices( List list )
      {
      choice = list;
      }
      /**
       * Set list1
       */
    public void setChoiceLabels( List list )
      {
      choiceLabels = list;
      }
      /**
       * Set list1
       */
    public List getChoices()
      {
      return choice;
      }
      /**
       * Set list1
       */
    public List getChoiceLabels()
      {
      return choiceLabels;
      }
  
     /**
      * Is this form submitted ?
      */
    public boolean isSubmitted()
      {
      return validate != null;
      }
  
     /**
      * Is this form submitted ?
      */
    public void setValidate( String value)
      {
      this.validate = value;
      }
  
      /**
       * Reset properties
       */
    public void reset()
      {
      remaining =  empty;
      validate = null;
      columns.clear();
      columnLabels.clear();
      newCols.clear();
      }
      /**
       * Initialized flag
       */
    public boolean isInitialized()
      {
      return initialized;
      }
      /**
       * Initialized flag
       */
    public void setInitialized( boolean isInitialized)
      {
      initialized = isInitialized;
      }
  }