Coverage report

  %line %branch
org.apache.jetspeed.layout.impl.RemovePortletAction
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.Map;
 20  
 
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 import org.apache.jetspeed.JetspeedActions;
 24  
 import org.apache.jetspeed.ajax.AJAXException;
 25  
 import org.apache.jetspeed.ajax.AjaxAction;
 26  
 import org.apache.jetspeed.ajax.AjaxBuilder;
 27  
 import org.apache.jetspeed.components.portletregistry.PortletRegistry;
 28  
 import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
 29  
 import org.apache.jetspeed.layout.PortletPlacementContext;
 30  
 import org.apache.jetspeed.om.page.Fragment;
 31  
 import org.apache.jetspeed.om.page.Page;
 32  
 import org.apache.jetspeed.page.PageManager;
 33  
 import org.apache.jetspeed.pipeline.PipelineException;
 34  
 import org.apache.jetspeed.request.RequestContext;
 35  
 
 36  
 
 37  
 /**
 38  
  * Remove Portlet portlet placement action
 39  
  * 
 40  
  * AJAX Parameters: 
 41  
  *    id = the fragment id of the portlet to remove
 42  
  *    page = (implied in the URL)
 43  
  *    
 44  
  * @author <a>David Gurney </a>
 45  
  * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
 46  
  * @version $Id: $
 47  
  */
 48  
 public class RemovePortletAction 
 49  
     extends BasePortletAction 
 50  
     implements AjaxAction, AjaxBuilder, Constants
 51  
 {
 52  0
     protected static final Log log = LogFactory.getLog(RemovePortletAction.class);
 53  
     
 54  
     private PortletRegistry registry;
 55  
 
 56  
     public RemovePortletAction( String template, String errorTemplate, PortletRegistry registry )
 57  
         throws PipelineException
 58  
     {
 59  0
         this( template, errorTemplate, registry, null, class="keyword">null );
 60  0
     }
 61  
 
 62  
     public RemovePortletAction( String template, 
 63  
                                 String errorTemplate,
 64  
                                 PortletRegistry registry,
 65  
                                 PageManager pageManager, 
 66  
                                 PortletActionSecurityBehavior securityBehavior )
 67  
         throws PipelineException
 68  
     {
 69  0
         super( template, errorTemplate, pageManager, securityBehavior );
 70  0
         this.registry = registry;
 71  0
     }
 72  
     
 73  
     public boolean runBatch(RequestContext requestContext, Map resultMap) throws AJAXException
 74  
     {
 75  0
         return runAction(requestContext, resultMap, true);
 76  
     }    
 77  
     
 78  
     public boolean run(RequestContext requestContext, Map resultMap)
 79  
             throws AJAXException
 80  
     {
 81  0
         return runAction(requestContext, resultMap, false);
 82  
     }
 83  
     
 84  
     public boolean runAction(RequestContext requestContext, Map resultMap, class="keyword">boolean batch)
 85  
     {
 86  0
         boolean success = true;
 87  0
         String status = "success";
 88  
         try
 89  
         {
 90  0
             resultMap.put( ACTION, "remove" );
 91  
             // Get the necessary parameters off of the request
 92  0
             String portletId = getActionParameter( requestContext, PORTLETID );
 93  0
             if (portletId == null) 
 94  
             { 
 95  0
                 success = false;
 96  0
                 resultMap.put( REASON, "Portlet ID not provided" );
 97  0
                 return success;
 98  
             }
 99  0
             resultMap.put( PORTLETID, portletId );
 100  0
             if ( false == checkAccess( requestContext, JetspeedActions.EDIT ) )
 101  
             {
 102  0
                 Page page = requestContext.getPage();
 103  0
                 Fragment fragment = page.getFragmentById( portletId );
 104  0
                 if ( fragment == null )
 105  
                 {
 106  0
                     success = false;
 107  0
                     resultMap.put( REASON, "Fragment not found" );
 108  0
                     return success;                    
 109  
                 }
 110  
                 
 111  0
                 NestedFragmentContext removeFragmentContext = null;
 112  
                 try
 113  
                 {
 114  0
                 	removeFragmentContext = new NestedFragmentContext( fragment, page, registry );
 115  
                 }
 116  0
                 catch ( Exception ex )
 117  
                 {
 118  0
                 	log.error( "Failure to construct nested context for fragment " + portletId, ex );
 119  0
                 	success = false;
 120  0
                     resultMap.put( REASON, "Cannot construct nested context for fragment" );
 121  0
                     return success;
 122  0
                 }
 123  
                 
 124  0
                 if ( ! createNewPageOnEdit( requestContext ) )
 125  
                 {                
 126  0
                     success = false;
 127  0
                     resultMap.put( REASON, "Insufficient access to edit page" );
 128  0
                     return success;
 129  
                 }
 130  0
                 status = "refresh";
 131  
                 
 132  0
                 Page newPage = requestContext.getPage();
 133  
 
 134  
                 // using NestedFragmentContext, find portlet id for copy of target portlet in the new page 
 135  0
                 Fragment newFragment = null;
 136  
                 try
 137  
                 {
 138  0
                 	newFragment = removeFragmentContext.getFragmentOnNewPage( newPage, registry );
 139  
                 }
 140  0
                 catch ( Exception ex )
 141  
                 {
 142  0
                 	log.error( "Failure to locate copy of fragment " + portletId, ex );
 143  0
                 	success = false;
 144  0
                     resultMap.put( REASON, "Failed to find new fragment for portlet id: " + portletId );
 145  0
                     return success;
 146  0
                 }
 147  0
                 portletId = newFragment.getId();
 148  
             }
 149  
             
 150  
             // Use the Portlet Placement Manager to accomplish the removal
 151  0
             Page page = requestContext.getPage();
 152  0
             Fragment root = page.getRootFragment();
 153  0
             Fragment layoutContainerFragment = getParentFragmentById( portletId, root );
 154  0
             PortletPlacementContext placement = null;
 155  0
             Fragment fragment = null;
 156  0
             if ( layoutContainerFragment != null )
 157  
             {
 158  0
             	placement = new PortletPlacementContextImpl( page, registry, layoutContainerFragment );
 159  0
             	fragment = placement.getFragmentById( portletId );
 160  
             }
 161  0
             if ( fragment == null )
 162  
             {
 163  0
                 success = false;
 164  0
                 resultMap.put( REASON, "Fragment not found" );
 165  0
                 return success;                
 166  
             }
 167  0
             placement.remove(fragment);
 168  0
             page = placement.syncPageFragments();
 169  0
             page.removeFragmentById( fragment.getId() );
 170  0
             if (!batch)
 171  
             {
 172  0
                 if (pageManager != null)
 173  0
                     pageManager.updatePage( page );
 174  
             }
 175  
             // Build the results for the response
 176  0
             resultMap.put( PORTLETID, portletId );            
 177  0
             resultMap.put( STATUS, status );
 178  0
             resultMap.put( OLDCOL, String.valueOf( fragment.getLayoutColumn() ) );
 179  0
             resultMap.put( OLDROW, String.valueOf( fragment.getLayoutRow() ) );
 180  
         } 
 181  0
         catch ( Exception e )
 182  
         {
 183  
             // Log the exception
 184  0
             log.error("exception while adding a portlet", e);
 185  0
             resultMap.put(REASON, e.toString());
 186  
             // Return a failure indicator
 187  0
             success = false;
 188  0
         }
 189  
 
 190  0
         return success;
 191  
     }
 192  
     
 193  
     protected PortletRegistry getPortletRegistry()
 194  
     {
 195  0
     	return this.registry;
 196  
     }
 197  
 }

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