/*
 * 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.coa.document;

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

import org.apache.commons.lang.StringUtils;
import org.kuali.kfs.coa.businessobject.Chart;
import org.kuali.kfs.sys.KFSConstants;
import org.kuali.kfs.sys.context.SpringContext;
import org.kuali.kfs.sys.identity.KfsKimAttributes;
import org.kuali.rice.kim.bo.types.dto.AttributeSet;
import org.kuali.rice.kim.service.RoleManagementService;
import org.kuali.rice.kns.bo.DocumentHeader;
import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;

/**
 * Maintainable implementation for the chart maintenance document
 */
public class ChartMaintainableImpl extends KualiMaintainableImpl {

    /**
     * Override to push chart manager id into KIM
     * 
     * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#handleRouteStatusChange(org.kuali.rice.kns.bo.DocumentHeader)
     */
    @Override
    public void handleRouteStatusChange(DocumentHeader documentHeader) {
        KualiWorkflowDocument workflowDocument = documentHeader.getWorkflowDocument();

        if (workflowDocument.stateIsProcessed()) {
            Chart chart = (Chart) getBusinessObject();

            RoleManagementService roleManagementService = SpringContext.getBean(RoleManagementService.class);

            List<String> roleIds = new ArrayList<String>();
            roleIds.add(roleManagementService.getRoleIdByName(KFSConstants.ParameterNamespaces.KFS, KFSConstants.SysKimConstants.CHART_MANAGER_KIM_ROLE_NAME));

            AttributeSet qualification = new AttributeSet();
            qualification.put(KfsKimAttributes.CHART_OF_ACCOUNTS_CODE, chart.getChartOfAccountsCode());

            if (StringUtils.isNotBlank(chart.getFinCoaManagerPrincipalId()) && !roleManagementService.principalHasRole(chart.getFinCoaManagerPrincipalId(), roleIds, qualification)) {
                roleManagementService.assignPrincipalToRole(chart.getFinCoaManagerPrincipalId(), KFSConstants.ParameterNamespaces.KFS, KFSConstants.SysKimConstants.CHART_MANAGER_KIM_ROLE_NAME, qualification);
            }
        }

        super.handleRouteStatusChange(documentHeader);
    }

}

@@ -671,63 +671,8 @@
 %0D%0A%0D%0A
-import java.util.ArrayList;%0D%0Aimport java.util.List;%0D%0A%0D%0A
 impo
@@ -756,24 +756,72 @@
 ect.Chart;%0D%0A
+import org.kuali.kfs.coa.service.ChartService;%0D%0A
 import org.k
@@ -946,24 +946,62 @@
 ttributes;%0D%0A
+import org.kuali.rice.kim.bo.Person;%0D%0A
 import org.k
@@ -2048,469 +2048,354 @@
     
-List%3CString%3E roleIds = new ArrayList%3CString%3E();%0D%0A            roleIds.add(roleManagementService.getRoleIdByName(KFSConstants.ParameterNamespaces.KFS, KFSConstants.SysKimConstants.CHART_MANAGER_KIM_ROLE_NAME));%0D%0A%0D%0A            AttributeSet qualification = new AttributeSet();%0D%0A            qualification.put(KfsKimAttributes.CHART_OF_ACCOUNTS_CODE, chart.getChartOfAccountsCode());%0D%0A%0D%0A            if (StringUtils.isNotBlank(chart.getFinCoaManagerPrincipalId()) && !
+AttributeSet qualification = new AttributeSet();%0D%0A            qualification.put(KfsKimAttributes.CHART_OF_ACCOUNTS_CODE, chart.getChartOfAccountsCode());%0D%0A%0D%0A            Person oldChartManager = SpringContext.getBean(ChartService.class).getChartManager(chart.getChartOfAccountsCode());%0D%0A            if (oldChartManager != null) %7B%0D%0A                
 role
@@ -2416,17 +2416,23 @@
 ice.
-p
+removeP
 rincipal
 HasR
@@ -2431,38 +2431,36 @@
 ipal
-Has
+From
 Role(
-chart.getFinCoa
+oldChart
 Manager
+.get
 Prin
@@ -2474,30 +2474,212 @@
 (), 
-roleIds, qualification
+KFSConstants.ParameterNamespaces.KFS, KFSConstants.SysKimConstants.CHART_MANAGER_KIM_ROLE_NAME, qualification);%0D%0A            %7D%0D%0A%0D%0A            if (StringUtils.isNotBlank(chart.getFinCoaManagerPrincipalId()
 )) %7B

Fix to remove previous chart manager