Coverage report

  %line %branch
org.apache.jetspeed.layout.impl.UpdatePageAction
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.Map;
 21  
 
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 import org.apache.jetspeed.JetspeedActions;
 25  
 import org.apache.jetspeed.ajax.AJAXException;
 26  
 import org.apache.jetspeed.ajax.AjaxAction;
 27  
 import org.apache.jetspeed.ajax.AjaxBuilder;
 28  
 import org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent;
 29  
 import org.apache.jetspeed.components.portletentity.PortletEntityNotStoredException;
 30  
 import org.apache.jetspeed.container.window.FailedToRetrievePortletWindow;
 31  
 import org.apache.jetspeed.container.window.PortletWindowAccessor;
 32  
 import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
 33  
 import org.apache.jetspeed.om.folder.Folder;
 34  
 import org.apache.jetspeed.om.page.ContentFragment;
 35  
 import org.apache.jetspeed.om.page.ContentFragmentImpl;
 36  
 import org.apache.jetspeed.om.page.Fragment;
 37  
 import org.apache.jetspeed.om.page.Page;
 38  
 import org.apache.jetspeed.page.PageManager;
 39  
 import org.apache.jetspeed.page.document.Node;
 40  
 import org.apache.jetspeed.request.RequestContext;
 41  
 import org.apache.pluto.om.window.PortletWindow;
 42  
 
 43  
 /**
 44  
  * Update Page action -- updates various parts of the PSML page
 45  
  * 
 46  
  * AJAX Parameters: 
 47  
  *    action = updatepage
 48  
  *    General methods:
 49  
  *    method = add | remove 
 50  
  *    Info methods:
 51  
  *    | info 
 52  
  *    Meta methods:
 53  
  *    | add-meta | update-meta | remove-meta
 54  
  *    Security methods:
 55  
  *    | add-secref | remove-secref
 56  
  *    Fragment methods:
 57  
  *    | update-fragment | add-fragment | remove-fragment
 58  
  *    
 59  
  *    update-fragment params: id, layout(name), sizes, layoutid (add)
 60  
  *    
 61  
  * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
 62  
  * @version $Id: $
 63  
  */
 64  
 public class UpdatePageAction 
 65  
     extends BaseSiteUpdateAction 
 66  
     implements AjaxAction, AjaxBuilder, Constants
 67  
 {
 68  0
     protected Log log = LogFactory.getLog(UpdatePageAction.class);
 69  
     protected PortletWindowAccessor windowAccess;
 70  
     protected PortletEntityAccessComponent entityAccess;
 71  
     
 72  
     public UpdatePageAction(String template, 
 73  
                             String errorTemplate, 
 74  
                             PageManager pm,
 75  
                             PortletWindowAccessor windowAccess,
 76  
                             PortletEntityAccessComponent entityAccess,
 77  
                             PortletActionSecurityBehavior securityBehavior)                            
 78  
                             
 79  
     {
 80  0
         super(template, errorTemplate, pm, securityBehavior);
 81  0
         this.windowAccess = windowAccess;
 82  0
         this.entityAccess = entityAccess;
 83  0
     }
 84  
     
 85  
     public boolean run(RequestContext requestContext, Map resultMap)
 86  
             throws AJAXException
 87  
     {
 88  0
         boolean success = true;
 89  0
         String status = "success";
 90  
         try
 91  
         {
 92  0
             resultMap.put(ACTION, "updatepage");
 93  
             // Get the necessary parameters off of the request
 94  0
             String method = getActionParameter(requestContext, "method");
 95  0
             if (method == null) 
 96  
             { 
 97  0
                 throw new RuntimeException("Method not provided"); 
 98  
             }            
 99  0
             resultMap.put("method", method);
 100  0
             if (false == checkAccess(requestContext, JetspeedActions.EDIT))
 101  
             {
 102  0
                 success = false;
 103  0
                 resultMap.put(REASON, "Insufficient access to administer portal permissions");                
 104  0
                 return success;
 105  
             }           
 106  0
             int count = 0;
 107  0
             Page page = null;            
 108  0
             String path = getActionParameter(requestContext, "path");
 109  0
             if (path == null)
 110  
             {
 111  0
                 page = requestContext.getPage();
 112  
             }
 113  
             else
 114  
             {
 115  0
                 if (!method.equals("add"))
 116  
                 {
 117  0
                     page = pageManager.getPage(path);
 118  
                 }
 119  
                 else
 120  
                 {
 121  0
                     if (pageManager.pageExists(path))
 122  
                     {
 123  0
                         success = false;
 124  0
                         resultMap.put(REASON, "Can't create: Page already exists: " + path);                
 125  0
                         return success;                
 126  
                     }
 127  
                 }
 128  
             }
 129  0
             if (method.equals("info"))
 130  
             {
 131  0
                 count = updateInformation(requestContext, resultMap, page, path);
 132  
             }
 133  0
             else if (method.equals("add-meta"))
 134  
             {
 135  0
                 count = insertMetadata(requestContext, resultMap, page);
 136  
             }
 137  0
             else if (method.equals("update-meta"))
 138  
             {
 139  0
                 count = updateMetadata(requestContext, resultMap, page);
 140  
             }
 141  0
             else if (method.equals("remove-meta"))
 142  
             {
 143  0
                 count = removeMetadata(requestContext, resultMap, page);
 144  
             }
 145  0
             else if (method.equals("add-secref"))
 146  
             {
 147  0
                 count = insertSecurityReference(requestContext, resultMap, page);
 148  
             }
 149  0
             else if (method.equals("update-secref"))
 150  
             {
 151  0
                 count = updateSecurityReference(requestContext, resultMap, page);
 152  
             }                        
 153  0
             else if (method.equals("remove-secref"))
 154  
             {
 155  0
                 count = removeSecurityReference(requestContext, resultMap, page);
 156  
             }
 157  0
             else if (method.equals("remove-secdef"))
 158  
             {
 159  0
                 count = removeSecurityDef(requestContext, resultMap, page);
 160  
             }            
 161  0
             else if (method.equals("add"))
 162  
             {
 163  0
                 page = pageManager.newPage(path);
 164  0
                 page.setTitle(getActionParameter(requestContext, TITLE));
 165  0
                 String s = getActionParameter(requestContext, SHORT_TITLE );
 166  0
                 if (!isBlank(s))
 167  0
                     page.setShortTitle(s);                
 168  0
                 page.getRootFragment().setName(getActionParameter(requestContext, DEFAULT_LAYOUT));
 169  0
                 count++;                
 170  0
             }
 171  0
             else if (method.equals("copy"))
 172  
             {            	
 173  0
             	String destination = getActionParameter(requestContext, "destination");
 174  0
             	String name = getActionParameter(requestContext, RESOURCE_NAME);
 175  0
             	destination = destination + Folder.PATH_SEPARATOR + name;           	
 176  0
             	Page newPage = pageManager.copyPage(page,destination);
 177  0
             	pageManager.updatePage(newPage);
 178  0
             }
 179  0
             else if (method.equals("move"))
 180  
             {            	
 181  0
             	String destination = getActionParameter(requestContext, "destination");
 182  0
             	String name = getActionParameter(requestContext, RESOURCE_NAME);            	
 183  0
             	destination = destination + Folder.PATH_SEPARATOR + name;
 184  0
             	Page newPage = pageManager.copyPage(page, destination);
 185  0
             	pageManager.updatePage(newPage);
 186  0
             	pageManager.removePage(page);
 187  0
             } 
 188  0
             else if (method.equals("remove"))
 189  
             {
 190  0
                 pageManager.removePage(page);
 191  
             }
 192  0
             else if (method.equals("update-fragment"))
 193  
             {
 194  0
                 String fragmentId = getActionParameter(requestContext, PORTLETID);
 195  0
                 String layout = getActionParameter(requestContext, LAYOUT);                
 196  0
                 if (isBlank(fragmentId) || isBlank(layout))
 197  
                 {
 198  0
                     resultMap.put(REASON, "Missing parameter to update fragment");                
 199  0
                     return false;                    
 200  
                 }                
 201  0
                 count = updateFragment(requestContext, resultMap, page, fragmentId, layout);
 202  0
             }
 203  0
             else if (method.equals("add-fragment"))
 204  
             {
 205  0
                 String parentId = getActionParameter(requestContext, LAYOUTID);
 206  0
                 String layout = getActionParameter(requestContext, LAYOUT);                
 207  0
                 if (isBlank(parentId) || isBlank(layout))
 208  
                 {
 209  0
                     resultMap.put(REASON, "Missing parameter to add fragment");                
 210  0
                     return false;                    
 211  
                 }                
 212  0
                 count = addFragment(requestContext, resultMap, page, parentId, layout);
 213  0
             }
 214  0
             else if (method.equals("remove-fragment"))
 215  
             {
 216  0
                 String fragmentId = getActionParameter(requestContext, PORTLETID);
 217  0
                 if (isBlank(fragmentId))
 218  
                 {
 219  0
                     resultMap.put(REASON, "Missing parameter to remove fragment");                
 220  0
                     return false;                    
 221  
                 }                
 222  0
                 count = removeFragment(requestContext, resultMap, page, fragmentId);                
 223  0
             }
 224  0
             else if (method.equals("update-portlet-decorator"))
 225  
             {
 226  0
                 String fragmentId = getActionParameter(requestContext, PORTLETID);
 227  0
             	String portletDecorator = getActionParameter(requestContext, "portlet-decorator");
 228  0
                 if (isBlank(fragmentId) || isBlank(portletDecorator))
 229  
                 {
 230  0
                     resultMap.put(REASON, "Missing parameter to update portlet decorator");                
 231  0
                     return false;                    
 232  
                 }                
 233  0
                 count = updatePortletDecorator(requestContext, resultMap, page, fragmentId, portletDecorator);
 234  0
             }
 235  
             else
 236  
             {
 237  0
                 success = false;
 238  0
                 resultMap.put(REASON, "Unsupported Site Update method: " + method);                
 239  0
                 return success;                
 240  
             }
 241  0
             if (count > 0)
 242  
             {
 243  0
                 pageManager.updatePage(page);                
 244  
             }                        
 245  0
             resultMap.put("count", Integer.toString(count));
 246  0
             resultMap.put(STATUS, status);
 247  
         } 
 248  0
         catch (Exception e)
 249  
         {
 250  0
             log.error("exception administering Site update", e);
 251  0
             resultMap.put(REASON, e.toString());
 252  0
             success = false;
 253  0
         }
 254  0
         return success;
 255  
     }
 256  
     
 257  
     protected int updatePortletDecorator(RequestContext requestContext, Map resultMap, Page page, String fragmentId, String portletDecorator)
 258  
     throws PortletEntityNotStoredException, FailedToRetrievePortletWindow
 259  
     {
 260  0
     	int count = 0;
 261  0
     	Fragment fragment = page.getFragmentById(fragmentId);
 262  0
         if (fragment != null)
 263  
         {                
 264  0
         	fragment.setDecorator( portletDecorator );
 265  0
         	count++;
 266  
         }
 267  0
     	return count;
 268  
     }
 269  
     
 270  
     protected int updateFragment(RequestContext requestContext, Map resultMap, Page page, String fragmentId, String layout)
 271  
     throws PortletEntityNotStoredException, FailedToRetrievePortletWindow
 272  
     {
 273  0
         int count = 0;
 274  0
         String sizes = getActionParameter(requestContext, SIZES);
 275  0
         Fragment fragment = page.getFragmentById(fragmentId);
 276  0
         if (fragment != null)
 277  
         {                
 278  0
             if (!layout.equals(fragment.getName()))
 279  
             {
 280  0
                 fragment.setName(layout);
 281  0
                 ContentFragment contentFragment = new ContentFragmentImpl(fragment, class="keyword">new HashMap());                    
 282  0
                 PortletWindow window = windowAccess.getPortletWindow(contentFragment);
 283  0
                 if (window != null)
 284  
                 {
 285  0
                     entityAccess.updatePortletEntity(window.getPortletEntity(), contentFragment);
 286  0
                     entityAccess.storePortletEntity(window.getPortletEntity());
 287  0
                     windowAccess.createPortletWindow(window.getPortletEntity(), contentFragment.getId());
 288  0
                     count++;
 289  0
                     if ( isBlank(sizes) )
 290  
                     {
 291  0
                         fragment.setLayoutSizes(null);
 292  
                     }
 293  
                     else
 294  
                     {
 295  0
                         fragment.setLayoutSizes(sizes);
 296  
                     }
 297  0
                     count++;
 298  
                 }
 299  0
             }
 300  
             else
 301  
             {
 302  0
                 if (!isBlank(sizes))
 303  
                 {
 304  0
                     fragment.setLayoutSizes(sizes);
 305  0
                     count++;
 306  
                 }
 307  
             }
 308  
         }
 309  0
         return count;
 310  
     }
 311  
 
 312  
     protected int addFragment(RequestContext requestContext, Map resultMap, Page page, String parentFragmentId, String layout)
 313  
     {
 314  0
         int count = 0;
 315  0
         String sizes = getActionParameter(requestContext, SIZES);
 316  0
         Fragment fragment = page.getFragmentById(parentFragmentId);
 317  0
         if (fragment != null)
 318  
         {
 319  0
             Fragment newFragment = pageManager.newFragment();
 320  0
             newFragment.setType(Fragment.LAYOUT);            
 321  0
             newFragment.setName(layout);
 322  0
             fragment.getFragments().add(newFragment);            
 323  0
             resultMap.put(PORTLETID, newFragment.getId());                        
 324  0
             count++;
 325  0
             if (!isBlank(sizes))
 326  
             {
 327  0
                 newFragment.setLayoutSizes(sizes);
 328  0
                 count++;
 329  
             }
 330  
         }
 331  0
         return count;
 332  
     }
 333  
 
 334  
     protected int removeFragment(RequestContext requestContext, Map resultMap, Page page, String fragmentId)
 335  
     {
 336  0
         int count = 0;
 337  0
         Fragment fragment = page.getFragmentById(fragmentId);
 338  0
         if (fragment != null)
 339  
         {
 340  0
             page.removeFragmentById(fragment.getId());
 341  0
             count++;
 342  
         }
 343  0
         return count;
 344  
     }    
 345  
         
 346  
     protected int updateInformation(RequestContext requestContext, Map resultMap, Node node, String path)
 347  
     throws AJAXException    
 348  
     {
 349  0
         int count = 0;
 350  
         try
 351  
         {
 352  0
             Page page = (Page)node;            
 353  0
             String title = getActionParameter(requestContext, "title");
 354  0
             if (title != null && isFieldModclass="keyword">ified(title, page.getTitle()))
 355  0
                 page.setTitle(title);
 356  0
             String shortTitle = getActionParameter(requestContext, "short-title");
 357  0
             if (shortTitle != null && isFieldModclass="keyword">ified(shortTitle, page.getShortTitle()))
 358  0
                 page.setShortTitle(shortTitle);
 359  0
             String layoutDecorator = getActionParameter(requestContext, "layout-decorator");
 360  0
             if (layoutDecorator != null && isFieldModclass="keyword">ified(layoutDecorator, page.getDefaultDecorator(Fragment.LAYOUT)))
 361  
             {
 362  0
                 if (isBlank(layoutDecorator))
 363  0
                     layoutDecorator = null; 
 364  0
                 page.setDefaultDecorator(layoutDecorator, Fragment.LAYOUT);
 365  
             }
 366  0
             String portletDecorator = getActionParameter(requestContext, "portlet-decorator");
 367  0
             if (portletDecorator != null && isFieldModclass="keyword">ified(portletDecorator, page.getDefaultDecorator(Fragment.PORTLET)))
 368  
             {
 369  0
                 if (isBlank(portletDecorator))
 370  0
                     portletDecorator = null;                 
 371  0
                 page.setDefaultDecorator(portletDecorator, Fragment.PORTLET);
 372  
             }
 373  0
             String theme = getActionParameter(requestContext, "theme");
 374  0
             if (theme != null && isFieldModclass="keyword">ified(theme, page.getSkin()))
 375  
             {
 376  0
                 if (isBlank(theme))
 377  0
                     theme = null;                 
 378  0
                 page.setSkin(theme);
 379  
             }
 380  0
             String hidden = getActionParameter(requestContext, "hidden");
 381  0
             if (hidden != null && isBooleanModclass="keyword">ified(hidden, page.isHidden()))
 382  0
                 page.setHidden(!page.isHidden());                                    
 383  0
             count++;
 384  
         }
 385  0
         catch (Exception e)
 386  
         {
 387  0
             throw new AJAXException(e);
 388  0
         }        
 389  0
         return count;
 390  
     }
 391  
     
 392  
 }

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