View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.layout.impl;
18  
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import javax.portlet.PortletMode;
24  import javax.portlet.WindowState;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.jetspeed.JetspeedActions;
29  import org.apache.jetspeed.ajax.AJAXException;
30  import org.apache.jetspeed.ajax.AjaxAction;
31  import org.apache.jetspeed.ajax.AjaxBuilder;
32  import org.apache.jetspeed.container.state.MutableNavigationalState;
33  import org.apache.jetspeed.container.window.PortletWindowAccessor;
34  import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
35  import org.apache.jetspeed.om.page.ContentFragment;
36  import org.apache.jetspeed.om.page.ContentPage;
37  import org.apache.jetspeed.page.PageManager;
38  import org.apache.jetspeed.request.RequestContext;
39  import org.apache.pluto.om.window.PortletWindow;
40  
41  /***
42   * Changes the window state or portlet mode for a given portlet window
43   *
44   * AJAX Parameters: 
45   *    id = the fragment id of the portlet to move
46   *    page = (implied in the URL)
47   *    state = the new window state
48   *    mode = the new portlet mode
49   *    
50   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
51   * @version $Id: $
52   */
53  public class ChangePortletAction 
54      extends BasePortletAction 
55      implements AjaxAction, AjaxBuilder, Constants
56  {
57      protected static final Log log = LogFactory.getLog(ChangePortletAction.class);
58      protected String action;
59      protected Map validWindowStates = new HashMap();
60      protected Map validPortletModes = new HashMap();
61      protected PortletWindowAccessor windowAccessor;
62      
63      public ChangePortletAction(String template, 
64              String errorTemplate, 
65              String action,
66              PortletWindowAccessor windowAccessor)            
67      throws AJAXException    
68      {
69          this(template, errorTemplate, action, null, windowAccessor, null);
70      }
71      
72      public ChangePortletAction(String template, 
73                               String errorTemplate, 
74                               String action,
75                               PageManager pageManager,
76                               PortletWindowAccessor windowAccessor,                             
77                               PortletActionSecurityBehavior securityBehavior)
78      throws AJAXException
79      {
80          super(template, errorTemplate, pageManager, securityBehavior);
81          this.action = action;
82          this.windowAccessor = windowAccessor;
83          
84          // Build the maps of allowed (internal) modes and states
85          Iterator modes = JetspeedActions.getStandardPortletModes().iterator();        
86          while (modes.hasNext())
87          {
88              String mode = modes.next().toString();
89              this.validPortletModes.put(mode, mode);
90          }
91          modes = JetspeedActions.getExtendedPortletModes().iterator();
92          while (modes.hasNext())
93          {
94              String mode = modes.next().toString();
95              this.validPortletModes.put(mode, mode);
96          }
97          Iterator states = JetspeedActions.getStandardWindowStates().iterator();        
98          while (states.hasNext())
99          {
100             String state = states.next().toString();
101             this.validWindowStates.put(state, state);
102         }        
103         states = JetspeedActions.getExtendedWindowStates().iterator();        
104         while (states.hasNext())
105         {
106             String state = states.next().toString();
107             this.validWindowStates.put(state, state);
108         }        
109     }
110 
111     public boolean runBatch(RequestContext requestContext, Map resultMap) throws AJAXException
112     {
113         return runAction(requestContext, resultMap, true);
114     }    
115     
116     public boolean run(RequestContext requestContext, Map resultMap)
117             throws AJAXException
118     {
119         return runAction(requestContext, resultMap, false);
120     }
121     
122     public boolean runAction(RequestContext requestContext, Map resultMap, boolean batch)
123     {
124         boolean success = true;
125         String status = "success";
126         try
127         {
128             resultMap.put(ACTION, action);
129             // Get the necessary parameters off of the request
130             String portletId = getActionParameter(requestContext, PORTLETID);
131             if (portletId == null) 
132             { 
133                 throw new Exception("portlet id not provided"); 
134             }            
135             resultMap.put(PORTLETID, portletId);
136             
137             String windowState = getActionParameter(requestContext, WINDOW_STATE);
138             String portletMode = getActionParameter(requestContext, PORTLET_MODE);
139             if (windowState == null && portletMode == null) 
140             { 
141                 throw new Exception("portlet window state or mode not provided"); 
142             }           
143             if (windowState != null && !isValidWindowState(windowState))
144             {
145                 throw new Exception("portlet window state " + windowState + " is not supported");
146             }
147             if (portletMode != null && !isValidPortletMode(portletMode))
148             {
149                 throw new Exception("portlet mode " + portletMode + " is not supported");
150             }
151 
152             ContentPage page = requestContext.getPage();            
153             ContentFragment fragment = page.getContentFragmentById(portletId);
154             
155             String oldState = fragment.getState();
156             String oldMode = fragment.getMode();
157             
158             // Now Change the transient navigational state
159             MutableNavigationalState navState = (MutableNavigationalState)requestContext.getPortalURL().getNavigationalState();
160             PortletWindow portletWindow = windowAccessor.getPortletWindow(fragment);
161             if (portletWindow != null)
162             {
163                 oldState = navState.getState(portletWindow).toString();
164                 oldMode =  navState.getMode(portletWindow).toString();
165                 if (windowState != null)
166                 {
167                     navState.setState(portletWindow, new WindowState(windowState));
168                 }
169                 if (portletMode != null)
170                 {
171                     navState.setMode(portletWindow, new PortletMode(portletMode));
172                 }
173                 navState.sync(requestContext);                                
174             }
175             
176 
177             if (checkAccess(requestContext, JetspeedActions.EDIT))
178             {
179                 if (windowState != null)
180                     fragment.setState(windowState);
181                 if (portletMode != null)
182                     fragment.setMode(portletMode);
183                 
184                 if (pageManager != null && !batch)
185                 {
186                     pageManager.updatePage(page);
187                 }
188             }
189             
190             //requestContext.getPortalURL().getNavigationalState().
191             resultMap.put(STATUS, status);
192             
193             if (windowState != null)
194             {
195                 resultMap.put(OLD_WINDOW_STATE, oldState);
196                 resultMap.put(WINDOW_STATE, windowState);
197             }
198 
199             if (portletMode != null)
200             {
201                 resultMap.put(OLD_PORTLET_MODE, oldMode);
202                 resultMap.put(PORTLET_MODE, portletMode);
203             }
204             
205         } 
206         catch (Exception e)
207         {
208             // Log the exception
209             log.error("exception while moving a portlet", e);
210             resultMap.put(REASON, e.toString());
211             // Return a failure indicator
212             success = false;
213         }
214 
215         return success;
216     }
217     
218     // TODO: The validWindowStates and validPortletModes maps only contain 
219     //       internal (portal level) valid modes and states.
220     //       *if* a pa defines a custom mode/state with a different name but
221     //       mapped onto a internal (portal) mode/state 
222     //       *then* first the real internal mode/state needs to be retrieved from the 
223     //       targetted portlet its application:
224     //       o.a.j.om.common.portlet.PortletApplication.getMappedMode(customMode) and
225     //       o.a.j.om.common.portlet.PortletApplication.getMappedState(customState)        
226     
227     protected boolean isValidWindowState(String windowState)
228     {
229         return this.validWindowStates.containsKey(windowState);
230     }
231     protected boolean isValidPortletMode(String portletMode)
232     {
233         return this.validPortletModes.containsKey(portletMode);
234     }
235     
236 }