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 FragmentValidator implements Validator { |
12 | |
13 | private AclService aclService; |
14 | |
15 | public boolean supports(Class candidate) { |
16 | return WebFragment.class.isAssignableFrom(candidate); |
17 | } |
18 | |
19 | public void validate(Object obj, Errors errors) { |
20 | WebFragment webFragment = (WebFragment) obj; |
21 | if (Constants.RSS_TYPE.equals(webFragment.getFragment().getType())) { |
22 | validateRSSPage1(webFragment, errors); |
23 | } else if (Constants.URL_TYPE.equals(webFragment.getFragment().getType())) { |
24 | validateURLPage1(webFragment, errors); |
25 | |
26 | } else if (Constants.XML_TYPE.equals(webFragment.getFragment().getType())) { |
27 | validateXMLPage1(webFragment, errors); |
28 | } |
29 | validatePage2(webFragment, errors); |
30 | validatePage3(webFragment, errors); |
31 | } |
32 | |
33 | public void validateRSSPage1(WebFragment webFragment, Errors errors) { |
34 | ValidationUtils.rejectIfEmpty(errors, "fragment.url", "URL_REQUIRED", "URL is required."); |
35 | } |
36 | |
37 | public void validateXMLPage1(WebFragment webFragment, Errors errors) { |
38 | ValidationUtils.rejectIfEmpty(errors, "fragment.fragmentText.text", "FRAGMENT_TEXT_REQUIRED", "XML is required."); |
39 | } |
40 | |
41 | public void validateURLPage1(WebFragment webFragment, Errors errors) { |
42 | ValidationUtils.rejectIfEmpty(errors, "fragment.url", "URL_REQUIRED", "URL is required."); |
43 | } |
44 | |
45 | public void validatePage2(WebFragment webFragment, Errors errors) { |
46 | ValidationUtils.rejectIfEmpty(errors, "fragment.title", "TITLE_REQUIRED", "Title is required."); |
47 | ValidationUtils.rejectIfEmpty(errors, "fragment.description", "DESCRIPTION_REQUIRED", "Description is required."); |
48 | ValidationUtils.rejectIfEmpty(errors, "fragment.contactEmail", "CONTACT_EMAIL_REQUIRED", "Email is required."); |
49 | } |
50 | |
51 | public void validatePage3(WebFragment webFragment, Errors errors) { |
52 | if (webFragment.isAddViewGroup()) { |
53 | if (webFragment.getAclViewGroup() == null || "".equals(webFragment.getAclViewGroup())) { |
54 | errors.rejectValue("aclViewGroup", "ACL_VIEW_GROUP_REQUIRED", "Group is required."); |
55 | } else { |
56 | if (getAclService().isValidGroup(webFragment.getAclViewGroup())) { |
57 | Acl acl = new Acl(); |
58 | acl.setValue(webFragment.getAclViewGroup()); |
59 | acl.setType(Constants.GROUP_VIEW); |
60 | acl.setKey(Constants.GROUP_KEY); |
61 | acl.setFragment(webFragment.getFragment()); |
62 | webFragment.getFragment().getAcls().add(acl); |
63 | webFragment.setAclViewGroup(""); |
64 | } else { |
65 | errors.rejectValue("aclViewGroup", "ACL_VIEW_GROUP_INVALID", "Group is invalid."); |
66 | } |
67 | } |
68 | } |
69 | if (webFragment.isAddViewRole()) { |
70 | if (webFragment.getAclViewRole() == null || "".equals(webFragment.getAclViewRole())) { |
71 | errors.rejectValue("aclViewRole", "ACL_VIEW_ROLE_REQUIRED", "Role is required."); |
72 | } |
73 | if (webFragment.getAclViewRoleKey() == null || "".equals(webFragment.getAclViewRoleKey())) { |
74 | errors.rejectValue("aclViewRoleKey", "ACL_VIEW_ROLE_KEY_REQUIRED", "Role Id is required."); |
75 | } |
76 | if (webFragment.getAclViewRole() != null && !"".equals(webFragment.getAclViewRole()) && webFragment.getAclViewRoleKey() != null && !"".equals(webFragment.getAclViewRoleKey())) { |
77 | Acl acl = new Acl(); |
78 | acl.setValue(webFragment.getAclViewRole()); |
79 | acl.setType(Constants.ROLE_VIEW); |
80 | acl.setKey(webFragment.getAclViewRoleKey()); |
81 | acl.setFragment(webFragment.getFragment()); |
82 | webFragment.getFragment().getAcls().add(acl); |
83 | webFragment.setAclViewRole(""); |
84 | webFragment.setAclViewRoleKey(""); |
85 | } |
86 | } |
87 | if (webFragment.isAddPublishGroup()) { |
88 | if (webFragment.getAclPublishGroup() == null || "".equals(webFragment.getAclPublishGroup())) { |
89 | errors.rejectValue("aclPublishGroup", "ACL_PUBLISH_GROUP_REQUIRED", "Group is required."); |
90 | } else { |
91 | if (getAclService().isValidGroup(webFragment.getAclPublishGroup())) { |
92 | Acl acl = new Acl(); |
93 | acl.setValue(webFragment.getAclPublishGroup()); |
94 | acl.setType(Constants.GROUP_PUBLISH); |
95 | acl.setKey(Constants.GROUP_KEY); |
96 | acl.setFragment(webFragment.getFragment()); |
97 | webFragment.getFragment().getAcls().add(acl); |
98 | webFragment.setAclPublishGroup(""); |
99 | } else { |
100 | errors.rejectValue("aclPublishGroup", "ACL_PUBLISH_GROUP_INVALID", "Group is invalid."); |
101 | } |
102 | } |
103 | } |
104 | } |
105 | |
106 | public void validatePage4(WebFragment webFragment, Errors errors) { |
107 | |
108 | } |
109 | |
110 | public AclService getAclService() { |
111 | return aclService; |
112 | } |
113 | |
114 | public void setAclService(AclService aclService) { |
115 | this.aclService = aclService; |
116 | } |
117 | } |