Coverage Report - org.kuali.rice.kim.impl.jaxb.PermissionXmlUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
PermissionXmlUtil
0%
0/20
0%
0/12
4.333
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kim.impl.jaxb;
 17  
 
 18  
 import javax.xml.bind.UnmarshalException;
 19  
 
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.kuali.rice.kim.api.permission.Permission;
 22  
 import org.kuali.rice.kim.api.permission.PermissionContract;
 23  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 24  
 
 25  
 /**
 26  
  * Helper class containing static methods for aiding in parsing parsing XML.
 27  
  * 
 28  
  * <p>All non-private methods are package-private so that only the KIM-parsing-related code can make use of them. (TODO: Is that necessary?)
 29  
  * 
 30  
  * <p>TODO: Should this be converted into a service instead?
 31  
  * 
 32  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 33  
  */
 34  
 public final class PermissionXmlUtil {
 35  
     // Do not allow outside code to instantiate this class.
 36  0
     private PermissionXmlUtil() {}
 37  
     
 38  
     /**
 39  
      * Validates a new permission and then saves it.
 40  
      * 
 41  
      * @param newPermission
 42  
      * @throws IllegalArgumentException if newPermission is null.
 43  
      * @throws UnmarshalException if newPermission contains invalid data.
 44  
      */
 45  
     static void validateAndPersistNewPermission(PermissionXmlDTO newPermission) throws UnmarshalException {
 46  0
         if (newPermission == null) {
 47  0
             throw new IllegalArgumentException("Cannot persist a null permission");
 48  
         }
 49  
         
 50  
         // Validate the new permission.
 51  0
         validatePermission(newPermission);
 52  
         
 53  
         // Save the permission.
 54  0
         Permission.Builder builder = Permission.Builder.create(newPermission.getNamespaceCode(), newPermission.getPermissionName());
 55  0
         builder.setDescription(newPermission.getPermissionDescription());
 56  0
         builder.setActive(newPermission.getActive().booleanValue());
 57  0
         builder.setAttributes(newPermission.getPermissionDetails());
 58  
         
 59  0
         KimApiServiceLocator.getPermissionService().createPermission(builder.build());
 60  0
     }
 61  
 
 62  
     /**
 63  
      * Validates a permission to ensure that the required fields have been filled.
 64  
      * 
 65  
      * @throws UnmarshalException if newPermission contains invalid data.
 66  
      */
 67  
     private static void validatePermission(PermissionXmlDTO newPermission) throws UnmarshalException {
 68  
         // Ensure that the permission name, namespace, template, and description have been filled in.
 69  0
         if (StringUtils.isBlank(newPermission.getPermissionName()) || StringUtils.isBlank(newPermission.getNamespaceCode())) {
 70  0
             throw new UnmarshalException("Cannot create a permission with a blank name or namespace");
 71  0
         } else if (StringUtils.isBlank(newPermission.getPermissionTemplateId())) {
 72  0
             throw new UnmarshalException("Cannot create a permission without specifying a permission template");
 73  0
         } else if (StringUtils.isBlank(newPermission.getPermissionDescription())) {
 74  0
             throw new UnmarshalException("Cannot create a permission with a blank description");
 75  
         }
 76  
         
 77  
         // If another permission with that name and namespace exists, use its ID on the new permission.
 78  0
         PermissionContract permission = KimApiServiceLocator.getPermissionService().findPermByNamespaceCodeAndName(
 79  
                 newPermission.getNamespaceCode(), newPermission.getPermissionName());
 80  0
         if (permission != null) {
 81  0
             newPermission.setPermissionId(permission.getId());
 82  
         }
 83  0
     }
 84  
 }