View Javadoc

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