001/* 002 * Copyright 2011 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl1.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.kim.impl.jaxb; 017 018import javax.xml.bind.UnmarshalException; 019 020import org.apache.commons.lang.StringUtils; 021import org.kuali.rice.kim.api.permission.Permission; 022import org.kuali.rice.kim.api.permission.PermissionContract; 023import org.kuali.rice.kim.api.services.KimApiServiceLocator; 024 025/** 026 * Helper class containing static methods for aiding in parsing parsing XML. 027 * 028 * <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?) 029 * 030 * <p>TODO: Should this be converted into a service instead? 031 * 032 * @author Kuali Rice Team (rice.collab@kuali.org) 033 */ 034public final class PermissionXmlUtil { 035 // Do not allow outside code to instantiate this class. 036 private PermissionXmlUtil() {} 037 038 /** 039 * Validates a new permission and then saves it. 040 * 041 * @param newPermission 042 * @throws IllegalArgumentException if newPermission is null. 043 * @throws UnmarshalException if newPermission contains invalid data. 044 */ 045 static void validateAndPersistNewPermission(PermissionXmlDTO newPermission) throws UnmarshalException { 046 if (newPermission == null) { 047 throw new IllegalArgumentException("Cannot persist a null permission"); 048 } 049 050 // Validate the new permission. 051 validatePermission(newPermission); 052 053 // Save the permission. 054 Permission.Builder builder = Permission.Builder.create(newPermission.getNamespaceCode(), newPermission.getPermissionName()); 055 builder.setDescription(newPermission.getPermissionDescription()); 056 builder.setActive(newPermission.getActive().booleanValue()); 057 builder.setAttributes(newPermission.getPermissionDetails()); 058 059 KimApiServiceLocator.getPermissionService().createPermission(builder.build()); 060 } 061 062 /** 063 * Validates a permission to ensure that the required fields have been filled. 064 * 065 * @throws UnmarshalException if newPermission contains invalid data. 066 */ 067 private static void validatePermission(PermissionXmlDTO newPermission) throws UnmarshalException { 068 // Ensure that the permission name, namespace, template, and description have been filled in. 069 if (StringUtils.isBlank(newPermission.getPermissionName()) || StringUtils.isBlank(newPermission.getNamespaceCode())) { 070 throw new UnmarshalException("Cannot create a permission with a blank name or namespace"); 071 } else if (StringUtils.isBlank(newPermission.getPermissionTemplateId())) { 072 throw new UnmarshalException("Cannot create a permission without specifying a permission template"); 073 } else if (StringUtils.isBlank(newPermission.getPermissionDescription())) { 074 throw new UnmarshalException("Cannot create a permission with a blank description"); 075 } 076 077 // If another permission with that name and namespace exists, use its ID on the new permission. 078 PermissionContract permission = KimApiServiceLocator.getPermissionService().findPermByNamespaceCodeAndName( 079 newPermission.getNamespaceCode(), newPermission.getPermissionName()); 080 if (permission != null) { 081 newPermission.setPermissionId(permission.getId()); 082 } 083 } 084}