Coverage Report - org.kuali.rice.ken.web.spring.BaseSendNotificationController
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseSendNotificationController
0%
0/38
0%
0/18
2.333
 
 1  
 /*
 2  
  * Copyright 2007-2009 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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.core.api.namespace.Namespace;
 21  
 import org.kuali.rice.core.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.principal.Principal;
 26  
 import org.kuali.rice.kim.api.services.IdentityService;
 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  
  * Base class for KEN controllers for sending notifications
 37  
  *
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  *
 40  
  */
 41  0
 public class BaseSendNotificationController extends MultiActionController {
 42  0
     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  0
         if ( identityService == null ) {
 55  0
             identityService = KimApiServiceLocator.getIdentityService();
 56  
         }
 57  0
         return identityService;
 58  
     }
 59  
 
 60  
     protected static GroupService getGroupService() {
 61  0
         if ( groupService == null ) {
 62  0
             groupService = KimApiServiceLocator.getGroupService();
 63  
         }
 64  0
         return groupService;
 65  
     }
 66  
     
 67  
     protected static NamespaceService getNamespaceService() {
 68  0
         if ( namespaceService == null ) {
 69  0
             namespaceService = KRADServiceLocatorInternal.getNamespaceService();
 70  
         }
 71  0
         return namespaceService;
 72  
     }
 73  
     
 74  
     protected String[] parseUserRecipients(HttpServletRequest request) {
 75  0
         return parseCommaSeparatedValues(request, USER_RECIPS_PARAM);
 76  
     }
 77  
 
 78  
     protected String[] parseWorkgroupRecipients(HttpServletRequest request) {
 79  0
         return parseCommaSeparatedValues(request, WORKGROUP_RECIPS_PARAM);
 80  
     }
 81  
 
 82  
     protected String[] parseWorkgroupNamespaceCodes(HttpServletRequest request) {
 83  0
             return parseCommaSeparatedValues(request, WORKGROUP_NAMESPACE_CODES_PARAM);
 84  
     }
 85  
     
 86  
     protected String[] parseCommaSeparatedValues(HttpServletRequest request, String param) {
 87  0
         String vals = request.getParameter(param);
 88  0
         if (vals != null) {
 89  0
             String[] split = vals.split(SPLIT_REGEX);
 90  0
             List<String> strs = new ArrayList<String>();
 91  0
             for (String component: split) {
 92  0
                 if (StringUtils.isNotBlank(component)) {
 93  0
                     strs.add(component.trim());
 94  
                 }
 95  
             }
 96  0
             return strs.toArray(new String[strs.size()]);
 97  
         } else {
 98  0
             return new String[0];
 99  
         }
 100  
     }
 101  
 
 102  
     protected boolean isUserRecipientValid(String user, ErrorList errors) {
 103  0
         boolean valid = true;
 104  0
         Principal principal = getIdentityService().getPrincipalByPrincipalName(user);
 105  0
         if (principal == null) {
 106  0
                 valid = false;
 107  0
                 errors.addError("'" + user + "' is not a valid principal name");
 108  
         }
 109  
 
 110  0
         return valid;
 111  
     }
 112  
 
 113  
     protected boolean isWorkgroupRecipientValid(String groupName, String namespaceCode, ErrorList errors) {
 114  0
             Namespace nSpace = getNamespaceService().getNamespace(namespaceCode);
 115  0
             if (nSpace == null) {
 116  0
                     errors.addError((new StringBuilder()).append('\'').append(namespaceCode).append("' is not a valid namespace code").toString());
 117  0
                     return false;
 118  
             } else {
 119  0
                     Group i = getGroupService().getGroupByName(namespaceCode, groupName);
 120  0
                        if (i == null) {
 121  0
                                errors.addError((new StringBuilder()).append('\'').append(groupName).append(
 122  
                                                "' is not a valid group name for namespace code '").append(namespaceCode).append('\'').toString());
 123  0
                                return false;
 124  
                        } else {
 125  0
                                return true;
 126  
                        }
 127  
             }
 128  
     }
 129  
 }