1 | package edu.iu.uis.sit.portal.portlet.publishing.web; |
2 | |
3 | import org.springframework.validation.Errors; |
4 | import org.springframework.validation.ValidationUtils; |
5 | import org.springframework.validation.Validator; |
6 | |
7 | import edu.iu.uis.sit.portal.portlet.publishing.domain.Acl; |
8 | import edu.iu.uis.sit.portal.portlet.utility.Constants; |
9 | import edu.iu.uis.sit.portal.user.service.AclService; |
10 | |
11 | public class TabValidator implements Validator { |
12 | |
13 | private AclService aclService; |
14 | |
15 | public boolean supports(Class candidate) { |
16 | return WebTab.class.isAssignableFrom(candidate); |
17 | } |
18 | |
19 | public void validate(Object obj, Errors errors) { |
20 | WebTab webTab = (WebTab) obj; |
21 | |
22 | validatePage1(webTab, errors); |
23 | validatePage2(webTab, errors); |
24 | validatePage3(webTab, errors); |
25 | } |
26 | |
27 | public void validatePage1(WebTab webTab, Errors errors) { |
28 | ValidationUtils.rejectIfEmpty(errors, "tab.name", "NAME_REQUIRED", "Name is required."); |
29 | ValidationUtils.rejectIfEmpty(errors, "tab.description", "DESCRIPTION_REQUIRED", "Description is required."); |
30 | ValidationUtils.rejectIfEmpty(errors, "tab.contactEmail", "CONTACT_EMAIL_REQUIRED", "Email is required."); |
31 | } |
32 | |
33 | public void validatePage2(WebTab webTab, Errors errors) { |
34 | if (webTab.isAddViewGroup()) { |
35 | if (webTab.getAclViewGroup() == null || "".equals(webTab.getAclViewGroup())) { |
36 | errors.rejectValue("aclViewGroup", "ACL_VIEW_GROUP_REQUIRED", "Group is required."); |
37 | } else { |
38 | if (getAclService().isValidGroup(webTab.getAclViewGroup())) { |
39 | Acl acl = new Acl(); |
40 | acl.setValue(webTab.getAclViewGroup()); |
41 | acl.setType(Constants.GROUP_VIEW); |
42 | acl.setKey(Constants.GROUP_KEY); |
43 | acl.setTab(webTab.getTab()); |
44 | webTab.getTab().getAcls().add(acl); |
45 | webTab.setAclViewGroup(""); |
46 | } else { |
47 | errors.rejectValue("aclViewGroup", "ACL_VIEW_GROUP_INVALID", "Group is invalid."); |
48 | } |
49 | } |
50 | } |
51 | if (webTab.isAddViewRole()) { |
52 | if (webTab.getAclViewRole() == null || "".equals(webTab.getAclViewRole())) { |
53 | errors.rejectValue("aclViewRole", "ACL_VIEW_ROLE_REQUIRED", "Role is required."); |
54 | } |
55 | if (webTab.getAclViewRoleKey() == null || "".equals(webTab.getAclViewRoleKey())) { |
56 | errors.rejectValue("aclViewRoleKey", "ACL_VIEW_ROLE_KEY_REQUIRED", "Role Id is required."); |
57 | } |
58 | if (webTab.getAclViewRole() != null && !"".equals(webTab.getAclViewRole()) && webTab.getAclViewRoleKey() != null && !"".equals(webTab.getAclViewRoleKey())) { |
59 | Acl acl = new Acl(); |
60 | acl.setValue(webTab.getAclViewRole()); |
61 | acl.setType(Constants.ROLE_VIEW); |
62 | acl.setKey(webTab.getAclViewRoleKey()); |
63 | acl.setTab(webTab.getTab()); |
64 | webTab.getTab().getAcls().add(acl); |
65 | webTab.setAclViewRole(""); |
66 | webTab.setAclViewRoleKey(""); |
67 | } |
68 | } |
69 | if (webTab.isAddPublishGroup()) { |
70 | if (webTab.getAclPublishGroup() == null || "".equals(webTab.getAclPublishGroup())) { |
71 | errors.rejectValue("aclPublishGroup", "ACL_PUBLISH_GROUP_REQUIRED", "Group is required."); |
72 | } else { |
73 | if (getAclService().isValidGroup(webTab.getAclPublishGroup())) { |
74 | Acl acl = new Acl(); |
75 | acl.setValue(webTab.getAclPublishGroup()); |
76 | acl.setType(Constants.GROUP_PUBLISH); |
77 | acl.setKey(Constants.GROUP_KEY); |
78 | acl.setTab(webTab.getTab()); |
79 | webTab.getTab().getAcls().add(acl); |
80 | webTab.setAclPublishGroup(""); |
81 | } else { |
82 | errors.rejectValue("aclPublishGroup", "ACL_PUBLISH_GROUP_INVALID", "Group is invalid."); |
83 | } |
84 | } |
85 | } |
86 | } |
87 | |
88 | public void validatePage3(WebTab webTab, Errors errors) { |
89 | |
90 | } |
91 | |
92 | public AclService getAclService() { |
93 | return aclService; |
94 | } |
95 | |
96 | public void setAclService(AclService aclService) { |
97 | this.aclService = aclService; |
98 | } |
99 | } |