portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tay...@apache.org
Subject cvs commit: jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/model ForwardConstants.java
Date Thu, 04 Nov 2004 22:17:30 GMT
taylor      2004/11/04 14:17:30

  Modified:    portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks
                        GenericFrameworkPortlet.java
  Added:       portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks
                        ForwardConstants.java Forwarder.java
                        FrameworkConstants.java
  Removed:     portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/model
                        ForwardConstants.java
  Log:
  new features implemented:
  - portlet forwards
  - 1..n views per portlet mode
  
  Revision  Changes    Path
  1.2       +228 -206  jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/GenericFrameworkPortlet.java
  
  Index: GenericFrameworkPortlet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/GenericFrameworkPortlet.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- GenericFrameworkPortlet.java	4 Nov 2004 18:09:33 -0000	1.1
  +++ GenericFrameworkPortlet.java	4 Nov 2004 22:17:30 -0000	1.2
  @@ -39,7 +39,6 @@
   
   import org.apache.commons.beanutils.BeanUtils;
   
  -import org.apache.portals.bridges.frameworks.model.ForwardConstants;
   import org.apache.portals.bridges.frameworks.model.ModelBean;
   import org.apache.portals.bridges.frameworks.model.PortletApplicationModel;
   import org.apache.portals.bridges.frameworks.spring.PortletApplicationModelImpl;
  @@ -48,11 +47,13 @@
   /**
    * SpringVelocityPortlet
    * 
  - * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
  - * @version $Id$
  + * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
  + * @version $Id: GenericFrameworkPortlet.java,v 1.1 2004/11/04 18:09:33 taylor
  + *          Exp $
    */
   public class GenericFrameworkPortlet extends GenericVelocityPortlet
  -{    
  +{
  +
       /**
        * Init Parameter: default spring configuration property
        */
  @@ -62,303 +63,329 @@
        * Init Parameter: default velocity configuration property
        */
       private static final String INITPARAM_VALIDATOR_CONFIG = "validator-configuration";
  +
  +    private static final String PREFS_SUFFIX = ".prefs";
       
       /**
        * Action signature for calling velocity portlet actions
        */
  -    private static final Class[] VELOCITY_PORTLET_ACTION_SIGNATURE = 
  -    {
  -            ActionRequest.class,
  -            ActionResponse.class
  -    };
  -    
  +    private static final Class[] VELOCITY_PORTLET_ACTION_SIGNATURE =
  +    { ActionRequest.class, ActionResponse.class};
  +
       private static PortletApplicationModel model = null;
  -    
  +
       private static Object semaphore = new Object();
  -    
  +
       public GenericFrameworkPortlet()
       {
       }
   
  -    public void init(PortletConfig config)
  -    throws PortletException
  +    public void init(PortletConfig config) throws PortletException
       {
           super.init(config);
   
           String springConfig = this.getInitParameter(INITPARAM_SPRING_CONFIG);
  -        if (springConfig == null)
  -        {
  -            throw new PortletException("Spring Configuration file not specified");    
  -        }
  +        if (springConfig == null) { throw new PortletException(
  +                "Spring Configuration file not specified"); }
   
  -        String validatorConfig = this.getInitParameter(INITPARAM_VALIDATOR_CONFIG);
  +        String validatorConfig = this
  +                .getInitParameter(INITPARAM_VALIDATOR_CONFIG);
   
           synchronized (semaphore)
           {
               if (null == model)
               {
  -                model = new PortletApplicationModelImpl(springConfig, validatorConfig);
  +                model = new PortletApplicationModelImpl(springConfig,
  +                        validatorConfig);
                   model.init(config);
               }
  -        }        
  +        }
       }
  -    
  -    /** 
  -     * Invoke the velocity portlet pipeline:
  -     * (1) determine the logical view
  -     * (2) restore state from Form to Bean
  -     * (3) validate the bean
  -     * -- or --
  -     * (2) restore state from Form to Prefs
  -     * 
  -     * (4) execute the velocity action
  -     * (5) forward to another view 
  +
  +    /**
  +     * Invoke the velocity portlet pipeline: (1) determine the logical view (2)
  +     * restore state from Form to Bean (3) validate the bean -- or -- (2)
  +     * restore state from Form to Prefs
        * 
  +     * (4) execute the velocity action (5) forward to another view
  +     *  
        */
       public void processAction(ActionRequest request, ActionResponse response)
  -    throws PortletException, IOException
  +            throws PortletException, IOException
       {
  -        // (1) Determine the current view        
  +        // (1) Determine the current view
           String view = determineLogicalView(request);
  -        
  +
           Object bean = null;
  -        ModelBean mb = model.getBean(view); 
  -            
  +        ModelBean mb = model.getBean(view);
  +
           if (mb.getBeanType() == ModelBean.PREFS_MAP)
           {
               // (2) restore state from Form to Prefs
               bean = formToPrefs(request, view, mb);
  -        }
  -        else
  +        } else
           {
               // (2) restore state from Form to Bean
               bean = formToBean(request, view, mb);
           }
  -        
  +
           String forward = null;
  -        
  +
           // (3) validate the bean
           if (model.validate(bean, view))
           {
  -        
  +
               // (4) execute the velocity action
  -            String action = request.getParameter("velocity.action");
  +            String action = request.getParameter(FrameworkConstants.BRIDGES_ACTION);
               if (null == action)
               {
                   if (mb.getBeanType() == ModelBean.PREFS_MAP)
                   {
                       // store prefs
  -                    storePreferences(request, (Map)bean);
  +                    storePreferences(request, (Map) bean);
                   }
  -                
  +
                   forward = model.getForward(view, ForwardConstants.SUCCESS);
  -            }
  -            else
  +            } else
               {
                   // call the specified action in the post params
  -                String actionForward = invokeVelocityPortletAction(action, request, response);
  -                forward = model.getForward(actionForward);                
  -            }            
  -        }                
  -        else
  +                String actionForward = invokeVelocityPortletAction(action,
  +                        request, response);
  +                forward = model.getForward(actionForward);
  +            }
  +        } else
           {
               // failed validation
  -            forward = model.getForward(view, ForwardConstants.FAILURE); 
  +            forward = model.getForward(view, ForwardConstants.FAILURE);
           }
   
  -        // (5) forward to another view 
  +        // (5) forward to another view
           forwardToView(request, response, forward);
  -        
  +
       }
  -    
  -    protected void forwardToView(PortletRequest request, ActionResponse response, String
forward)
  +
  +    protected void forwardToView(ActionRequest request,
  +            ActionResponse response, String forward)
       {
  -        if (forward == null)
  -        {
  -            return; // stay on same page
  +        if (forward == null) { return; // stay on same page
           }
  -        
  -        StringTokenizer tokenizer = new StringTokenizer(forward, ",");
  +
  +        String logicalView = null;
  +        PortletMode newMode = null;
  +        StringTokenizer tokenizer = new StringTokenizer(forward, ForwardConstants.DELIMITER);
           while (tokenizer.hasMoreTokens())
           {
               String token = tokenizer.nextToken();
  -            if (token.startsWith("mode:"))
  +            if (token.startsWith(ForwardConstants.MODE_PREFIX))
               {
  -                setPortletMode(response, token.substring("mode:".length()));
  -            }
  -            else if (token.startsWith("state:"))
  +                newMode = setPortletMode(response, token.substring(ForwardConstants.MODE_PREFIX
  +                        .length()));
  +            } else if (token.startsWith(ForwardConstants.STATE_PREFIX))
               {
  -                setWindowState(response, token.substring("state:".length()));         
      
  -            }
  -            else
  +                setWindowState(response, token.substring(ForwardConstants.STATE_PREFIX.length()));
  +            } else
               {
  -                setLogicalView(response, token);
  +                logicalView = token;
               }
  -        }                                        
  -        
  +        }
  +        if (logicalView != null)
  +        {
  +            setLogicalView(request, response, logicalView, newMode);
  +        }
  +
       }
   
       private void setWindowState(ActionResponse response, String forward)
       {
           try
           {
  -            if (forward.equals("maximized"))
  +            if (forward.equals(ForwardConstants.MAXIMIZED))
               {
                   response.setWindowState(WindowState.MAXIMIZED);
  -            }
  -            else if (forward.equals("minimized"))
  +            } else if (forward.equals(ForwardConstants.MINIMIZED))
               {
                   response.setWindowState(WindowState.MINIMIZED);
  -            }
  -            else if (forward.equals("normal"))
  +            } else if (forward.equals(ForwardConstants.NORMAL))
               {
                   response.setWindowState(WindowState.NORMAL);
               }
  -        }
  -        catch (WindowStateException e)
  +        } catch (WindowStateException e)
           {
           }
       }
  -    
  -    private void setPortletMode(ActionResponse response, String forward)
  +
  +    private PortletMode setPortletMode(ActionResponse response, String forward)
       {
  +        PortletMode mode = null;
           try
           {
  -            if (forward.equals("view"))
  +            if (forward.equals(ForwardConstants.VIEW))
               {
                   response.setPortletMode(PortletMode.VIEW);
  -            }
  -            else if (forward.equals("edit"))
  +                mode = PortletMode.VIEW;
  +            } else if (forward.equals(ForwardConstants.EDIT))
               {
                   response.setPortletMode(PortletMode.EDIT);
  -            }
  -            else if (forward.equals("help"))
  +                mode = PortletMode.EDIT;
  +            } else if (forward.equals(ForwardConstants.HELP))
               {
                   response.setPortletMode(PortletMode.HELP);
  -            }            
  -        }
  -        catch (PortletModeException e)
  +                mode = PortletMode.HELP;
  +            }
  +        } catch (PortletModeException e)
           {
           }
  +        return mode;
       }
  -    
  +
       protected void storePreferences(PortletRequest request, Map bean)
  -    throws IOException, PortletException
  +            throws IOException, PortletException
       {
           String key = "none";
  -        
  +
           try
           {
               PortletPreferences prefs = request.getPreferences();
               Iterator it = bean.entrySet().iterator();
               while (it.hasNext())
               {
  -                Map.Entry entry = (Map.Entry)it.next();
  -                key = (String)entry.getKey();
  +                Map.Entry entry = (Map.Entry) it.next();
  +                key = (String) entry.getKey();
                   if (!prefs.isReadOnly(key))
                   {
  -                    prefs.setValue(key, (String)entry.getValue());
  +                    prefs.setValue(key, (String) entry.getValue());
                   }
               }
               prefs.store();
  -        }
  -        catch (ReadOnlyException roe)
  +        } catch (ReadOnlyException roe)
           {
  -            throw new PortletException("Failed to set preference " + key + ", value is
readonly");
  +            throw new PortletException("Failed to set preference " + key
  +                    + ", value is readonly");
           }
  -        
  +
       }
  -        
  +
       /**
  -     * Get the current logical view based on velocity.view request parameter
  -     * If the request parameter is not found, fall back to init param
  -     *  
  +     * Get the current logical view based on velocity.view request parameter If
  +     * the request parameter is not found, fall back to init param
  +     * 
        * @param request
        * @return the current view
        * @throws PortletException
        */
       protected String determineLogicalView(PortletRequest request)
  -    throws PortletException
  +            throws PortletException
       {
  -        // TODO: we could store the current view in the session
  -        String view = request.getParameter("velocity.view");
  -        if (view == null)
  +        String view = null;
  +        if (request.getPortletMode().equals(PortletMode.VIEW))
           {
  -            if (request.getPortletMode().equals(PortletMode.VIEW))
  +            view = request.getParameter(FrameworkConstants.VIEW_VIEW_MODE);
  +            if (view == null)
               {
                   view = this.getDefaultViewPage();
               }
  -            else if (request.getPortletMode().equals(PortletMode.EDIT))
  +        } else if (request.getPortletMode().equals(PortletMode.EDIT))
  +        {
  +            view = request.getParameter(FrameworkConstants.VIEW_EDIT_MODE);
  +            if (view == null)
               {
                   view = this.getDefaultEditPage();
               }
  -            else if (request.getPortletMode().equals(PortletMode.HELP))
  +        } else if (request.getPortletMode().equals(PortletMode.HELP))
  +        {
  +            view = request.getParameter(FrameworkConstants.VIEW_VIEW_MODE);
  +            if (view == null)
               {
                   view = this.getDefaultHelpPage();
               }
  -            if (null == view)
  -            {
  -                throw new PortletException("Portlet error: cant find view resource for
portlet: " + this.getPortletName());
  -            }
           }
  -        return view;        
  +        if (null == view) { throw new PortletException(
  +                "Portlet error: cant find view resource for portlet: "
  +                        + this.getPortletName()); }
  +        return view;
       }
  -        
  -    protected void setLogicalView(ActionResponse response, String view)
  +
  +    protected void setLogicalView(ActionRequest request,
  +            ActionResponse response, String view, PortletMode newMode)
       {
  -        response.setRenderParameter("velocity.view", view);
  +        if (newMode == null)
  +        {
  +            if (request.getPortletMode().equals(PortletMode.VIEW))
  +            {
  +                response.setRenderParameter(FrameworkConstants.VIEW_VIEW_MODE, view);
  +            } else if (request.getPortletMode().equals(PortletMode.EDIT))
  +            {
  +                response.setRenderParameter(FrameworkConstants.VIEW_EDIT_MODE, view);
  +            } else if (request.getPortletMode().equals(PortletMode.HELP))
  +            {
  +                response.setRenderParameter(FrameworkConstants.VIEW_HELP_MODE, view);
  +            }
  +        } else
  +        {
  +            if (newMode.equals(PortletMode.VIEW))
  +            {
  +                response.setRenderParameter(FrameworkConstants.VIEW_VIEW_MODE, view);
  +            } else if (newMode.equals(PortletMode.EDIT))
  +            {
  +                response.setRenderParameter(FrameworkConstants.VIEW_EDIT_MODE, view);
  +            } else if (newMode.equals(PortletMode.HELP))
  +            {
  +                response.setRenderParameter(FrameworkConstants.VIEW_HELP_MODE, view);
  +            }
  +        }
       }
  -    
  +
       protected Object formToBean(ActionRequest request, String view, ModelBean mb)
  -    throws PortletException
  -    {        
  -        
  +            throws PortletException
  +    {
  +
           // try to get the bean from the session first
  -        Object bean = request.getPortletSession().getAttribute(mb.getBeanName());
  +        Object bean = request.getPortletSession()
  +                .getAttribute(mb.getBeanName());
           if (bean == null)
           {
  -            bean = model.createBean(mb);            
  -            if (bean == null)
  -            {
  -                throw new PortletException("Portlet Action error in creating bean for view:
" + view);
  -            }
  +            bean = model.createBean(mb);
  +            if (bean == null) { throw new PortletException(
  +                    "Portlet Action error in creating bean for view: " + view); }
               request.getPortletSession().setAttribute(mb.getBeanName(), bean);
           }
  -        
  +
           Map params = request.getParameterMap();
           try
           {
               BeanUtils.populate(bean, params);
  -        }
  -        catch (Exception e)
  +        } catch (Exception e)
           {
  -            throw new PortletException("Portlet Action error in  populating bean: " + mb.getBeanName(),
e);
  +            throw new PortletException(
  +                    "Portlet Action error in  populating bean: "
  +                            + mb.getBeanName(), e);
           }
           return bean;
       }
  -    
  -    protected Object formToPrefs(ActionRequest request, String view, ModelBean mb)
  -    throws PortletException
  -    {        
  +
  +    protected Object formToPrefs(ActionRequest request, String view,
  +            ModelBean mb) throws PortletException
  +    {
           Map params = request.getParameterMap();
  -        Map bean = (Map)request.getPortletSession().getAttribute(view + ".prefs");
  +        Map bean = (Map) request.getPortletSession().getAttribute(
  +                view + PREFS_SUFFIX);
           if (bean == null)
           {
               PortletPreferences prefs = request.getPreferences();
  -            
  -            bean = model.createPrefsBean(mb, prefs.getMap());            
  -            
  -            request.getPortletSession().setAttribute(view + ".prefs", bean);          
 
  +
  +            bean = model.createPrefsBean(mb, prefs.getMap());
  +
  +            request.getPortletSession().setAttribute(view + PREFS_SUFFIX, bean);
           }
  -        
  +
           try
           {
               Iterator it = params.entrySet().iterator();
               while (it.hasNext())
               {
  -                Map.Entry entry = (Map.Entry)it.next();
  -                Object value = entry.getValue();                
  -                String key = (String)entry.getKey();
  +                Map.Entry entry = (Map.Entry) it.next();
  +                Object value = entry.getValue();
  +                String key = (String) entry.getKey();
                   if (null == bean.get(key))
                   {
                       continue;
  @@ -366,133 +393,128 @@
                   if (value instanceof String)
                   {
                       bean.put(key, value);
  -                }
  -                else if (value instanceof String[])
  +                } else if (value instanceof String[])
                   {
  -                    bean.put(key, ((String[])value)[0]);
  +                    bean.put(key, ((String[]) value)[0]);
                   }
  -            }           
  -        }
  -        catch (Exception e)
  +            }
  +        } catch (Exception e)
           {
  -            throw new PortletException("Portlet Action error in  populating bean: ", e);
  +            throw new PortletException(
  +                    "Portlet Action error in  populating bean: ", e);
           }
           return bean;
       }
  -    
  +
       /**
  -     * Invokes a specific Velocity Portlet Action
  -     * All portlet actions must have the signature:
  +     * Invokes a specific Velocity Portlet Action All portlet actions must have
  +     * the signature:
        * 
        * String methodName(ActionRequest request, ActionResponse response)
        * 
        * @param methodName
        */
  -    protected String invokeVelocityPortletAction(String methodName, ActionRequest request,
ActionResponse response)
  -    throws PortletException
  +    protected String invokeVelocityPortletAction(String methodName,
  +            ActionRequest request, ActionResponse response)
  +            throws PortletException
       {
           try
  -        {                                           
  -            Method method = this.getClass().getMethod(methodName, VELOCITY_PORTLET_ACTION_SIGNATURE);
  -            Object[] parameters = 
  -            {
  -                    request,
  -                    response
  -            };
  -            String result = (String)method.invoke(this, parameters);
  +        {
  +            Method method = this.getClass().getMethod(methodName,
  +                    VELOCITY_PORTLET_ACTION_SIGNATURE);
  +            Object[] parameters =
  +            { request, response};
  +            String result = (String) method.invoke(this, parameters);
               return result;
  -        }
  -        catch (Exception e)
  +        } catch (Exception e)
           {
  -            throw new PortletException("Failed to invoke portlet action: " + methodName,
e);
  +            throw new PortletException("Failed to invoke portlet action: "
  +                    + methodName, e);
           }
       }
   
       public void doView(RenderRequest request, RenderResponse response)
  -    throws PortletException, IOException
  +            throws PortletException, IOException
       {
           doRender(request, response);
       }
   
       public void doHelp(RenderRequest request, RenderResponse response)
  -    throws PortletException, IOException
  +            throws PortletException, IOException
       {
           doRender(request, response);
       }
  -    
  +
       public void doEdit(RenderRequest request, RenderResponse response)
  -    throws PortletException, IOException
  +            throws PortletException, IOException
       {
           doRender(request, response);
       }
  -    
  +
       protected void doRender(RenderRequest request, RenderResponse response)
  -    throws PortletException, IOException
  +            throws PortletException, IOException
       {
           String view = determineLogicalView(request);
  -        if (view == null)
  -        {
  -            throw new PortletException("Logical View not found: " + view);
  -        }
  -        
  +        if (view == null) { throw new PortletException(
  +                "Logical View not found: " + view); }
  +
           String template = model.getTemplate(view);
  -        if (template == null)
  -        {
  -            throw new PortletException("Template not found for Logical View: " + view);
  -        }
  +        if (template == null) { throw new PortletException(
  +                "Template not found for Logical View: " + view); }
   
           ModelBean mb = model.getBean(view);
           if (mb.getBeanType() == ModelBean.PREFS_MAP)
           {
               prefsToContext(request, view, mb);
  -        }
  -        else
  +        } else
           {
               beanToContext(request, view, mb);
           }
  -        
  -        PortletContext context = getPortletContext();        
  +
  +        putRequestVariable(request, FrameworkConstants.FORWARD_TOOL, new Forwarder(model,
request,
  +                response));
  +
  +        PortletContext context = getPortletContext();
           PortletRequestDispatcher rd = context.getRequestDispatcher(template);
           rd.include(request, response);
  -    } 
  -        
  -    
  +    }
  +
       private void beanToContext(RenderRequest request, String view, ModelBean mb)
       {
  -        Object bean = request.getPortletSession().getAttribute(mb.getBeanName());
  +        Object bean = request.getPortletSession()
  +                .getAttribute(mb.getBeanName());
           if (bean == null)
           {
               bean = model.createBean(mb);
  -            if (bean == null)
  -            {
  -                return;
  -            }
  +            if (bean == null) { return; }
               request.getPortletSession().setAttribute(mb.getBeanName(), bean);
           }
  -        putRequestVariable(request, mb.getBeanName(), bean);        
  +        putRequestVariable(request, mb.getBeanName(), bean);
       }
  -    
  +
       private void prefsToContext(RenderRequest request, String view, ModelBean mb)
  -    {                   
  -        Map bean = (Map)request.getPortletSession().getAttribute(view + ".prefs");
  +    {
  +        Map bean = (Map) request.getPortletSession().getAttribute(
  +                view + PREFS_SUFFIX);
           if (bean == null)
           {
  -            PortletPreferences prefs = request.getPreferences();            
  -            bean = model.createPrefsBean(mb, prefs.getMap());            
  -            request.getPortletSession().setAttribute(view + ".prefs", bean);          
 
  +            PortletPreferences prefs = request.getPreferences();
  +            bean = model.createPrefsBean(mb, prefs.getMap());
  +            request.getPortletSession().setAttribute(view + PREFS_SUFFIX, bean);
           }
  -        putRequestVariable(request, "prefs", bean);        
  +        putRequestVariable(request, FrameworkConstants.PREFS_VARIABLE, bean);
       }
  -    
  +
       /**
        * Specific for Velocity
        * 
        * @param name
        * @param value
        */
  -    protected void putRequestVariable(RenderRequest request, String name, Object value)
  +    protected void putRequestVariable(RenderRequest request, String name,
  +            Object value)
       {
           request.setAttribute(name, value);
       }
   
  -}
  +}
  \ No newline at end of file
  
  
  
  1.1                  jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/ForwardConstants.java
  
  Index: ForwardConstants.java
  ===================================================================
  /*
   * Copyright 2000-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.portals.bridges.frameworks;
  
  
  /**
   * ForwardConstants
   * 
   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
   * @version $Id: ForwardConstants.java,v 1.1 2004/11/04 22:17:30 taylor Exp $
   */
  public interface ForwardConstants
  {
      final String SUCCESS = "success";
      final String FAILURE = "failure";
      
      final String MODE_PREFIX = "mode:";
      final String STATE_PREFIX = "state:";
      
      final String MAXIMIZED = "maximized";
      final String MINIMIZED = "minimized";
      final String NORMAL = "normal";
      
      final String VIEW = "view";
      final String EDIT = "edit";
      final String HELP = "help";
      
      final String DELIMITER = ",";
  }
  
  
  
  1.1                  jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/Forwarder.java
  
  Index: Forwarder.java
  ===================================================================
  /*
   * Copyright 2000-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.portals.bridges.frameworks;
  
  import java.util.StringTokenizer;
  
  import javax.portlet.PortletMode;
  import javax.portlet.PortletModeException;
  import javax.portlet.PortletURL;
  import javax.portlet.RenderRequest;
  import javax.portlet.RenderResponse;
  import javax.portlet.WindowState;
  import javax.portlet.WindowStateException;
  
  import org.apache.portals.bridges.frameworks.model.PortletApplicationModel;
  
  
  /**
   * Forwarder
   * 
   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
   * @version $Id: Forwarder.java,v 1.1 2004/11/04 22:17:30 taylor Exp $
   */
  public class Forwarder
  {
      PortletApplicationModel model;
      RenderRequest  request;
      RenderResponse response;    
      
      public Forwarder(PortletApplicationModel model,
                       RenderRequest  request,
                       RenderResponse response)
      {
          this.model = model;
          this.response = response;
      }
      
      private Forwarder()
      {        
      }
      
      /**
       * Get a link from a view name plus optional comma separated mode, window state 
       * Supports syntax from forwards
       * Examples of viewName parameter:
       *   "myview" 
       *   "myview"
       *   "myview,state:maximized"
       *   "myview,state:normal"
       *   "myview,mode:view,state:maximized"
       *   "myview,mode:edit,state:normal"
       * 
       * @param actionForward
       * @return
       */    
      public PortletURL getView(String viewName)
      {
          PortletURL url = response.createRenderURL();        
          buildLink(viewName, url);
          return url;
      }
      
      /**
       * Get a link from a action forward logical name
       * in the form of view:action where action can be
       * "success" or "failure"
       *  
       * @param actionForward
       * @return
       */
      public PortletURL getLink(String actionForward)
      {
          String forwardName = model.getForward(actionForward);
          PortletURL url = response.createRenderURL();
          
          return url;
      }
  
      /**
       * Get a link from a action forward logical name
       * for the given action
       * 
       * @param actionForward
       * @return
       */    
      public PortletURL getLink(String forward, String action)
      {
          String actionForward = model.getForward(forward, action);
          PortletURL url = response.createRenderURL();
          buildLink(actionForward, url);
          return url;
      }
      
      // TODO: signatures of getLink with 'dynamic' parameters i.e. pass in a map of runtime
binding parameters
      
      private void buildLink(String actionForward, PortletURL url)
      {
          if (actionForward == null)
          {
              return; // no parameters
          }
          
          PortletMode mode = null;
          StringTokenizer tokenizer = new StringTokenizer(actionForward, ForwardConstants.DELIMITER);
          while (tokenizer.hasMoreTokens())
          {
              String token = tokenizer.nextToken();
              if (token.startsWith(ForwardConstants.MODE_PREFIX))
              {
                  mode = setPortletMode(token.substring(ForwardConstants.MODE_PREFIX.length()),
url);
              }
              else if (token.startsWith(ForwardConstants.STATE_PREFIX))
              {
                  setWindowState(token.substring(ForwardConstants.STATE_PREFIX.length()),
url);                
              }
              else
              {
                  if (mode == null)
                  {
                      mode = request.getPortletMode();
                  }
                  if (mode.equals(PortletMode.VIEW))
                  {
                      url.setParameter(FrameworkConstants.VIEW_VIEW_MODE, token);
                  }
                  else if (mode.equals(PortletMode.EDIT))
                  {
                      url.setParameter(FrameworkConstants.VIEW_EDIT_MODE, token);        
           
                  }
                  else if (mode.equals(PortletMode.HELP))
                  {
                      url.setParameter(FrameworkConstants.VIEW_HELP_MODE, token);        
           
                  }
              }
          }                                        
      }
      
      private void setWindowState(String forward, PortletURL url)
      {
          try
          {
              if (forward.equals(ForwardConstants.MAXIMIZED))
              {
                  url.setWindowState(WindowState.MAXIMIZED);
              }
              else if (forward.equals(ForwardConstants.MINIMIZED))
              {
                  url.setWindowState(WindowState.MINIMIZED);
              }
              else if (forward.equals(ForwardConstants.NORMAL))
              {
                  url.setWindowState(WindowState.NORMAL);
              }
          }
          catch (WindowStateException e)
          {
          }
      }
      
      private PortletMode setPortletMode(String forward, PortletURL url)
      {
          PortletMode mode = null;
          try
          {
              if (forward.equals(ForwardConstants.VIEW))
              {
                  url.setPortletMode(PortletMode.VIEW);
                  mode = PortletMode.VIEW;
              }
              else if (forward.equals(ForwardConstants.EDIT))
              {
                  url.setPortletMode(PortletMode.EDIT);
                  mode = PortletMode.EDIT;                
              }
              else if (forward.equals(ForwardConstants.HELP))
              {
                  url.setPortletMode(PortletMode.HELP);
                  mode = PortletMode.HELP;                
              }            
          }
          catch (PortletModeException e)
          {
          }
          return mode;
      }
      
  }
  
  
  
  1.1                  jakarta-jetspeed-2/portals-bridges/frameworks/src/java/org/apache/portals/bridges/frameworks/FrameworkConstants.java
  
  Index: FrameworkConstants.java
  ===================================================================
  /*
   * Copyright 2000-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.portals.bridges.frameworks;
  
  
  /**
   * FrameworkConstants
   * 
   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
   * @version $Id: FrameworkConstants.java,v 1.1 2004/11/04 22:17:30 taylor Exp $
   */
  public interface FrameworkConstants
  {
      /**
       * Use this post parameter name for your actions
       */
      final String BRIDGES_ACTION = "velocity.action";
  
      /**
       * The name of the forward tool
       */
      final String FORWARD_TOOL = "forward";
      
      /**
       * The name of the prefs variable
       */
      final String PREFS_VARIABLE = "prefs";
      
      /**
       * Current view for view mode
       */
      final String VIEW_VIEW_MODE = "bridges.view.view";
      
      /**
       * Current view for edit mode
       */
      final String VIEW_EDIT_MODE = "bridges.view.edit";
      
      /**
       * Current view for help mode
       */
      final String VIEW_HELP_MODE = "bridges.view.help";
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org


Mime
View raw message