View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.rice.kim.impl.jaxb;
20  
21  import javax.xml.bind.UnmarshalException;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.rice.kim.api.permission.Permission;
25  import org.kuali.rice.kim.api.permission.PermissionContract;
26  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
27  
28  /**
29   * Helper class containing static methods for aiding in parsing parsing XML.
30   * 
31   * <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?)
32   * 
33   * <p>TODO: Should this be converted into a service instead?
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public final class PermissionXmlUtil {
38      // Do not allow outside code to instantiate this class.
39      private PermissionXmlUtil() {}
40      
41      /**
42       * Validates a new permission and then saves it.
43       * 
44       * @param newPermission
45       * @throws IllegalArgumentException if newPermission is null.
46       * @throws UnmarshalException if newPermission contains invalid data.
47       */
48      static void validateAndPersistNewPermission(PermissionXmlDTO newPermission) throws UnmarshalException {
49          if (newPermission == null) {
50              throw new IllegalArgumentException("Cannot persist a null permission");
51          }
52          
53          // Validate the new permission.
54          validatePermission(newPermission);
55          
56          // Save the permission.
57          Permission.Builder builder = Permission.Builder.create(newPermission.getNamespaceCode(), newPermission.getPermissionName());
58          builder.setDescription(newPermission.getPermissionDescription());
59          builder.setActive(newPermission.getActive().booleanValue());
60          builder.setAttributes(newPermission.getPermissionDetails());
61          
62          KimApiServiceLocator.getPermissionService().createPermission(builder.build());
63      }
64  
65      /**
66       * Validates a permission to ensure that the required fields have been filled.
67       * 
68       * @throws UnmarshalException if newPermission contains invalid data.
69       */
70      private static void validatePermission(PermissionXmlDTO newPermission) throws UnmarshalException {
71          // Ensure that the permission name, namespace, template, and description have been filled in.
72          if (StringUtils.isBlank(newPermission.getPermissionName()) || StringUtils.isBlank(newPermission.getNamespaceCode())) {
73              throw new UnmarshalException("Cannot create a permission with a blank name or namespace");
74          } else if (StringUtils.isBlank(newPermission.getPermissionTemplateId())) {
75              throw new UnmarshalException("Cannot create a permission without specifying a permission template");
76          } else if (StringUtils.isBlank(newPermission.getPermissionDescription())) {
77              throw new UnmarshalException("Cannot create a permission with a blank description");
78          }
79          
80          // If another permission with that name and namespace exists, use its ID on the new permission.
81          PermissionContract permission = KimApiServiceLocator.getPermissionService().findPermByNamespaceCodeAndName(
82                  newPermission.getNamespaceCode(), newPermission.getPermissionName());
83          if (permission != null) {
84              newPermission.setPermissionId(permission.getId());
85          }
86      }
87  }