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