1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
27
28
29
30
31
32
33
34 public final class PermissionXmlUtil {
35
36 private PermissionXmlUtil() {}
37
38
39
40
41
42
43
44
45 static void validateAndPersistNewPermission(PermissionXmlDTO newPermission) throws UnmarshalException {
46 if (newPermission == null) {
47 throw new IllegalArgumentException("Cannot persist a null permission");
48 }
49
50
51 validatePermission(newPermission);
52
53
54 Permission.Builder builder = Permission.Builder.create(newPermission.getNamespaceCode(), newPermission.getPermissionName());
55 builder.setDescription(newPermission.getPermissionDescription());
56 builder.setActive(newPermission.getActive().booleanValue());
57 builder.setAttributes(newPermission.getPermissionDetails());
58
59 KimApiServiceLocator.getPermissionService().createPermission(builder.build());
60 }
61
62
63
64
65
66
67 private static void validatePermission(PermissionXmlDTO newPermission) throws UnmarshalException {
68
69 if (StringUtils.isBlank(newPermission.getPermissionName()) || StringUtils.isBlank(newPermission.getNamespaceCode())) {
70 throw new UnmarshalException("Cannot create a permission with a blank name or namespace");
71 } else if (StringUtils.isBlank(newPermission.getPermissionTemplateId())) {
72 throw new UnmarshalException("Cannot create a permission without specifying a permission template");
73 } else if (StringUtils.isBlank(newPermission.getPermissionDescription())) {
74 throw new UnmarshalException("Cannot create a permission with a blank description");
75 }
76
77
78 PermissionContract permission = KimApiServiceLocator.getPermissionService().findPermByNamespaceCodeAndName(
79 newPermission.getNamespaceCode(), newPermission.getPermissionName());
80 if (permission != null) {
81 newPermission.setPermissionId(permission.getId());
82 }
83 }
84 }