1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.rice.ken.web.spring;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.coreservice.api.namespace.Namespace;
21  import org.kuali.rice.coreservice.api.namespace.NamespaceService;
22  import org.kuali.rice.ken.exception.ErrorList;
23  import org.kuali.rice.kim.api.group.Group;
24  import org.kuali.rice.kim.api.group.GroupService;
25  import org.kuali.rice.kim.api.identity.IdentityService;
26  import org.kuali.rice.kim.api.identity.principal.Principal;
27  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
28  import org.kuali.rice.krad.service.KRADServiceLocatorInternal;
29  import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  
36  
37  
38  
39  
40  
41  public class BaseSendNotificationController extends MultiActionController {
42      private static final Logger LOG = Logger.getLogger(BaseSendNotificationController.class);
43  
44      private static final String USER_RECIPS_PARAM = "userRecipients";
45      private static final String WORKGROUP_RECIPS_PARAM = "workgroupRecipients";
46      private static final String WORKGROUP_NAMESPACE_CODES_PARAM = "workgroupNamespaceCodes";
47      private static final String SPLIT_REGEX = "(%2C|,)";
48      
49      private static IdentityService identityService;
50      private static GroupService groupService;
51      private static NamespaceService namespaceService;
52  
53      protected static IdentityService getIdentityService() {
54          if ( identityService == null ) {
55              identityService = KimApiServiceLocator.getIdentityService();
56          }
57          return identityService;
58      }
59  
60      protected static GroupService getGroupService() {
61          if ( groupService == null ) {
62              groupService = KimApiServiceLocator.getGroupService();
63          }
64          return groupService;
65      }
66      
67      protected static NamespaceService getNamespaceService() {
68          if ( namespaceService == null ) {
69              namespaceService = KRADServiceLocatorInternal.getNamespaceService();
70          }
71          return namespaceService;
72      }
73      
74      protected String[] parseUserRecipients(HttpServletRequest request) {
75          return parseCommaSeparatedValues(request, USER_RECIPS_PARAM);
76      }
77  
78      protected String[] parseWorkgroupRecipients(HttpServletRequest request) {
79          return parseCommaSeparatedValues(request, WORKGROUP_RECIPS_PARAM);
80      }
81  
82      protected String[] parseWorkgroupNamespaceCodes(HttpServletRequest request) {
83      	return parseCommaSeparatedValues(request, WORKGROUP_NAMESPACE_CODES_PARAM);
84      }
85      
86      protected String[] parseCommaSeparatedValues(HttpServletRequest request, String param) {
87          String vals = request.getParameter(param);
88          if (vals != null) {
89              String[] split = vals.split(SPLIT_REGEX);
90              List<String> strs = new ArrayList<String>();
91              for (String component: split) {
92                  if (StringUtils.isNotBlank(component)) {
93                      strs.add(component.trim());
94                  }
95              }
96              return strs.toArray(new String[strs.size()]);
97          } else {
98              return new String[0];
99          }
100     }
101 
102     protected boolean isUserRecipientValid(String user, ErrorList errors) {
103         boolean valid = true;
104         Principal principal = getIdentityService().getPrincipalByPrincipalName(user);
105         if (principal == null) {
106         	valid = false;
107         	errors.addError("'" + user + "' is not a valid principal name");
108         }
109 
110         return valid;
111     }
112 
113     protected boolean isWorkgroupRecipientValid(String groupName, String namespaceCode, ErrorList errors) {
114     	Namespace nSpace = getNamespaceService().getNamespace(namespaceCode);
115     	if (nSpace == null) {
116     		errors.addError((new StringBuilder()).append('\'').append(namespaceCode).append("' is not a valid namespace code").toString());
117     		return false;
118     	} else {
119     		Group i = getGroupService().getGroupByNamespaceCodeAndName(namespaceCode, groupName);
120        		if (i == null) {
121        			errors.addError((new StringBuilder()).append('\'').append(groupName).append(
122        					"' is not a valid group name for namespace code '").append(namespaceCode).append('\'').toString());
123        			return false;
124        		} else {
125        			return true;
126        		}
127     	}
128     }
129 }