Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PermissionXmlUtil |
|
| 4.666666666666667;4.667 |
1 | /* | |
2 | * Copyright 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/ecl1.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.PermissionContract; | |
22 | import org.kuali.rice.kim.api.services.KimApiServiceLocator; | |
23 | ||
24 | /** | |
25 | * Helper class containing static methods for aiding in parsing parsing XML. | |
26 | * | |
27 | * <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?) | |
28 | * | |
29 | * <p>TODO: Should this be converted into a service instead? | |
30 | * | |
31 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
32 | */ | |
33 | public final class PermissionXmlUtil { | |
34 | // Do not allow outside code to instantiate this class. | |
35 | 0 | private PermissionXmlUtil() {} |
36 | ||
37 | /** | |
38 | * Validates a new permission and then saves it. | |
39 | * | |
40 | * @param newPermission | |
41 | * @throws IllegalArgumentException if newPermission is null. | |
42 | * @throws UnmarshalException if newPermission contains invalid data. | |
43 | */ | |
44 | static void validateAndPersistNewPermission(PermissionXmlDTO newPermission) throws UnmarshalException { | |
45 | 0 | if (newPermission == null) { |
46 | 0 | throw new IllegalArgumentException("Cannot persist a null permission"); |
47 | } | |
48 | ||
49 | // Validate the new permission. | |
50 | 0 | validatePermission(newPermission); |
51 | ||
52 | // If the permission is a new one and not an override of an existing one, set its ID. | |
53 | 0 | if (StringUtils.isBlank(newPermission.getPermissionId())) { |
54 | 0 | newPermission.setPermissionId(KimApiServiceLocator.getPermissionUpdateService().getNextAvailablePermissionId()); |
55 | } | |
56 | ||
57 | // Save the permission. | |
58 | 0 | KimApiServiceLocator.getPermissionUpdateService().savePermission(newPermission.getPermissionId(), newPermission.getPermissionTemplateId(), |
59 | newPermission.getNamespaceCode(), newPermission.getPermissionName(), newPermission.getPermissionDescription(), | |
60 | newPermission.getActive().booleanValue(), newPermission.getPermissionDetails()); | |
61 | 0 | } |
62 | ||
63 | /** | |
64 | * Validates a permission to ensure that the required fields have been filled. | |
65 | * | |
66 | * @throws UnmarshalException if newPermission contains invalid data. | |
67 | */ | |
68 | private static void validatePermission(PermissionXmlDTO newPermission) throws UnmarshalException { | |
69 | // Ensure that the permission name, namespace, template, and description have been filled in. | |
70 | 0 | if (StringUtils.isBlank(newPermission.getPermissionName()) || StringUtils.isBlank(newPermission.getNamespaceCode())) { |
71 | 0 | throw new UnmarshalException("Cannot create a permission with a blank name or namespace"); |
72 | 0 | } else if (StringUtils.isBlank(newPermission.getPermissionTemplateId())) { |
73 | 0 | throw new UnmarshalException("Cannot create a permission without specifying a permission template"); |
74 | 0 | } else if (StringUtils.isBlank(newPermission.getPermissionDescription())) { |
75 | 0 | throw new UnmarshalException("Cannot create a permission with a blank description"); |
76 | } | |
77 | ||
78 | // If another permission with that name and namespace exists, use its ID on the new permission. | |
79 | 0 | PermissionContract permission = KimApiServiceLocator.getPermissionService().getPermissionsByName(newPermission.getNamespaceCode(), newPermission.getPermissionName()); |
80 | 0 | if (permission != null) { |
81 | 0 | newPermission.setPermissionId(permission.getId()); |
82 | } | |
83 | 0 | } |
84 | } |