/*
 * Copyright 2009 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.kfs.fp.identity;

import java.util.ArrayList;
import java.util.List;

import org.kuali.kfs.sys.identity.KfsKimAttributes;
import org.kuali.rice.kim.bo.role.KimRole;
import org.kuali.rice.kim.bo.role.dto.RoleMembershipInfo;
import org.kuali.rice.kim.bo.types.dto.AttributeSet;
import org.kuali.rice.kim.service.RoleManagementService;
import org.kuali.rice.kim.service.support.impl.KimDerivedRoleTypeServiceBase;

/**
 * A role which determines membership in role for people who can initiate Cash Receipts.  Members are those 
 * users which belong to KFS-SYS User but NOT KFS-FP Cash Manager.
 */
public class CashReceiptInitiatorDerivedRoleTypeServiceImpl extends KimDerivedRoleTypeServiceBase {
    private RoleManagementService roleManagementService;
    
    private static final String SYS_USER_ROLE_NAMESPACE = "KFS-SYS";
    private static final String SYS_USER_ROLE_NAME = "User";
    private static final String CASH_MANAGER_ROLE_NAMESPACE = "KFS-FP";
    private static final String CASH_MANAGER_ROLE_NAME = "Cash Manager";

    /**
     * Overridden to check principal id is in KFS-SYS User and not in KFS-FP Cash Manager
     * @see org.kuali.rice.kim.service.support.impl.KimDerivedRoleTypeServiceBase#getRoleMembersFromApplicationRole(java.lang.String, java.lang.String, org.kuali.rice.kim.bo.types.dto.AttributeSet)
     */
    @Override
    public List<RoleMembershipInfo> getRoleMembersFromApplicationRole(String namespaceCode, String roleName, AttributeSet qualification) {
        List<RoleMembershipInfo> members = new ArrayList<RoleMembershipInfo>();
        if (qualification.containsKey(KfsKimAttributes.PRINCIPAL_ID)) {
            final String principalId = qualification.get(KfsKimAttributes.PRINCIPAL_ID);
            if (principalMemberOfSysUsers(principalId, qualification)) {
                if (!principalMemberOfCashManagers(principalId, qualification)) {
                    members.add(new RoleMembershipInfo(null, null, principalId, KimRole.PRINCIPAL_MEMBER_TYPE, qualification));
                }
            }
        }
        return members;
    }
    
    /**
     * Determines if the given principal with the given qualification is a member of KFS-SYS User
     * @param principalId the principal id of the shmoe trying to initiate a cash receipt
     * @param qualification the qualification of said shmoe
     * @return true if the said shmoe is indeed a member of KFS-SYS User; false otherwise
     */
    protected boolean principalMemberOfSysUsers(String principalId, AttributeSet qualification) {
        return hasRoleMembership(principalId, qualification, SYS_USER_ROLE_NAMESPACE, SYS_USER_ROLE_NAME);
    }
    
    /**
     * Determines if the given principal with the given qualification is a member of KFS-FP Cash Manager
     * @param principalId the principal idea of the user attempting to initiate a cash receipt
     * @param qualification the qualification of said principal
     * @return true if principal is a member of KFS-FP Cash Manager, false otherwise
     */
    protected boolean principalMemberOfCashManagers(String principalId, AttributeSet qualification) {
        return hasRoleMembership(principalId, qualification, CASH_MANAGER_ROLE_NAMESPACE, CASH_MANAGER_ROLE_NAME);
    }
    
    /**
     * Determines if a principal has a given role
     * @param principalId the id of the principal
     * @param qualification their qualifications from the permission/responsibility call
     * @param namespaceCode the namespace code of the role
     * @param roleName the name of the role
     * @return true if the principal is a member of the role, false otherwise
     */
    final protected boolean hasRoleMembership(String principalId, AttributeSet qualification, String namespaceCode, String roleName) {
        final String roleId = getRoleManagementService().getRoleIdByName(namespaceCode, roleName);
        List<String> roleIds = new ArrayList<String>();
        roleIds.add(roleId);
        
        return getRoleManagementService().principalHasRole(principalId, roleIds, qualification);
    }

    /**
     * Sets the roleManagementService attribute value.
     * @param roleManagementService The roleManagementService to set.
     */
    public void setRoleManagementService(RoleManagementService roleManagementService) {
        this.roleManagementService = roleManagementService;
    }

    /**
     * Gets the roleManagementService attribute. 
     * @return Returns the roleManagementService.
     */
    public RoleManagementService getRoleManagementService() {
        return roleManagementService;
    }
}

@@ -1702,16 +1702,20 @@
 ager%22;%0D%0A
+    
 %0D%0A    /*
@@ -1855,39 +1855,32 @@
 support.impl.Kim
-Derived
 RoleTypeServiceB
@@ -1887,42 +1887,61 @@
 ase#
-getRoleMembersFromApplicationRole(
+hasApplicationRole(java.lang.String, java.util.List, 
 java
@@ -2058,385 +2058,148 @@
 lic 
-List%3CRoleMembershipInfo%3E getRoleMembersFromApplicationRole(String namespaceCode, String roleName, AttributeSet qualification) %7B%0D%0A        List%3CRoleMembershipInfo%3E members = new ArrayList%3CRoleMembershipInfo%3E();%0D%0A        if (qualification.containsKey(KfsKimAttributes.PRINCIPAL_ID)) %7B%0D%0A            final String principalId = qualification.get(KfsKimAttributes.PRINCIPAL_ID);%0D%0A    
+boolean hasApplicationRole(String principalId, List%3CString%3E groupIds, String namespaceCode, String roleName, AttributeSet qualification) %7B%0D%0A
     
@@ -2272,28 +2272,24 @@
             
- 
-   
 if (!princip
@@ -2363,138 +2363,20 @@
     
-    members.add(new RoleMembershipInfo(null, null, principalId, KimRole.PRINCIPAL_MEMBER_TYPE, qualification));%0D%0A                %7D
+return true;
 %0D%0A  
@@ -2414,23 +2414,21 @@
  return 
-members
+false
 ;%0D%0A    %7D
@@ -2421,36 +2421,32 @@
  false;%0D%0A    %7D%0D%0A
-    
 %0D%0A    /**%0D%0A     

fix role service