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.PermissionContract;
22 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23
24
25
26
27
28
29
30
31
32
33 public final class PermissionXmlUtil {
34
35 private PermissionXmlUtil() {}
36
37
38
39
40
41
42
43
44 static void validateAndPersistNewPermission(PermissionXmlDTO newPermission) throws UnmarshalException {
45 if (newPermission == null) {
46 throw new IllegalArgumentException("Cannot persist a null permission");
47 }
48
49
50 validatePermission(newPermission);
51
52
53 if (StringUtils.isBlank(newPermission.getPermissionId())) {
54 newPermission.setPermissionId(KimApiServiceLocator.getPermissionUpdateService().getNextAvailablePermissionId());
55 }
56
57
58 KimApiServiceLocator.getPermissionUpdateService().savePermission(newPermission.getPermissionId(), newPermission.getPermissionTemplateId(),
59 newPermission.getNamespaceCode(), newPermission.getPermissionName(), newPermission.getPermissionDescription(),
60 newPermission.getActive().booleanValue(), newPermission.getPermissionDetails());
61 }
62
63
64
65
66
67
68 private static void validatePermission(PermissionXmlDTO newPermission) throws UnmarshalException {
69
70 if (StringUtils.isBlank(newPermission.getPermissionName()) || StringUtils.isBlank(newPermission.getNamespaceCode())) {
71 throw new UnmarshalException("Cannot create a permission with a blank name or namespace");
72 } else if (StringUtils.isBlank(newPermission.getPermissionTemplateId())) {
73 throw new UnmarshalException("Cannot create a permission without specifying a permission template");
74 } else if (StringUtils.isBlank(newPermission.getPermissionDescription())) {
75 throw new UnmarshalException("Cannot create a permission with a blank description");
76 }
77
78
79 PermissionContract permission = KimApiServiceLocator.getPermissionService().getPermissionsByName(newPermission.getNamespaceCode(), newPermission.getPermissionName());
80 if (permission != null) {
81 newPermission.setPermissionId(permission.getId());
82 }
83 }
84 }