001/*
002 * Copyright 2008 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.ole.coa.document;
017
018import java.util.List;
019
020import org.apache.commons.lang.StringUtils;
021import org.apache.log4j.Logger;
022import org.kuali.ole.coa.businessobject.Account;
023import org.kuali.ole.coa.businessobject.IndirectCostRecoveryRateDetail;
024import org.kuali.ole.coa.service.AccountService;
025import org.kuali.ole.gl.GeneralLedgerConstants;
026import org.kuali.ole.sys.OLEConstants;
027import org.kuali.ole.sys.OLEPropertyConstants;
028import org.kuali.ole.sys.context.SpringContext;
029import org.kuali.ole.sys.document.FinancialSystemMaintainable;
030import org.kuali.rice.krad.bo.PersistableBusinessObject;
031import org.kuali.rice.krad.util.ObjectUtils;
032
033public class IndirectCostRecoveryRateMaintainableImpl extends FinancialSystemMaintainable {
034    private static final Logger LOG = Logger.getLogger(IndirectCostRecoveryRateMaintainableImpl.class);  
035
036    private Integer indirectCostRecoveryRateNextEntryNumber;
037
038    /**
039     * Hook for quantity and setting asset numbers.
040     * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#addNewLineToCollection(java.lang.String)
041     */
042    @Override
043    public void addNewLineToCollection(String collectionName) {
044
045        // create handle for the addline section of the doc
046        IndirectCostRecoveryRateDetail addLine = (IndirectCostRecoveryRateDetail) newCollectionLines.get(collectionName);
047        List<IndirectCostRecoveryRateDetail> maintCollection = (List<IndirectCostRecoveryRateDetail>) ObjectUtils.getPropertyValue(getBusinessObject(), collectionName);
048
049        if(StringUtils.isBlank(addLine.getSubAccountNumber()) || StringUtils.containsOnly(addLine.getSubAccountNumber(), "-")) {
050            addLine.setSubAccountNumber(OLEConstants.getDashSubAccountNumber());
051        }
052        if(StringUtils.isBlank(addLine.getFinancialSubObjectCode()) || StringUtils.containsOnly(addLine.getFinancialSubObjectCode(), "-")) {
053            addLine.setFinancialSubObjectCode(OLEConstants.getDashFinancialSubObjectCode());
054        }
055
056        Integer icrEntryNumberMax = 0;
057        for(IndirectCostRecoveryRateDetail item : maintCollection) {
058            if(icrEntryNumberMax < item.getAwardIndrCostRcvyEntryNbr()) {
059                icrEntryNumberMax = item.getAwardIndrCostRcvyEntryNbr();
060            }
061        }
062
063        // addLine.setActive(true); // TODO remove after active indicator fixes
064        addLine.setNewCollectionRecord(true);
065        addLine.setAwardIndrCostRcvyEntryNbr(icrEntryNumberMax + 1);
066        maintCollection.add(addLine);
067        initNewCollectionLine(collectionName);
068    }
069
070    /**
071     * @see org.kuali.ole.sys.document.FinancialSystemMaintainable#populateChartOfAccountsCodeFields()
072     * 
073     * Special treatment is needed to populate the chart code from the account number field in IndirectCostRecoveryRateDetails, 
074     * as the potential reference account doesn't exist in the collection due to wild cards, which also needs special handling.  
075     */
076    @Override
077    protected void populateChartOfAccountsCodeFields() {
078        // calling super method in case there're reference accounts/collections other than ICR rates
079        super.populateChartOfAccountsCodeFields();
080        
081        PersistableBusinessObject bo = getBusinessObject();        
082        AccountService acctService = SpringContext.getBean(AccountService.class);    
083        PersistableBusinessObject newAccount = getNewCollectionLine(OLEPropertyConstants.INDIRECT_COST_RECOVERY_RATE_DETAILS);
084        String accountNumber = (String)ObjectUtils.getPropertyValue(newAccount, OLEPropertyConstants.ACCOUNT_NUMBER);
085        String coaCode = null;
086        
087        // if accountNumber is wild card, populate chart code with the same wild card
088        if (GeneralLedgerConstants.PosterService.SYMBOL_USE_EXPENDITURE_ENTRY.equals(accountNumber) || 
089            GeneralLedgerConstants.PosterService.SYMBOL_USE_ICR_FROM_ACCOUNT.equals(accountNumber)) {
090            coaCode = accountNumber;
091        }
092        // otherwise do the normal account lookup
093        else {
094            Account account = acctService.getUniqueAccountForAccountNumber(accountNumber);            
095            if (ObjectUtils.isNotNull(account)) {
096                coaCode = account.getChartOfAccountsCode();
097            }
098        }
099         
100        // populate chart code field
101        try {
102            ObjectUtils.setObjectProperty(newAccount, OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, coaCode); 
103        }
104        catch (Exception e) {
105            LOG.error("Error in setting property value for " + OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE);
106        } 
107    }    
108    
109}