001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kim.impl.responsibility;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.util.RiceKeyConstants;
020import org.kuali.rice.kim.api.KimConstants;
021import org.kuali.rice.kim.api.responsibility.Responsibility;
022import org.kuali.rice.kim.bo.ui.KimDocumentRoleResponsibility;
023import org.kuali.rice.kim.document.IdentityManagementRoleDocument;
024import org.kuali.rice.kns.rules.DocumentRuleBase;
025import org.kuali.rice.krad.util.GlobalVariables;
026
027import java.util.HashMap;
028import java.util.Map;
029
030/**
031 * This is a description of what this class does - shyu don't forget to fill this in. 
032 * 
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 *
035 */
036public class KimDocumentResponsibilityRule extends DocumentRuleBase implements AddResponsibilityRule {
037
038        public static final String ERROR_PATH = "document.responsibility.responsibilityId";
039
040        public boolean processAddResponsibility(AddResponsibilityEvent addResponsibilityEvent) {
041                KimDocumentRoleResponsibility newResponsibility = addResponsibilityEvent.getResponsibility();
042                if(newResponsibility==null){
043                        GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Responsibility"});
044                        return false;
045                }
046
047                ResponsibilityBo kimResponsibilityImpl = newResponsibility.getKimResponsibility();
048                if(kimResponsibilityImpl==null){
049                        GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Responsibility"});
050                        return false;
051                }
052
053                IdentityManagementRoleDocument document = (IdentityManagementRoleDocument)addResponsibilityEvent.getDocument();         
054            boolean rulePassed = true;
055                if (!hasPermissionToGrantResponsibility(ResponsibilityBo.to(kimResponsibilityImpl), document)) {
056            GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_ASSIGN_RESPONSIBILITY, 
057                        new String[] {kimResponsibilityImpl.getNamespaceCode(), kimResponsibilityImpl.getTemplate().getName()});
058            return false;
059                }
060                
061                if (newResponsibility == null || StringUtils.isBlank(newResponsibility.getResponsibilityId())) {
062            rulePassed = false;
063            GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Responsibility"});
064        } else {
065                int i = 0;
066                    for (KimDocumentRoleResponsibility responsibility: document.getResponsibilities()) {
067                        if (responsibility.getResponsibilityId().equals(newResponsibility.getResponsibilityId())) {
068                            rulePassed = false;
069                            GlobalVariables.getMessageMap().putError("document.responsibilities["+i+"].responsibilityId", RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Responsibility"});
070                        }
071                        i++;
072                    }
073        }
074                return rulePassed;
075        } 
076
077        public boolean hasPermissionToGrantResponsibility(Responsibility kimResponsibilityInfo, IdentityManagementRoleDocument document){
078                Map<String,String> responsibilityDetails = new HashMap<String,String>();
079                responsibilityDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, kimResponsibilityInfo.getNamespaceCode());
080                responsibilityDetails.put(KimConstants.AttributeConstants.RESPONSIBILITY_NAME, kimResponsibilityInfo.getName());
081                if (!getDocumentDictionaryService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
082                                document, 
083                                KimConstants.NAMESPACE_CODE, 
084                                KimConstants.PermissionTemplateNames.GRANT_RESPONSIBILITY,
085                                GlobalVariables.getUserSession().getPerson().getPrincipalId(), 
086                                responsibilityDetails, null)) {
087            return false;
088                }
089                return true;
090        }
091}