View Javadoc

1   /*
2    * Copyright 2000-2001,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   
17  package org.apache.jetspeed.modules.actions.portlets;
18  
19  import java.util.List;
20  import java.util.Iterator;
21  import java.util.Map;
22  import java.util.HashMap;
23  
24  import org.apache.jetspeed.portal.portlets.VelocityPortlet;
25  
26  // Turbine stuff
27  import org.apache.turbine.util.RunData;
28  
29  // Velocity Stuff
30  import org.apache.velocity.context.Context;
31  
32  import org.apache.jetspeed.services.forward.ForwardService;
33  import org.apache.jetspeed.services.forward.configuration.Forward;
34  import org.apache.jetspeed.services.forward.configuration.PortletForward;
35  import org.apache.jetspeed.util.ServiceUtil;
36  import org.apache.jetspeed.util.PortletConfigState;
37  import org.apache.jetspeed.util.PortletSessionState;
38  import org.apache.jetspeed.util.HtmlItem;
39  
40  /***
41   * Demo of Forward stuff
42   * 
43   * 
44   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
45   * @version $Id: ForwardDemoAction.java,v 1.6 2004/02/23 02:56:58 jford Exp $ 
46   */
47  public class ForwardDemoAction extends VelocityPortletAction
48  {
49      private static final String PARAM_NEXT = "fda_next";
50      private static final String PARAM_TARGET = "fda_target";
51  
52      private static final String VAR_FORWARDS = "fda_forwards";
53      private static final String VAR_TARGETS = "fda_targets";
54  
55      private static final String PORTLET_NAME = "ForwardDemo"; // this is fu'd up
56  
57      /*** 
58       * Subclasses must override this method to provide default behavior 
59       * for the portlet action
60       */
61      protected void buildNormalContext( VelocityPortlet portlet, 
62                                         Context context,
63                                         RunData rundata )
64      {
65          String next = (String)PortletSessionState.getAttribute(rundata, PARAM_NEXT);
66          if (null == next)
67          {
68              next = (String)PortletConfigState.getParameter(portlet, rundata, PARAM_NEXT, "NOT_SET");
69  
70              PortletSessionState.setAttribute(rundata, PARAM_NEXT, next);
71          }
72          String target = (String)PortletSessionState.getAttribute(rundata, PARAM_TARGET);
73          if (null == target)
74          {
75              target = (String)PortletConfigState.getParameter(portlet, rundata, PARAM_TARGET, "NOT_SET");
76  
77              PortletSessionState.setAttribute(rundata, PARAM_TARGET, target);
78          }
79  
80          List forwards = (List)PortletSessionState.getAttribute(rundata, VAR_FORWARDS);
81          if (null == forwards)
82          {
83              forwards = getAllForwards(next);
84              PortletSessionState.setAttribute(rundata, VAR_FORWARDS, forwards);
85          }
86          
87          List portletForwards = (List)PortletSessionState.getAttribute(rundata, VAR_TARGETS);
88          if (null == portletForwards)
89          {
90              portletForwards = getPortletForwards(target);
91              PortletSessionState.setAttribute(rundata, VAR_TARGETS, portletForwards);
92          }
93  
94          context.put(VAR_FORWARDS, forwards);
95          context.put(PARAM_NEXT, next);
96          context.put(VAR_TARGETS, portletForwards);
97          context.put(PARAM_TARGET, target);
98  
99      }
100 
101     public void doUpdate(RunData rundata, Context context)
102     {
103         // get posted new target
104         String next = (String)rundata.getParameters().getString(PARAM_NEXT);
105         
106         if (next!=null)
107         {
108             PortletSessionState.setAttribute( rundata, PARAM_NEXT, next);
109 
110             List forwards = (List)PortletSessionState.getAttribute(rundata, VAR_FORWARDS);
111             if (forwards != null)
112             {
113                 Iterator it = forwards.iterator();
114 
115                 while (it.hasNext())
116                 {
117                     HtmlItem item = (HtmlItem)it.next();
118 
119                     if (item.getName().equals(next))
120                     {
121                         item.setSelected(true);
122                     }
123                     else
124                     {
125                         item.setSelected(false);
126                     }
127                 }
128             }
129 
130             ForwardService forward = (ForwardService)ServiceUtil.getServiceByName(ForwardService.SERVICE_NAME);
131             forward.forward(rundata, next);
132         }
133     }
134 
135     private List getAllForwards(String next)
136     {
137         ForwardService fs = (ForwardService)ServiceUtil.getServiceByName(ForwardService.SERVICE_NAME);
138         List vList = new java.util.LinkedList();
139         Iterator it = fs.getForwards().iterator();
140         int index = 1;
141         while (it.hasNext())
142         {
143             Forward forward = (Forward)it.next();
144             boolean selected = forward.getName().equals(next);
145             vList.add(new HtmlItem(index, forward.getName(), selected));
146             index++;
147         }
148 
149         return vList;
150     }
151 
152 
153     public void doTarget(RunData rundata, Context context)
154     {
155         // get posted new target
156         String target = (String)rundata.getParameters().getString(PARAM_TARGET);
157         if (target!=null)
158         {
159             PortletSessionState.setAttribute( rundata, PARAM_TARGET, target);
160 
161             List forwards = (List)PortletSessionState.getAttribute(rundata, VAR_TARGETS);
162             if (forwards != null)
163             {
164                 Iterator it = forwards.iterator();
165 
166                 while (it.hasNext())
167                 {
168                     HtmlItem item = (HtmlItem)it.next();
169 
170                     if (item.getName().equals(target))
171                     {
172                         item.setSelected(true);
173                     }
174                     else
175                     {
176                         item.setSelected(false);
177                     }
178                 }
179             }
180 
181             ForwardService fs = (ForwardService)ServiceUtil.getServiceByName(ForwardService.SERVICE_NAME);
182             fs.forward(rundata, PORTLET_NAME, target);
183         }
184     }
185 
186     private List getPortletForwards(String target)
187     {
188         ForwardService fs = (ForwardService)ServiceUtil.getServiceByName(ForwardService.SERVICE_NAME);
189         List vList = new java.util.LinkedList();
190         Iterator it = fs.getPortletForwards().iterator();
191         int index = 1;
192         while (it.hasNext())
193         {
194             PortletForward forward = (PortletForward)it.next();
195             boolean selected = forward.getTarget().equals(target);
196             vList.add(new HtmlItem(index, forward.getTarget(), selected));
197             index++;
198         }
199 
200         return vList;
201     }
202 
203     public void doDynamic(RunData rundata, Context context)
204     {
205         Map map = new HashMap();
206         map.put("dynamic", "33");
207         ForwardService fs = (ForwardService)ServiceUtil.getServiceByName(ForwardService.SERVICE_NAME);
208         fs.forwardDynamic(rundata, "ApacheGroupNews", map);
209     }
210 
211     public void doDynamic2(RunData rundata, Context context)
212     {
213         Map map = new HashMap();
214         map.put("dynamic", "44");
215         map.put("msgok", "no");
216         map.put("msg", "3");
217 
218         ForwardService fs = (ForwardService)ServiceUtil.getServiceByName(ForwardService.SERVICE_NAME);
219         fs.forwardDynamic(rundata, PORTLET_NAME, "Success", map);
220     }
221 
222 }
223