001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.coa.document;
017    
018    import java.util.List;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.apache.log4j.Logger;
022    import org.kuali.kfs.coa.businessobject.Account;
023    import org.kuali.kfs.coa.businessobject.IndirectCostRecoveryRateDetail;
024    import org.kuali.kfs.coa.service.AccountService;
025    import org.kuali.kfs.gl.GeneralLedgerConstants;
026    import org.kuali.kfs.sys.KFSConstants;
027    import org.kuali.kfs.sys.KFSPropertyConstants;
028    import org.kuali.kfs.sys.context.SpringContext;
029    import org.kuali.kfs.sys.document.FinancialSystemMaintainable;
030    import org.kuali.rice.kns.bo.PersistableBusinessObject;
031    import org.kuali.rice.kns.util.ObjectUtils;
032    
033    public 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(KFSConstants.getDashSubAccountNumber());
051            }
052            if(StringUtils.isBlank(addLine.getFinancialSubObjectCode()) || StringUtils.containsOnly(addLine.getFinancialSubObjectCode(), "-")) {
053                addLine.setFinancialSubObjectCode(KFSConstants.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.kfs.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(KFSPropertyConstants.INDIRECT_COST_RECOVERY_RATE_DETAILS);
084            String accountNumber = (String)ObjectUtils.getPropertyValue(newAccount, KFSPropertyConstants.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, KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, coaCode); 
103            }
104            catch (Exception e) {
105                LOG.error("Error in setting property value for " + KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
106            } 
107        }    
108        
109    }