001/**
002 * Copyright 2005-2017 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.rules.ui;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.uif.RemotableAttributeError;
020import org.kuali.rice.core.api.util.RiceKeyConstants;
021import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMember;
022import org.kuali.rice.kim.document.IdentityManagementRoleDocument;
023import org.kuali.rice.kim.document.rule.AttributeValidationHelper;
024import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
025import org.kuali.rice.kim.framework.type.KimTypeService;
026import org.kuali.rice.kim.rule.event.ui.AddDelegationMemberEvent;
027import org.kuali.rice.kim.rule.ui.AddDelegationMemberRule;
028import org.kuali.rice.kns.rules.DocumentRuleBase;
029import org.kuali.rice.krad.util.GlobalVariables;
030
031import java.util.ArrayList;
032import java.util.List;
033import java.util.Map;
034
035/**
036 * This is a description of what this class does - shyu don't forget to fill this in. 
037 * 
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 *
040 */
041public class RoleDocumentDelegationMemberRule extends DocumentRuleBase implements AddDelegationMemberRule {
042
043        public static final String ERROR_PATH = "document.delegationMember.memberId";
044
045        protected AttributeValidationHelper attributeValidationHelper = new AttributeValidationHelper();
046        
047        public boolean processAddDelegationMember(AddDelegationMemberEvent addDelegationMemberEvent){
048                RoleDocumentDelegationMember newMember = addDelegationMemberEvent.getDelegationMember();
049                IdentityManagementRoleDocument document = (IdentityManagementRoleDocument)addDelegationMemberEvent.getDocument();
050            boolean rulePassed = true;
051        if(newMember == null || StringUtils.isBlank(newMember.getMemberId())){
052            GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Delegation Member"});
053            return false;
054        }
055        if(StringUtils.isBlank(newMember.getRoleMemberId())){
056            GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Role Member"});
057            return false;
058        }
059                List<Map<String, String>> mapListToValidate = new ArrayList<Map<String, String>>();
060                Map<String, String> mapToValidate;
061                List<RemotableAttributeError> validationErrors = new ArrayList<RemotableAttributeError>();
062        KimTypeService kimTypeService = KimFrameworkServiceLocator.getKimTypeService(document.getKimType());
063
064                for(RoleDocumentDelegationMember roleMember: document.getDelegationMembers()) {
065                        mapToValidate = attributeValidationHelper.convertQualifiersToMap(roleMember.getQualifiers());
066                        mapListToValidate.add(mapToValidate);
067        }
068
069
070            int i = 0;
071            for (RoleDocumentDelegationMember member: document.getDelegationMembers()){
072                List<RemotableAttributeError> localErrors = kimTypeService.validateUniqueAttributes(
073                                        document.getKimType().getId(),
074                                        attributeValidationHelper.convertQualifiersToMap(newMember.getQualifiers()), 
075                                        attributeValidationHelper.convertQualifiersToMap(member.getQualifiers()));
076                if (!localErrors.isEmpty() && (member.getMemberId().equals(newMember.getMemberId()) &&
077                                member.getMemberTypeCode().equals(newMember.getMemberTypeCode()))){
078                    rulePassed = false;
079                    GlobalVariables.getMessageMap().putError("delegationMember.memberId", RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Delegation Member"});
080                    break;
081                }
082                i++;
083            }
084        
085        if ( kimTypeService != null && !newMember.isRole()) {
086                List<RemotableAttributeError> localErrors = kimTypeService.validateAttributes( document.getKimType().getId(), attributeValidationHelper.convertQualifiersToMap( newMember.getQualifiers() ) );
087                validationErrors.addAll( attributeValidationHelper.convertErrors("delegationMember",
088                    attributeValidationHelper.convertQualifiersToAttrIdxMap(newMember.getQualifiers()), localErrors) );
089        }
090        if (!validationErrors.isEmpty()) {
091                attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors);
092                rulePassed = false;
093        }
094                return rulePassed;
095        } 
096
097}