001 /** 002 * Copyright 2005-2012 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.coreservice.api.namespace.Namespace; 021 import org.kuali.rice.coreservice.api.namespace.NamespaceService; 022 import org.kuali.rice.ken.exception.ErrorList; 023 import org.kuali.rice.kim.api.group.Group; 024 import org.kuali.rice.kim.api.group.GroupService; 025 import org.kuali.rice.kim.api.identity.IdentityService; 026 import org.kuali.rice.kim.api.identity.principal.Principal; 027 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 028 import org.kuali.rice.krad.service.KRADServiceLocatorInternal; 029 import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 030 031 import javax.servlet.http.HttpServletRequest; 032 import java.util.ArrayList; 033 import java.util.List; 034 035 /** 036 * Base class for KEN controllers for sending notifications 037 * 038 * @author Kuali Rice Team (rice.collab@kuali.org) 039 * 040 */ 041 public class BaseSendNotificationController extends MultiActionController { 042 private static final Logger LOG = Logger.getLogger(BaseSendNotificationController.class); 043 044 private static final String USER_RECIPS_PARAM = "userRecipients"; 045 private static final String WORKGROUP_RECIPS_PARAM = "workgroupRecipients"; 046 private static final String WORKGROUP_NAMESPACE_CODES_PARAM = "workgroupNamespaceCodes"; 047 private static final String SPLIT_REGEX = "(%2C|,)"; 048 049 private static IdentityService identityService; 050 private static GroupService groupService; 051 private static NamespaceService namespaceService; 052 053 protected static IdentityService getIdentityService() { 054 if ( identityService == null ) { 055 identityService = KimApiServiceLocator.getIdentityService(); 056 } 057 return identityService; 058 } 059 060 protected static GroupService getGroupService() { 061 if ( groupService == null ) { 062 groupService = KimApiServiceLocator.getGroupService(); 063 } 064 return groupService; 065 } 066 067 protected static NamespaceService getNamespaceService() { 068 if ( namespaceService == null ) { 069 namespaceService = KRADServiceLocatorInternal.getNamespaceService(); 070 } 071 return namespaceService; 072 } 073 074 protected String[] parseUserRecipients(HttpServletRequest request) { 075 return parseCommaSeparatedValues(request, USER_RECIPS_PARAM); 076 } 077 078 protected String[] parseWorkgroupRecipients(HttpServletRequest request) { 079 return parseCommaSeparatedValues(request, WORKGROUP_RECIPS_PARAM); 080 } 081 082 protected String[] parseWorkgroupNamespaceCodes(HttpServletRequest request) { 083 return parseCommaSeparatedValues(request, WORKGROUP_NAMESPACE_CODES_PARAM); 084 } 085 086 protected String[] parseCommaSeparatedValues(HttpServletRequest request, String param) { 087 String vals = request.getParameter(param); 088 if (vals != null) { 089 String[] split = vals.split(SPLIT_REGEX); 090 List<String> strs = new ArrayList<String>(); 091 for (String component: split) { 092 if (StringUtils.isNotBlank(component)) { 093 strs.add(component.trim()); 094 } 095 } 096 return strs.toArray(new String[strs.size()]); 097 } else { 098 return new String[0]; 099 } 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 }