Coverage report

  %line %branch
org.apache.jetspeed.layout.impl.BaseSiteUpdateAction
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.Collection;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 import java.util.Locale;
 23  
 import java.util.Map;
 24  
 
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 import org.apache.jetspeed.ajax.AJAXException;
 28  
 import org.apache.jetspeed.ajax.AjaxAction;
 29  
 import org.apache.jetspeed.ajax.AjaxBuilder;
 30  
 import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
 31  
 import org.apache.jetspeed.om.common.LocalizedField;
 32  
 import org.apache.jetspeed.om.common.SecurityConstraints;
 33  
 import org.apache.jetspeed.page.PageManager;
 34  
 import org.apache.jetspeed.page.document.Node;
 35  
 import org.apache.jetspeed.request.RequestContext;
 36  
 
 37  
 /**
 38  
  * Abstract Site update action for folders, pages and links
 39  
  *
 40  
  * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
 41  
  * @version $Id: $
 42  
  */
 43  
 public abstract class BaseSiteUpdateAction
 44  
     extends BasePortletAction
 45  
     implements AjaxAction, AjaxBuilder, Constants 
 46  
 {
 47  0
     protected static final Log log = LogFactory.getLog(BaseSiteUpdateAction.class);    
 48  
     
 49  
     public BaseSiteUpdateAction(String template, 
 50  
             String errorTemplate, 
 51  
             PageManager pageManager)
 52  
     {
 53  0
         super(template, errorTemplate, pageManager);
 54  0
     }
 55  
     
 56  
     public BaseSiteUpdateAction(String template, 
 57  
                              String errorTemplate, 
 58  
                              PortletActionSecurityBehavior securityBehavior)
 59  
     {
 60  0
         super(template, errorTemplate, securityBehavior);
 61  0
     }
 62  
 
 63  
     public BaseSiteUpdateAction(String template, 
 64  
                              String errorTemplate, 
 65  
                              PageManager pageManager,
 66  
                              PortletActionSecurityBehavior securityBehavior)
 67  
     {
 68  0
         super(template, errorTemplate, pageManager, securityBehavior);        
 69  0
     }
 70  
 
 71  
     protected abstract int updateInformation(RequestContext requestContext, Map resultMap, Node node, String path)
 72  
     throws AJAXException;
 73  
     
 74  
     protected int insertMetadata(RequestContext requestContext, Map resultMap, Node node)
 75  
     throws AJAXException
 76  
     {
 77  0
         String name = getActionParameter(requestContext, "name");
 78  0
         String language = getActionParameter(requestContext, "lang");
 79  0
         String value = getActionParameter(requestContext, "value");
 80  0
         if (isBlank(name) || isBlank(language))
 81  0
             throw new AJAXException("Invalid Metadata: name, language invalid data.");
 82  0
         Locale locale = new Locale(language);
 83  0
         node.getMetadata().addField(locale, name, value);        
 84  0
         return 1;
 85  
     }
 86  
 
 87  
     protected int updateMetadata(RequestContext requestContext, Map resultMap, Node node)
 88  
     throws AJAXException
 89  
     {
 90  0
         String name = getActionParameter(requestContext, "name");
 91  0
         String language = getActionParameter(requestContext, "lang");
 92  0
         String value = getActionParameter(requestContext, "value");
 93  0
         String oldName = getActionParameter(requestContext, "oldname");
 94  0
         String oldLanguage = getActionParameter(requestContext, "oldlang");
 95  
 
 96  0
         if (isBlank(name) || isBlank(language) || isBlank(oldName) || isBlank(oldLanguage))
 97  0
             throw new AJAXException("Invalid Metadata: name, language invalid data.");
 98  
                 
 99  0
         Collection cfields = node.getMetadata().getFields(oldName);
 100  0
         if (cfields == null || cfields.size() == 0)
 101  
         {
 102  0
             return insertMetadata(requestContext, resultMap, node);            
 103  
         }
 104  0
         boolean found = false;
 105  0
         Iterator fields = cfields.iterator();
 106  0
         while (fields.hasNext())
 107  
         {
 108  0
             LocalizedField field  = (LocalizedField)fields.next();
 109  0
             if (areFieldsSame(field.getName(), oldName) &&
 110  
                 areFieldsSame(field.getLocale().toString(), oldLanguage))
 111  
             {
 112  0
                 field.setName(name);
 113  0
                 field.setLocale(new Locale(language));
 114  0
                 field.setValue(value);
 115  0
                 found = true;
 116  0
                 break;
 117  
             }
 118  0
         }
 119  0
         if (!found)
 120  0
             return insertMetadata(requestContext, resultMap, node);
 121  0
         return 1;
 122  
     }
 123  
     
 124  
     protected int removeMetadata(RequestContext requestContext, Map resultMap, Node node)
 125  
     throws AJAXException
 126  
     {
 127  0
         String name = getActionParameter(requestContext, "name");
 128  0
         String language = getActionParameter(requestContext, "lang");
 129  0
         if (isBlank(name) || isBlank(language))
 130  0
             throw new AJAXException("Invalid Metadata: name, language invalid data.");
 131  0
         Collection cfields = node.getMetadata().getFields(name);
 132  0
         Collection allFields = node.getMetadata().getFields();
 133  0
         if (cfields == null || cfields.size() == 0)
 134  
         {
 135  0
             return 0;            
 136  
         }
 137  0
         boolean found = false;        
 138  0
         Iterator fields = cfields.iterator();
 139  0
         while (fields.hasNext())
 140  
         {
 141  0
             LocalizedField field  = (LocalizedField)fields.next();
 142  0
             if (areFieldsSame(field.getName(), name) &&
 143  
                 areFieldsSame(field.getLocale().toString(), language))
 144  
             {
 145  0
                 cfields.remove(field);
 146  0
                 if (allFields.remove(field))
 147  
                 {
 148  0
                     node.getMetadata().setFields(allFields);
 149  
                 }
 150  0
                 found = true;
 151  0
                 break;
 152  
             }
 153  0
         }    
 154  
         
 155  0
         return (found) ? 1 : 0;
 156  
     }
 157  
 
 158  
     protected int insertSecurityReference(RequestContext requestContext, Map resultMap, Node node)
 159  
     throws AJAXException
 160  
     {
 161  0
         String name = getActionParameter(requestContext, "name");
 162  0
         String kind = getActionParameter(requestContext, "kind");
 163  0
         if (isBlank(name) || isBlank(kind))
 164  0
             throw new AJAXException("Invalid Security Ref: name invalid data.");
 165  0
         if (node.getSecurityConstraints() == null)
 166  
         {
 167  0
             SecurityConstraints cons = node.newSecurityConstraints();
 168  0
             node.setSecurityConstraints(cons);             
 169  
         }
 170  0
         if (kind.equals("Owner"))
 171  
         {
 172  0
             node.getSecurityConstraints().setOwner(name);
 173  
         }
 174  
         else
 175  
         {
 176  0
             List refs = node.getSecurityConstraints().getSecurityConstraintsRefs();
 177  0
             if (refs.contains(name))
 178  0
                 return 0; // do nothing
 179  0
             refs.add(name);
 180  
         }
 181  0
         return 1;        
 182  
     }
 183  
 
 184  
     protected int updateSecurityReference(RequestContext requestContext, Map resultMap, Node node)
 185  
     throws AJAXException
 186  
     {
 187  0
         String name = getActionParameter(requestContext, "name");
 188  0
         String oldName = getActionParameter(requestContext, "oldname");
 189  0
         String kind = getActionParameter(requestContext, "kind");
 190  0
         if (isBlank(name) || isBlank(oldName) || isBlank(kind))
 191  0
             throw new AJAXException("Invalid Security Ref: name invalid data.");
 192  0
         if (node.getSecurityConstraints() == null)
 193  
         {
 194  0
             SecurityConstraints cons = node.newSecurityConstraints();
 195  0
             node.setSecurityConstraints(cons);             
 196  
         }                
 197  0
         List refs = node.getSecurityConstraints().getSecurityConstraintsRefs();        
 198  0
         if (refs == null || refs.size() == 0)
 199  
         {
 200  0
             return insertSecurityReference(requestContext, resultMap, node);            
 201  
         }
 202  0
         boolean found = false;
 203  0
         if (kind.equals("Owner"))
 204  
         {
 205  0
             node.getSecurityConstraints().setOwner(name);
 206  0
             found = true;
 207  
         }
 208  
         else
 209  
         {            
 210  0
             for (int ix = 0; ix < refs.size(); ix++)
 211  
             {
 212  0
                 String ref = (String)refs.get(ix);  
 213  0
                 if (areFieldsSame(ref, oldName))
 214  
                 {
 215  0
                     refs.set(ix, name);
 216  0
                     found = true;
 217  0
                     break;
 218  
                 }
 219  
             }
 220  
         }
 221  0
         if (!found)
 222  0
             return insertSecurityReference(requestContext, resultMap, node);
 223  0
         return 1;
 224  
     }
 225  
     
 226  
     protected int removeSecurityReference(RequestContext requestContext, Map resultMap, Node node)
 227  
     throws AJAXException
 228  
     {
 229  0
         String name = getActionParameter(requestContext, "name");
 230  0
         String kind = getActionParameter(requestContext, "kind");
 231  0
         if (isBlank(name) || isBlank(kind))
 232  0
             throw new AJAXException("Invalid Security Ref: name invalid data.");
 233  0
         if (node.getSecurityConstraints() == null)
 234  
         {
 235  0
             return 0;
 236  
         }
 237  0
         if (kind.equals("Owner"))
 238  
         {
 239  0
             node.getSecurityConstraints().setOwner(null);
 240  
         }
 241  
         else
 242  
         {
 243  0
             List refs = node.getSecurityConstraints().getSecurityConstraintsRefs();
 244  0
             if (!refs.contains(name))
 245  0
                 return 0; // nothing to do
 246  0
             refs.remove(name);
 247  
         }
 248  0
         return 1;
 249  
     }
 250  
 
 251  
     protected int removeSecurityDef(RequestContext requestContext, Map resultMap, Node node)
 252  
     throws AJAXException
 253  
     {
 254  0
         String id = getActionParameter(requestContext, "id");
 255  0
         if (isBlank(id))
 256  0
             throw new AJAXException("Invalid Security Ref: id invalid data.");
 257  0
         if (node.getSecurityConstraints() == null)
 258  
         {
 259  0
             return 0;
 260  
         }
 261  0
         List defs = node.getSecurityConstraints().getSecurityConstraints();
 262  0
         if (defs == null || defs.size() == 0)
 263  
         {
 264  0
             return 0;
 265  
         }
 266  0
         if (id.length() == 1)
 267  0
             return 0;
 268  0
         id = id.substring(1);
 269  0
         int index = Integer.parseInt(id) - 1;
 270  0
         if (index < 0)
 271  
         {
 272  0
             return 0;
 273  
         }
 274  0
         defs.remove(index);
 275  0
         return 1;
 276  
     }
 277  
     
 278  
     protected boolean isBlank(String field)
 279  
     {
 280  0
         if (field == null || field.trim().length() == 0)
 281  0
             return true;
 282  0
         return false;
 283  
     }
 284  
     protected boolean isFieldModified(String paramValue, String prevValue)
 285  
     {
 286  0
         if (paramValue == null)
 287  
         {
 288  0
             if (prevValue == null)
 289  0
                 return false;
 290  
             else
 291  0
                 return true;
 292  
         }
 293  
         else
 294  
         {
 295  0
             if (prevValue == null)
 296  0
                 return true;
 297  0
             if (prevValue.equals(paramValue))
 298  0
                 return false;
 299  
             else
 300  0
                 return true;
 301  
         }
 302  
     }
 303  
     protected boolean areFieldsSame(String f1, String f2)
 304  
     {
 305  0
         return !isFieldModified(f1, f2);
 306  
     }
 307  
     protected boolean isBooleanModified(String paramValue, class="keyword">boolean prevValue)
 308  
     {
 309  0
         if (paramValue == null)
 310  
         {
 311  0
             if (prevValue == false)
 312  0
                 return false;
 313  
             else
 314  0
                 return true;
 315  
         }
 316  
         else
 317  
         {
 318  0
             if (prevValue == false)
 319  0
                 return true;
 320  
             else
 321  0
                 return false;
 322  
         }
 323  
     }        
 324  
 }

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