Coverage Report - org.kuali.rice.kim.rules.ui.KimDocumentResponsibilityRule
 
Classes in this File Line Coverage Branch Coverage Complexity
KimDocumentResponsibilityRule
0%
0/30
0%
0/16
7
 
 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 responsibilitys and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kim.rules.ui;
 17  
 
 18  
 import java.util.HashMap;
 19  
 import java.util.Map;
 20  
 
 21  
 import org.apache.commons.lang.StringUtils;
 22  
 import org.kuali.rice.kim.bo.impl.KimAttributes;
 23  
 import org.kuali.rice.kim.bo.role.dto.KimResponsibilityInfo;
 24  
 import org.kuali.rice.kim.bo.role.impl.KimResponsibilityImpl;
 25  
 import org.kuali.rice.kim.bo.ui.KimDocumentRoleResponsibility;
 26  
 import org.kuali.rice.kim.document.IdentityManagementRoleDocument;
 27  
 import org.kuali.rice.kim.rule.event.ui.AddResponsibilityEvent;
 28  
 import org.kuali.rice.kim.rule.ui.AddResponsibilityRule;
 29  
 import org.kuali.rice.kim.util.KimConstants;
 30  
 import org.kuali.rice.kns.rules.DocumentRuleBase;
 31  
 import org.kuali.rice.kns.util.GlobalVariables;
 32  
 import org.kuali.rice.kns.util.RiceKeyConstants;
 33  
 
 34  
 /**
 35  
  * This is a description of what this class does - shyu don't forget to fill this in. 
 36  
  * 
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  *
 39  
  */
 40  0
 public class KimDocumentResponsibilityRule extends DocumentRuleBase implements AddResponsibilityRule {
 41  
 
 42  
         public static final String ERROR_PATH = "document.responsibility.responsibilityId";
 43  
 
 44  
         public boolean processAddResponsibility(AddResponsibilityEvent addResponsibilityEvent) {
 45  0
                 KimDocumentRoleResponsibility newResponsibility = addResponsibilityEvent.getResponsibility();
 46  0
                 if(newResponsibility==null){
 47  0
                         GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Responsibility"});
 48  0
                         return false;
 49  
                 }
 50  
 
 51  0
                 KimResponsibilityImpl kimResponsibilityImpl = newResponsibility.getKimResponsibility();
 52  0
                 if(kimResponsibilityImpl==null){
 53  0
                         GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Responsibility"});
 54  0
                         return false;
 55  
                 }
 56  
 
 57  0
                 IdentityManagementRoleDocument document = (IdentityManagementRoleDocument)addResponsibilityEvent.getDocument();                
 58  0
             boolean rulePassed = true;
 59  0
                 if (!hasPermissionToGrantResponsibility(kimResponsibilityImpl.toSimpleInfo(), document)) {
 60  0
             GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_ASSIGN_RESPONSIBILITY, 
 61  
                             new String[] {kimResponsibilityImpl.getNamespaceCode(), kimResponsibilityImpl.getTemplate().getName()});
 62  0
             return false;
 63  
                 }
 64  
                 
 65  0
                 if (newResponsibility == null || StringUtils.isBlank(newResponsibility.getResponsibilityId())) {
 66  0
             rulePassed = false;
 67  0
             GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Responsibility"});
 68  
         } else {
 69  0
                 int i = 0;
 70  0
                     for (KimDocumentRoleResponsibility responsibility: document.getResponsibilities()) {
 71  0
                             if (responsibility.getResponsibilityId().equals(newResponsibility.getResponsibilityId())) {
 72  0
                             rulePassed = false;
 73  0
                             GlobalVariables.getMessageMap().putError("document.responsibilities["+i+"].responsibilityId", RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Responsibility"});
 74  
                             }
 75  0
                             i++;
 76  
                     }
 77  
         }
 78  0
                 return rulePassed;
 79  
         } 
 80  
 
 81  
         public boolean hasPermissionToGrantResponsibility(KimResponsibilityInfo kimResponsibilityInfo, IdentityManagementRoleDocument document){
 82  0
                 Map<String,String> responsibilityDetails = new HashMap<String,String>();
 83  0
                 responsibilityDetails.put(KimAttributes.NAMESPACE_CODE, kimResponsibilityInfo.getNamespaceCode());
 84  0
                 responsibilityDetails.put(KimAttributes.RESPONSIBILITY_NAME, kimResponsibilityInfo.getName());
 85  0
                 if (!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
 86  
                                 document, 
 87  
                                 KimConstants.NAMESPACE_CODE, 
 88  
                                 KimConstants.PermissionTemplateNames.GRANT_RESPONSIBILITY, 
 89  
                                 GlobalVariables.getUserSession().getPerson().getPrincipalId(), 
 90  
                                 responsibilityDetails, null)) {
 91  0
             return false;
 92  
                 }
 93  0
                 return true;
 94  
         }
 95  
 }