001 /**
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.ken.web.spring;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.apache.log4j.Logger;
020 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
021 import org.kuali.rice.coreservice.api.namespace.Namespace;
022 import org.kuali.rice.coreservice.api.namespace.NamespaceService;
023 import org.kuali.rice.ken.exception.ErrorList;
024 import org.kuali.rice.kim.api.group.Group;
025 import org.kuali.rice.kim.api.group.GroupService;
026 import org.kuali.rice.kim.api.identity.IdentityService;
027 import org.kuali.rice.kim.api.identity.principal.Principal;
028 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
029 import org.kuali.rice.krad.service.KRADServiceLocatorInternal;
030 import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
031
032 import javax.servlet.http.HttpServletRequest;
033 import java.util.ArrayList;
034 import java.util.List;
035
036 /**
037 * Base class for KEN controllers for sending notifications
038 *
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 *
041 */
042 public class BaseSendNotificationController extends MultiActionController {
043 private static final Logger LOG = Logger.getLogger(BaseSendNotificationController.class);
044
045 private static final String USER_RECIPS_PARAM = "userRecipients";
046 private static final String WORKGROUP_RECIPS_PARAM = "workgroupRecipients";
047 private static final String WORKGROUP_NAMESPACE_CODES_PARAM = "workgroupNamespaceCodes";
048 private static final String SPLIT_REGEX = "(%2C|,)";
049
050 private static IdentityService identityService;
051 private static GroupService groupService;
052 private static NamespaceService namespaceService;
053
054 protected static IdentityService getIdentityService() {
055 if ( identityService == null ) {
056 identityService = KimApiServiceLocator.getIdentityService();
057 }
058 return identityService;
059 }
060
061 protected static GroupService getGroupService() {
062 if ( groupService == null ) {
063 groupService = KimApiServiceLocator.getGroupService();
064 }
065 return groupService;
066 }
067
068 protected static NamespaceService getNamespaceService() {
069 if ( namespaceService == null ) {
070 namespaceService = KRADServiceLocatorInternal.getNamespaceService();
071 }
072 return namespaceService;
073 }
074
075 protected String[] parseUserRecipients(HttpServletRequest request) {
076 return parseCommaSeparatedValues(request, USER_RECIPS_PARAM);
077 }
078
079 protected String[] parseWorkgroupRecipients(HttpServletRequest request) {
080 return parseCommaSeparatedValues(request, WORKGROUP_RECIPS_PARAM);
081 }
082
083 protected String[] parseWorkgroupNamespaceCodes(HttpServletRequest request) {
084 return parseCommaSeparatedValues(request, WORKGROUP_NAMESPACE_CODES_PARAM);
085 }
086
087 protected String[] parseCommaSeparatedValues(HttpServletRequest request, String param) {
088 String vals = request.getParameter(param);
089 if (vals != null) {
090 String[] split = vals.split(SPLIT_REGEX);
091 List<String> strs = new ArrayList<String>();
092 for (String component: split) {
093 if (StringUtils.isNotBlank(component)) {
094 strs.add(component.trim());
095 }
096 }
097 return strs.toArray(new String[strs.size()]);
098 } else {
099 return new String[0];
100 }
101 }
102
103 protected boolean isUserRecipientValid(String user, ErrorList errors) {
104 boolean valid = true;
105 Principal principal = getIdentityService().getPrincipalByPrincipalName(user);
106 if (principal == null) {
107 valid = false;
108 errors.addError("'" + user + "' is not a valid principal name");
109 }
110
111 return valid;
112 }
113
114 protected boolean isWorkgroupRecipientValid(String groupName, String namespaceCode, ErrorList errors) {
115 Namespace nSpace = getNamespaceService().getNamespace(namespaceCode);
116 if (nSpace == null) {
117 errors.addError((new StringBuilder()).append('\'').append(namespaceCode).append("' is not a valid namespace code").toString());
118 return false;
119 } else {
120 Group i = getGroupService().getGroupByNamespaceCodeAndName(namespaceCode, groupName);
121 if (i == null) {
122 errors.addError((new StringBuilder()).append('\'').append(groupName).append(
123 "' is not a valid group name for namespace code '").append(namespaceCode).append('\'').toString());
124 return false;
125 } else {
126 return true;
127 }
128 }
129 }
130 protected String getPrincipalIdFromIdOrName(String principalIdOrName) {
131 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalIdOrName);
132 if (principal == null) {
133 principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalIdOrName);
134 }
135 if (principal == null) {
136 throw new RiceIllegalArgumentException("Could not locate a principal as initiator with the given remoteUser of " + principalIdOrName);
137 }
138 return principal.getPrincipalId();
139 }
140 }