001/* 002 * The Kuali Financial System, a comprehensive financial management system for higher education. 003 * 004 * Copyright 2005-2014 The Kuali Foundation 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU Affero General Public License as 008 * published by the Free Software Foundation, either version 3 of the 009 * License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU Affero General Public License for more details. 015 * 016 * You should have received a copy of the GNU Affero General Public License 017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package org.kuali.rice.kim.impl.jaxb; 020 021import javax.xml.bind.UnmarshalException; 022 023import org.apache.commons.lang.StringUtils; 024import org.kuali.rice.kim.api.permission.Permission; 025import org.kuali.rice.kim.api.permission.PermissionContract; 026import org.kuali.rice.kim.api.services.KimApiServiceLocator; 027 028/** 029 * Helper class containing static methods for aiding in parsing parsing XML. 030 * 031 * <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?) 032 * 033 * <p>TODO: Should this be converted into a service instead? 034 * 035 * @author Kuali Rice Team (rice.collab@kuali.org) 036 */ 037public final class PermissionXmlUtil { 038 // Do not allow outside code to instantiate this class. 039 private PermissionXmlUtil() {} 040 041 /** 042 * Validates a new permission and then saves it. 043 * 044 * @param newPermission 045 * @throws IllegalArgumentException if newPermission is null. 046 * @throws UnmarshalException if newPermission contains invalid data. 047 */ 048 static void validateAndPersistNewPermission(PermissionXmlDTO newPermission) throws UnmarshalException { 049 if (newPermission == null) { 050 throw new IllegalArgumentException("Cannot persist a null permission"); 051 } 052 053 // Validate the new permission. 054 validatePermission(newPermission); 055 056 // Save the permission. 057 Permission.Builder builder = Permission.Builder.create(newPermission.getNamespaceCode(), newPermission.getPermissionName()); 058 builder.setDescription(newPermission.getPermissionDescription()); 059 builder.setActive(newPermission.getActive().booleanValue()); 060 builder.setAttributes(newPermission.getPermissionDetails()); 061 062 KimApiServiceLocator.getPermissionService().createPermission(builder.build()); 063 } 064 065 /** 066 * Validates a permission to ensure that the required fields have been filled. 067 * 068 * @throws UnmarshalException if newPermission contains invalid data. 069 */ 070 private static void validatePermission(PermissionXmlDTO newPermission) throws UnmarshalException { 071 // Ensure that the permission name, namespace, template, and description have been filled in. 072 if (StringUtils.isBlank(newPermission.getPermissionName()) || StringUtils.isBlank(newPermission.getNamespaceCode())) { 073 throw new UnmarshalException("Cannot create a permission with a blank name or namespace"); 074 } else if (StringUtils.isBlank(newPermission.getPermissionTemplateId())) { 075 throw new UnmarshalException("Cannot create a permission without specifying a permission template"); 076 } else if (StringUtils.isBlank(newPermission.getPermissionDescription())) { 077 throw new UnmarshalException("Cannot create a permission with a blank description"); 078 } 079 080 // If another permission with that name and namespace exists, use its ID on the new permission. 081 PermissionContract permission = KimApiServiceLocator.getPermissionService().findPermByNamespaceCodeAndName( 082 newPermission.getNamespaceCode(), newPermission.getPermissionName()); 083 if (permission != null) { 084 newPermission.setPermissionId(permission.getId()); 085 } 086 } 087}