View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.portals.bridges.frameworks;
17  
18  import java.util.StringTokenizer;
19  
20  import javax.portlet.PortletMode;
21  import javax.portlet.PortletModeException;
22  import javax.portlet.PortletURL;
23  import javax.portlet.RenderRequest;
24  import javax.portlet.RenderResponse;
25  import javax.portlet.WindowState;
26  import javax.portlet.WindowStateException;
27  
28  import org.apache.portals.bridges.frameworks.model.PortletApplicationModel;
29  
30  
31  /***
32   * Forwarder
33   * 
34   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
35   * @version $Id: Forwarder.java 187899 2004-11-05 04:56:53 +0100 (Fri, 05 Nov 2004) taylor $
36   */
37  public class Forwarder
38  {
39      PortletApplicationModel model;
40      RenderRequest  request;
41      RenderResponse response;    
42      
43      public Forwarder(PortletApplicationModel model,
44                       RenderRequest  request,
45                       RenderResponse response)
46      {
47          this.model = model;
48          this.request = request;        
49          this.response = response;
50      }
51      
52      public String toString()
53      {
54          return response.createRenderURL().toString();        
55      }
56      
57      private Forwarder()
58      {        
59      }
60      
61      /***
62       * Get a link from a view name plus optional comma separated mode, window state 
63       * Supports syntax from forwards
64       * Examples of viewName parameter:
65       *   "myview" 
66       *   "myview"
67       *   "myview,state:maximized"
68       *   "myview,state:normal"
69       *   "myview,mode:view,state:maximized"
70       *   "myview,mode:edit,state:normal"
71       * 
72       * @param actionForward
73       * @return
74       */    
75      public PortletURL getView(String viewName)
76      {
77          PortletURL url = response.createRenderURL();        
78          buildLink(viewName, url);
79          return url;
80      }
81      
82      /***
83       * Get a link from a action forward logical name
84       * in the form of view:action where action can be
85       * "success" or "failure"
86       *  
87       * @param actionForward
88       * @return
89       */
90      public PortletURL getLink(String actionForward)
91      {
92          String forwardName = model.getForward(actionForward);
93          PortletURL url = response.createRenderURL();
94          buildLink(forwardName, url);
95          return url;
96      }
97  
98      /***
99       * Get a link from a action forward logical name
100      * for the given action
101      * 
102      * @param actionForward
103      * @return
104      */    
105     public PortletURL getLink(String forward, String action)
106     {
107         String actionForward = model.getForward(forward, action);
108         PortletURL url = response.createRenderURL();
109         buildLink(actionForward, url);
110         return url;
111     }
112     
113     // TODO: signatures of getLink with 'dynamic' parameters i.e. pass in a map of runtime binding parameters
114     
115     private void buildLink(String actionForward, PortletURL url)
116     {
117         if (actionForward == null)
118         {
119             return; // no parameters
120         }
121         
122         PortletMode mode = null;
123         StringTokenizer tokenizer = new StringTokenizer(actionForward, ForwardConstants.DELIMITER);
124         while (tokenizer.hasMoreTokens())
125         {
126             String token = tokenizer.nextToken();
127             if (token.startsWith(ForwardConstants.MODE_PREFIX))
128             {
129                 mode = setPortletMode(token.substring(ForwardConstants.MODE_PREFIX.length()), url);
130             }
131             else if (token.startsWith(ForwardConstants.STATE_PREFIX))
132             {
133                 setWindowState(token.substring(ForwardConstants.STATE_PREFIX.length()), url);                
134             }
135             else
136             {
137                 if (mode == null)
138                 {
139                     mode = request.getPortletMode();
140                 }
141                 if (mode.equals(PortletMode.VIEW))
142                 {
143                     url.setParameter(FrameworkConstants.VIEW_VIEW_MODE, token);
144                 }
145                 else if (mode.equals(PortletMode.EDIT))
146                 {
147                     url.setParameter(FrameworkConstants.VIEW_EDIT_MODE, token);                    
148                 }
149                 else if (mode.equals(PortletMode.HELP))
150                 {
151                     url.setParameter(FrameworkConstants.VIEW_HELP_MODE, token);                    
152                 }
153             }
154         }                                        
155     }
156     
157     private void setWindowState(String forward, PortletURL url)
158     {
159         try
160         {
161             if (forward.equals(ForwardConstants.MAXIMIZED))
162             {
163                 url.setWindowState(WindowState.MAXIMIZED);
164             }
165             else if (forward.equals(ForwardConstants.MINIMIZED))
166             {
167                 url.setWindowState(WindowState.MINIMIZED);
168             }
169             else if (forward.equals(ForwardConstants.NORMAL))
170             {
171                 url.setWindowState(WindowState.NORMAL);
172             }
173         }
174         catch (WindowStateException e)
175         {
176         }
177     }
178     
179     private PortletMode setPortletMode(String forward, PortletURL url)
180     {
181         PortletMode mode = null;
182         try
183         {
184             if (forward.equals(ForwardConstants.VIEW))
185             {
186                 url.setPortletMode(PortletMode.VIEW);
187                 mode = PortletMode.VIEW;
188             }
189             else if (forward.equals(ForwardConstants.EDIT))
190             {
191                 url.setPortletMode(PortletMode.EDIT);
192                 mode = PortletMode.EDIT;                
193             }
194             else if (forward.equals(ForwardConstants.HELP))
195             {
196                 url.setPortletMode(PortletMode.HELP);
197                 mode = PortletMode.HELP;                
198             }            
199         }
200         catch (PortletModeException e)
201         {
202         }
203         return mode;
204     }
205     
206 }