Coverage Report - org.kuali.rice.ken.web.spring.BaseSendNotificationController
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseSendNotificationController
0%
0/35
0%
0/16
2.375
 
 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 java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import javax.servlet.http.HttpServletRequest;
 22  
 
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.apache.log4j.Logger;
 25  
 import org.kuali.rice.ken.exception.ErrorList;
 26  
 import org.kuali.rice.kim.bo.Group;
 27  
 import org.kuali.rice.kim.bo.entity.KimPrincipal;
 28  
 import org.kuali.rice.kim.service.IdentityManagementService;
 29  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 30  
 import org.kuali.rice.kns.bo.Namespace;
 31  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 32  
 import org.kuali.rice.kns.service.NamespaceService;
 33  
 import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
 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 IdentityManagementService identityManagementService;
 50  
     private static NamespaceService namespaceService;
 51  
 
 52  
     protected static IdentityManagementService getIdentityManagementService() {
 53  0
         if ( identityManagementService == null ) {
 54  0
             identityManagementService = KIMServiceLocator.getIdentityManagementService();
 55  
         }
 56  0
         return identityManagementService;
 57  
     }
 58  
     
 59  
     protected static NamespaceService getNamespaceService() {
 60  0
         if ( namespaceService == null ) {
 61  0
             namespaceService = KNSServiceLocator.getNamespaceService();
 62  
         }
 63  0
         return namespaceService;
 64  
     }
 65  
     
 66  
     protected String[] parseUserRecipients(HttpServletRequest request) {
 67  0
         return parseCommaSeparatedValues(request, USER_RECIPS_PARAM);
 68  
     }
 69  
 
 70  
     protected String[] parseWorkgroupRecipients(HttpServletRequest request) {
 71  0
         return parseCommaSeparatedValues(request, WORKGROUP_RECIPS_PARAM);
 72  
     }
 73  
 
 74  
     protected String[] parseWorkgroupNamespaceCodes(HttpServletRequest request) {
 75  0
             return parseCommaSeparatedValues(request, WORKGROUP_NAMESPACE_CODES_PARAM);
 76  
     }
 77  
     
 78  
     protected String[] parseCommaSeparatedValues(HttpServletRequest request, String param) {
 79  0
         String vals = request.getParameter(param);
 80  0
         if (vals != null) {
 81  0
             String[] split = vals.split(SPLIT_REGEX);
 82  0
             List<String> strs = new ArrayList<String>();
 83  0
             for (String component: split) {
 84  0
                 if (StringUtils.isNotBlank(component)) {
 85  0
                     strs.add(component.trim());
 86  
                 }
 87  
             }
 88  0
             return strs.toArray(new String[strs.size()]);
 89  
         } else {
 90  0
             return new String[0];
 91  
         }
 92  
     }
 93  
 
 94  
     protected boolean isUserRecipientValid(String user, ErrorList errors) {
 95  0
         boolean valid = true;
 96  0
         KimPrincipal principal = getIdentityManagementService().getPrincipalByPrincipalName(user);
 97  0
         if (principal == null) {
 98  0
                 valid = false;
 99  0
                 errors.addError("'" + user + "' is not a valid principal name");
 100  
         }
 101  
 
 102  0
         return valid;
 103  
     }
 104  
 
 105  
     protected boolean isWorkgroupRecipientValid(String groupName, String namespaceCode, ErrorList errors) {
 106  0
             Namespace nSpace = getNamespaceService().getNamespace(namespaceCode);
 107  0
             if (nSpace == null) {
 108  0
                     errors.addError((new StringBuilder()).append('\'').append(namespaceCode).append("' is not a valid namespace code").toString());
 109  0
                     return false;
 110  
             } else {
 111  0
                     Group i = getIdentityManagementService().getGroupByName(namespaceCode, groupName);
 112  0
                        if (i == null) {
 113  0
                                errors.addError((new StringBuilder()).append('\'').append(groupName).append(
 114  
                                                "' is not a valid group name for namespace code '").append(namespaceCode).append('\'').toString());
 115  0
                                return false;
 116  
                        } else {
 117  0
                                return true;
 118  
                        }
 119  
             }
 120  
     }
 121  
 }