Coverage report

  %line %branch
org.apache.jetspeed.layout.impl.ChangePortletAction
0% 
0% 

 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  0
     protected static final Log log = LogFactory.getLog(ChangePortletAction.class);
 58  
     protected String action;
 59  0
     protected Map validWindowStates = new HashMap();
 60  0
     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  0
         this(template, errorTemplate, action, null, windowAccessor, class="keyword">null);
 70  0
     }
 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  0
         super(template, errorTemplate, pageManager, securityBehavior);
 81  0
         this.action = action;
 82  0
         this.windowAccessor = windowAccessor;
 83  
         
 84  
         // Build the maps of allowed (internal) modes and states
 85  0
         Iterator modes = JetspeedActions.getStandardPortletModes().iterator();        
 86  0
         while (modes.hasNext())
 87  
         {
 88  0
             String mode = modes.next().toString();
 89  0
             this.validPortletModes.put(mode, mode);
 90  0
         }
 91  0
         modes = JetspeedActions.getExtendedPortletModes().iterator();
 92  0
         while (modes.hasNext())
 93  
         {
 94  0
             String mode = modes.next().toString();
 95  0
             this.validPortletModes.put(mode, mode);
 96  0
         }
 97  0
         Iterator states = JetspeedActions.getStandardWindowStates().iterator();        
 98  0
         while (states.hasNext())
 99  
         {
 100  0
             String state = states.next().toString();
 101  0
             this.validWindowStates.put(state, state);
 102  0
         }        
 103  0
         states = JetspeedActions.getExtendedWindowStates().iterator();        
 104  0
         while (states.hasNext())
 105  
         {
 106  0
             String state = states.next().toString();
 107  0
             this.validWindowStates.put(state, state);
 108  0
         }        
 109  0
     }
 110  
 
 111  
     public boolean runBatch(RequestContext requestContext, Map resultMap) throws AJAXException
 112  
     {
 113  0
         return runAction(requestContext, resultMap, true);
 114  
     }    
 115  
     
 116  
     public boolean run(RequestContext requestContext, Map resultMap)
 117  
             throws AJAXException
 118  
     {
 119  0
         return runAction(requestContext, resultMap, false);
 120  
     }
 121  
     
 122  
     public boolean runAction(RequestContext requestContext, Map resultMap, class="keyword">boolean batch)
 123  
     {
 124  0
         boolean success = true;
 125  0
         String status = "success";
 126  
         try
 127  
         {
 128  0
             resultMap.put(ACTION, action);
 129  
             // Get the necessary parameters off of the request
 130  0
             String portletId = getActionParameter(requestContext, PORTLETID);
 131  0
             if (portletId == null) 
 132  
             { 
 133  0
                 throw new Exception("portlet id not provided"); 
 134  
             }            
 135  0
             resultMap.put(PORTLETID, portletId);
 136  
             
 137  0
             String windowState = getActionParameter(requestContext, WINDOW_STATE);
 138  0
             String portletMode = getActionParameter(requestContext, PORTLET_MODE);
 139  0
             if (windowState == null && portletMode == class="keyword">null) 
 140  
             { 
 141  0
                 throw new Exception("portlet window state or mode not provided"); 
 142  
             }           
 143  0
             if (windowState != null && !isValidWindowState(windowState))
 144  
             {
 145  0
                 throw new Exception("portlet window state " + windowState + " is not supported");
 146  
             }
 147  0
             if (portletMode != null && !isValidPortletMode(portletMode))
 148  
             {
 149  0
                 throw new Exception("portlet mode " + portletMode + " is not supported");
 150  
             }
 151  
 
 152  0
             ContentPage page = requestContext.getPage();            
 153  0
             ContentFragment fragment = page.getContentFragmentById(portletId);
 154  
             
 155  0
             String oldState = fragment.getState();
 156  0
             String oldMode = fragment.getMode();
 157  
             
 158  
             // Now Change the transient navigational state
 159  0
             MutableNavigationalState navState = (MutableNavigationalState)requestContext.getPortalURL().getNavigationalState();
 160  0
             PortletWindow portletWindow = windowAccessor.getPortletWindow(fragment);
 161  0
             if (portletWindow != null)
 162  
             {
 163  0
                 oldState = navState.getState(portletWindow).toString();
 164  0
                 oldMode =  navState.getMode(portletWindow).toString();
 165  0
                 if (windowState != null)
 166  
                 {
 167  0
                     navState.setState(portletWindow, new WindowState(windowState));
 168  
                 }
 169  0
                 if (portletMode != null)
 170  
                 {
 171  0
                     navState.setMode(portletWindow, new PortletMode(portletMode));
 172  
                 }
 173  0
                 navState.sync(requestContext);                                
 174  
             }
 175  
             
 176  
 
 177  0
             if (checkAccess(requestContext, JetspeedActions.EDIT))
 178  
             {
 179  0
                 if (windowState != null)
 180  0
                     fragment.setState(windowState);
 181  0
                 if (portletMode != null)
 182  0
                     fragment.setMode(portletMode);
 183  
                 
 184  0
                 if (pageManager != null && !batch)
 185  
                 {
 186  0
                     pageManager.updatePage(page);
 187  
                 }
 188  
             }
 189  
             
 190  
             //requestContext.getPortalURL().getNavigationalState().
 191  0
             resultMap.put(STATUS, status);
 192  
             
 193  0
             if (windowState != null)
 194  
             {
 195  0
                 resultMap.put(OLD_WINDOW_STATE, oldState);
 196  0
                 resultMap.put(WINDOW_STATE, windowState);
 197  
             }
 198  
 
 199  0
             if (portletMode != null)
 200  
             {
 201  0
                 resultMap.put(OLD_PORTLET_MODE, oldMode);
 202  0
                 resultMap.put(PORTLET_MODE, portletMode);
 203  
             }
 204  
             
 205  
         } 
 206  0
         catch (Exception e)
 207  
         {
 208  
             // Log the exception
 209  0
             log.error("exception while moving a portlet", e);
 210  0
             resultMap.put(REASON, e.toString());
 211  
             // Return a failure indicator
 212  0
             success = false;
 213  0
         }
 214  
 
 215  0
         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  0
         return this.validWindowStates.containsKey(windowState);
 230  
     }
 231  
     protected boolean isValidPortletMode(String portletMode)
 232  
     {
 233  0
         return this.validPortletModes.containsKey(portletMode);
 234  
     }
 235  
     
 236  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.