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  
             else
 225  
             {
 226  0
                 success = false;
 227  0
                 resultMap.put(REASON, "Unsupported Site Update method: " + method);                
 228  0
                 return success;                
 229  
             }
 230  0
             if (count > 0)
 231  
             {
 232  0
                 pageManager.updatePage(page);                
 233  
             }                        
 234  0
             resultMap.put("count", Integer.toString(count));
 235  0
             resultMap.put(STATUS, status);
 236  
         } 
 237  0
         catch (Exception e)
 238  
         {
 239  0
             log.error("exception administering Site update", e);
 240  0
             resultMap.put(REASON, e.toString());
 241  0
             success = false;
 242  0
         }
 243  0
         return success;
 244  
     }
 245  
     
 246  
     protected int updateFragment(RequestContext requestContext, Map resultMap, Page page, String fragmentId, String layout)
 247  
     throws PortletEntityNotStoredException, FailedToRetrievePortletWindow
 248  
     {
 249  0
         int count = 0;
 250  0
         String sizes = getActionParameter(requestContext, SIZES);
 251  0
         Fragment fragment = page.getFragmentById(fragmentId);
 252  0
         if (fragment != null)
 253  
         {                
 254  0
             if (!layout.equals(fragment.getName()))
 255  
             {
 256  0
                 fragment.setName(layout);
 257  0
                 ContentFragment contentFragment = new ContentFragmentImpl(fragment, class="keyword">new HashMap());                    
 258  0
                 PortletWindow window = windowAccess.getPortletWindow(contentFragment);
 259  0
                 if (window != null)
 260  
                 {
 261  0
                     entityAccess.updatePortletEntity(window.getPortletEntity(), contentFragment);
 262  0
                     entityAccess.storePortletEntity(window.getPortletEntity());
 263  0
                     windowAccess.createPortletWindow(window.getPortletEntity(), contentFragment.getId());
 264  0
                     count++;
 265  0
                     if ( isBlank(sizes) )
 266  
                     {
 267  0
                         fragment.setLayoutSizes(null);
 268  
                     }
 269  
                     else
 270  
                     {
 271  0
                         fragment.setLayoutSizes(sizes);
 272  
                     }
 273  0
                     count++;
 274  
                 }
 275  0
             }
 276  
             else
 277  
             {
 278  0
                 if (!isBlank(sizes))
 279  
                 {
 280  0
                     fragment.setLayoutSizes(sizes);
 281  0
                     count++;
 282  
                 }
 283  
             }
 284  
         }
 285  0
         return count;
 286  
     }
 287  
 
 288  
     protected int addFragment(RequestContext requestContext, Map resultMap, Page page, String parentFragmentId, String layout)
 289  
     {
 290  0
         int count = 0;
 291  0
         String sizes = getActionParameter(requestContext, SIZES);
 292  0
         Fragment fragment = page.getFragmentById(parentFragmentId);
 293  0
         if (fragment != null)
 294  
         {
 295  0
             Fragment newFragment = pageManager.newFragment();
 296  0
             newFragment.setType(Fragment.LAYOUT);            
 297  0
             newFragment.setName(layout);
 298  0
             fragment.getFragments().add(newFragment);            
 299  0
             resultMap.put(PORTLETID, newFragment.getId());                        
 300  0
             count++;
 301  0
             if (!isBlank(sizes))
 302  
             {
 303  0
                 newFragment.setLayoutSizes(sizes);
 304  0
                 count++;
 305  
             }
 306  
         }
 307  0
         return count;
 308  
     }
 309  
 
 310  
     protected int removeFragment(RequestContext requestContext, Map resultMap, Page page, String fragmentId)
 311  
     {
 312  0
         int count = 0;
 313  0
         Fragment fragment = page.getFragmentById(fragmentId);
 314  0
         if (fragment != null)
 315  
         {
 316  0
             page.removeFragmentById(fragment.getId());
 317  0
             count++;
 318  
         }
 319  0
         return count;
 320  
     }    
 321  
         
 322  
     protected int updateInformation(RequestContext requestContext, Map resultMap, Node node, String path)
 323  
     throws AJAXException    
 324  
     {
 325  0
         int count = 0;
 326  
         try
 327  
         {
 328  0
             Page page = (Page)node;            
 329  0
             String title = getActionParameter(requestContext, "title");
 330  0
             if (title != null && isFieldModclass="keyword">ified(title, page.getTitle()))
 331  0
                 page.setTitle(title);
 332  0
             String shortTitle = getActionParameter(requestContext, "short-title");
 333  0
             if (shortTitle != null && isFieldModclass="keyword">ified(shortTitle, page.getShortTitle()))
 334  0
                 page.setShortTitle(shortTitle);
 335  0
             String layoutDecorator = getActionParameter(requestContext, "layout-decorator");
 336  0
             if (layoutDecorator != null && isFieldModclass="keyword">ified(layoutDecorator, page.getDefaultDecorator(Fragment.LAYOUT)))
 337  
             {
 338  0
                 if (isBlank(layoutDecorator))
 339  0
                     layoutDecorator = null; 
 340  0
                 page.setDefaultDecorator(layoutDecorator, Fragment.LAYOUT);
 341  
             }
 342  0
             String portletDecorator = getActionParameter(requestContext, "portlet-decorator");
 343  0
             if (portletDecorator != null && isFieldModclass="keyword">ified(portletDecorator, page.getDefaultDecorator(Fragment.PORTLET)))
 344  
             {
 345  0
                 if (isBlank(portletDecorator))
 346  0
                     portletDecorator = null;                 
 347  0
                 page.setDefaultDecorator(portletDecorator, Fragment.PORTLET);
 348  
             }
 349  0
             String theme = getActionParameter(requestContext, "theme");
 350  0
             if (theme != null && isFieldModclass="keyword">ified(theme, page.getSkin()))
 351  
             {
 352  0
                 if (isBlank(theme))
 353  0
                     theme = null;                 
 354  0
                 page.setSkin(theme);
 355  
             }
 356  0
             String hidden = getActionParameter(requestContext, "hidden");
 357  0
             if (hidden != null && isBooleanModclass="keyword">ified(hidden, page.isHidden()))
 358  0
                 page.setHidden(!page.isHidden());                                    
 359  0
             count++;
 360  
         }
 361  0
         catch (Exception e)
 362  
         {
 363  0
             throw new AJAXException(e);
 364  0
         }        
 365  0
         return count;
 366  
     }
 367  
     
 368  
 }

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