/*
 * Copyright 2008 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.module.bc.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.kuali.kfs.module.bc.businessobject.BudgetConstructionIntendedIncumbent;
import org.kuali.kfs.module.bc.businessobject.Incumbent;
import org.kuali.kfs.module.bc.exception.BudgetIncumbentAlreadyExistsException;
import org.kuali.kfs.module.bc.service.BudgetConstructionIntendedIncumbentService;
import org.kuali.kfs.module.bc.service.HumanResourcesPayrollService;
import org.kuali.kfs.sys.KFSPropertyConstants;
import org.kuali.rice.kns.service.BusinessObjectService;
import org.kuali.rice.kns.util.spring.Logged;

/**
 * implements the service methods defined in BudgetConstructionIntendedIncumbentService
 */
public class BudgetConstructionIntendedIncumbentServiceImpl implements BudgetConstructionIntendedIncumbentService {
    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BudgetConstructionIntendedIncumbentServiceImpl.class);

    private BusinessObjectService businessObjectService;
    private HumanResourcesPayrollService humanResourcesPayrollService;

    /**
     * @see org.kuali.kfs.module.bc.service.BudgetConstructionIntendedIncumbentService#pullNewIncumbentFromExternal(java.lang.String)
     */
    public synchronized void pullNewIncumbentFromExternal(String emplid) throws BudgetIncumbentAlreadyExistsException {
        // call humanResourcesPayrollService service to pull record
        Incumbent incumbent = humanResourcesPayrollService.getIncumbent(emplid);

        // populate BudgetConstructionIntendedIncumbent
        BudgetConstructionIntendedIncumbent bcIncumbent = new BudgetConstructionIntendedIncumbent();
        bcIncumbent.setEmplid(incumbent.getEmplid());
        bcIncumbent.setName(incumbent.getName());

        // check if incumbent already exists in budget incumbent table, if not add incumbent
        BudgetConstructionIntendedIncumbent retrievedIncumbent = getByPrimaryId(emplid);
        if (retrievedIncumbent != null) {
            throw new BudgetIncumbentAlreadyExistsException(emplid);
        }

        businessObjectService.save(bcIncumbent);
    }

    /**
     * @see org.kuali.kfs.module.bc.service.BudgetConstructionIntendedIncumbentService#refreshIncumbentFromExternal(java.lang.String)
     */
    public void refreshIncumbentFromExternal(String emplid) {
        // call humanResourcesPayrollService service to pull record
        Incumbent incumbent = humanResourcesPayrollService.getIncumbent(emplid);

        // populate BudgetConstructionIntendedIncumbent
        BudgetConstructionIntendedIncumbent bcIncumbent = new BudgetConstructionIntendedIncumbent();
        bcIncumbent.setEmplid(incumbent.getEmplid());
        bcIncumbent.setName(incumbent.getName());

        // update budget record
        BudgetConstructionIntendedIncumbent retrievedIncumbent = getByPrimaryId(emplid);
        bcIncumbent.setVersionNumber(retrievedIncumbent.getVersionNumber());

        businessObjectService.save(bcIncumbent);
    }

    /**
     * @see org.kuali.kfs.module.bc.service.BudgetConstructionIntendedIncumbentService#getByPrimaryId(java.lang.String)
     */
    public BudgetConstructionIntendedIncumbent getByPrimaryId(String emplid) {
        Map<String, Object> primaryKeys = new HashMap<String, Object>();
        primaryKeys.put(KFSPropertyConstants.EMPLID, emplid);

        return (BudgetConstructionIntendedIncumbent) businessObjectService.findByPrimaryKey(BudgetConstructionIntendedIncumbent.class, primaryKeys);
    }

    /**
     * Sets the businessObjectService attribute value.
     * 
     * @param businessObjectService The businessObjectService to set.
     */
    public void setBusinessObjectService(BusinessObjectService businessObjectService) {
        this.businessObjectService = businessObjectService;
    }

    /**
     * Sets the humanResourcesPayrollService attribute value.
     * 
     * @param humanResourcesPayrollService The humanResourcesPayrollService to set.
     */
    public void setHumanResourcesPayrollService(HumanResourcesPayrollService humanResourcesPayrollService) {
        this.humanResourcesPayrollService = humanResourcesPayrollService;
    }
}


@@ -2384,32 +2384,77 @@
 bent.getName());
+%0A        bcIncumbent.setActive(Boolean.TRUE);
 %0A%0A        // che
@@ -3616,24 +3616,86 @@
 onNumber());
+%0A        bcIncumbent.setActive(retrievedIncumbent.isActive());
 %0A%0A        bu

fix bug where active indicator not set when refreshing or getting new incumbent